首页
社区
课程
招聘
未解决 [求助] win10 1803 逆向 DbgkCreateThread返回之后 CR3 切换错误
发表于: 2020-9-29 18:24 2960

未解决 [求助] win10 1803 逆向 DbgkCreateThread返回之后 CR3 切换错误

2020-9-29 18:24
2960

代码跑完之后 返回 cr3 切换错误了

 

图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
插入代码
VOID   NewDbgkCreateThread(PETHREAD_S Thread)
{
 
    PEPROCESS_S Process = (PEPROCESS_S)PsGetCurrentProcess();
    DBGKM_APIMSG m;
    PDBGKM_CREATE_THREAD CreateThreadArgs;
    PDBGKM_CREATE_PROCESS CreateProcessArgs;
    PDBGKM_LOAD_DLL LoadDllArgs;
    NTSTATUS status;
    PIMAGE_NT_HEADERS NtHeaders;
    ULONG OldFlags;
    ULONG    index;
    PMODULE_INFO ModuleInfo;
    ULONG64 DebugObject=NULL;
    PSYSTEM_DLL    SystemDll;
    PVOID    Object;
    PFILE_OBJECT FileObject;
    PKTHREAD_S    CurrentThread;
    PUNICODE_STRING CurProcName = GetProcNameByEproc((PEPROCESS)Process);
 
    if ((CurProcName->Length != 0) && (wcsstr(CurProcName->Buffer, L"Dbgview.exe")))
    {
 
        PVOID64 Flags = (PVOID64)(ULONG64(Process) + 0x304);
        RtlMoveMemory(&Process->Flags, (PVOID)Flags, 8);
        DbgPrint("Flags %p\n", &Process->Flags);
        DbgPrint("Flags %p\n", Process->Flags);
 
        PVOID64 SectionBaseAddress = (PVOID64)(ULONG64(Process) + 0x3C0);
        RtlMoveMemory(&Process->SectionBaseAddress, (PVOID)SectionBaseAddress, 8);
        DbgPrint("SectionBaseAddress %p\n", &Process->SectionBaseAddress);
        DbgPrint("SectionBaseAddress %p\n", Process->SectionBaseAddress);
 
        PVOID64 UniqueProcessId = (PVOID64)(ULONG64(Process) + 0x2E0);
        RtlMoveMemory(&Process->UniqueProcessId, (PVOID)UniqueProcessId, 8);
        DbgPrint("UniqueProcessId %p\n", &Process->UniqueProcessId);
        DbgPrint("UniqueProcessId %p\n", Process->UniqueProcessId);
 
        PVOID64 SectionObject = (PVOID64)(ULONG64(Process) + 0x3B8);
        RtlMoveMemory(&Process->SectionObject, (PVOID)SectionObject, 8);
        DbgPrint("UniqueProcessId %p\n", &Process->SectionObject);
        DbgPrint("UniqueProcessId %p\n", Process->SectionObject);
 
 
    //    __debugbreak();
    }
 
 
 
    OldFlags = RtlInterlockedSetBits(&Process->Flags, 0x400001);// RtlInterlockedSetBits 对由多线程共享的变量执行原子位或操作。
 
    /*检查是否我们是第一个设置它们的线程,或者是否有其他线程与我们竞赛*/
    if ((OldFlags & 0x00400000) == 0 &&
        (*PspNotifyEnableMask & 0x1))
    {
 
        IMAGE_INFO_EX ImageInfoEx;
        PUNICODE_STRING ImageName;
        POBJECT_NAME_INFORMATION FileNameInfo;
 
        ImageInfoEx.ImageInfo.Properties = 0;
        ImageInfoEx.ImageInfo.ImageAddressingMode = IMAGE_ADDRESSING_MODE_32BIT;
        ImageInfoEx.ImageInfo.ImageBase = Process->SectionBaseAddress;
        ImageInfoEx.ImageInfo.ImageSize = 0;
        DbgPrint("%p\n", Process->SectionBaseAddress);
        /* Get the NT Headers */
        _try{
            NtHeaders = RtlImageNtHeader(Process->SectionBaseAddress);  //获取一个header
 
            if (NtHeaders)
            {
                ImageInfoEx.ImageInfo.ImageSize = NtHeaders->OptionalHeader.SizeOfImage;
 
            }
        } _except(EXCEPTION_EXECUTE_HANDLER) {
            ImageInfoEx.ImageInfo.ImageSize = 0;
        }
        ImageInfoEx.ImageInfo.ImageSelector = 0;
        ImageInfoEx.ImageInfo.ImageSectionNumber = 0;
 
        PsReferenceProcessFilePointer((PEPROCESS)Process, (PVOID*)&FileObject);
        status = SeLocateProcessImageName((PEPROCESS)Process, &ImageName);
        if (!NT_SUCCESS(status))
        {
            ImageName = NULL;
        }
        DbgPrint("%p\n", Process->UniqueProcessId);
        DbgPrint("%p\n", ImageName);
        DbgPrint("%p\n", &ImageInfoEx);
        DbgPrint("%p\n", FileObject);
        //Call the notify routines
        if ((CurProcName->Length != 0) && (wcsstr(CurProcName->Buffer, L"Dbgview.exe")))//测试用 
        {
            PsCallImageNotifyRoutines(
                ImageName,
                &Process->UniqueProcessId,
                &ImageInfoEx,
                FileObject);
        }
        else
        {
            PsCallImageNotifyRoutines(
                ImageName,
                Process->UniqueProcessId,
                &ImageInfoEx,
                FileObject);
        }
 
 
        if (ImageName)
        {
            ExFreePoolWithTag(ImageName, 0);
        }
        //PsReferenceProcessFilePointer增加了引用计数
        //ObfDereferenceObject(FileObject);
        //__debugbreak();
        index = 0;
        while (index < 2)
        {
            ModuleInfo = (PMODULE_INFO)PsQuerySystemDllInfo(index);
            //ModuleInfo2 = (PMODULE_INFO)PsQuerySystemDllInfo2_(index);
            //DbgPrint("ModuleInfo:%p\n", ModuleInfo);
 
            if (ModuleInfo != NULL)
            {
 
                ImageInfoEx.ImageInfo.Properties = 0;
                ImageInfoEx.ImageInfo.ImageAddressingMode = IMAGE_ADDRESSING_MODE_32BIT;
                ImageInfoEx.ImageInfo.ImageBase = ModuleInfo->BaseOfDll;
                ImageInfoEx.ImageInfo.ImageSize = 0;
 
                _try{
                    NtHeaders = RtlImageNtHeader(ModuleInfo->BaseOfDll);
                    if (NtHeaders)
                    {
                        ImageInfoEx.ImageInfo.ImageSize = NtHeaders->OptionalHeader.SizeOfImage;
                    }
                }_except(EXCEPTION_EXECUTE_HANDLER) {
                    ImageInfoEx.ImageInfo.ImageSize = 0;
                }
 
                ImageInfoEx.ImageInfo.ImageSelector = 0;
                ImageInfoEx.ImageInfo.ImageSectionNumber = 0;
 
                //实际就是PspSystemDlls
 
                SystemDll = (PSYSTEM_DLL)((ULONG_PTR)ModuleInfo - 0x10);
                Object = ObFastReferenceObject(&SystemDll->FastRef);
 
                if (Object == NULL)
                {
                    KeEnterCriticalRegion();
 
                    ExfAcquirePushLockShared((ULONG_PTR)&SystemDll->Lock);
 
                    Object = ObFastReferenceObjectLocked(&SystemDll->FastRef);
 
                    ExfReleasePushLockShared((ULONG_PTR)&SystemDll->Lock);
 
                    KeLeaveCriticalRegion();
 
                }
                //这段可能有问题
                CurrentThread = (PKTHREAD_S)KeGetCurrentThread();
                //判断APC链表是否为空
                if (&CurrentThread->ApcState.ApcListHead[0] != CurrentThread->ApcState.ApcListHead[0].Flink)
                {
                    //判断APC是否禁用
                    if (CurrentThread->SpecialApcDisable != 0)
                    {
                        KiCheckForKernelApcDelivery();
                    }
                }
                //获取文件对象
                //__debugbreak();
                FileObject = (PFILE_OBJECT)ObFastReferenceObject(&((PSEGMENT_OBJECT)Object)->Subsection->ControlArea->FilePointer);
 
                if (FileObject == 0)
                {
                    //DbgPrint("%p\n", Object);
                    Object  =MiSectionControlArea2_(Object);
                    //DbgPrint("%p\n", Object);
                    FileObject = MiReferenceControlAreaFile((PCONTROL_AREA)Object);//这里蓝屏了   已解决了
                    //FileObject = MiReferenceControlAreaFile2_((PCONTROL_AREA)Object);//这里蓝屏了
                }
                if (FileObject != NULL)
                {
 
                    ObFastDereferenceObject(
                        &SystemDll->FastRef,
                        FileObject);
                }
 
                if ((CurProcName->Length != 0) && (wcsstr(CurProcName->Buffer, L"Dbgview.exe")))
                {
                    PsCallImageNotifyRoutines(
                        &SystemDll->ModuleInfo.FileName,
                        &Process->UniqueProcessId,
                        &ImageInfoEx,
                        FileObject);
                }
                else
                {
                    PsCallImageNotifyRoutines(
                        &SystemDll->ModuleInfo.FileName,
                        Process->UniqueProcessId,
                        &ImageInfoEx,
                        FileObject);
                }
 
 
 
 
 
 
                //ObfDereferenceObject(FileObject);
            }
            //ObfDereferenceObject(FileObject);
            index++;
        }
    }
    //---------------------------------------
 
        PVOID64 addr=(PVOID64)(ULONG64(Process) + 0x420);
    //    DbgPrint("Process: %p\n", (PVOID64)(ULONG64(Process) + 0x420));
 
        RtlMoveMemory(&DebugObject, (PVOID)addr, 8);
        DbgPrint("Process1: %p\n", Process);
    //    ULONG64 tt = DebugObject;
    //    RtlMoveMemory(&DebugObject, (PVOID)tt, 8);
    //    DbgPrint("%llX", DebugObject);
 
    //DebugObject = (PDEBUG_OBJECT)Process->DebugPort;
 
 
 
 
    if (DebugObject == NULL) {
        return;
    }
    __debugbreak();
    if ((OldFlags & 0x1) == 0)
    {
 
        CreateThreadArgs = &m.u.CreateProcessInfo.InitialThread;
        CreateThreadArgs->SubSystemKey = 0;
 
        CreateProcessArgs = &m.u.CreateProcessInfo;
        CreateProcessArgs->SubSystemKey = 0;
        CreateProcessArgs->FileHandle = DbgkpSectionToFileHandle(
            &Process->SectionObject//3B8
        );
 
        CreateProcessArgs->BaseOfImage = &Process->SectionBaseAddress;//3c0
        CreateThreadArgs->StartAddress = NULL;
        CreateProcessArgs->DebugInfoFileOffset = 0;
        CreateProcessArgs->DebugInfoSize = 0;
 
        _try{
 
            NtHeaders = RtlImageNtHeader(&Process->SectionBaseAddress);
 
            if (NtHeaders) {
 
 
                CreateThreadArgs->StartAddress = (PVOID)(NtHeaders->OptionalHeader.ImageBase + NtHeaders->OptionalHeader.AddressOfEntryPoint);
 
                CreateProcessArgs->DebugInfoFileOffset = NtHeaders->FileHeader.PointerToSymbolTable;
                CreateProcessArgs->DebugInfoSize = NtHeaders->FileHeader.NumberOfSymbols;
            }
        } _except(EXCEPTION_EXECUTE_HANDLER) {
            CreateThreadArgs->StartAddress = NULL;
            CreateProcessArgs->DebugInfoFileOffset = 0;
            CreateProcessArgs->DebugInfoSize = 0;
        }
        m.h.u1.Length = 0x600038;
        m.h.u2.ZeroInit = 8;
        m.ApiNumber = DbgKmCreateProcessApi;
 
        __debugbreak();
        DbgkpSendApiMessage(PsGetCurrentProcess(),FALSE, &m);
 
        if (CreateProcessArgs->FileHandle != NULL) {
            ObCloseHandle(CreateProcessArgs->FileHandle, KernelMode);
        }
 
        DbgkSendSystemDllMessages
        (
            NULL,
            NULL,
            &m);
    }
    else {
 
        CreateThreadArgs = &m.u.CreateThread;
        CreateThreadArgs->SubSystemKey = 0;
        CreateThreadArgs->StartAddress = Thread->Win32StartAddress;
        m.h.u1.Length = 0x400018;
        m.h.u2.ZeroInit = 8;
        m.ApiNumber = DbgKmCreateThreadApi;
        DbgkpSendApiMessage(PsGetCurrentProcess(),TRUE, &m);
    }
 
    if (Thread->ClonedThread == TRUE)
    {
        DbgkpPostModuleMessages(
            (PEPROCESS)Process,
            (PETHREAD)Thread,
            NULL);
    }
 
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

最后于 2020-9-29 19:47 被叽叽‘and1=1编辑 ,原因:
收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//