-
Notifications
You must be signed in to change notification settings - Fork 15
/
hook_file.c
1126 lines (926 loc) · 33.9 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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Cuckoo Sandbox - Automated Malware Analysis
Copyright (C) 2010-2015 Cuckoo Sandbox Developers, Optiv, Inc. ([email protected])
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 "ntapi.h"
#include <shlwapi.h>
#include "hooking.h"
#include "log.h"
#include "pipe.h"
#include "misc.h"
#include "ignore.h"
#include "lookup.h"
#include "config.h"
#define DUMP_FILE_MASK ((GENERIC_ALL | GENERIC_WRITE | FILE_GENERIC_WRITE | \
FILE_WRITE_DATA | FILE_APPEND_DATA | STANDARD_RIGHTS_WRITE | MAXIMUM_ALLOWED) & ~SYNCHRONIZE)
// 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;
typedef struct _file_log_t {
unsigned int read_count;
unsigned int write_count;
} file_log_t;
static lookup_t g_files;
static lookup_t g_file_logs;
void file_init()
{
specialname_map_init();
lookup_init(&g_files);
lookup_init(&g_file_logs);
}
static void add_file_to_log_tracking(HANDLE fhandle)
{
file_log_t *r = lookup_add(&g_file_logs, (ULONG_PTR)fhandle, sizeof(file_log_t));
memset(r, 0, sizeof(*r));
}
static unsigned int increment_file_log_read_count(HANDLE fhandle)
{
file_log_t *r = lookup_get(&g_file_logs, (ULONG_PTR)fhandle, NULL);
if (r != NULL)
return ++r->read_count;
return 0;
}
static unsigned int increment_file_log_write_count(HANDLE fhandle)
{
file_log_t *r = lookup_get(&g_file_logs, (ULONG_PTR)fhandle, NULL);
if (r != NULL)
return ++r->write_count;
return 0;
}
void remove_file_from_log_tracking(HANDLE fhandle)
{
lookup_del(&g_file_logs, (ULONG_PTR)fhandle);
}
static void new_file_path_ascii(const char *fname)
{
char *absolutename = malloc(32768);
if (absolutename != NULL) {
unsigned int len;
ensure_absolute_ascii_path(absolutename, fname);
len = (unsigned int)strlen(absolutename);
pipe("FILE_NEW:%s", len, absolutename);
}
}
static void new_file_path_unicode(const wchar_t *fname)
{
wchar_t *absolutename = malloc(32768 * sizeof(wchar_t));
if (absolutename != NULL) {
unsigned int len;
ensure_absolute_unicode_path(absolutename, fname);
len = lstrlenW(absolutename);
pipe("FILE_NEW:%S", len, absolutename);
}
}
static void new_file(const UNICODE_STRING *obj)
{
const wchar_t *str = obj->Buffer;
unsigned int len = obj->Length / sizeof(wchar_t);
// maybe it's an absolute path (or a relative path with a harddisk,
// such as C:abc.txt)
if(isalpha(str[0]) != 0 && str[1] == ':') {
pipe("FILE_NEW:%S", len, str);
}
}
static void cache_file(HANDLE file_handle, const wchar_t *path,
unsigned int length_in_chars, unsigned int attributes)
{
file_record_t *r = lookup_add(&g_files, (ULONG_PTR)file_handle,
sizeof(file_record_t) + length_in_chars * sizeof(wchar_t) + sizeof(wchar_t));
memset(r, 0, sizeof(*r));
r->attributes = attributes;
r->length = length_in_chars;
wcsncpy(r->filename, path, r->length + 1);
}
void file_write(HANDLE file_handle)
{
file_record_t *r;
lasterror_t lasterror;
get_lasterrors(&lasterror);
r = lookup_get(&g_files, (ULONG_PTR)file_handle, NULL);
if(r != NULL) {
UNICODE_STRING str;
str.Length = (USHORT)r->length * sizeof(wchar_t);
str.MaximumLength = ((USHORT)r->length + 1) * sizeof(wchar_t);
str.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, (ULONG_PTR)file_handle);
}
set_lasterrors(&lasterror);
}
static void check_for_logging_resumption(const OBJECT_ATTRIBUTES *obj)
{
lasterror_t lasterror;
get_lasterrors(&lasterror);
if (g_config.file_of_interest && g_config.suspend_logging) {
wchar_t *fname = calloc(1, 32768 * sizeof(wchar_t));
wchar_t *absolutename = malloc(32768 * sizeof(wchar_t));
BOOLEAN ret = FALSE;
path_from_object_attributes(obj, fname, 32768);
ensure_absolute_unicode_path(absolutename, fname);
if (!wcsicmp(absolutename, g_config.file_of_interest))
g_config.suspend_logging = FALSE;
free(absolutename);
free(fname);
}
set_lasterrors(&lasterror);
}
static void handle_new_file(HANDLE file_handle, const OBJECT_ATTRIBUTES *obj)
{
lasterror_t lasterror;
get_lasterrors(&lasterror);
if(is_directory_objattr(obj) == 0) {
wchar_t *fname = calloc(1, 32768 * sizeof(wchar_t));
wchar_t *absolutename = calloc(1, 32768 * sizeof(wchar_t));
path_from_object_attributes(obj, fname, 32768);
if (absolutename != NULL) {
unsigned int len;
ensure_absolute_unicode_path(absolutename, fname);
len = lstrlenW(absolutename);
// cache this file
if (is_ignored_file_unicode(absolutename, len) == 0)
cache_file(file_handle, absolutename, len, obj->Attributes);
free(absolutename);
}
else {
if (is_ignored_file_objattr(obj) == 0)
cache_file(file_handle, fname, lstrlenW(fname), obj->Attributes);
}
free(fname);
}
set_lasterrors(&lasterror);
}
void file_close(HANDLE file_handle)
{
lasterror_t lasterror;
get_lasterrors(&lasterror);
lookup_del(&g_files, (ULONG_PTR) file_handle);
set_lasterrors(&lasterror);
}
static BOOLEAN is_protected_objattr(POBJECT_ATTRIBUTES obj)
{
wchar_t path[MAX_PATH_PLUS_TOLERANCE];
wchar_t *absolutepath = malloc(32768 * sizeof(wchar_t));
if (absolutepath) {
path_from_object_attributes(obj, path, MAX_PATH_PLUS_TOLERANCE);
ensure_absolute_unicode_path(absolutepath, path);
if (!wcsnicmp(g_config.w_analyzer, absolutepath, wcslen(g_config.w_analyzer))) {
lasterror_t lasterror;
lasterror.NtstatusError = STATUS_ACCESS_DENIED;
lasterror.Win32Error = ERROR_ACCESS_DENIED;
set_lasterrors(&lasterror);
free(absolutepath);
return TRUE;
}
free(absolutepath);
}
return FALSE;
}
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;
BOOL file_existed;
check_for_logging_resumption(ObjectAttributes);
if (is_protected_objattr(ObjectAttributes))
return STATUS_ACCESS_DENIED;
file_existed = file_exists(ObjectAttributes);
ret = Old_NtCreateFile(FileHandle, DesiredAccess,
ObjectAttributes, IoStatusBlock, AllocationSize, FileAttributes,
ShareAccess | FILE_SHARE_READ, CreateDisposition, CreateOptions, EaBuffer, EaLength);
LOQ_ntstatus("filesystem", "PhOiihss", "FileHandle", FileHandle, "DesiredAccess", DesiredAccess,
"FileName", ObjectAttributes, "CreateDisposition", CreateDisposition,
"ShareAccess", ShareAccess, "FileAttributes", FileAttributes, "ExistedBefore", file_existed ? "yes" : "no", "StackPivoted", is_stack_pivoted() ? "yes" : "no");
if(NT_SUCCESS(ret)) {
add_file_to_log_tracking(*FileHandle);
if (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;
check_for_logging_resumption(ObjectAttributes);
if (is_protected_objattr(ObjectAttributes))
return STATUS_ACCESS_DENIED;
ret = Old_NtOpenFile(FileHandle, DesiredAccess, ObjectAttributes,
IoStatusBlock, ShareAccess | FILE_SHARE_READ, OpenOptions);
LOQ_ntstatus("filesystem", "PhOi", "FileHandle", FileHandle, "DesiredAccess", DesiredAccess,
"FileName", ObjectAttributes, "ShareAccess", ShareAccess);
if (NT_SUCCESS(ret)) {
add_file_to_log_tracking(*FileHandle);
if (DesiredAccess & DUMP_FILE_MASK)
handle_new_file(*FileHandle, ObjectAttributes);
}
return ret;
}
static HANDLE LastFileHandle;
static ULONG AccumulatedLength;
static LONG volatile init_readfile_critsec;
static CRITICAL_SECTION readfile_critsec;
static PVOID InitialBuffer;
static SIZE_T InitialBufferLength;
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);
wchar_t *fname;
BOOLEAN deletelast;
unsigned int read_count = 0;
ULONG_PTR length;
if (NT_SUCCESS(ret))
length = IoStatusBlock->Information;
else
length = 0;
if (!InterlockedExchange(&init_readfile_critsec, 1))
InitializeCriticalSection(&readfile_critsec);
if (get_last_api() == API_NTREADFILE && FileHandle == LastFileHandle) {
// can overflow, but we don't care much
AccumulatedLength += (ULONG)length;
deletelast = TRUE;
}
else {
PVOID prev;
SIZE_T len = min(length, buffer_log_max);
PVOID newbuf;
EnterCriticalSection(&readfile_critsec);
newbuf = malloc(len);
memcpy(newbuf, Buffer, len);
prev = InitialBuffer;
InitialBuffer = newbuf;
if (prev)
free(prev);
LastFileHandle = FileHandle;
AccumulatedLength = (ULONG)length;
InitialBufferLength = len;
LeaveCriticalSection(&readfile_critsec);
deletelast = FALSE;
read_count = increment_file_log_read_count(FileHandle);
}
set_special_api(API_NTREADFILE, deletelast);
if (read_count <= 50) {
fname = calloc(32768, sizeof(wchar_t));
path_from_handle(FileHandle, fname, 32768);
if (read_count < 50)
LOQ_ntstatus("filesystem", "pFbl", "FileHandle", FileHandle,
"HandleName", fname, "Buffer", InitialBufferLength, InitialBuffer, "Length", AccumulatedLength);
else if (read_count == 50)
LOQ_ntstatus("filesystem", "pFbls", "FileHandle", FileHandle,
"HandleName", fname, "Buffer", InitialBufferLength, InitialBuffer, "Length", AccumulatedLength, "Status", "Maximum logged reads reached for this file");
free(fname);
}
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);
wchar_t *fname;
unsigned int write_count;
ULONG_PTR length;
if (NT_SUCCESS(ret))
length = IoStatusBlock->Information;
else
length = 0;
write_count = increment_file_log_write_count(FileHandle);
if (write_count <= 50) {
fname = calloc(32768, sizeof(wchar_t));
path_from_handle(FileHandle, fname, 32768);
if (write_count < 50) {
LOQ_ntstatus("filesystem", "pFbl", "FileHandle", FileHandle,
"HandleName", fname, "Buffer", length, Buffer, "Length", length);
}
else if (write_count == 50) {
LOQ_ntstatus("filesystem", "pFbls", "FileHandle", FileHandle,
"HandleName", fname, "Buffer", length, Buffer, "Length", length, "Status", "Maximum logged writes reached for this file");
}
free(fname);
}
if(NT_SUCCESS(ret)) {
file_write(FileHandle);
}
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtDeleteFile,
__in POBJECT_ATTRIBUTES ObjectAttributes
) {
wchar_t path[MAX_PATH_PLUS_TOLERANCE];
wchar_t *absolutepath = malloc(32768 * sizeof(wchar_t));
NTSTATUS ret;
path_from_object_attributes(ObjectAttributes, path, MAX_PATH_PLUS_TOLERANCE);
ensure_absolute_unicode_path(absolutepath, path);
pipe("FILE_DEL:%Z", absolutepath);
ret = Old_NtDeleteFile(ObjectAttributes);
LOQ_ntstatus("filesystem", "u", "FileName", absolutepath);
free(absolutepath);
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
) {
ULONG_PTR length;
NTSTATUS ret = Old_NtDeviceIoControlFile(FileHandle, Event,
ApcRoutine, ApcContext, IoStatusBlock, IoControlCode,
InputBuffer, InputBufferLength, OutputBuffer,
OutputBufferLength);
if (NT_SUCCESS(ret))
length = IoStatusBlock->Information;
else
length = 0;
LOQ_ntstatus("device", "phbb", "FileHandle", FileHandle,
"IoControlCode", IoControlCode,
"InputBuffer", InputBufferLength, InputBuffer,
"OutputBuffer", length, OutputBuffer);
if (!g_config.no_stealth && NT_SUCCESS(ret) && OutputBuffer)
perform_device_fakery(OutputBuffer, OutputBufferLength, IoControlCode);
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
) {
OBJECT_ATTRIBUTES objattr;
NTSTATUS ret;
ULONG_PTR length;
memset(&objattr, 0, sizeof(objattr));
objattr.ObjectName = FileName;
objattr.RootDirectory = FileHandle;
ret = Old_NtQueryDirectoryFile(FileHandle, Event,
ApcRoutine, ApcContext, IoStatusBlock, FileInformation,
Length, FileInformationClass, ReturnSingleEntry,
FileName, RestartScan);
if (NT_SUCCESS(ret))
length = IoStatusBlock->Information;
else
length = 0;
/* don't log the resulting buffer, otherwise we can't turn these calls into simple duplicates */
if (FileInformationClass == FileNamesInformation) {
LOQ_ntstatus("filesystem", "pOi", "FileHandle", FileHandle,
"FileName", &objattr, "FileInformationClass", FileInformationClass);
}
else {
LOQ_ntstatus("filesystem", "pbOi", "FileHandle", FileHandle,
"FileInformation", length, FileInformation,
"FileName", &objattr, "FileInformationClass", FileInformationClass);
}
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
) {
wchar_t *fname = calloc(32768, sizeof(wchar_t));
wchar_t *absolutepath = calloc(32768, sizeof(wchar_t));
NTSTATUS ret;
ULONG_PTR length;
path_from_handle(FileHandle, fname, 32768);
ensure_absolute_unicode_path(absolutepath, fname);
ret = Old_NtQueryInformationFile(FileHandle, IoStatusBlock,
FileInformation, Length, FileInformationClass);
if (NT_SUCCESS(ret))
length = IoStatusBlock->Information;
else
length = 0;
LOQ_ntstatus("filesystem", "puib", "FileHandle", FileHandle, "HandleName", absolutepath, "FileInformationClass", FileInformationClass,
"FileInformation", length, FileInformation);
free(fname);
free(absolutepath);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtQueryAttributesFile,
__in POBJECT_ATTRIBUTES ObjectAttributes,
__out PFILE_BASIC_INFORMATION FileInformation
) {
NTSTATUS ret = Old_NtQueryAttributesFile(ObjectAttributes, FileInformation);
LOQ_ntstatus("filesystem", "O", "FileName", ObjectAttributes);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtQueryFullAttributesFile,
__in POBJECT_ATTRIBUTES ObjectAttributes,
__out PFILE_NETWORK_OPEN_INFORMATION FileInformation
) {
NTSTATUS ret = Old_NtQueryFullAttributesFile(ObjectAttributes, FileInformation);
LOQ_ntstatus("filesystem", "O", "FileName", ObjectAttributes);
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
) {
wchar_t *fname = calloc(32768, sizeof(wchar_t));
wchar_t *absolutepath = calloc(32768, sizeof(wchar_t));
NTSTATUS ret;
path_from_handle(FileHandle, fname, 32768);
ensure_absolute_unicode_path(absolutepath, fname);
if(FileInformation != NULL && Length == sizeof(BOOLEAN) &&
FileInformationClass == FileDispositionInformation &&
*(BOOLEAN *) FileInformation != FALSE) {
pipe("FILE_DEL:%Z", absolutepath);
}
ret = Old_NtSetInformationFile(FileHandle, IoStatusBlock,
FileInformation, Length, FileInformationClass);
LOQ_ntstatus("filesystem", "puib", "FileHandle", FileHandle, "HandleName", absolutepath, "FileInformationClass", FileInformationClass,
"FileInformation", Length, FileInformation);
free(fname);
free(absolutepath);
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_ntstatus("filesystem", "PhO", "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_ntstatus("filesystem", "PhO", "DirectoryHandle", DirectoryHandle,
"DesiredAccess", DesiredAccess, "ObjectAttributes", ObjectAttributes);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtQueryDirectoryObject,
__in HANDLE DirectoryHandle,
__out_opt PVOID Buffer,
__in ULONG Length,
__in BOOLEAN ReturnSingleEntry,
__in BOOLEAN RestartScan,
__inout PULONG Context,
__out_opt PULONG ReturnLength
) {
NTSTATUS ret = Old_NtQueryDirectoryObject(DirectoryHandle, Buffer, Length,
ReturnSingleEntry, RestartScan, Context, ReturnLength);
// Don't log STATUS_BUFFER_TOO_SMALL
if (ret != 0xC0000023)
LOQ_ntstatus("filesystem", "p", "DirectoryHandle", DirectoryHandle);
return ret;
}
HOOKDEF(BOOL, WINAPI, CreateDirectoryW,
__in LPWSTR lpPathName,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes
) {
BOOL ret = Old_CreateDirectoryW(lpPathName, lpSecurityAttributes);
LOQ_bool("filesystem", "F", "DirectoryName", lpPathName);
return ret;
}
HOOKDEF(BOOL, WINAPI, CreateDirectoryExW,
__in LPWSTR lpTemplateDirectory,
__in LPWSTR lpNewDirectory,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes
) {
BOOL ret = Old_CreateDirectoryExW(lpTemplateDirectory, lpNewDirectory,
lpSecurityAttributes);
LOQ_bool("filesystem", "F", "DirectoryName", lpNewDirectory);
return ret;
}
HOOKDEF(BOOL, WINAPI, RemoveDirectoryA,
__in LPCSTR lpPathName
) {
char path[MAX_PATH];
BOOL ret;
ensure_absolute_ascii_path(path, lpPathName);
ret = Old_RemoveDirectoryA(lpPathName);
LOQ_bool("filesystem", "s", "DirectoryName", path);
return ret;
}
HOOKDEF(BOOL, WINAPI, RemoveDirectoryW,
__in LPWSTR lpPathName
) {
wchar_t *path = malloc(32768 * sizeof(wchar_t));
BOOL ret;
ensure_absolute_unicode_path(path, lpPathName);
ret = Old_RemoveDirectoryW(lpPathName);
LOQ_bool("filesystem", "u", "DirectoryName", path);
free(path);
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
) {
wchar_t *path = malloc(32768 * sizeof(wchar_t));
BOOL ret;
ensure_absolute_unicode_path(path, lpExistingFileName);
ret = Old_MoveFileWithProgressW(lpExistingFileName, lpNewFileName,
lpProgressRoutine, lpData, dwFlags);
LOQ_bool("filesystem", "uFh", "ExistingFileName", path,
"NewFileName", lpNewFileName, "Flags", dwFlags);
if (ret != FALSE) {
if (lpNewFileName)
pipe("FILE_MOVE:%Z::%F", path, lpNewFileName);
else {
// we can do this here because it's not scheduled for deletion until reboot
pipe("FILE_DEL:%Z", path);
}
}
free(path);
return ret;
}
HOOKDEF(HANDLE, WINAPI, FindFirstFileExA,
__in LPCSTR lpFileName,
__in FINDEX_INFO_LEVELS fInfoLevelId,
__out LPVOID lpFindFileData,
__in FINDEX_SEARCH_OPS fSearchOp,
__reserved LPVOID lpSearchFilter,
__in DWORD dwAdditionalFlags
) {
HANDLE ret = Old_FindFirstFileExA(lpFileName, fInfoLevelId,
lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags);
// XXX: change me if we ever move the analyzer dir out of the root directory
if (!g_config.no_stealth && ret != INVALID_HANDLE_VALUE && !stricmp(((PWIN32_FIND_DATAA)lpFindFileData)->cFileName, g_config.analyzer + 3)) {
lasterror_t lasterror;
lasterror.Win32Error = 0x00000002;
lasterror.NtstatusError = 0xc000000f;
FindClose(ret);
set_lasterrors(&lasterror);
ret = INVALID_HANDLE_VALUE;
}
if (!g_config.no_stealth && ret != INVALID_HANDLE_VALUE && (!stricmp(lpFileName, "c:\\windows") || !stricmp(lpFileName, "c:\\pagefile.sys")))
perform_create_time_fakery(&((PWIN32_FIND_DATAA)lpFindFileData)->ftCreationTime);
if (g_config.serial_number && ret != INVALID_HANDLE_VALUE && !stricmp(lpFileName, "c:\\System Volume Information"))
((PWIN32_FIND_DATAA)lpFindFileData)->ftCreationTime = g_config.sysvol_ctime;
if (g_config.serial_number && ret != INVALID_HANDLE_VALUE && !stricmp(lpFileName, "c:\\windows\\system32"))
((PWIN32_FIND_DATAA)lpFindFileData)->ftCreationTime = g_config.sys32_ctime;
if (ret != INVALID_HANDLE_VALUE)
LOQ_handle("filesystem", "fhh", "FileName", lpFileName,
"FirstCreateTimeLow", ((PWIN32_FIND_DATAA)lpFindFileData)->ftCreationTime.dwLowDateTime,
"FirstCreateTimeHigh", ((PWIN32_FIND_DATAA)lpFindFileData)->ftCreationTime.dwHighDateTime);
else
LOQ_handle("filesystem", "f", "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
) {
HANDLE ret = Old_FindFirstFileExW(lpFileName, fInfoLevelId,
lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags);
// XXX: change me if we ever move the analyzer dir out of the root directory
if (!g_config.no_stealth && ret != INVALID_HANDLE_VALUE && !wcsicmp(((PWIN32_FIND_DATAW)lpFindFileData)->cFileName, g_config.w_analyzer + 3)) {
lasterror_t lasterror;
lasterror.Win32Error = 0x00000002;
lasterror.NtstatusError = 0xc000000f;
FindClose(ret);
set_lasterrors(&lasterror);
ret = INVALID_HANDLE_VALUE;
}
if (!g_config.no_stealth && ret != INVALID_HANDLE_VALUE && (!wcsicmp(lpFileName, L"c:\\windows") || !wcsicmp(lpFileName, L"c:\\pagefile.sys")))
perform_create_time_fakery(&((PWIN32_FIND_DATAW)lpFindFileData)->ftCreationTime);
if (g_config.serial_number && ret != INVALID_HANDLE_VALUE && !wcsicmp(lpFileName, L"c:\\System Volume Information"))
((PWIN32_FIND_DATAW)lpFindFileData)->ftCreationTime = g_config.sysvol_ctime;
if (g_config.serial_number && ret != INVALID_HANDLE_VALUE && !wcsicmp(lpFileName, L"c:\\windows\\system32"))
((PWIN32_FIND_DATAW)lpFindFileData)->ftCreationTime = g_config.sys32_ctime;
if (ret != INVALID_HANDLE_VALUE)
LOQ_handle("filesystem", "Fhh", "FileName", lpFileName,
"FirstCreateTimeLow", ((PWIN32_FIND_DATAW)lpFindFileData)->ftCreationTime.dwLowDateTime,
"FirstCreateTimeHigh", ((PWIN32_FIND_DATAW)lpFindFileData)->ftCreationTime.dwHighDateTime);
else
LOQ_handle("filesystem", "F", "FileName", lpFileName);
return ret;
}
HOOKDEF(BOOL, WINAPI, FindNextFileW,
__in HANDLE hFindFile,
__out LPWIN32_FIND_DATAW lpFindFileData
) {
BOOL ret = Old_FindNextFileW(hFindFile, lpFindFileData);
if (!g_config.no_stealth && ret && !wcsicmp(lpFindFileData->cFileName, g_config.w_analyzer + 3)) {
ret = Old_FindNextFileW(hFindFile, lpFindFileData);
}
// not logging this due to the flood of logs it would cause
return ret;
}
HOOKDEF(BOOL, WINAPI, CopyFileA,
__in LPCSTR lpExistingFileName,
__in LPCSTR lpNewFileName,
__in BOOL bFailIfExists
) {
BOOL ret;
BOOL file_existed = FALSE;
if (GetFileAttributesA(lpNewFileName) != INVALID_FILE_ATTRIBUTES)
file_existed = TRUE;
ret = Old_CopyFileA(lpExistingFileName, lpNewFileName,
bFailIfExists);
LOQ_bool("filesystem", "ffs", "ExistingFileName", lpExistingFileName,
"NewFileName", lpNewFileName, "ExistedBefore", file_existed ? "yes" : "no");
if (ret)
new_file_path_ascii(lpNewFileName);
return ret;
}
HOOKDEF(BOOL, WINAPI, CopyFileW,
__in LPWSTR lpExistingFileName,
__in LPWSTR lpNewFileName,
__in BOOL bFailIfExists
) {
BOOL ret;
BOOL file_existed = FALSE;
if (GetFileAttributesW(lpNewFileName) != INVALID_FILE_ATTRIBUTES)
file_existed = TRUE;
ret = Old_CopyFileW(lpExistingFileName, lpNewFileName,
bFailIfExists);
LOQ_bool("filesystem", "FFs", "ExistingFileName", lpExistingFileName,
"NewFileName", lpNewFileName, "ExistedBefore", file_existed ? "yes" : "no");
if (ret)
new_file_path_unicode(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
) {
BOOL ret;
BOOL file_existed = FALSE;
if (GetFileAttributesW(lpNewFileName) != INVALID_FILE_ATTRIBUTES)
file_existed = TRUE;
ret = Old_CopyFileExW(lpExistingFileName, lpNewFileName,
lpProgressRoutine, lpData, pbCancel, dwCopyFlags);
LOQ_bool("filesystem", "FFis", "ExistingFileName", lpExistingFileName,
"NewFileName", lpNewFileName, "CopyFlags", dwCopyFlags, file_existed ? "yes" : "no");
if (ret)
new_file_path_unicode(lpNewFileName);
return ret;
}
HOOKDEF(BOOL, WINAPI, DeleteFileA,
__in LPCSTR lpFileName
) {
char path[MAX_PATH];
BOOL ret;
ensure_absolute_ascii_path(path, lpFileName);
pipe("FILE_DEL:%z", path);
ret = Old_DeleteFileA(lpFileName);
LOQ_bool("filesystem", "s", "FileName", path);
return ret;
}
HOOKDEF(BOOL, WINAPI, DeleteFileW,
__in LPWSTR lpFileName
) {
wchar_t *path = malloc(32768 * sizeof(wchar_t));
BOOL ret;
if (path) {
ensure_absolute_unicode_path(path, lpFileName);
pipe("FILE_DEL:%Z", path);
}
ret = Old_DeleteFileW(lpFileName);
if (path) {
LOQ_bool("filesystem", "u", "FileName", path);
free(path);
}
else {
LOQ_bool("filesystem", "u", "FileName", lpFileName);
}
return ret;
}
HOOKDEF(BOOL, WINAPI, GetDiskFreeSpaceExA,
_In_opt_ PCSTR lpDirectoryName,
_Out_opt_ PULARGE_INTEGER lpFreeBytesAvailable,
_Out_opt_ PULARGE_INTEGER lpTotalNumberOfBytes,
_Out_opt_ PULARGE_INTEGER lpTotalNumberOfFreeBytes
) {
BOOL ret = Old_GetDiskFreeSpaceExA(lpDirectoryName, lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes);
LOQ_bool("filesystem", "s", "DirectoryName", lpDirectoryName);
/* Fake harddrive size to 256GB */
if (!g_config.no_stealth && ret && lpTotalNumberOfBytes) {
lpTotalNumberOfBytes->QuadPart = 256060514304L;
}
return ret;
}
HOOKDEF(BOOL, WINAPI, GetDiskFreeSpaceExW,
_In_opt_ PCWSTR lpDirectoryName,
_Out_opt_ PULARGE_INTEGER lpFreeBytesAvailable,
_Out_opt_ PULARGE_INTEGER lpTotalNumberOfBytes,
_Out_opt_ PULARGE_INTEGER lpTotalNumberOfFreeBytes
) {
BOOL ret = Old_GetDiskFreeSpaceExW(lpDirectoryName, lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes);
LOQ_bool("filesystem", "u", "DirectoryName", lpDirectoryName);
/* Fake harddrive size to 256GB */
if (!g_config.no_stealth && ret && lpTotalNumberOfBytes) {
lpTotalNumberOfBytes->QuadPart = 256060514304L;
}
return ret;
}
HOOKDEF(BOOL, WINAPI, GetDiskFreeSpaceA,
_In_ PCSTR lpRootPathName,
_Out_ LPDWORD lpSectorsPerCluster,
_Out_ LPDWORD lpBytesPerSector,
_Out_ LPDWORD lpNumberOfFreeClusters,
_Out_ LPDWORD lpTotalNumberOfClusters
) {
BOOL ret = Old_GetDiskFreeSpaceA(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters);
LOQ_bool("filesystem", "s", "RootPathName", lpRootPathName);
/* Fake harddrive size to 256GB */
if (!g_config.no_stealth && *lpSectorsPerCluster && *lpBytesPerSector) {
*lpTotalNumberOfClusters = (DWORD)(256060514304L / (*lpSectorsPerCluster * *lpBytesPerSector));
}
return ret;
}
HOOKDEF(BOOL, WINAPI, GetDiskFreeSpaceW,
_In_ PCWSTR lpRootPathName,
_Out_ LPDWORD lpSectorsPerCluster,
_Out_ LPDWORD lpBytesPerSector,
_Out_ LPDWORD lpNumberOfFreeClusters,
_Out_ LPDWORD lpTotalNumberOfClusters
) {
BOOL ret = Old_GetDiskFreeSpaceW(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters);
LOQ_bool("filesystem", "u", "RootPathName", lpRootPathName);
/* Fake harddrive size to 256GB */