-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.cpp
260 lines (210 loc) · 5.63 KB
/
main.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
#include <Windows.h>
#include <DbgHelp.h>
#include <stdio.h>
#if _DEBUG
#pragma comment(lib,"Dbghelp.lib")
#endif
extern "C" VOID InstrumentationCallbackThunk(VOID);
extern "C" VOID InstrumentationCallback(PCONTEXT ctx);
#define RIP_SANITY_CHECK(Rip,BaseAddress,ModuleSize) (Rip > BaseAddress) && (Rip < (BaseAddress + ModuleSize))
static ULONG_PTR g_NtdllBase;
static ULONG_PTR g_W32UBase;
static DWORD g_NtdllSize;
static DWORD g_W32USize;
VOID GetBaseAddresses() {
PIMAGE_DOS_HEADER piDH;
PIMAGE_NT_HEADERS piNH;
g_NtdllBase = (ULONG_PTR)GetModuleHandle(TEXT("ntdll.dll"));
piDH = (PIMAGE_DOS_HEADER)g_NtdllBase;
piNH = (PIMAGE_NT_HEADERS)(g_NtdllBase + piDH->e_lfanew);
g_NtdllSize = piNH->OptionalHeader.SizeOfImage;
g_W32UBase = (ULONG_PTR)GetModuleHandle(TEXT("win32u.dll"));
if (g_W32UBase) {
piDH = (PIMAGE_DOS_HEADER)g_W32UBase;
piNH = (PIMAGE_NT_HEADERS)(g_W32UBase + piDH->e_lfanew);
g_W32USize = piNH->OptionalHeader.SizeOfImage;
}
}
VOID InstrumentationCallback(PCONTEXT ctx)
{
BOOLEAN bInstrumentationCallbackDisabled;
ULONG_PTR NtdllBase;
ULONG_PTR W32UBase;
DWORD NtdllSize;
DWORD W32USize;
#if _DEBUG
BOOLEAN SymbolLookupResult;
DWORD64 Displacement;
PSYMBOL_INFO SymbolInfo;
PCHAR SymbolBuffer[sizeof(SYMBOL_INFO) + 1024];
#endif
ULONG_PTR pTEB = (ULONG_PTR)NtCurrentTeb();
//
// https://www.geoffchappell.com/studies/windows/win32/ntdll/structs/teb/index.htm
//
ctx->Rip = *((ULONG_PTR*)(pTEB + 0x02D8)); // TEB->InstrumentationCallbackPreviousPc
ctx->Rsp = *((ULONG_PTR*)(pTEB + 0x02E0)); // TEB->InstrumentationCallbackPreviousSp
ctx->Rcx = ctx->R10;
//
// Prevent recursion. TEB->InstrumentationCallbackDisabled
//
bInstrumentationCallbackDisabled = *((BOOLEAN*)pTEB + 0x1b8);
if (!bInstrumentationCallbackDisabled) {
//
// Disabling for no recursion
//
*((BOOLEAN*)pTEB + 0x1b8) = TRUE;
#if _DEBUG
SymbolInfo = (PSYMBOL_INFO)SymbolBuffer;
RtlSecureZeroMemory(SymbolInfo, sizeof(SYMBOL_INFO) + 1024);
SymbolInfo->SizeOfStruct = sizeof(SYMBOL_INFO);
SymbolInfo->MaxNameLen = 1024;
SymbolLookupResult = SymFromAddr(
GetCurrentProcess(),
ctx->Rip,
&Displacement,
SymbolInfo
);
#endif
#if _DEBUG
if (SymbolLookupResult) {
#endif
NtdllBase = (ULONG_PTR)InterlockedCompareExchangePointer(
(PVOID*)&g_NtdllBase,
NULL,
NULL
);
W32UBase = (ULONG_PTR)InterlockedCompareExchangePointer(
(PVOID*)&g_W32UBase,
NULL,
NULL
);
NtdllSize = InterlockedCompareExchange(
(DWORD*)&g_NtdllSize,
NULL,
NULL
);
W32USize = InterlockedCompareExchange(
(DWORD*)&g_W32USize,
NULL,
NULL
);
if (RIP_SANITY_CHECK(ctx->Rip, NtdllBase, NtdllSize)) {
if (NtdllBase) {
#if _DEBUG
//
// See if we can look up by name
//
PVOID pFunction = GetProcAddress((HMODULE)NtdllBase, SymbolInfo->Name);
if (!pFunction) {
printf("[-] Reverse lookup failed for function: %s.\n", SymbolInfo->Name);
}
else {
printf("[+] Reverse lookup successful for function %s.\n", SymbolInfo->Name);
}
#endif
}
else {
printf("[-] ntdll.dll not found.\n");
}
}
else if (RIP_SANITY_CHECK(ctx->Rip, W32UBase, W32USize)) {
if (W32UBase) {
#if _DEBUG
//
// See if we can look up by name
//
PVOID pFunction = GetProcAddress((HMODULE)W32UBase, SymbolInfo->Name);
if (!pFunction) {
printf("[-] Reverse lookup failed for function: %s.\n", SymbolInfo->Name);
}
else {
printf("[+] Reverse lookup successful for function %s.\n", SymbolInfo->Name);
}
#endif
}
else {
printf("[-] win32u.dll not found.\n");
}
}
else {
printf("[SYSCALL-DETECT] Kernel returns to unverified module, preventing further execution!\n");
#if _DEBUG
printf("[SYSCALL-DETECT] Function: %s\n", SymbolInfo->Name);
#endif
DebugBreak();
}
#if _DEBUG
}
else {
//
// SymFromAddr failed
//
printf("SymFromAddr failed.\n");
// DebugBreak();
}
#endif
//
// Enabling so we can catch next callback.
//
* ((BOOLEAN*)pTEB + 0x1b8) = FALSE;
}
RtlRestoreContext(ctx, NULL);
}
typedef NTSTATUS(NTAPI* pNtSetInformationProcess)(
HANDLE ProcessHandle,
PROCESS_INFORMATION_CLASS ProcessInformationClass,
PVOID ProcessInformation,
ULONG ProcessInformationLength
);
typedef struct _PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION
{
ULONG Version;
ULONG Reserved;
PVOID Callback;
} PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION, * PPROCESS_INSTRUMENTATION_CALLBACK_INFORMATION;
pNtSetInformationProcess NtSetInformationProcess = (pNtSetInformationProcess)GetProcAddress(GetModuleHandle(L"ntdll"),
"NtSetInformationProcess");
BOOL WINAPI DllMain(
HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
{
//
// Obtain ntdll and optionally w32u.dll's base address
//
GetBaseAddresses();
#if _DEBUG
LoadLibraryA("dbghelp.dll");
SymSetOptions(SYMOPT_UNDNAME);
SymInitialize(GetCurrentProcess(), NULL, TRUE);
#endif
AllocConsole();
freopen("CONOUT$", "w", stdout);
printf("[SYSCALL-DETECT] Console logging started...\n");
printf("[SYSCALL-DETECT] ntdll BaseAddress: 0x%lu\n", g_NtdllBase);
printf("[SYSCALL-DETECT] win32u BaseAddress: 0x%lu\n", g_W32UBase);
PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION nirvana;
nirvana.Callback = (PVOID)(ULONG_PTR)InstrumentationCallbackThunk;
nirvana.Reserved = 0;
nirvana.Version = 0;
NtSetInformationProcess(
GetCurrentProcess(),
(PROCESS_INFORMATION_CLASS)40,
&nirvana,
sizeof(nirvana));
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}