forked from cuckoosandbox/cuckoomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuckoomon.c
426 lines (353 loc) · 11.6 KB
/
cuckoomon.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
/*
Cuckoo Sandbox - Automated Malware Analysis
Copyright (C) 2010-2014 Cuckoo Sandbox Developers
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <windows.h>
#include "ntapi.h"
#include "misc.h"
#include "hooking.h"
#include "hooks.h"
#include "log.h"
#include "pipe.h"
#include "ignore.h"
#include "hook_file.h"
#include "hook_sleep.h"
#include "config.h"
#define HOOK(library, funcname) {L###library, #funcname, NULL, \
&New_##funcname, (void **) &Old_##funcname}
#define HOOK2(library, funcname, recursion) {L###library, #funcname, NULL, \
&New2_##funcname, (void **) &Old2_##funcname, recursion}
static hook_t g_hooks[] = {
//
// Special Hooks
//
// NOTE: due to the fact that the "special" hooks don't use a hook count
// (whereas the "normal" hooks, those with allow_hook_recursion set to
// zero, do) we have to hook the "special" hooks first. Otherwise the
// execution flow will end up in an infinite loop, because of hook count
// and whatnot.
//
// In other words, do *NOT* place "special" hooks behind "normal" hooks.
//
HOOK2(ntdll, LdrLoadDll, TRUE),
HOOK2(kernel32, CreateProcessInternalW, TRUE),
//
// File Hooks
//
HOOK(ntdll, NtCreateFile),
HOOK(ntdll, NtOpenFile),
HOOK(ntdll, NtReadFile),
HOOK(ntdll, NtWriteFile),
HOOK(ntdll, NtDeleteFile),
HOOK(ntdll, NtDeviceIoControlFile),
HOOK(ntdll, NtQueryDirectoryFile),
HOOK(ntdll, NtQueryInformationFile),
HOOK(ntdll, NtSetInformationFile),
HOOK(ntdll, NtOpenDirectoryObject),
HOOK(ntdll, NtCreateDirectoryObject),
// CreateDirectoryExA calls CreateDirectoryExW
// CreateDirectoryW does not call CreateDirectoryExW
HOOK(kernel32, CreateDirectoryW),
HOOK(kernel32, CreateDirectoryExW),
HOOK(kernel32, RemoveDirectoryA),
HOOK(kernel32, RemoveDirectoryW),
// lowest variant of MoveFile()
HOOK(kernel32, MoveFileWithProgressW),
HOOK(kernel32, FindFirstFileExA),
HOOK(kernel32, FindFirstFileExW),
// Covered by NtCreateFile() but still grap this information
HOOK(kernel32, CopyFileA),
HOOK(kernel32, CopyFileW),
HOOK(kernel32, CopyFileExW),
// Covered by NtSetInformationFile() but still grap this information
HOOK(kernel32, DeleteFileA),
HOOK(kernel32, DeleteFileW),
//
// Registry Hooks
//
// Note: Most, if not all, of the Registry API go natively from both the
// A as well as the W versions. In other words, we have to hook all the
// ascii *and* unicode APIs of those functions.
//
HOOK(advapi32, RegOpenKeyExA),
HOOK(advapi32, RegOpenKeyExW),
HOOK(advapi32, RegCreateKeyExA),
HOOK(advapi32, RegCreateKeyExW),
// Note that RegDeleteKeyEx() is available for 64bit XP/Vista+
HOOK(advapi32, RegDeleteKeyA),
HOOK(advapi32, RegDeleteKeyW),
// RegEnumKeyA() calls RegEnumKeyExA(), but RegEnumKeyW() does *not*
// call RegEnumKeyExW()
HOOK(advapi32, RegEnumKeyW),
HOOK(advapi32, RegEnumKeyExA),
HOOK(advapi32, RegEnumKeyExW),
HOOK(advapi32, RegEnumValueA),
HOOK(advapi32, RegEnumValueW),
HOOK(advapi32, RegSetValueExA),
HOOK(advapi32, RegSetValueExW),
HOOK(advapi32, RegQueryValueExA),
HOOK(advapi32, RegQueryValueExW),
HOOK(advapi32, RegDeleteValueA),
HOOK(advapi32, RegDeleteValueW),
HOOK(advapi32, RegQueryInfoKeyA),
HOOK(advapi32, RegQueryInfoKeyW),
HOOK(advapi32, RegCloseKey),
//
// Native Registry Hooks
//
HOOK(ntdll, NtCreateKey),
HOOK(ntdll, NtOpenKey),
HOOK(ntdll, NtOpenKeyEx),
HOOK(ntdll, NtRenameKey),
HOOK(ntdll, NtReplaceKey),
HOOK(ntdll, NtEnumerateKey),
HOOK(ntdll, NtEnumerateValueKey),
HOOK(ntdll, NtSetValueKey),
HOOK(ntdll, NtQueryValueKey),
HOOK(ntdll, NtQueryMultipleValueKey),
HOOK(ntdll, NtDeleteKey),
HOOK(ntdll, NtDeleteValueKey),
HOOK(ntdll, NtLoadKey),
HOOK(ntdll, NtLoadKey2),
HOOK(ntdll, NtLoadKeyEx),
HOOK(ntdll, NtQueryKey),
HOOK(ntdll, NtSaveKey),
HOOK(ntdll, NtSaveKeyEx),
//
// Window Hooks
//
HOOK(user32, FindWindowA),
HOOK(user32, FindWindowW),
HOOK(user32, FindWindowExA),
HOOK(user32, FindWindowExW),
//
// Sync Hooks
//
HOOK(ntdll, NtCreateMutant),
HOOK(ntdll, NtOpenMutant),
HOOK(ntdll, NtCreateNamedPipeFile),
//
// Process Hooks
//
HOOK(ntdll, NtCreateProcess),
HOOK(ntdll, NtCreateProcessEx),
HOOK(ntdll, NtCreateUserProcess),
HOOK(ntdll, RtlCreateUserProcess),
//HOOK(ntdll, NtOpenProcess),
HOOK(ntdll, NtTerminateProcess),
HOOK(ntdll, NtCreateSection),
HOOK(ntdll, NtMakeTemporaryObject),
HOOK(ntdll, NtMakePermanentObject),
HOOK(ntdll, NtOpenSection),
//HOOK(kernel32, CreateProcessInternalW),
HOOK(ntdll, ZwMapViewOfSection),
HOOK(kernel32, ExitProcess),
// all variants of ShellExecute end up in ShellExecuteExW
HOOK(shell32, ShellExecuteExW),
HOOK(ntdll, NtUnmapViewOfSection),
// HOOK(ntdll, NtAllocateVirtualMemory),
HOOK(ntdll, NtReadVirtualMemory),
HOOK(kernel32, ReadProcessMemory),
HOOK(ntdll, NtWriteVirtualMemory),
HOOK(kernel32, WriteProcessMemory),
HOOK(ntdll, NtProtectVirtualMemory),
HOOK(kernel32, VirtualProtectEx),
HOOK(ntdll, NtFreeVirtualMemory),
//HOOK(kernel32, VirtualFreeEx),
HOOK(msvcrt, system),
//
// Thread Hooks
//
HOOK(ntdll, NtCreateThread),
HOOK(ntdll, NtCreateThreadEx),
HOOK(ntdll, NtOpenThread),
HOOK(ntdll, NtGetContextThread),
HOOK(ntdll, NtSetContextThread),
HOOK(ntdll, NtSuspendThread),
HOOK(ntdll, NtResumeThread),
HOOK(ntdll, NtTerminateThread),
HOOK(kernel32, CreateThread),
HOOK(kernel32, CreateRemoteThread),
HOOK(kernel32, ExitThread),
HOOK(ntdll, RtlCreateUserThread),
//
// Misc Hooks
//
HOOK(user32, SetWindowsHookExA),
HOOK(user32, SetWindowsHookExW),
HOOK(user32, UnhookWindowsHookEx),
//HOOK(ntdll, LdrLoadDll),
HOOK(ntdll, LdrGetDllHandle),
HOOK(ntdll, LdrGetProcedureAddress),
HOOK(kernel32, DeviceIoControl),
HOOK(user32, ExitWindowsEx),
HOOK(kernel32, IsDebuggerPresent),
HOOK(advapi32, LookupPrivilegeValueW),
//HOOK(ntdll, NtClose),
HOOK(kernel32, WriteConsoleA),
HOOK(kernel32, WriteConsoleW),
HOOK(user32, GetSystemMetrics),
HOOK(user32, GetCursorPos),
//
// Network Hooks
//
HOOK(urlmon, URLDownloadToFileW),
HOOK(wininet, InternetOpenA),
HOOK(wininet, InternetOpenW),
HOOK(wininet, InternetConnectA),
HOOK(wininet, InternetConnectW),
HOOK(wininet, InternetOpenUrlA),
HOOK(wininet, InternetOpenUrlW),
HOOK(wininet, HttpOpenRequestA),
HOOK(wininet, HttpOpenRequestW),
HOOK(wininet, HttpSendRequestA),
HOOK(wininet, HttpSendRequestW),
HOOK(wininet, InternetReadFile),
HOOK(wininet, InternetWriteFile),
HOOK(wininet, InternetCloseHandle),
HOOK(dnsapi, DnsQuery_A),
HOOK(dnsapi, DnsQuery_UTF8),
HOOK(dnsapi, DnsQuery_W),
HOOK(ws2_32, getaddrinfo),
HOOK(ws2_32, GetAddrInfoW),
//
// Service Hooks
//
HOOK(advapi32, OpenSCManagerA),
HOOK(advapi32, OpenSCManagerW),
HOOK(advapi32, CreateServiceA),
HOOK(advapi32, CreateServiceW),
HOOK(advapi32, OpenServiceA),
HOOK(advapi32, OpenServiceW),
HOOK(advapi32, StartServiceA),
HOOK(advapi32, StartServiceW),
HOOK(advapi32, ControlService),
HOOK(advapi32, DeleteService),
//
// Sleep Hooks
//
HOOK(ntdll, NtDelayExecution),
HOOK(kernel32, GetLocalTime),
HOOK(kernel32, GetSystemTime),
HOOK(kernel32, GetTickCount),
HOOK(ntdll, NtQuerySystemTime),
//
// Socket Hooks
//
HOOK(ws2_32, WSAStartup),
HOOK(ws2_32, gethostbyname),
HOOK(ws2_32, socket),
HOOK(ws2_32, connect),
HOOK(ws2_32, send),
HOOK(ws2_32, sendto),
HOOK(ws2_32, recv),
HOOK(ws2_32, recvfrom),
HOOK(ws2_32, accept),
HOOK(ws2_32, bind),
HOOK(ws2_32, listen),
HOOK(ws2_32, select),
HOOK(ws2_32, setsockopt),
HOOK(ws2_32, ioctlsocket),
HOOK(ws2_32, closesocket),
HOOK(ws2_32, shutdown),
HOOK(ws2_32, WSARecv),
HOOK(ws2_32, WSARecvFrom),
HOOK(ws2_32, WSASend),
HOOK(ws2_32, WSASendTo),
HOOK(ws2_32, WSASocketA),
HOOK(ws2_32, WSASocketW),
// HOOK(wsock32, connect),
// HOOK(wsock32, send),
// HOOK(wsock32, recv),
HOOK(mswsock, ConnectEx),
HOOK(mswsock, TransmitFile),
};
// get a random hooking method, except for hook_jmp_direct
//#define HOOKTYPE randint(HOOK_NOP_JMP_DIRECT, HOOK_MOV_EAX_INDIRECT_PUSH_RETN)
// error testing with hook_jmp_direct only
#define HOOKTYPE HOOK_JMP_DIRECT
void set_hooks_dll(const wchar_t *library, int len)
{
for (int i = 0; i < ARRAYSIZE(g_hooks); i++) {
if(!wcsnicmp(g_hooks[i].library, library, len)) {
hook_api(&g_hooks[i], HOOKTYPE);
}
}
}
void set_hooks()
{
// the hooks contain executable code as well, so they have to be RWX
DWORD old_protect;
VirtualProtect(g_hooks, sizeof(g_hooks), PAGE_EXECUTE_READWRITE,
&old_protect);
hook_disable();
// now, hook each api :)
for (int i = 0; i < ARRAYSIZE(g_hooks); i++) {
if(g_hooks[i].allow_hook_recursion != FALSE) {
hook_api(&g_hooks[i], HOOKTYPE);
}
else {
hook_api(&g_hooks[i], HOOKTYPE);
}
}
hook_enable();
}
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
if(dwReason == DLL_PROCESS_ATTACH) {
// make sure advapi32 is loaded
LoadLibrary("advapi32");
// there's a small list of processes which we don't want to inject
if(is_ignored_process()) {
return TRUE;
}
// hide our module from peb
hide_module_from_peb(hModule);
// obtain all protected pids
int pids[MAX_PROTECTED_PIDS], length = sizeof(pids);
pipe2(pids, &length, "GETPIDS");
for (int i = 0; i < length / sizeof(pids[0]); i++) {
add_protected_pid(pids[i]);
}
// initialize file stuff
file_init();
// read the config settings
read_config();
g_pipe_name = g_config.pipe_name;
// initialize the log file
log_init(g_config.host_ip, g_config.host_port, 0);
// initialize the Sleep() skipping stuff
init_sleep_skip(g_config.first_process);
// we skip a random given amount of milliseconds each run
init_startup_time(g_config.startup_time);
// disable the retaddr check if the user wants so
if(g_config.retaddr_check == 0) {
hook_disable_retaddr_check();
}
// initialize all hooks
set_hooks();
// notify analyzer.py that we've loaded
char name[64];
sprintf(name, "CuckooEvent%d", GetCurrentProcessId());
HANDLE event_handle = OpenEvent(EVENT_ALL_ACCESS, FALSE, name);
if(event_handle != NULL) {
SetEvent(event_handle);
CloseHandle(event_handle);
}
}
else if(dwReason == DLL_PROCESS_DETACH) {
log_free();
}
return TRUE;
}