-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathWrapper.Windows.cpp
549 lines (450 loc) · 13.5 KB
/
Wrapper.Windows.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#if defined _WIN32 || defined __CYGWIN__
//#include <Windows.h>
#include "Wrapper.Global.h"
#include "TokenRing.h"
#include "TokenRing.Windows.h"
#include "RevtracerWrapper.h"
#include "../CommonCrossPlatform/BasicTypes.h"
using namespace nodep;
#define CALL_API(LIB, FUNC, TYPE) ((TYPE)((unsigned char *)revwrapper::wrapperImports.libraries->winLib.##LIB##Base + revwrapper::wrapperImports.functions.winFunc.##LIB##.##FUNC))
typedef long NTSTATUS;
#ifndef NT_SUCCESS
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif
// ------------------- Memory allocation ----------------------
typedef NTSTATUS(__stdcall *AllocateMemoryHandler)(
HANDLE ProcessHandle,
LPVOID *BaseAddress,
DWORD ZeroBits,
SIZE_T *RegionSize,
DWORD AllocationType,
DWORD Protect);
LPVOID __stdcall Kernel32VirtualAllocEx(HANDLE hProcess, LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect) {
LPVOID addr = lpAddress;
if (lpAddress && (unsigned int)lpAddress < 0x10000) {
//RtlSetLastWin32Error(87);
}
else {
//NTSTATUS ret = ((AllocateMemoryHandler)((unsigned char *)libLayout->windows.ntdllBase + windowsFunctions.ntdll._virtualAlloc))(
NTSTATUS ret = CALL_API(ntdll, _virtualAlloc, AllocateMemoryHandler) (
hProcess,
&addr,
0,
&dwSize,
flAllocationType & 0xFFFFFFF0,
flProtect
);
if (NT_SUCCESS(ret)) {
return addr;
}
//SetLastError(ret);
}
return 0;
}
LPVOID __stdcall Kernel32VirtualAlloc(LPVOID lpAddress, size_t dwSize, DWORD flAllocationType, DWORD flProtect) {
return Kernel32VirtualAllocEx((HANDLE)0xFFFFFFFF, lpAddress, dwSize, flAllocationType, flProtect);
//unsigned long access, unsigned long offset, unsigned long size, void *address
}
#define MEM_COMMIT 0x1000
#define MEM_RESERVE 0x2000
#define MEM_DECOMMIT 0x4000
#define MEM_RELEASE 0x8000
#define PAGE_EXECUTE 0x10
#define PAGE_EXECUTE_READ 0x20
#define PAGE_EXECUTE_READWRITE 0x40
#define PAGE_EXECUTE_WRITECOPY 0x80
void *WinAllocateVirtual(unsigned long size) {
return Kernel32VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
}
// -------------------- Flush Memory Cache --------------------
typedef BOOL(*RtlFlushSecureMemoryCacheHandler)(
LPVOID MemoryCache,
SIZE_T MemoryLength
);
// ------------------- Memory deallocation --------------------
typedef NTSTATUS(*FreeMemoryHandler)(
HANDLE ProcessHandle,
LPVOID *BaseAddress,
PULONG RegionSize,
ULONG FreeType
);
BOOL Kernel32VirtualFreeEx(
HANDLE hProcess,
LPVOID lpAddress,
SIZE_T dwSize,
DWORD dwFreeType
) {
NTSTATUS ret;
if ((unsigned __int16)(dwFreeType & 0x8000) && dwSize) {
return FALSE;
} else {
ret = CALL_API(ntdll, _virtualFree, FreeMemoryHandler) (
hProcess,
&lpAddress,
(PULONG)dwSize,
dwFreeType
);
if (ret >= 0) {
return TRUE;
}
if ((0xC0000045 == ret) && ((HANDLE)0xFFFFFFFF == hProcess)) {
if (FALSE == CALL_API(ntdll, _flushMemoryCache, RtlFlushSecureMemoryCacheHandler) (lpAddress, dwSize)) {
return FALSE;
}
ret = CALL_API(ntdll, _virtualFree, FreeMemoryHandler) (
hProcess,
&lpAddress,
(PULONG)dwSize,
dwFreeType
);
return (ret >= 0) ? TRUE : FALSE;
} else {
return FALSE;
}
}
}
void WinFreeVirtual(void *address) {
Kernel32VirtualFreeEx((HANDLE)0xFFFFFFFF, &address, 0, MEM_RELEASE);
}
// ------------------- Process termination --------------------
typedef NTSTATUS(__stdcall *TerminateProcessHandler)(
HANDLE ProcessHandle,
NTSTATUS ExitStatus
);
void WinTerminateProcess(int retCode) {
CALL_API(ntdll, _terminateProcess, TerminateProcessHandler) ((HANDLE)0xFFFFFFFF, (NTSTATUS)retCode);
}
void *WinGetTerminationCodeFunc() {
return (void *)CALL_API(ntdll, _terminateProcess, TerminateProcessHandler);
}
// ------------------- Write file -----------------------------
void *GetTEB() {
DWORD r;
__asm mov eax, dword ptr fs : [0x18];
__asm mov r, eax
return (void *)r;
}
void *GetPEB(void *teb) {
return (void *)*((DWORD *)teb + 0x0C);
}
typedef struct _RTL_USER_PROCESS_PARAMETERS
{
ULONG MaximumLength;
ULONG Length;
ULONG Flags;
ULONG DebugFlags;
LPVOID ConsoleHandle;
ULONG ConsoleFlags;
LPVOID StandardInput;
LPVOID StandardOutput;
LPVOID StandardError;
} RTL_USER_PROCESS_PARAMETERS, *PRTL_USER_PROCESS_PARAMETERS;
typedef struct _IO_STATUS_BLOCK {
union {
NTSTATUS Status;
LPVOID Pointer;
};
ULONG_PTR Information;
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
PRTL_USER_PROCESS_PARAMETERS GetUserProcessParameters(void *peb) {
return (PRTL_USER_PROCESS_PARAMETERS)(*((DWORD *)peb + 0x10));
}
typedef NTSTATUS(__stdcall *WriteFileHandler)(
HANDLE FileHandle,
HANDLE Event,
LPVOID ApcRoutine,
LPVOID ApcContext,
PIO_STATUS_BLOCK IoStatusBlock,
LPVOID Buffer,
ULONG Length,
LPVOID ByteOffset,
LPVOID Key
);
typedef NTSTATUS(__stdcall *NtWaitForSingleObjectFunc)(
HANDLE Handle,
BOOL Alertable,
LPVOID Timeout
);
#define STATUS_WAIT_0 ((DWORD )0x00000000L)
#define STATUS_ABANDONED_WAIT_0 ((DWORD )0x00000080L)
#define STATUS_USER_APC ((DWORD )0x000000C0L)
#define STATUS_TIMEOUT ((DWORD )0x00000102L)
#define STATUS_PENDING ((DWORD )0x00000103L)
BOOL Kernel32WriteFile(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
DWORD *lpNumberOfBytesWritten
) {
IO_STATUS_BLOCK ioStatus;
ioStatus.Status = 0;
ioStatus.Information = 0;
if (lpNumberOfBytesWritten) {
*lpNumberOfBytesWritten = 0;
}
PRTL_USER_PROCESS_PARAMETERS upp = GetUserProcessParameters(GetPEB(GetTEB()));
HANDLE hIntFile = hFile;
switch ((DWORD)hFile) {
case 0xFFFFFFF4:
hIntFile = upp->StandardError;
break;
case 0xFFFFFFF5:
hIntFile = upp->StandardOutput;
break;
case 0xFFFFFFF6:
hIntFile = upp->StandardInput;
break;
};
NTSTATUS ret = CALL_API(ntdll, _writeFile, WriteFileHandler) (
hIntFile,
NULL,
NULL,
NULL,
&ioStatus,
lpBuffer,
nNumberOfBytesToWrite,
NULL,
NULL
);
if (ret == STATUS_PENDING) {
ret = CALL_API(ntdll, _waitForSingleObject, NtWaitForSingleObjectFunc) (
hIntFile,
FALSE,
NULL
);
if (ret < 0) {
if ((ret & 0xC0000000) == 0x80000000) {
*lpNumberOfBytesWritten = ioStatus.Information;
}
//DefaultSetLastError(ret);
return FALSE;
}
ret = ioStatus.Status;
}
if (ret >= 0) {
*lpNumberOfBytesWritten = ioStatus.Information;
return TRUE;
}
if ((ret & 0xC0000000) == 0x80000000) {
*lpNumberOfBytesWritten = ioStatus.Information;
}
//DefaultSetLastError(ret);
return FALSE;
}
bool WinWriteFile(void *handle, void *buffer, size_t size, unsigned long *written) {
return TRUE == Kernel32WriteFile(handle, buffer, size, written);
}
// ------------------- Events ---------------------------------
//TODO: fix compilation warnings on windows (we _ARE_ including Windows.h here)
#define CREATE_EVENT_MANUAL_RESET 1
#define CREATE_EVENT_INITIAL_SET 2
#define EVENT_ALL_ACCESS 0x1F0003
typedef struct _LSA_UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} LSA_UNICODE_STRING, *PLSA_UNICODE_STRING, UNICODE_STRING, *PUNICODE_STRING;
typedef struct _OBJECT_ATTRIBUTES {
ULONG Length;
HANDLE RootDirectory;
PUNICODE_STRING ObjectName;
ULONG Attributes;
LPVOID SecurityDescriptor;
LPVOID SecurityQualityOfService;
} OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;
int __stdcall BaseFormatObjectAttributes(OBJECT_ATTRIBUTES *objectAttributes)
{
objectAttributes->SecurityQualityOfService = 0;
objectAttributes->Attributes = 0;
//v7 = (void(__stdcall *)(OBJECT_ATTRIBUTES *))dword_10152984;
objectAttributes->RootDirectory = nullptr;
objectAttributes->Length = 24;
objectAttributes->ObjectName = nullptr;
objectAttributes->SecurityDescriptor = nullptr;
/*if (v7)
{
if (v7 == BasepAdjustObjectAttributesForPrivateNamespace)
{
BasepAdjustObjectAttributesForPrivateNamespace(objectAttributes);
}
else
{
__guard_check_icall_fptr(v7);
v7(objectAttributes);
}
}*/
//*(_DWORD *)a4 = objectAttributes;
return 0;
}
typedef enum _EVENT_TYPE {
NotificationEvent,
SynchronizationEvent
} EVENT_TYPE;
typedef DWORD ACCESS_MASK;
typedef NTSTATUS(__stdcall *NtCreateEventFunc)(
HANDLE *EventHandle,
ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes,
EVENT_TYPE EventType,
BOOLEAN InitialState
);
HANDLE __stdcall CreateEventExW(DWORD flags, ACCESS_MASK DesiredAccess)
{
NTSTATUS ret; // eax@3
NTSTATUS v6; // ecx@12
OBJECT_ATTRIBUTES ObjectAttributes; // [sp+8h] [bp-28h]@3
HANDLE EventHandle; // [sp+Ch] [bp-24h]@5
//char v10; // [sp+18h] [bp-18h]@3
if (flags & 0xFFFFFFFC)
{
v6 = 0xC00000F1;
return nullptr;
}
ret = BaseFormatObjectAttributes(&ObjectAttributes);
if (ret < 0) {
return nullptr;
}
ret = CALL_API(ntdll, _createEvent, NtCreateEventFunc) (
&EventHandle,
DesiredAccess,
&ObjectAttributes,
(EVENT_TYPE)(~(BYTE)flags & 1),
((BYTE)flags >> 1) & 1
);
if (ret < 0) {
return nullptr;
}
/*if (ret == 0x40000000)
RtlSetLastWin32Error(0xB7);
else
RtlSetLastWin32Error(0);*/
return EventHandle;
}
HANDLE __stdcall CreateEventW(/*LPSECURITY_ATTRIBUTES lpEventAttributes = nullptr,*/ BOOL bManualReset, BOOL bInitialState /*, LPCWSTR lpName = nullptr */)
{
DWORD flags;
flags = 0;
if (bManualReset) {
flags = CREATE_EVENT_MANUAL_RESET;
}
if (bInitialState) {
flags |= CREATE_EVENT_INITIAL_SET;
}
return CreateEventExW(flags, EVENT_ALL_ACCESS);
}
typedef NTSTATUS(__stdcall *NtSetEventFunc)(
HANDLE EventHandle,
PULONG PreviousState
);
BOOL __stdcall SetEvent(HANDLE hEvent) {
NTSTATUS ret = CALL_API(ntdll, _setEvent, NtSetEventFunc) (hEvent, 0);
if (ret < 0) {
return FALSE;
}
return TRUE;
}
bool WinInitEvent(void *handle, bool isSet) {
*(HANDLE *)handle = CreateEventW(false, isSet);
return true;
}
bool WinWaitEvent(void *handle, int timeout) {
LARGE_INTEGER liWait, *pliWait = nullptr;
if (timeout != WAIT_INFINITE) {
liWait.QuadPart = -10000 * timeout; //hundreds of nanoseconds
pliWait = &liWait;
}
return STATUS_WAIT_0 == CALL_API(ntdll, _waitForSingleObject, NtWaitForSingleObjectFunc) (
*(HANDLE *) handle,
FALSE,
pliWait
);
}
bool WinPostEvent(void *handle) {
return TRUE == SetEvent(*(HANDLE *)handle);
}
void WinDestroyEvent(void *handle) {
// NOT IMPLEMENTED
}
void WinGetValueEvent(void *handle, int *value) {
// NOT IMPLEMENTED
*value = -1;
}
// ------------------- Error codes ----------------------------
typedef DWORD(__stdcall *ConvertToSystemErrorHandler)(
NTSTATUS status
);
long WinToErrno(long ntStatus) {
return CALL_API(ntdll, _systemError, ConvertToSystemErrorHandler) (ntStatus);
}
// ------------------- Formatted print ------------------------
#define _TRUNCATE ((size_t)-1)
typedef char* va_list;
typedef int (*FormatPrintHandler)(
char *buffer,
size_t sizeOfBuffer,
size_t count,
const char *format,
va_list argptr
);
int WinFormatPrint(char *buffer, size_t sizeOfBuffer, const char *format, char *argptr) {
return CALL_API(ntdll, _formatPrint, FormatPrintHandler) (buffer, sizeOfBuffer, _TRUNCATE, format, (va_list)argptr);
}
// ------------------- Flush instruction cache ----------------
typedef NTSTATUS(*FlushInstructionCacheHandler)(
HANDLE hProcess,
LPVOID address,
SIZE_T size
);
void WinFlushInstructionCache(void) {
CALL_API(ntdll, _flushInstructionCache, FlushInstructionCacheHandler) ((HANDLE)0xFFFFFFFF, NULL, 0);
}
// ------------------- Token ring -----------------------------
namespace revwrapper {
bool TokenRingWait(TokenRing *_this, long userId, bool blocking) {
//ShmTokenRingWin *_this = (ShmTokenRingWin *)ring;
//TokenRingWinData *_data = (TokenRingWinData *)_this->osData;
return WinWaitEvent(&((TokenRingOsData *)_this->osData)->waitSem[userId], blocking ? WAIT_INFINITE : 1000);
}
void TokenRingRelease(TokenRing *_this, long userId) {
long nextId = userId + 1;
if (((TokenRingOsData *)_this->osData)->userCount == nextId) {
nextId = 0;
}
WinPostEvent(&((TokenRingOsData *)_this->osData)->postSem[nextId]);
}
};
// ------------------- Initialization -------------------------
namespace revwrapper {
extern "C" {
bool InitRevtracerWrapper(void *configPage) {
flushInstructionCache = WinFlushInstructionCache;
return true;
}
};
TokenRingOps trOps = {
TokenRingWait,
TokenRingRelease
};
TokenRing tokenRing = { &trOps };
DLL_WRAPPER_PUBLIC WrapperExports wrapperExports = {
InitRevtracerWrapper, // remove if unused in linux
WinAllocateVirtual,
WinFreeVirtual,
WinTerminateProcess,
WinGetTerminationCodeFunc,
WinFormatPrint,
WinWriteFile,
nullptr, //WinInitEvent,
nullptr, //WinWaitEvent,
nullptr, //WinPostEvent,
nullptr, //WinDestroyEvent,
nullptr, //WinGetValueEvent,
nullptr, //CallOpenSharedMemory,
nullptr, //CallUnlinkSharedMemory
&tokenRing
};
}; // namespace revwrapper
DWORD DllEntry() {
return 1;
}
#endif