-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkillcharms.cpp
444 lines (372 loc) · 8.34 KB
/
killcharms.cpp
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// File: charmkiller.cpp
// Author: Michael Bailey
// License: WTFPL - http://www.wtfpl.net/
//
// Adapted from MSDN:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682621%28v=vs.85%
// 29.aspx
// And from "How to get the module name associated with a thread" (Rohitab):
// http://www.rohitab.com/discuss/topic/36675-how-to-get-the-module-name-associ
// ated-with-a-thread/
#include <windows.h>
#include <Tlhelp32.h>
#include <tchar.h>
#include <stdio.h>
#include <psapi.h>
// TODO: To ensure correct symbol resolution, compile with -DPSAPI_VERSION=1
#pragma comment(lib, "psapi.lib")
#define CONFIG_IMMASCULATE 0
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#define STATUS_ACCESS_IS_DENIED ((NTSTATUS)0x00000005L)
#define IMMERSIVE_SHELL_DLL "windows.immersiveshell.serviceprovider.dll"
#define EXPLORER_EXE "explorer.exe"
#define ThreadQuerySetWin32StartAddress 9
struct ThreadSpec
{
DWORD Pid;
HANDLE hProcess;
ULONG_PTR StartAddress;
ULONG_PTR EndAddress;
HMODULE hModule;
};
typedef NTSTATUS (WINAPI *NTQUERYINFOMATIONTHREAD)(
HANDLE,
LONG,
PVOID,
ULONG,
PULONG
);
BOOL InitializeFuncPtrs(void);
int strchr_rev(char *stack, char needle);
char *FinalComponent(char *name);
int FinalComponentIs(char *path, char *name);
BOOL AllocAndEnumProcs(DWORD **Pids, int *PidsLen);
template <class T> BOOL _AllocAndEnumItems(
T **Array,
int *Count,
HANDLE hProc=NULL
);
HANDLE FindProc(DWORD *Pids, int PidsLen, char *ProcNameSought);
HMODULE FindMod(HANDLE hProc, char *ModNameSought);
HANDLE FindThread(HANDLE hProc, HMODULE hModImm);
// Globals
NTQUERYINFOMATIONTHREAD NtQueryInformationThread;
int
main(void)
{
int ret = 1;
BOOL Okay;
DWORD *Pids;
int PidsLen;
DWORD Pid, Tid;
HANDLE hProc, hThread;
HMODULE hModImm;
if (!InitializeFuncPtrs())
{
fprintf(
stderr,
"Failed to initialize function pointers, GLE=%d\n",
GetLastError()
);
goto end;
}
if (!AllocAndEnumProcs(&Pids, &PidsLen))
{
fprintf(stderr, "Failed to allocate space and enumerate processes\n");
goto end;
}
hProc = FindProc(Pids, PidsLen, EXPLORER_EXE);
if (hProc == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Failed to find %s\n", EXPLORER_EXE);
goto end;
}
Pid = GetProcessId(hProc);
printf("Located %s(%d)\n", EXPLORER_EXE, Pid);
hModImm = FindMod(hProc, IMMERSIVE_SHELL_DLL);
if (hModImm == 0)
{
fprintf(stderr, "Failed to find %s\n", IMMERSIVE_SHELL_DLL);
goto end;
}
printf("Located %s(0x%08X)\n", IMMERSIVE_SHELL_DLL, hModImm);
hThread = FindThread(hProc, hModImm);
if (hThread == NULL)
{
fprintf(stderr, "Failed to find thread\n");
goto end;
}
Tid = GetThreadId(hThread);
if (!TerminateThread(hThread, 1))
{
fprintf(
stderr,
"Failed to terminate thread, GLE=%d\n",
GetLastError()
);
goto end;
}
printf("Terminated thread %d\n", Tid);
end:
return ret;
}
// Link to NtQueryInformationThread()
BOOL
InitializeFuncPtrs(void)
{
HMODULE hNtdll;
hNtdll = LoadLibrary("ntdll.dll");
if (hNtdll == INVALID_HANDLE_VALUE)
{
return FALSE;
}
NtQueryInformationThread = (NTQUERYINFOMATIONTHREAD) GetProcAddress(
LoadLibrary("ntdll.dll"),
"NtQueryInformationThread"
);
if (NtQueryInformationThread == NULL)
{
return FALSE;
}
return TRUE;
}
BOOL
AllocAndEnumProcs(DWORD **Pids, int *PidsLen)
{
return _AllocAndEnumItems<DWORD>(Pids, PidsLen);
}
BOOL AllocAndEnumModules(HANDLE hProc, HMODULE **Mods, int *ModsLen)
{
return _AllocAndEnumItems<HMODULE>(Mods, ModsLen, hProc);
}
// Allocate progressively larger PID/HMODULE arrays up to Limit elements and
// repeatedly call EnumProcesses() / EnumProcessModules() to populate with the
// current list of PIDs/modules. In practice, 1024 is enough for the "Works On
// My Machine" level of quality, but I'm self-conscious about code that others
// may see.
template <class T>
BOOL
_AllocAndEnumItems(T **Array, int *Count, HANDLE hProc)
{
const int ArrayLen0 = 1024;
const int MaxLen = 32768;
DWORD Size, Needed;
BOOL Okay, Ret;
Ret = FALSE;
*Count = ArrayLen0;
while (1)
{
Size = *Count * sizeof(T);
*Array = (T *)LocalAlloc(0, Size);
if (*Array == NULL)
{
break;
}
// Casts are used to make these subtly different functions coexist in
// the template situations that they are NOT used for (HMODULE vs
// DWORD).
if (hProc != NULL) {
Okay = EnumProcessModules(hProc, (HMODULE *)*Array, Size, &Needed);
} else {
Okay = EnumProcesses((DWORD *)*Array, Size, &Needed);
}
if (!Okay)
{
break;
}
if (Needed < Size)
{
Ret = TRUE;
*Count = Needed / sizeof(T);
break;
}
// Set up for next resized allocation
LocalFree(*Array);
*Count += ArrayLen0;
if (*Count > MaxLen)
{
break;
}
}
return Ret;
}
HANDLE
FindProc(DWORD *Pids, int PidsLen, char *ProcNameSought)
{
HANDLE hProc;
HANDLE Ret = INVALID_HANDLE_VALUE;
DWORD Len;
int i;
char Name[MAX_PATH];
// For each process...
for (i=0; i<PidsLen; i++)
{
// Pop it open
hProc = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
Pids[i]
);
// If a given process cannot be opened, move on to the next
if ((hProc == NULL) || (hProc == INVALID_HANDLE_VALUE)) { continue; }
// Get the filename
Len = GetProcessImageFileName(hProc, Name, MAX_PATH);
// If a given process filename cannot be obtained, move on to the next
if (Len == 0) { continue; }
// If the process name is not what was sought, move on to the next
if (FinalComponentIs(Name, ProcNameSought))
{
Ret = hProc;
break;
}
}
return Ret;
}
HMODULE
FindMod(HANDLE hProc, char *ModNameSought)
{
HMODULE *Mods;
int ModsLen;
BOOL Okay;
int i;
TCHAR szModName[MAX_PATH];
HMODULE Ret = 0;
Okay = AllocAndEnumModules(hProc, &Mods, &ModsLen);
if (Okay)
{
for (i=0; i<ModsLen; i++)
{
Okay = GetModuleFileNameEx(
hProc,
Mods[i],
szModName,
sizeof(szModName) / sizeof(TCHAR)
);
if (!Okay)
{
continue;
}
if (FinalComponentIs(szModName, ModNameSought))
{
Ret = Mods[i];
break;
}
}
}
return Ret;
}
HANDLE
FindThread(HANDLE hProc, HMODULE hModImm)
{
ULONG_PTR Start, End;
MODULEINFO ModInfo;
HANDLE hSnapshot, hThread, Ret;
DWORD Pid;
THREADENTRY32 Thread = {0};
ULONG_PTR ThreadEntryAddr;
NTSTATUS Status;
Ret = hSnapshot = hThread = INVALID_HANDLE_VALUE;
if (!GetModuleInformation(hProc, hModImm, &ModInfo, sizeof(ModInfo)))
{
goto end;
}
// Query bounds for thread classification
Start = (ULONG_PTR) ModInfo.lpBaseOfDll;
End = (ULONG_PTR) ModInfo.lpBaseOfDll + ModInfo.SizeOfImage;
Pid = GetProcessId(hProc);
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, Pid);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
goto end;
}
Thread.dwSize = sizeof(Thread);
Thread.cntUsage = 0;
// TODO: confirm usage and verify return
if (!Thread32First(hSnapshot, &Thread))
{
goto end;
}
do
{
// Note to self: why is it necessary to check this. Why is the PID
// specified in the call to CreateToolhelp32Snapshot() not the only PID
// that appears in its results?
if (Thread.th32OwnerProcessID == Pid)
{
hThread = OpenThread(
#if CONFIG_IMMASCULATE
THREAD_QUERY_INFORMATION,
#else
THREAD_ALL_ACCESS,
#endif
FALSE,
Thread.th32ThreadID
);
if (hThread == INVALID_HANDLE_VALUE)
{
// Move on to the next
continue;
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms68428
// 3%28v=vs.85%29.aspx
Status = NtQueryInformationThread(
hThread,
ThreadQuerySetWin32StartAddress,
&ThreadEntryAddr,
sizeof(ThreadEntryAddr),
NULL
);
if (Status != STATUS_SUCCESS)
{
// Move on to the next
continue;
}
if ((ThreadEntryAddr >= Start) && (ThreadEntryAddr < End))
{
Ret = hThread;
break;
}
}
}
while (Thread32Next(hSnapshot, &Thread));
end:
if (hSnapshot != INVALID_HANDLE_VALUE)
{
CloseHandle(hSnapshot);
}
return hThread;
}
// I didn't think I'd have to implement this, but as far as I could tell at the
// time, it doesn't exist.
int
strchr_rev(char *stack, char needle)
{
size_t len;
int i;
len = strlen(stack);
for (i=len-1; i>0; i--)
{
if (stack[i] == needle)
{
return i;
}
}
return -1;
}
char
*FinalComponent(char *name)
{
int slash;
char *ret = name;
slash = strchr_rev(name, '\\');
if (slash != -1)
{
ret = (name + slash + 1);
}
return ret;
}
int
FinalComponentIs(char *path, char *name)
{
return (stricmp(FinalComponent(path), name) == 0);
}