-
Notifications
You must be signed in to change notification settings - Fork 47
/
syscall.c
442 lines (353 loc) · 14.8 KB
/
syscall.c
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
#include "syscall.h"
#pragma region GlobalVariables
PVOID rhwhpqe;
HANDLE zjmdf;
HANDLE gNtdql;
UINT64 nqFncAddr;
UINT64 k32FncAddr;
UINT64 rgGdtAddr;
UINT64 stkArgs[ARG_LEN];
UINT64 clRgGdtAddr;
UINT64 clRgGdtAddrRt;
char clRgGdtVal;
UINT64 rgBkp;
#pragma endregion
#pragma region BinaryPatternMatching
BOOL CmprMsk(const BYTE* dt, const BYTE* msk, const char* szMsk)
{
for (; *szMsk; ++szMsk, ++dt, ++msk)
if (*szMsk == 'x' && *dt != *msk)
return FALSE;
return TRUE;
}
DWORD_PTR FndPtrn(DWORD_PTR dAddr, DWORD dLen, PBYTE msk, PCHAR szMsk)
{
for (DWORD i = 0; i < dLen; i++)
if (CmprMsk((PBYTE)(dAddr + i), msk, szMsk))
return (DWORD_PTR)(dAddr + i);
return 0;
}
DWORD_PTR FndInMdl(LPCSTR mdlName, PBYTE msk, PCHAR szMsk)
{
DWORD_PTR dAddr = 0;
PIMAGE_DOS_HEADER imgBase = (PIMAGE_DOS_HEADER)GetModuleHandleA(mdlName);
if (!imgBase)
return 0;
DWORD_PTR sctnOffset = (DWORD_PTR)imgBase + imgBase->e_lfanew + sizeof(IMAGE_NT_HEADERS);
if (!sctnOffset)
return 0;
PIMAGE_SECTION_HEADER txtSctn = (PIMAGE_SECTION_HEADER)(sctnOffset);
dAddr = FndPtrn((DWORD_PTR)imgBase + txtSctn->VirtualAddress, txtSctn->SizeOfRawData, msk, szMsk);
return dAddr;
}
#pragma endregion
#pragma region PEBGetProcAddress
UINT64 GtMdlAddr(LPWSTR mdlName) {
PPEB peb = (PPEB)__readgsqword(X64_PEB_OFF);
LIST_ENTRY* MdlList = NULL;
if (!mdlName)
return 0;
for (LIST_ENTRY* pListEntry = peb->LoaderData->InMemoryOrderModuleList.Flink;
pListEntry != &peb->LoaderData->InMemoryOrderModuleList;
pListEntry = pListEntry->Flink) {
PLDR_DATA_TABLE_ENTRY pEntry = CONTAINING_RECORD(pListEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
if (wcsstr(pEntry->FullDllName.Buffer, mdlName)) {
return (UINT64)pEntry->DllBase;
}
}
return 0;
}
UINT64 GtSymbAddr(UINT64 mdlBase, const char* fncName) {
UINT64 fncAddr = 0;
PIMAGE_DOS_HEADER dosHdr = (PIMAGE_DOS_HEADER)mdlBase;
// Checking that the image is valid PE file.
if (dosHdr->e_magic != IMAGE_DOS_SIGNATURE) {
return 0;
}
PIMAGE_NT_HEADERS ntHdrs = (PIMAGE_NT_HEADERS)(mdlBase + dosHdr->e_lfanew);
if (ntHdrs->Signature != IMAGE_NT_SIGNATURE) {
return fncAddr;
}
IMAGE_OPTIONAL_HEADER optHdr = ntHdrs->OptionalHeader;
if (optHdr.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress == 0) {
return fncAddr;
}
// Iterating the export directory.
PIMAGE_EXPORT_DIRECTORY expDir = (PIMAGE_EXPORT_DIRECTORY)(mdlBase + optHdr.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
DWORD* addrs = (DWORD*)(mdlBase + expDir->AddressOfFunctions);
WORD* ordnls = (WORD*)(mdlBase + expDir->AddressOfNameOrdinals);
DWORD* nms = (DWORD*)(mdlBase + expDir->AddressOfNames);
for (DWORD j = 0; j < expDir->NumberOfNames; j++) {
if (_stricmp((char*)(mdlBase + nms[j]), fncName) == 0) {
fncAddr = mdlBase + addrs[ordnls[j]];
break;
}
}
return fncAddr;
}
#pragma endregion
#pragma region HalosGate
// DWORD64 FndSysclNum(DWORD64 fncAddr) {
// WORD sysclNum = 0;
// for (WORD idx = 1; idx <= 500; idx++) {
// // check neighboring syscall down
// if (*((PBYTE)fncAddr + idx * DN) == 0x4c
// && *((PBYTE)fncAddr + 1 + idx * DN) == 0x8b
// && *((PBYTE)fncAddr + 2 + idx * DN) == 0xd1
// && *((PBYTE)fncAddr + 3 + idx * DN) == 0xb8
// && *((PBYTE)fncAddr + 6 + idx * DN) == 0x00
// && *((PBYTE)fncAddr + 7 + idx * DN) == 0x00) {
// BYTE high = *((PBYTE)fncAddr + 5 + idx * DN);
// BYTE low = *((PBYTE)fncAddr + 4 + idx * DN);
// sysclNum = (high << 8) | low - idx;
// DPNT("[+] SSN: 0x%X\n", sysclNum);
// break;
// }
// // check neighboring syscall up
// if (*((PBYTE)fncAddr + idx * UP) == 0x4c
// && *((PBYTE)fncAddr + 1 + idx * UP) == 0x8b
// && *((PBYTE)fncAddr + 2 + idx * UP) == 0xd1
// and*((PBYTE)fncAddr + 3 + idx * UP) == 0xb8
// && *((PBYTE)fncAddr + 6 + idx * UP) == 0x00
// && *((PBYTE)fncAddr + 7 + idx * UP) == 0x00) {
// BYTE high = *((PBYTE)fncAddr + 5 + idx * UP);
// BYTE low = *((PBYTE)fncAddr + 4 + idx * UP);
// sysclNum = (high << 8) | low + idx;
// DPNT("[+] SSN: 0x%X\n", sysclNum);
// break;
// }
// }
// if (sysclNum == 0)
// DPNT("[-] SSN not found\n");
// return sysclNum;
// }
DWORD64 FndSysclNum(DWORD64 fncAddr) {
WORD syscallNumber = 0;
BOOL found = FALSE;
if (*((PBYTE)fncAddr) == 0x4c
&& *((PBYTE)fncAddr + 1) == 0x8b
&& *((PBYTE)fncAddr + 2) == 0xd1
&& *((PBYTE)fncAddr + 3) == 0xb8
&& *((PBYTE)fncAddr + 6) == 0x00
&& *((PBYTE)fncAddr + 7) == 0x00) {
BYTE high = *((PBYTE)fncAddr + 5);
BYTE low = *((PBYTE)fncAddr + 4);
syscallNumber = (high << 8) | low;
found = TRUE;
DPNT("[+] Found SSN: 0x%X\n", syscallNumber);
return syscallNumber;
}
// Enhanced check for jumps at the beginning, similar to Method 2
if (*((PBYTE)fncAddr) == 0xe9 ||
*((PBYTE)fncAddr + 3) == 0xe9 ||
*((PBYTE)fncAddr + 8) == 0xe9 ||
*((PBYTE)fncAddr + 10) == 0xe9 ||
*((PBYTE)fncAddr + 12) == 0xe9) {
// After finding a jump, start looking for the syscall pattern.
for (WORD idx = 1; idx <= 500; idx++) {
// Checking DN direction for the syscall pattern
if (*((PBYTE)fncAddr + idx * DN) == 0x4c
&& *((PBYTE)fncAddr + 1 + idx * DN) == 0x8b
&& *((PBYTE)fncAddr + 2 + idx * DN) == 0xd1
&& *((PBYTE)fncAddr + 3 + idx * DN) == 0xb8
&& *((PBYTE)fncAddr + 6 + idx * DN) == 0x00
&& *((PBYTE)fncAddr + 7 + idx * DN) == 0x00) {
BYTE high = *((PBYTE)fncAddr + 5 + idx * DN);
BYTE low = *((PBYTE)fncAddr + 4 + idx * DN);
syscallNumber = (high << 8) | low - idx;
found = TRUE;
DPNT("[+] Found SSN: 0x%X\n", syscallNumber);
break;
}
// Checking UP direction for the syscall pattern
if (*((PBYTE)fncAddr + idx * UP) == 0x4c
&& *((PBYTE)fncAddr + 1 + idx * UP) == 0x8b
&& *((PBYTE)fncAddr + 2 + idx * UP) == 0xd1
&& *((PBYTE)fncAddr + 3 + idx * UP) == 0xb8
&& *((PBYTE)fncAddr + 6 + idx * UP) == 0x00
&& *((PBYTE)fncAddr + 7 + idx * UP) == 0x00) {
BYTE high = *((PBYTE)fncAddr + 5 + idx * UP);
BYTE low = *((PBYTE)fncAddr + 4 + idx * UP);
syscallNumber = (high << 8) | low + idx;
found = TRUE;
DPNT("[+] Found SSN: 0x%X\n", syscallNumber);
break;
}
}
}
// for (WORD idx = 1; idx <= 500; idx++) {
// // check neighboring syscall down
// if (*((PBYTE)fncAddr + idx * DN) == 0x4c
// && *((PBYTE)fncAddr + 1 + idx * DN) == 0x8b
// && *((PBYTE)fncAddr + 2 + idx * DN) == 0xd1
// && *((PBYTE)fncAddr + 3 + idx * DN) == 0xb8
// && *((PBYTE)fncAddr + 6 + idx * DN) == 0x00
// && *((PBYTE)fncAddr + 7 + idx * DN) == 0x00) {
// BYTE high = *((PBYTE)fncAddr + 5 + idx * DN);
// BYTE low = *((PBYTE)fncAddr + 4 + idx * DN);
// syscallNumber = (high << 8) | low - idx;
// found = TRUE;
// DPNT("[+] Found SSN: 0x%X\n", syscallNumber);
// break;
// }
// // check neighboring syscall up
// if (*((PBYTE)fncAddr + idx * UP) == 0x4c
// && *((PBYTE)fncAddr + 1 + idx * UP) == 0x8b
// && *((PBYTE)fncAddr + 2 + idx * UP) == 0xd1
// && *((PBYTE)fncAddr + 3 + idx * UP) == 0xb8
// && *((PBYTE)fncAddr + 6 + idx * UP) == 0x00
// && *((PBYTE)fncAddr + 7 + idx * UP) == 0x00) {
// BYTE high = *((PBYTE)fncAddr + 5 + idx * UP);
// BYTE low = *((PBYTE)fncAddr + 4 + idx * UP);
// syscallNumber = (high << 8) | low + idx;
// DPNT("[+] Found SSN: 0x%X\n", syscallNumber);
// break;
// }
// }
if (syscallNumber == 0)
DPNT("[-] Could not find SSN\n");
return syscallNumber;
}
DWORD64 FndSysclRtnAddr(DWORD64 fncAddr) {
DWORD64 sysclRtnAddr = 0;
for (WORD idx = 1; idx <= 32; idx++) {
if (*((PBYTE)fncAddr + idx) == 0x0f && *((PBYTE)fncAddr + idx + 1) == 0x05) {
sysclRtnAddr = (DWORD64)((PBYTE)fncAddr + idx);
DPNT("[+] \"syscall;ret;\" addr: 0x%I64X\n", sysclRtnAddr);
break;
}
}
if (sysclRtnAddr == 0)
DPNT("[-] \"syscall;ret;\" addr not found\n");
return sysclRtnAddr;
}
#pragma endregion
UINT64 PrprSyscl(char* fncName) {
return nqFncAddr;
}
bool StMainBP() {
// Dynamically find the GetThreadContext and SetThreadContext functions
GtThrdCtxt_t pGtThrdCtxt = (GtThrdCtxt_t)GtSymbAddr(GtMdlAddr((LPWSTR)L"KERNEL32.DLL"), "GetThreadContext");
StThrdCtxt_t pStThrdCtxt = (StThrdCtxt_t)GtSymbAddr(GtMdlAddr((LPWSTR)L"KERNEL32.DLL"), "SetThreadContext");
DWORD old = 0;
CONTEXT ctx = { 0 };
ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
// Get current thread context
pGtThrdCtxt(zjmdf, &ctx);
// Set hardware breakpoint on PrprSyscl function
ctx.Dr0 = (UINT64)&PrprSyscl;
ctx.Dr7 |= (1 << 0);
ctx.Dr7 &= ~(1 << 16);
ctx.Dr7 &= ~(1 << 17);
ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
// Apply the modified context to the current thread
if (!pStThrdCtxt(zjmdf, &ctx)) {
DPNT("[-] Thread context set failed: 0x%X", GetLastError());
return false;
}
DPNT("[+] Main HWBP set\n");
return true;
}
LONG HWScExHndlr(EXCEPTION_POINTERS* ExInfo) {
if (ExInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP) {
if (ExInfo->ContextRecord->Rip == (DWORD64)&PrprSyscl) {
DPNT("\n===============HWSCALLS DEBUG===============");
DPNT("\n[+] PrprSyscl BP Hit (%#llx)!\n", ExInfo->ExceptionRecord->ExceptionAddress);
// Find the address of the syscall function in ntdll we got as the first argument of the PrprSyscl function
nqFncAddr = GtSymbAddr((UINT64)gNtdql, (const char*)(ExInfo->ContextRecord->Rcx));
DPNT("[+] Found %s addr: 0x%I64X\n", (const char*)(ExInfo->ContextRecord->Rcx), nqFncAddr);
// Move breakpoint to the NTAPI function;
DPNT("[+] Moving BP to %#llx\n", nqFncAddr);
ExInfo->ContextRecord->Dr0 = nqFncAddr;
}
else if (ExInfo->ContextRecord->Rip == (DWORD64)nqFncAddr) {
DPNT("[+] NTAPI Func BP Hit (%#llx)!\n", (DWORD64)ExInfo->ExceptionRecord->ExceptionAddress);
// Create a new stack to spoof the kernel32 function address
// The stack size will be 0x70 which is compatible with the RET_GADGET we found.
// sub rsp, 70
ExInfo->ContextRecord->Rsp -= 0x70;
// mov rsp, RG_GDT_ADDR
*(PULONG64)(ExInfo->ContextRecord->Rsp) = rgGdtAddr;
DPNT("[+] New stack frame with RG_GDT as return addr\n", rgGdtAddr);
// Copy the stack arguments from the original stack
for (size_t idx = 0; idx < ARG_LEN; idx++)
{
const size_t offset = idx * ARG_LEN + ARG_RSP_OFF;
*(PULONG64)(ExInfo->ContextRecord->Rsp + offset) = *(PULONG64)(ExInfo->ContextRecord->Rsp + offset + 0x70);
}
DPNT("[+] Stack args copied to new stack\n");
DWORD64 pFncAddr = ExInfo->ContextRecord->Rip;
// char nonHookedSyscallBytes[] = { 0x4C,0x8B,0xD1,0xB8 };
unsigned char nhSysclBytes[] = { 0x4C, 0x8B, 0xD1, 0xB8 };
if (FndPtrn(pFncAddr, 4, (PBYTE)nhSysclBytes, (PCHAR)"xxxx")) {
DPNT("[+] Func not hooked\n");
DPNT("[+] Continuing normal exec\n");
}
else {
DPNT("[+] Func HOOKED!\n");
DPNT("[+] Finding SSN via Halos Gate\n");
/// Replace
WORD sysclNum = FndSysclNum(pFncAddr);
if (sysclNum == 0) {
ExInfo->ContextRecord->Dr0 = clRgGdtAddrRt;
return EXCEPTION_CONTINUE_EXECUTION;
}
/// replace
DWORD64 sysclRtnAddr = FndSysclRtnAddr(pFncAddr);
if (sysclRtnAddr == 0) {
ExInfo->ContextRecord->Dr0 = clRgGdtAddrRt;
return EXCEPTION_CONTINUE_EXECUTION;
}
// mov r10, rcx
DPNT("[+] RCX to R10 (mov r10, rcx)\n");
ExInfo->ContextRecord->R10 = ExInfo->ContextRecord->Rcx;
//mov eax, SSN
DPNT("[+] SSN to RAX (mov rax, 0x%X)\n", sysclNum);
ExInfo->ContextRecord->Rax = sysclNum;
//Set RIP to syscall;ret; opcode addr
DPNT("[+] Jump to \"syscall;ret;\" addr: 0x%I64X\n", sysclRtnAddr);
ExInfo->ContextRecord->Rip = sysclRtnAddr;
}
// Move BP back to PrprSyscl to catch next invoke
DPNT("[+] BP back to PrprSyscl for next invoke\n");
ExInfo->ContextRecord->Dr0 = (UINT64)&PrprSyscl;
DPNT("=============================================\n\n");
}
return EXCEPTION_CONTINUE_EXECUTION;
}
return EXCEPTION_CONTINUE_SEARCH;
}
bool FndRtGdt() {
// Dynamically search for a suitable "ADD RSP,68;RET" gadget in both kernel32 and kernelbase
rgGdtAddr = FndInMdl("KERNEL32.DLL", (PBYTE)"\x48\x83\xC4\x68\xC3", (PCHAR)"xxxxx");
if (rgGdtAddr != 0) {
DPNT("[+] Found RET_GADGET in kernel32.dll: %#llx\n", rgGdtAddr);
return true;
}
else {
rgGdtAddr = FndInMdl("kernelbase.dll", (PBYTE)"\x48\x83\xC4\x68\xC3", (PCHAR)"xxxxx");
DPNT("[+] Found RET_GADGET in kernelbase.dll: %#llx\n", rgGdtAddr);
if (rgGdtAddr != 0) {
return true;
}
}
return false;
}
bool OpnRICls() {
zjmdf = GetCurrentThread();
gNtdql = (HANDLE)GtMdlAddr((LPWSTR)L"ntdll.dll");
if (!FndRtGdt()) {
DPNT("[!] Proper \"ADD RSP,68;RET\" gadget not found. OpnRICls failed.");
return false;
}
// Register exception handler
rhwhpqe = AddVectoredExceptionHandler(1, &HWScExHndlr);
if (!rhwhpqe) {
DPNT("[!] VEH registration failed: 0x%X\n", GetLastError());
return false;
}
return StMainBP();
}
bool ClsRICls() {
return RemoveVectoredExceptionHandler(rhwhpqe) != 0;
}