-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkillprocess.cpp
More file actions
362 lines (329 loc) · 8.32 KB
/
Copy pathkillprocess.cpp
File metadata and controls
362 lines (329 loc) · 8.32 KB
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
#include "common.h"
#include "TlHelp32.h"
#include "osver.h"
#include "IAT_PRIV.h"
#include "IAT_NTDLL.H"
#include "PEHeaderHandling.h"
#include <ntstatus.h>
bool _cdecl StringToNumber_ForKillProcess(const char* input, int* output);
bool RemoteExitProcess(int PID, LWAnsiString* output)
{
SetLastError(0);
HANDLE hProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, PID);
if (hProcess != 0)
{
if (output != 0)
{
LWAnsiString_AppendA(output, "Process opened ok with the needed permssions\r\n");
LWAnsiString_AppendA(output, "Notice; CUrrently this mode is not implemented yet\r\n");
}
CloseHandle(hProcess);
return false;
}
else
{
if (output != 0)
{
LWAnsiString_AppendA(output, "Process couldn't be opened with the needed permssions\r\n");
}
return false;
}
}
bool DebugSnipe(int PIDLIST, int* result, const char** message_result, LWAnsiString* output)
{
if (output != 0)
{
LWAnsiString_AppendA(output, "Attempting to attach as a debugger (which on exit of will have Windows itself end the process): ");
}
SetLastError(0);
if (DebugActiveProcess((DWORD)PIDLIST) && (GetLastError() == 0))
{
if (output)
{
LWAnsiString_AppendA(output, "=> SUCESS\r\n");
}
if (DebugSetProcessKillOnExit(TRUE) && (GetLastError() == 0))
{
LWAnsiString_AppendA(output, "=> Requested Windows to end the target once midas exits\r\n");
// we're done
return true;
}
}
if (output != 0)
{
LWAnsiString_AppendA(output, "Could not attach as debugger. Could be ACLs/permission or target is not same bitness as midas.\r\n");
}
return false;
}
bool SnipeViaPID(int PIDList, int*result, const char** message_result, LWAnsiString* output)
{
bool ret = false;
if (output != 0)
{
LWAnsiString_AppendA(output, "Attempting to open handle to process: ");
}
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, PIDList);
if (hProcess == NULL)
{
if (output != 0)
{
LWAnsiString_AppendA(output, "=> FAILD!\r\n");
}
*message_result = "Either we don't have permssion to open the process or the PID is wrong";
return ret;
}
else
{
if (output != 0)
{
LWAnsiString_AppendA(output, "=> SUCCESS!\r\n");
}
if (TerminateProcess(hProcess, 0) )
{
if (output != 0)
{
LWAnsiString_AppendA(output, "Attempting call to directly terminate process worked. Confirming process is DOA\r\n");
}
DWORD await = WaitForSingleObject(hProcess, 250);
if (await != WAIT_OBJECT_0)
{
if (output != 0)
{
LWAnsiString_AppendA(output, "Waiting for the process to exit reveals it's still alive.\r\n");
}
ret = false;
}
else
{
if (output != 0)
{
LWAnsiString_AppendA(output, "Waiting for the process to exit reveals it's still doa.\r\n");
}
ret = true;
}
}
else
{
if (output != 0)
{
LWAnsiString_AppendA(output, "Attempting call to directly terminate process failed.\r\n");
}
}
CloseHandle(hProcess);
return ret;
}
}
bool AskForDebugPriv()
{
bool ret = false;
if ((IAT_DynamicLink_TokenPriv_Advapi32(IAT_PRIV_LINKING_ADJUSTTOKENPRIV | IAT_PRIV_LINKING_LOOKUPPRIVA | IAT_PRIV_LINKING_OPENPROCESSTOKEN)) == (IAT_PRIV_LINKING_LOOKUPPRIVA| IAT_PRIV_LINKING_ADJUSTTOKENPRIV | IAT_PRIV_LINKING_OPENPROCESSTOKEN))
{
TOKEN_PRIVILEGES Privs;
LUID luid;
HANDLE self = 0;
SetLastError(0);
if (IAT_OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &self))
{
if (IAT_LookupPrivilegeValueA(0, SE_DEBUG_NAME, &luid))
{
Privs.PrivilegeCount = 1;
Privs.Privileges[0].Luid = luid;
Privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (IAT_AdjustTokenPriv(self, false, &Privs, 0, 0, 0))
{
if (GetLastError() != ERROR_NOT_ALL_ASSIGNED)
{
if (GetLastError() == 0)
ret = true;
else
ret = false;
}
else
ret = false;
}
}
CloseHandle(self);
}
}
return ret;
}
bool SnipeViaPID_NtOnly(int PIDList, int* result, const char** message_result, LWAnsiString* output)
{
if (output)
{
LWAnsiString_AppendA(output, "Attempting to use direct call to ntdll to terminate the process\r\n");
}
{
bool ret = false;
if (IAT_NtTerminateProcess == 0)
{
if (output)
{
LWAnsiString_AppendA(output, "Skipping the call, due to required routine not found in ntdll.dll \r\n");
}
return ret;
}
SetLastError(0);
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, PIDList);
if (hProcess != 0)
{
if (output)
{
LWAnsiString_AppendA(output, "Got Handle to Process ok\r\n");
}
auto ok = IAT_NtTerminateProcess(hProcess, 0);
if (ok == STATUS_SUCCESS)
{
if (output)
{
LWAnsiString_AppendA(output, "Termiation request sent: ");
}
DWORD await = WaitForSingleObject(hProcess, 250);
if (await == WAIT_OBJECT_0)
{
if (output)
{
LWAnsiString_AppendA(output, "Wait signled ok. Windows reports process is gone.\r\n");
}
ret = true;
}
else
{
if (await == WAIT_TIMEOUT)
{
if (output)
{
LWAnsiString_AppendA(output, "Wait Signal Timeout. Windows reports process is alive somehow!?\r\n");
}
}
else
{
if (output)
{
LWAnsiString_AppendA(output, "Wait Signal Failure. Windows reports process is alive somehow.");
}
}
ret = false;
}
}
else
{
if (output)
{
LWAnsiString_AppendA(output, "NtTermiateProcess reports failure to send process exit request.");
}
}
if (hProcess != 0) CloseHandle(hProcess);
}
else
{
if (output)
{
LWAnsiString_AppendA(output, "Failed to get handle to try exiting the process\r\n");
}
}
return ret;
}
}
bool KillProcess(int* result, const char** message_result, const char* argv[], int argc)
{
LWAnsiString* output = LWAnsiString_CreateString(255);
FetchVersionInfo(&GlobalVersionInfo, &VERISON_INFO_IS_UNICODE);
bool some_fail = false;
int collection_offset = 0;
bool PID_MODE = false;
const char** collection = nullptr;
// what we're mostly doing is copying the command line args to a new array minus the first time and the /killprocess flag.
// we loop thru checking for the -PID flag and if we find it, we set the PID_MODE flag to true. We then copy the rest of the args to a new array.
collection = (const char**)LocalAlloc(LMEM_ZEROINIT, sizeof(char*) * (argc));
int collection_size = argc - 3;
if (collection == nullptr)
{
return false;
}
else
{
for (int i = 2; i < argc; i++)
{
if (lstrcmpiA("-PID", argv[i]) == 0)
{
PID_MODE = true;
}
else
{
collection[collection_offset] = argv[i];
collection_offset++;
}
}
}
if (PID_MODE)
{
bool DebugModeConfig = false;
for (int i = 0; i < collection_size; i++)
{
int PID = 0;
if (StringToNumber_ForKillProcess(collection[i], &PID))
{
LWAnsiString_AppendA(output, "Attempting to Stop Process with PID: ");
LWAnsiString_AppendA(output, collection[i]);
LWAnsiString_AppendNewLine(output);
if (PID != 0)
{
debug_retry:
LWAnsiString_AppendA(output, "Current Mode: DebugPriv ");
if (DebugModeConfig == false)
{
LWAnsiString_AppendA(output, "[OFF]\r\n");
}
else
{
LWAnsiString_AppendA(output, "[ON]\r\n");
}
bool res = SnipeViaPID(PID, result, message_result, output);
if (!res)
{
res = DebugSnipe(PID, result, message_result, output);
}
if (!res)
{
if (GlobalVersionInfo.A.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
if (IAT_DynamicLink_NTDLL(IAT_NTDLL_LINKING_NTTERMINATEPROCESS) == IAT_NTDLL_LINKING_NTTERMINATEPROCESS)
{
// ok we're go for attmepting to terminate the process again
res = SnipeViaPID_NtOnly(PID, result, message_result, output);
}
}
}
if (!res)
{
res = RemoteExitProcess(PID, output);
}
if (DebugModeConfig == false)
{
if (res == false)
{
if (!AskForDebugPriv())
{
LWAnsiString_AppendA(output, "Unable to enable SeDebugPriv. Try running as admin.\r\n");
goto clean;
}
else
{
DebugModeConfig = true;
goto debug_retry;
}
}
}
}
}
else
{
}
}
}
clean:
WriteStdout(output);
LWAnsiString_FreeString(output);
return true;
}