forked from cuckoosandbox/cuckoomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook_file.c
505 lines (435 loc) · 15.3 KB
/
hook_file.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
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
/*
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 <ctype.h>
#include <windows.h>
#include <shlwapi.h>
#include "hooking.h"
#include "ntapi.h"
#include "log.h"
#include "pipe.h"
#include "misc.h"
#include "ignore.h"
#include "lookup.h"
static IS_SUCCESS_NTSTATUS();
#define DUMP_FILE_MASK (GENERIC_WRITE | FILE_GENERIC_WRITE | \
FILE_WRITE_DATA | FILE_APPEND_DATA | STANDARD_RIGHTS_WRITE | \
STANDARD_RIGHTS_ALL)
#define HDDVOL1 L"\\Device\\HarddiskVolume1"
// length of a hardcoded unicode string
#define UNILEN(x) (sizeof(x) / sizeof(wchar_t) - 1)
typedef struct _file_record_t {
unsigned int attributes;
size_t length;
wchar_t filename[0];
} file_record_t;
static lookup_t g_files;
void file_init()
{
lookup_init(&g_files);
}
static void new_file(const UNICODE_STRING *obj)
{
const wchar_t *str = obj->Buffer;
unsigned int len = obj->Length / sizeof(wchar_t);
// if it's a path including \??\ then we can send it straight away,
// but we strip the \??\ part
if(len > 4 && !wcsncmp(str, L"\\??\\", 4)) {
pipe("FILE_NEW:%S", len - 4, str + 4);
}
// maybe it's an absolute path (or a relative path with a harddisk,
// such as C:abc.txt)
else if(isalpha(str[0]) != 0 && str[1] == ':') {
pipe("FILE_NEW:%S", len, str);
}
// the filename starts with \Device\HarddiskVolume1, which is
// basically just C:
else if(!wcsnicmp(str, HDDVOL1, UNILEN(HDDVOL1))) {
str += UNILEN(HDDVOL1), len -= UNILEN(HDDVOL1);
pipe("FILE_NEW:C:%S", len, str);
}
}
static void cache_file(HANDLE file_handle, const wchar_t *path,
unsigned int length, unsigned int attributes)
{
file_record_t *r = lookup_add(&g_files, (unsigned int) file_handle,
sizeof(file_record_t) + length * sizeof(wchar_t) + sizeof(wchar_t));
*r = (file_record_t) {
.attributes = attributes,
.length = length,
};
wcsncpy(r->filename, path, r->length + 1);
}
static void file_write(HANDLE file_handle)
{
file_record_t *r = lookup_get(&g_files, (unsigned int) file_handle, NULL);
if(r != NULL) {
UNICODE_STRING str = {
// microsoft actually meant "size"
.Length = r->length * sizeof(wchar_t),
.MaximumLength = (r->length + 1) * sizeof(wchar_t) ,
.Buffer = r->filename,
};
// we do in fact want to dump this file because it was written to
new_file(&str);
// delete the file record from the list
lookup_del(&g_files, (unsigned int) file_handle);
}
}
static void handle_new_file(HANDLE file_handle, const OBJECT_ATTRIBUTES *obj)
{
if(is_directory_objattr(obj) == 0 && is_ignored_file_objattr(obj) == 0) {
wchar_t fname[MAX_PATH_PLUS_TOLERANCE]; uint32_t length;
length = path_from_object_attributes(obj,
fname, MAX_PATH_PLUS_TOLERANCE);
length = ensure_absolute_path(fname, fname, length);
// cache this file
cache_file(file_handle, fname, length, obj->Attributes);
}
}
void file_close(HANDLE file_handle)
{
lookup_del(&g_files, (unsigned int) file_handle);
}
HOOKDEF(NTSTATUS, WINAPI, NtCreateFile,
__out PHANDLE FileHandle,
__in ACCESS_MASK DesiredAccess,
__in POBJECT_ATTRIBUTES ObjectAttributes,
__out PIO_STATUS_BLOCK IoStatusBlock,
__in_opt PLARGE_INTEGER AllocationSize,
__in ULONG FileAttributes,
__in ULONG ShareAccess,
__in ULONG CreateDisposition,
__in ULONG CreateOptions,
__in PVOID EaBuffer,
__in ULONG EaLength
) {
NTSTATUS ret = Old_NtCreateFile(FileHandle, DesiredAccess,
ObjectAttributes, IoStatusBlock, AllocationSize, FileAttributes,
ShareAccess, CreateDisposition, CreateOptions, EaBuffer, EaLength);
LOQ("PpOll", "FileHandle", FileHandle, "DesiredAccess", DesiredAccess,
"FileName", ObjectAttributes, "CreateDisposition", CreateDisposition,
"ShareAccess", ShareAccess);
if(NT_SUCCESS(ret) && DesiredAccess & DUMP_FILE_MASK) {
handle_new_file(*FileHandle, ObjectAttributes);
}
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtOpenFile,
__out PHANDLE FileHandle,
__in ACCESS_MASK DesiredAccess,
__in POBJECT_ATTRIBUTES ObjectAttributes,
__out PIO_STATUS_BLOCK IoStatusBlock,
__in ULONG ShareAccess,
__in ULONG OpenOptions
) {
NTSTATUS ret = Old_NtOpenFile(FileHandle, DesiredAccess, ObjectAttributes,
IoStatusBlock, ShareAccess, OpenOptions);
LOQ("PpOl", "FileHandle", FileHandle, "DesiredAccess", DesiredAccess,
"FileName", ObjectAttributes, "ShareAccess", ShareAccess);
if(NT_SUCCESS(ret) && DesiredAccess & DUMP_FILE_MASK) {
handle_new_file(*FileHandle, ObjectAttributes);
}
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtReadFile,
__in HANDLE FileHandle,
__in_opt HANDLE Event,
__in_opt PIO_APC_ROUTINE ApcRoutine,
__in_opt PVOID ApcContext,
__out PIO_STATUS_BLOCK IoStatusBlock,
__out PVOID Buffer,
__in ULONG Length,
__in_opt PLARGE_INTEGER ByteOffset,
__in_opt PULONG Key
) {
NTSTATUS ret = Old_NtReadFile(FileHandle, Event, ApcRoutine, ApcContext,
IoStatusBlock, Buffer, Length, ByteOffset, Key);
LOQ("pb", "FileHandle", FileHandle,
"Buffer", IoStatusBlock->Information, Buffer);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtWriteFile,
__in HANDLE FileHandle,
__in_opt HANDLE Event,
__in_opt PIO_APC_ROUTINE ApcRoutine,
__in_opt PVOID ApcContext,
__out PIO_STATUS_BLOCK IoStatusBlock,
__in PVOID Buffer,
__in ULONG Length,
__in_opt PLARGE_INTEGER ByteOffset,
__in_opt PULONG Key
) {
NTSTATUS ret = Old_NtWriteFile(FileHandle, Event, ApcRoutine, ApcContext,
IoStatusBlock, Buffer, Length, ByteOffset, Key);
LOQ("pb", "FileHandle", FileHandle,
"Buffer", IoStatusBlock->Information, Buffer);
if(NT_SUCCESS(ret)) {
file_write(FileHandle);
}
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtDeleteFile,
__in POBJECT_ATTRIBUTES ObjectAttributes
) {
pipe("FILE_DEL:%O", ObjectAttributes);
NTSTATUS ret = Old_NtDeleteFile(ObjectAttributes);
LOQ("O", "FileName", ObjectAttributes);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtDeviceIoControlFile,
__in HANDLE FileHandle,
__in HANDLE Event,
__in PIO_APC_ROUTINE ApcRoutine,
__in PVOID ApcContext,
__out PIO_STATUS_BLOCK IoStatusBlock,
__in ULONG IoControlCode,
__in PVOID InputBuffer,
__in ULONG InputBufferLength,
__out PVOID OutputBuffer,
__in ULONG OutputBufferLength
) {
NTSTATUS ret = Old_NtDeviceIoControlFile(FileHandle, Event,
ApcRoutine, ApcContext, IoStatusBlock, IoControlCode,
InputBuffer, InputBufferLength, OutputBuffer,
OutputBufferLength);
LOQ("pbb", "FileHandle", FileHandle,
"InputBuffer", InputBufferLength, InputBuffer,
"OutputBuffer", IoStatusBlock->Information, OutputBuffer);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtQueryDirectoryFile,
__in HANDLE FileHandle,
__in_opt HANDLE Event,
__in_opt PIO_APC_ROUTINE ApcRoutine,
__in_opt PVOID ApcContext,
__out PIO_STATUS_BLOCK IoStatusBlock,
__out PVOID FileInformation,
__in ULONG Length,
__in FILE_INFORMATION_CLASS FileInformationClass,
__in BOOLEAN ReturnSingleEntry,
__in_opt PUNICODE_STRING FileName,
__in BOOLEAN RestartScan
) {
NTSTATUS ret = Old_NtQueryDirectoryFile(FileHandle, Event,
ApcRoutine, ApcContext, IoStatusBlock, FileInformation,
Length, FileInformationClass, ReturnSingleEntry,
FileName, RestartScan);
LOQ("pbo", "FileHandle", FileHandle,
"FileInformation", IoStatusBlock->Information, FileInformation,
"FileName", FileName);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtQueryInformationFile,
__in HANDLE FileHandle,
__out PIO_STATUS_BLOCK IoStatusBlock,
__out PVOID FileInformation,
__in ULONG Length,
__in FILE_INFORMATION_CLASS FileInformationClass
) {
NTSTATUS ret = Old_NtQueryInformationFile(FileHandle, IoStatusBlock,
FileInformation, Length, FileInformationClass);
LOQ("pb", "FileHandle", FileHandle,
"FileInformation", IoStatusBlock->Information, FileInformation);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtSetInformationFile,
__in HANDLE FileHandle,
__out PIO_STATUS_BLOCK IoStatusBlock,
__in PVOID FileInformation,
__in ULONG Length,
__in FILE_INFORMATION_CLASS FileInformationClass
) {
if(FileInformation != NULL && Length == sizeof(BOOLEAN) &&
FileInformationClass == FileDispositionInformation &&
*(BOOLEAN *) FileInformation != FALSE) {
wchar_t path[MAX_PATH_PLUS_TOLERANCE];
path_from_handle(FileHandle, path, (unsigned int) MAX_PATH_PLUS_TOLERANCE);
pipe("FILE_DEL:%Z", path);
}
NTSTATUS ret = Old_NtSetInformationFile(FileHandle, IoStatusBlock,
FileInformation, Length, FileInformationClass);
LOQ("pb", "FileHandle", FileHandle,
"FileInformation", IoStatusBlock->Information, FileInformation);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtOpenDirectoryObject,
__out PHANDLE DirectoryHandle,
__in ACCESS_MASK DesiredAccess,
__in POBJECT_ATTRIBUTES ObjectAttributes
) {
NTSTATUS ret = Old_NtOpenDirectoryObject(DirectoryHandle, DesiredAccess,
ObjectAttributes);
LOQ("PlO", "DirectoryHandle", DirectoryHandle,
"DesiredAccess", DesiredAccess, "ObjectAttributes", ObjectAttributes);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtCreateDirectoryObject,
__out PHANDLE DirectoryHandle,
__in ACCESS_MASK DesiredAccess,
__in POBJECT_ATTRIBUTES ObjectAttributes
) {
NTSTATUS ret = Old_NtCreateDirectoryObject(DirectoryHandle, DesiredAccess,
ObjectAttributes);
LOQ("PlO", "DirectoryHandle", DirectoryHandle,
"DesiredAccess", DesiredAccess, "ObjectAttributes", ObjectAttributes);
return ret;
}
HOOKDEF(BOOL, WINAPI, CreateDirectoryW,
__in LPWSTR lpPathName,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_CreateDirectoryW(lpPathName, lpSecurityAttributes);
LOQ("u", "DirectoryName", lpPathName);
return ret;
}
HOOKDEF(BOOL, WINAPI, CreateDirectoryExW,
__in LPWSTR lpTemplateDirectory,
__in LPWSTR lpNewDirectory,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_CreateDirectoryExW(lpTemplateDirectory, lpNewDirectory,
lpSecurityAttributes);
LOQ("u", "DirectoryName", lpNewDirectory);
return ret;
}
HOOKDEF(BOOL, WINAPI, RemoveDirectoryA,
__in LPCTSTR lpPathName
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_RemoveDirectoryA(lpPathName);
LOQ("s", "DirectoryName", lpPathName);
return ret;
}
HOOKDEF(BOOL, WINAPI, RemoveDirectoryW,
__in LPWSTR lpPathName
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_RemoveDirectoryW(lpPathName);
LOQ("u", "DirectoryName", lpPathName);
return ret;
}
HOOKDEF(BOOL, WINAPI, MoveFileWithProgressW,
__in LPWSTR lpExistingFileName,
__in_opt LPWSTR lpNewFileName,
__in_opt LPPROGRESS_ROUTINE lpProgressRoutine,
__in_opt LPVOID lpData,
__in DWORD dwFlags
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_MoveFileWithProgressW(lpExistingFileName, lpNewFileName,
lpProgressRoutine, lpData, dwFlags);
LOQ("uu", "ExistingFileName", lpExistingFileName,
"NewFileName", lpNewFileName);
if(ret != FALSE) {
pipe("FILE_MOVE:%Z::%Z", lpExistingFileName, lpNewFileName);
}
return ret;
}
HOOKDEF(HANDLE, WINAPI, FindFirstFileExA,
__in LPCTSTR lpFileName,
__in FINDEX_INFO_LEVELS fInfoLevelId,
__out LPVOID lpFindFileData,
__in FINDEX_SEARCH_OPS fSearchOp,
__reserved LPVOID lpSearchFilter,
__in DWORD dwAdditionalFlags
) {
IS_SUCCESS_HANDLE();
HANDLE ret = Old_FindFirstFileExA(lpFileName, fInfoLevelId,
lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags);
LOQ("s", "FileName", lpFileName);
return ret;
}
HOOKDEF(HANDLE, WINAPI, FindFirstFileExW,
__in LPWSTR lpFileName,
__in FINDEX_INFO_LEVELS fInfoLevelId,
__out LPVOID lpFindFileData,
__in FINDEX_SEARCH_OPS fSearchOp,
__reserved LPVOID lpSearchFilter,
__in DWORD dwAdditionalFlags
) {
IS_SUCCESS_HANDLE();
HANDLE ret = Old_FindFirstFileExW(lpFileName, fInfoLevelId,
lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags);
LOQ("u", "FileName", lpFileName);
return ret;
}
HOOKDEF(BOOL, WINAPI, CopyFileA,
__in LPCTSTR lpExistingFileName,
__in LPCTSTR lpNewFileName,
__in BOOL bFailIfExists
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_CopyFileA(lpExistingFileName, lpNewFileName,
bFailIfExists);
LOQ("ss", "ExistingFileName", lpExistingFileName,
"NewFileName", lpNewFileName);
return ret;
}
HOOKDEF(BOOL, WINAPI, CopyFileW,
__in LPWSTR lpExistingFileName,
__in LPWSTR lpNewFileName,
__in BOOL bFailIfExists
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_CopyFileW(lpExistingFileName, lpNewFileName,
bFailIfExists);
LOQ("uu", "ExistingFileName", lpExistingFileName,
"NewFileName", lpNewFileName);
return ret;
}
HOOKDEF(BOOL, WINAPI, CopyFileExW,
_In_ LPWSTR lpExistingFileName,
_In_ LPWSTR lpNewFileName,
_In_opt_ LPPROGRESS_ROUTINE lpProgressRoutine,
_In_opt_ LPVOID lpData,
_In_opt_ LPBOOL pbCancel,
_In_ DWORD dwCopyFlags
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_CopyFileExW(lpExistingFileName, lpNewFileName,
lpProgressRoutine, lpData, pbCancel, dwCopyFlags);
LOQ("uul", "ExistingFileName", lpExistingFileName,
"NewFileName", lpNewFileName, "CopyFlags", dwCopyFlags);
return ret;
}
HOOKDEF(BOOL, WINAPI, DeleteFileA,
__in LPCSTR lpFileName
) {
IS_SUCCESS_BOOL();
wchar_t path[MAX_PATH];
// copy ascii to unicode string
for (int i = 0; lpFileName[i] != 0 && i < MAX_PATH; i++) {
path[i] = lpFileName[i];
}
ensure_absolute_path(path, path, strlen(lpFileName));
pipe("FILE_DEL:%Z", path);
BOOL ret = Old_DeleteFileA(lpFileName);
LOQ("s", "FileName", lpFileName);
return ret;
}
HOOKDEF(BOOL, WINAPI, DeleteFileW,
__in LPWSTR lpFileName
) {
IS_SUCCESS_BOOL();
wchar_t path[MAX_PATH];
ensure_absolute_path(path, lpFileName, lstrlenW(lpFileName));
pipe("FILE_DEL:%Z", path);
BOOL ret = Old_DeleteFileW(lpFileName);
LOQ("u", "FileName", lpFileName);
return ret;
}