forked from spender-sandbox/cuckoomon-modified
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook_misc.c
1020 lines (879 loc) · 31.3 KB
/
hook_misc.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 "ntapi.h"
#include "hooking.h"
#include "log.h"
#include "pipe.h"
#include "misc.h"
#include "hook_file.h"
#include "hook_sleep.h"
#include "config.h"
#include "ignore.h"
HOOKDEF(HHOOK, WINAPI, SetWindowsHookExA,
__in int idHook,
__in HOOKPROC lpfn,
__in HINSTANCE hMod,
__in DWORD dwThreadId
) {
HHOOK ret;
if (hMod && lpfn && dwThreadId) {
DWORD pid = get_pid_by_tid(dwThreadId);
if (pid && pid != GetCurrentProcessId())
pipe("PROCESS:%d:%d,%d", is_suspended(pid, dwThreadId), pid, dwThreadId);
}
ret = Old_SetWindowsHookExA(idHook, lpfn, hMod, dwThreadId);
LOQ_nonnull("system", "ippi", "HookIdentifier", idHook, "ProcedureAddress", lpfn,
"ModuleAddress", hMod, "ThreadId", dwThreadId);
return ret;
}
HOOKDEF(HHOOK, WINAPI, SetWindowsHookExW,
__in int idHook,
__in HOOKPROC lpfn,
__in HINSTANCE hMod,
__in DWORD dwThreadId
) {
HHOOK ret;
if (hMod && lpfn && dwThreadId) {
DWORD pid = get_pid_by_tid(dwThreadId);
if (pid && pid != GetCurrentProcessId())
pipe("PROCESS:%d:%d,%d", is_suspended(pid, dwThreadId), pid, dwThreadId);
}
ret = Old_SetWindowsHookExW(idHook, lpfn, hMod, dwThreadId);
LOQ_nonnull("system", "ippi", "HookIdentifier", idHook, "ProcedureAddress", lpfn,
"ModuleAddress", hMod, "ThreadId", dwThreadId);
return ret;
}
HOOKDEF(BOOL, WINAPI, UnhookWindowsHookEx,
__in HHOOK hhk
) {
BOOL ret = Old_UnhookWindowsHookEx(hhk);
LOQ_bool("hooking", "p", "HookHandle", hhk);
return ret;
}
HOOKDEF(LPTOP_LEVEL_EXCEPTION_FILTER, WINAPI, SetUnhandledExceptionFilter,
_In_ LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter
) {
BOOL ret = 1;
LPTOP_LEVEL_EXCEPTION_FILTER res;
if (g_config.debug)
res = NULL;
else
res = Old_SetUnhandledExceptionFilter(lpTopLevelExceptionFilter);
LOQ_bool("hooking", "");
return res;
}
HOOKDEF(UINT, WINAPI, SetErrorMode,
_In_ UINT uMode
) {
UINT ret = 0;
if (!g_config.debug)
ret = Old_SetErrorMode(uMode);
//LOQ_void("system", "h", "Mode", uMode);
disable_tail_call_optimization();
return ret;
}
// Called with the loader lock held
HOOKDEF(NTSTATUS, WINAPI, LdrGetDllHandle,
__in_opt PWORD pwPath,
__in_opt PVOID Unused,
__in PUNICODE_STRING ModuleFileName,
__out PHANDLE pHModule
) {
NTSTATUS ret = Old_LdrGetDllHandle(pwPath, Unused, ModuleFileName, pHModule);
LOQ_ntstatus("system", "oP", "FileName", ModuleFileName, "ModuleHandle", pHModule);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, LdrGetProcedureAddress,
__in HMODULE ModuleHandle,
__in_opt PANSI_STRING FunctionName,
__in_opt WORD Ordinal,
__out PVOID *FunctionAddress
) {
NTSTATUS ret = Old_LdrGetProcedureAddress(ModuleHandle, FunctionName,
Ordinal, FunctionAddress);
if (FunctionName != NULL && FunctionName->Length == 13 && FunctionName->Buffer != NULL &&
(!strncmp(FunctionName->Buffer, "EncodePointer", 13) || !strncmp(FunctionName->Buffer, "DecodePointer", 13)))
return ret;
LOQ_ntstatus("system", "opSiP", "ModuleName", get_basename_of_module(ModuleHandle), "ModuleHandle", ModuleHandle,
"FunctionName", FunctionName != NULL ? FunctionName->Length : 0,
FunctionName != NULL ? FunctionName->Buffer : NULL,
"Ordinal", Ordinal, "FunctionAddress", FunctionAddress);
if (hook_info()->main_caller_retaddr && g_config.first_process && FunctionName != NULL && (ret == 0xc000007a || ret == 0xc0000139) && FunctionName->Length == 7 &&
!strncmp(FunctionName->Buffer, "DllMain", 7) && wcsicmp(our_process_path, g_config.file_of_interest)) {
log_flush();
ExitThread(0);
}
return ret;
}
HOOKDEF(BOOL, WINAPI, DeviceIoControl,
__in HANDLE hDevice,
__in DWORD dwIoControlCode,
__in_opt LPVOID lpInBuffer,
__in DWORD nInBufferSize,
__out_opt LPVOID lpOutBuffer,
__in DWORD nOutBufferSize,
__out_opt LPDWORD lpBytesReturned,
__inout_opt LPOVERLAPPED lpOverlapped
) {
BOOL ret;
ENSURE_DWORD(lpBytesReturned);
ret = Old_DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer,
nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned,
lpOverlapped);
LOQ_bool("device", "phbb", "DeviceHandle", hDevice, "IoControlCode", dwIoControlCode,
"InBuffer", nInBufferSize, lpInBuffer,
"OutBuffer", *lpBytesReturned, lpOutBuffer);
if (!g_config.no_stealth && ret && lpOutBuffer)
perform_device_fakery(lpOutBuffer, *lpBytesReturned, dwIoControlCode);
return ret;
}
HOOKDEF_NOTAIL(WINAPI, NtShutdownSystem,
__in UINT Action
) {
DWORD ret = 0;
LOQ_zero("system", "i", "Action", Action);
pipe("SHUTDOWN:");
return ret;
}
HOOKDEF_NOTAIL(WINAPI, NtSetSystemPowerState,
__in UINT SystemAction,
__in UINT MinSystemState,
__in UINT Flags
) {
DWORD ret = 0;
LOQ_zero("system", "iih", "SystemAction", SystemAction, "MinSystemState", MinSystemState, "Flags", Flags);
pipe("SHUTDOWN:");
return ret;
}
HOOKDEF_NOTAIL(WINAPI, ExitWindowsEx,
__in UINT uFlags,
__in DWORD dwReason
) {
DWORD ret = 0;
LOQ_zero("system", "hi", "Flags", uFlags, "Reason", dwReason);
pipe("SHUTDOWN:");
return ret;
}
HOOKDEF_NOTAIL(WINAPI, InitiateShutdownW,
_In_opt_ LPWSTR lpMachineName,
_In_opt_ LPWSTR lpMessage,
_In_ DWORD dwGracePeriod,
_In_ DWORD dwShutdownFlags,
_In_ DWORD dwReason
) {
DWORD ret = 0;
LOQ_zero("system", "uuihh", "MachineName", lpMachineName, "Message", lpMessage, "GracePeriod", dwGracePeriod, "ShutdownFlags", dwShutdownFlags, "Reason", dwReason);
pipe("SHUTDOWN:");
return ret;
}
HOOKDEF_NOTAIL(WINAPI, InitiateSystemShutdownW,
_In_opt_ LPWSTR lpMachineName,
_In_opt_ LPWSTR lpMessage,
_In_ DWORD dwTimeout,
_In_ BOOL bForceAppsClosed,
_In_ BOOL bRebootAfterShutdown
) {
DWORD ret = 0;
LOQ_zero("system", "uuiii", "MachineName", lpMachineName, "Message", lpMessage, "Timeout", dwTimeout, "ForceAppsClosed", bForceAppsClosed, "RebootAfterShutdown", bRebootAfterShutdown);
pipe("SHUTDOWN:");
return ret;
}
HOOKDEF_NOTAIL(WINAPI, NtRaiseHardError,
IN NTSTATUS ErrorStatus,
IN ULONG NumberOfParameters,
IN ULONG UnicodeStringParameterMask,
IN PULONG_PTR Parameters,
IN ULONG ValidResponseOptions,
OUT PULONG Response
) {
DWORD ret = 0;
LOQ_zero("system", "hi", "ErrorStatus", ErrorStatus, "ResponseOptions", ValidResponseOptions);
if (ValidResponseOptions == OptionShutdownSystem)
pipe("SHUTDOWN:");
return ret;
}
HOOKDEF_NOTAIL(WINAPI, InitiateSystemShutdownExW,
_In_opt_ LPWSTR lpMachineName,
_In_opt_ LPWSTR lpMessage,
_In_ DWORD dwTimeout,
_In_ BOOL bForceAppsClosed,
_In_ BOOL bRebootAfterShutdown,
_In_ DWORD dwReason
) {
DWORD ret = 0;
LOQ_zero("system", "uuiiih", "MachineName", lpMachineName, "Message", lpMessage, "Timeout", dwTimeout, "ForceAppsClosed", bForceAppsClosed, "RebootAfterShutdown", bRebootAfterShutdown, "Reason", dwReason);
pipe("SHUTDOWN:");
return ret;
}
static int num_isdebuggerpresent;
HOOKDEF(BOOL, WINAPI, IsDebuggerPresent,
void
) {
BOOL ret = Old_IsDebuggerPresent();
num_isdebuggerpresent++;
if (num_isdebuggerpresent < 20)
LOQ_bool("system", "");
else if (num_isdebuggerpresent == 20)
LOQ_bool("system", "s", "Status", "Log limit reached");
#ifndef _WIN64
else if (num_isdebuggerpresent == 1000) {
lasterror_t lasterror;
get_lasterrors(&lasterror);
__try {
hook_info_t *hookinfo = hook_info();
PUCHAR p = (PUCHAR)hookinfo->main_caller_retaddr - 6;
if (p[0] == 0xff && p[1] == 0x15 && p[6] == 0x49) {
DWORD oldprot;
VirtualProtect(p, 6, PAGE_EXECUTE_READWRITE, &oldprot);
memcpy(p, "\x31\xc0\x31\xc9\x41\x90", 6);
VirtualProtect(p, 6, oldprot, &oldprot);
}
}
__except (EXCEPTION_EXECUTE_HANDLER) {
;
}
set_lasterrors(&lasterror);
}
#endif
return ret;
}
HOOKDEF(BOOL, WINAPI, LookupPrivilegeValueW,
__in_opt LPWSTR lpSystemName,
__in LPWSTR lpName,
__out PLUID lpLuid
) {
BOOL ret = Old_LookupPrivilegeValueW(lpSystemName, lpName, lpLuid);
LOQ_bool("system", "uu", "SystemName", lpSystemName, "PrivilegeName", lpName);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtClose,
__in HANDLE Handle
) {
NTSTATUS ret;
if (Handle == g_log_handle) {
ret = STATUS_INVALID_HANDLE;
LOQ_ntstatus("system", "ps", "Handle", Handle, "Alert", "Tried to close Cuckoo's log handle");
return ret;
}
ret = Old_NtClose(Handle);
LOQ_ntstatus("system", "p", "Handle", Handle);
if(NT_SUCCESS(ret)) {
remove_file_from_log_tracking(Handle);
file_close(Handle);
}
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtDuplicateObject,
__in HANDLE SourceProcessHandle,
__in HANDLE SourceHandle,
__in_opt HANDLE TargetProcessHandle,
__out_opt PHANDLE TargetHandle,
__in ACCESS_MASK DesiredAccess,
__in ULONG HandleAttributes,
__in ULONG Options
) {
NTSTATUS ret = Old_NtDuplicateObject(SourceProcessHandle, SourceHandle, TargetProcessHandle,
TargetHandle, DesiredAccess, HandleAttributes, Options);
if (TargetHandle)
LOQ_ntstatus("system", "pppPh", "SourceProcessHandle", SourceProcessHandle, "SourceHandle", SourceHandle, "TargetProcessHandle", TargetProcessHandle, "TargetHandle", TargetHandle, "Options", Options);
else
LOQ_ntstatus("system", "pph", "SourceProcessHandle", SourceProcessHandle, "SourceHandle", SourceHandle, "Options", Options);
if (NT_SUCCESS(ret)) {
if (TargetProcessHandle == NtCurrentProcess() && TargetHandle) {
handle_duplicate(SourceHandle, *TargetHandle);
handle_duplicate(SourceHandle, *TargetHandle);
}
if (SourceProcessHandle == NtCurrentProcess() && (Options & DUPLICATE_CLOSE_SOURCE)) {
remove_file_from_log_tracking(SourceHandle);
file_close(SourceHandle);
}
}
return ret;
}
HOOKDEF(BOOL, WINAPI, SaferIdentifyLevel,
_In_ DWORD dwNumProperties,
_In_opt_ PVOID pCodeProperties,
_Out_ PVOID pLevelHandle,
_Reserved_ LPVOID lpReserved
) {
BOOL ret;
ret = Old_SaferIdentifyLevel(dwNumProperties, pCodeProperties, pLevelHandle, lpReserved);
LOQ_bool("misc", "");
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtMakeTemporaryObject,
__in HANDLE ObjectHandle
) {
NTSTATUS ret = Old_NtMakeTemporaryObject(ObjectHandle);
LOQ_ntstatus("system", "p", "ObjectHandle", ObjectHandle);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtMakePermanentObject,
__in HANDLE ObjectHandle
) {
NTSTATUS ret = Old_NtMakePermanentObject(ObjectHandle);
LOQ_ntstatus("system", "p", "ObjectHandle", ObjectHandle);
return ret;
}
HOOKDEF(BOOL, WINAPI, WriteConsoleA,
_In_ HANDLE hConsoleOutput,
_In_ const VOID *lpBuffer,
_In_ DWORD nNumberOfCharsToWrite,
_Out_ LPDWORD lpNumberOfCharsWritten,
_Reserved_ LPVOID lpReseverd
) {
BOOL ret = Old_WriteConsoleA(hConsoleOutput, lpBuffer,
nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReseverd);
LOQ_bool("system", "pS", "ConsoleHandle", hConsoleOutput,
"Buffer", nNumberOfCharsToWrite, lpBuffer);
return ret;
}
HOOKDEF(BOOL, WINAPI, WriteConsoleW,
_In_ HANDLE hConsoleOutput,
_In_ const VOID *lpBuffer,
_In_ DWORD nNumberOfCharsToWrite,
_Out_ LPDWORD lpNumberOfCharsWritten,
_Reserved_ LPVOID lpReseverd
) {
BOOL ret = Old_WriteConsoleW(hConsoleOutput, lpBuffer,
nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReseverd);
LOQ_bool("system", "pU", "ConsoleHandle", hConsoleOutput,
"Buffer", nNumberOfCharsToWrite, lpBuffer);
return ret;
}
HOOKDEF(int, WINAPI, GetSystemMetrics,
_In_ int nIndex
) {
int ret = Old_GetSystemMetrics(nIndex);
if (nIndex == SM_CXSCREEN || nIndex == SM_CXVIRTUALSCREEN || nIndex == SM_CYSCREEN ||
nIndex == SM_CYVIRTUALSCREEN || nIndex == SM_REMOTECONTROL || nIndex == SM_REMOTESESSION ||
nIndex == SM_SHUTTINGDOWN || nIndex == SM_SWAPBUTTON)
LOQ_nonzero("misc", "i", "SystemMetricIndex", nIndex);
return ret;
}
typedef int (WINAPI * __GetSystemMetrics)(__in int nIndex);
__GetSystemMetrics _GetSystemMetrics;
DWORD WINAPI our_GetSystemMetrics(
__in int nIndex
) {
if (!_GetSystemMetrics) {
_GetSystemMetrics = (__GetSystemMetrics)GetProcAddress(LoadLibraryA("user32"), "GetSystemMetrics");
}
return _GetSystemMetrics(nIndex);
}
static LARGE_INTEGER last_skipped;
static int num_to_spoof;
static int num_spoofed;
static int lastx;
static int lasty;
HOOKDEF(BOOL, WINAPI, GetCursorPos,
_Out_ LPPOINT lpPoint
) {
BOOL ret = Old_GetCursorPos(lpPoint);
/* work around the fact that skipping sleeps prevents the human module from making the system look active */
if (lpPoint && time_skipped.QuadPart != last_skipped.QuadPart) {
int xres, yres;
xres = our_GetSystemMetrics(0);
yres = our_GetSystemMetrics(1);
if (!num_to_spoof)
num_to_spoof = (random() % 20) + 10;
if (num_spoofed < num_to_spoof) {
lpPoint->x = random() % xres;
lpPoint->y = random() % yres;
num_spoofed++;
}
else {
lpPoint->x = lastx;
lpPoint->y = lasty;
lastx = lpPoint->x;
lasty = lpPoint->y;
}
last_skipped.QuadPart = time_skipped.QuadPart;
}
else if (last_skipped.QuadPart == 0) {
last_skipped.QuadPart = time_skipped.QuadPart;
}
LOQ_bool("misc", "ii", "x", lpPoint != NULL ? lpPoint->x : 0,
"y", lpPoint != NULL ? lpPoint->y : 0);
return ret;
}
HOOKDEF(DWORD, WINAPI, GetLastError,
void
)
{
DWORD ret = Old_GetLastError();
LOQ_void("misc", "");
return ret;
}
HOOKDEF(BOOL, WINAPI, GetComputerNameA,
_Out_ LPSTR lpBuffer,
_Inout_ LPDWORD lpnSize
) {
BOOL ret = Old_GetComputerNameA(lpBuffer, lpnSize);
LOQ_bool("misc", "s", "ComputerName", lpBuffer);
return ret;
}
HOOKDEF(BOOL, WINAPI, GetComputerNameW,
_Out_ LPWSTR lpBuffer,
_Inout_ LPDWORD lpnSize
) {
BOOL ret = Old_GetComputerNameW(lpBuffer, lpnSize);
LOQ_bool("misc", "u", "ComputerName", lpBuffer);
return ret;
}
HOOKDEF(BOOL, WINAPI, GetUserNameA,
_Out_ LPSTR lpBuffer,
_Inout_ LPDWORD lpnSize
) {
BOOL ret = Old_GetUserNameA(lpBuffer, lpnSize);
LOQ_bool("misc", "s", "Name", lpBuffer);
return ret;
}
HOOKDEF(BOOL, WINAPI, GetUserNameW,
_Out_ LPWSTR lpBuffer,
_Inout_ LPDWORD lpnSize
) {
BOOL ret = Old_GetUserNameW(lpBuffer, lpnSize);
LOQ_bool("misc", "u", "Name", lpBuffer);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtLoadDriver,
__in PUNICODE_STRING DriverServiceName
) {
NTSTATUS ret = Old_NtLoadDriver(DriverServiceName);
LOQ_ntstatus("misc", "o", "DriverServiceName", DriverServiceName);
return ret;
}
static unsigned int asynckeystate_logcount;
HOOKDEF(SHORT, WINAPI, GetAsyncKeyState,
__in int vKey
) {
SHORT ret = Old_GetAsyncKeyState(vKey);
if (asynckeystate_logcount < 50 && ((vKey >= 0x30 && vKey <= 0x39) || (vKey >= 0x41 && vKey <= 0x5a))) {
asynckeystate_logcount++;
LOQ_nonzero("windows", "i", "KeyCode", vKey);
}
else if (asynckeystate_logcount == 50) {
asynckeystate_logcount++;
LOQ_nonzero("windows", "is", "KeyCode", vKey, "Status", "Log limit reached");
}
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, RtlDecompressBuffer,
__in USHORT CompressionFormat,
__out PUCHAR UncompressedBuffer,
__in ULONG UncompressedBufferSize,
__in PUCHAR CompressedBuffer,
__in ULONG CompressedBufferSize,
__out PULONG FinalUncompressedSize
) {
NTSTATUS ret = Old_RtlDecompressBuffer(CompressionFormat, UncompressedBuffer, UncompressedBufferSize,
CompressedBuffer, CompressedBufferSize, FinalUncompressedSize);
LOQ_ntstatus("misc", "pch", "UncompressedBufferAddress", UncompressedBuffer, "UncompressedBuffer",
ret ? 0 : *FinalUncompressedSize, UncompressedBuffer, "UncompressedBufferLength", ret ? 0 : *FinalUncompressedSize);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, RtlCompressBuffer,
_In_ USHORT CompressionFormatAndEngine,
_In_ PUCHAR UncompressedBuffer,
_In_ ULONG UncompressedBufferSize,
_Out_ PUCHAR CompressedBuffer,
_In_ ULONG CompressedBufferSize,
_In_ ULONG UncompressedChunkSize,
_Out_ PULONG FinalCompressedSize,
_In_ PVOID WorkSpace
) {
NTSTATUS ret = Old_RtlCompressBuffer(CompressionFormatAndEngine, UncompressedBuffer, UncompressedBufferSize,
CompressedBuffer, CompressedBufferSize, UncompressedChunkSize, FinalCompressedSize, WorkSpace);
LOQ_ntstatus("misc", "pbh", "UncompressedBufferAddress", UncompressedBuffer, "UncompressedBuffer",
ret ? 0 : UncompressedBufferSize, UncompressedBuffer, "UncompressedBufferLength", ret ? 0 : UncompressedBufferSize);
return ret;
}
HOOKDEF(void, WINAPI, GetSystemInfo,
__out LPSYSTEM_INFO lpSystemInfo
) {
int ret = 0;
Old_GetSystemInfo(lpSystemInfo);
if (!g_config.no_stealth && lpSystemInfo->dwNumberOfProcessors == 1)
lpSystemInfo->dwNumberOfProcessors = 2;
LOQ_void("misc", "");
return;
}
HOOKDEF(NTSTATUS, WINAPI, NtSetInformationProcess,
__in HANDLE ProcessHandle,
__in PROCESSINFOCLASS ProcessInformationClass,
__in PVOID ProcessInformation,
__in ULONG ProcessInformationLength
) {
NTSTATUS ret = Old_NtSetInformationProcess(ProcessHandle, ProcessInformationClass, ProcessInformation, ProcessInformationLength);
if (NT_SUCCESS(ret) && (ProcessInformationClass == ProcessInfoDEPPolicy || ProcessInformationClass == ProcessBreakOnTermination) && ProcessInformationLength == 4)
LOQ_ntstatus("misc", "ii", "ProcessInformationClass", ProcessInformationClass, "Value", *(int *)ProcessInformation);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtQuerySystemInformation,
_In_ ULONG SystemInformationClass,
_Inout_ PVOID SystemInformation,
_In_ ULONG SystemInformationLength,
_Out_opt_ PULONG ReturnLength
) {
NTSTATUS ret;
char *buf;
lasterror_t lasterror;
ENSURE_ULONG(ReturnLength);
if (SystemInformationClass != SystemProcessInformation || SystemInformation == NULL) {
normal_call:
ret = Old_NtQuerySystemInformation(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
LOQ_ntstatus("misc", "i", "SystemInformationClass", SystemInformationClass);
if (!g_config.no_stealth && SystemInformationClass == SystemBasicInformation && SystemInformationLength >= sizeof(SYSTEM_BASIC_INFORMATION) && NT_SUCCESS(ret)) {
PSYSTEM_BASIC_INFORMATION p = (PSYSTEM_BASIC_INFORMATION)SystemInformation;
p->NumberOfProcessors = 2;
}
/* This is nearly arbitrary and simply designed to test whether the Upatre author(s) or others
are reading this code */
if (!g_config.no_stealth && SystemInformationClass == SystemProcessorPerformanceInformation &&
NT_SUCCESS(ret) && SystemInformationLength >= (sizeof(LARGE_INTEGER) * 3)) {
PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION perf_info = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)SystemInformation;
perf_info->IdleTime.HighPart |= 2;
}
else if (!g_config.no_stealth && SystemInformationClass == SystemPerformanceInformation &&
NT_SUCCESS(ret) && SystemInformationLength >= sizeof(LARGE_INTEGER)) {
PLARGE_INTEGER perf_info = (PLARGE_INTEGER)SystemInformation;
perf_info->HighPart |= 2;
}
return ret;
}
get_lasterrors(&lasterror);
buf = calloc(1, SystemInformationLength);
set_lasterrors(&lasterror);
if (buf == NULL)
goto normal_call;
ret = Old_NtQuerySystemInformation(SystemInformationClass, buf, SystemInformationLength, ReturnLength);
LOQ_ntstatus("misc", "i", "SystemInformationClass", SystemInformationClass);
if (SystemInformationLength >= sizeof(SYSTEM_PROCESS_INFORMATION) && NT_SUCCESS(ret)) {
PSYSTEM_PROCESS_INFORMATION our_p = (PSYSTEM_PROCESS_INFORMATION)buf;
char *their_last_p = NULL;
char *their_p = (char *)SystemInformation;
ULONG lastlen = 0;
while (1) {
if (!is_protected_pid((DWORD)(ULONG_PTR)our_p->UniqueProcessId)) {
PSYSTEM_PROCESS_INFORMATION tmp;
if (our_p->NextEntryOffset)
lastlen = our_p->NextEntryOffset;
else
lastlen = *ReturnLength - (ULONG)((char *)our_p - buf);
// make sure we copy all data associated with the entry
memcpy(their_p, our_p, lastlen);
tmp = (PSYSTEM_PROCESS_INFORMATION)their_p;
tmp->NextEntryOffset = lastlen;
// adjust the only pointer field in the struct so that it points into the user's buffer,
// but only if the pointer exists, otherwise we'd rewrite a NULL pointer to something not NULL
if (tmp->ImageName.Buffer)
tmp->ImageName.Buffer = (PWSTR)(((ULONG_PTR)tmp->ImageName.Buffer - (ULONG_PTR)our_p) + (ULONG_PTR)their_p);
their_last_p = their_p;
their_p += lastlen;
}
if (!our_p->NextEntryOffset)
break;
our_p = (PSYSTEM_PROCESS_INFORMATION)((PCHAR)our_p + our_p->NextEntryOffset);
}
if (their_last_p) {
PSYSTEM_PROCESS_INFORMATION tmp;
tmp = (PSYSTEM_PROCESS_INFORMATION)their_last_p;
*ReturnLength = (ULONG)(their_last_p + tmp->NextEntryOffset - (char *)SystemInformation);
tmp->NextEntryOffset = 0;
}
}
free(buf);
return ret;
}
static GUID _CLSID_DiskDrive = { 0x4d36e967, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 };
static GUID _CLSID_CDROM = { 0x4d36e965, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 };
static GUID _CLSID_Display = { 0x4d36e968, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 };
static GUID _CLSID_FDC = { 0x4d36e969, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 };
static GUID _CLSID_HDC = { 0x4d36e96a, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 };
static GUID _CLSID_FloppyDisk = { 0x4d36e980, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 };
static char *known_object(IID *cls)
{
if (!memcmp(cls, &_CLSID_DiskDrive, sizeof(*cls)))
return "DiskDrive";
else if (!memcmp(cls, &_CLSID_CDROM, sizeof(*cls)))
return "CDROM";
else if (!memcmp(cls, &_CLSID_Display, sizeof(*cls)))
return "Display";
else if (!memcmp(cls, &_CLSID_FDC, sizeof(*cls)))
return "FDC";
else if (!memcmp(cls, &_CLSID_HDC, sizeof(*cls)))
return "HDC";
else if (!memcmp(cls, &_CLSID_FloppyDisk, sizeof(*cls)))
return "FloppyDisk";
return NULL;
}
HOOKDEF(HDEVINFO, WINAPI, SetupDiGetClassDevsA,
_In_opt_ const GUID *ClassGuid,
_In_opt_ PCSTR Enumerator,
_In_opt_ HWND hwndParent,
_In_ DWORD Flags
) {
IID id1;
char idbuf[40];
char *known;
lasterror_t lasterror;
HDEVINFO ret = Old_SetupDiGetClassDevsA(ClassGuid, Enumerator, hwndParent, Flags);
get_lasterrors(&lasterror);
if (ClassGuid) {
memcpy(&id1, ClassGuid, sizeof(id1));
sprintf(idbuf, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", id1.Data1, id1.Data2, id1.Data3,
id1.Data4[0], id1.Data4[1], id1.Data4[2], id1.Data4[3], id1.Data4[4], id1.Data4[5], id1.Data4[6], id1.Data4[7]);
set_lasterrors(&lasterror);
if ((known = known_object(&id1)))
LOQ_handle("misc", "ss", "ClassGuid", idbuf, "Known", known);
else
LOQ_handle("misc", "s", "ClassGuid", idbuf);
}
return ret;
}
HOOKDEF(HDEVINFO, WINAPI, SetupDiGetClassDevsW,
_In_opt_ const GUID *ClassGuid,
_In_opt_ PCWSTR Enumerator,
_In_opt_ HWND hwndParent,
_In_ DWORD Flags
) {
IID id1;
char idbuf[40];
char *known;
lasterror_t lasterror;
HDEVINFO ret = Old_SetupDiGetClassDevsW(ClassGuid, Enumerator, hwndParent, Flags);
if (ClassGuid) {
memcpy(&id1, ClassGuid, sizeof(id1));
sprintf(idbuf, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", id1.Data1, id1.Data2, id1.Data3,
id1.Data4[0], id1.Data4[1], id1.Data4[2], id1.Data4[3], id1.Data4[4], id1.Data4[5], id1.Data4[6], id1.Data4[7]);
set_lasterrors(&lasterror);
if ((known = known_object(&id1)))
LOQ_handle("misc", "ss", "ClassGuid", idbuf, "Known", known);
else
LOQ_handle("misc", "s", "ClassGuid", idbuf);
}
return ret;
}
HOOKDEF(BOOL, WINAPI, SetupDiGetDeviceRegistryPropertyA,
_In_ HDEVINFO DeviceInfoSet,
_In_ PSP_DEVINFO_DATA DeviceInfoData,
_In_ DWORD Property,
_Out_opt_ PDWORD PropertyRegDataType,
_Out_opt_ PBYTE PropertyBuffer,
_In_ DWORD PropertyBufferSize,
_Out_opt_ PDWORD RequiredSize
) {
BOOL ret;
ENSURE_DWORD(PropertyRegDataType);
ENSURE_DWORD(RequiredSize);
ret = Old_SetupDiGetDeviceRegistryPropertyA(DeviceInfoSet, DeviceInfoData, Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize, RequiredSize);
if (PropertyBuffer)
LOQ_bool("misc", "ir", "Property", Property, "PropertyBuffer", *PropertyRegDataType, PropertyBufferSize, PropertyBuffer);
if (!g_config.no_stealth && ret && PropertyBuffer) {
replace_ci_string_in_buf(PropertyBuffer, *RequiredSize, "VBOX", "DELL_");
replace_ci_string_in_buf(PropertyBuffer, *RequiredSize, "QEMU", "DELL");
replace_ci_string_in_buf(PropertyBuffer, *RequiredSize, "VMWARE", "DELL__");
}
return ret;
}
HOOKDEF(BOOL, WINAPI, SetupDiGetDeviceRegistryPropertyW,
_In_ HDEVINFO DeviceInfoSet,
_In_ PSP_DEVINFO_DATA DeviceInfoData,
_In_ DWORD Property,
_Out_opt_ PDWORD PropertyRegDataType,
_Out_opt_ PBYTE PropertyBuffer,
_In_ DWORD PropertyBufferSize,
_Out_opt_ PDWORD RequiredSize
) {
BOOL ret;
ENSURE_DWORD(PropertyRegDataType);
ENSURE_DWORD(RequiredSize);
ret = Old_SetupDiGetDeviceRegistryPropertyW(DeviceInfoSet, DeviceInfoData, Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize, RequiredSize);
if (PropertyBuffer)
LOQ_bool("misc", "iR", "Property", Property, "PropertyBuffer", *PropertyRegDataType, PropertyBufferSize, PropertyBuffer);
if (!g_config.no_stealth && ret && PropertyBuffer) {
replace_ci_wstring_in_buf((PWCHAR)PropertyBuffer, *RequiredSize / sizeof(WCHAR), L"VBOX", L"DELL_");
replace_ci_wstring_in_buf((PWCHAR)PropertyBuffer, *RequiredSize / sizeof(WCHAR), L"QEMU", L"DELL");
replace_ci_wstring_in_buf((PWCHAR)PropertyBuffer, *RequiredSize / sizeof(WCHAR), L"VMWARE", L"DELL__");
}
return ret;
}
HOOKDEF(BOOL, WINAPI, SetupDiBuildDriverInfoList,
_In_ HDEVINFO DeviceInfoSet,
_Inout_ PSP_DEVINFO_DATA DeviceInfoData,
_In_ DWORD DriverType
) {
BOOL ret;
ret = Old_SetupDiBuildDriverInfoList(DeviceInfoSet, DeviceInfoData, DriverType);
LOQ_bool("misc", "");
return ret;
}
HOOKDEF(HRESULT, WINAPI, DecodeImageEx,
__in PVOID pStream, // IStream *
__in PVOID pMap, // IMapMIMEToCLSID *
__in PVOID pEventSink, // IUnknown *
__in_opt LPCWSTR pszMIMETypeParam
) {
HRESULT ret = Old_DecodeImageEx(pStream, pMap, pEventSink, pszMIMETypeParam);
LOQ_hresult("misc", "");
return ret;
}
HOOKDEF(HRESULT, WINAPI, DecodeImage,
__in PVOID pStream, // IStream *
__in PVOID pMap, // IMapMIMEToCLSID *
__in PVOID pEventSink // IUnknown *
) {
HRESULT ret = Old_DecodeImage(pStream, pMap, pEventSink);
LOQ_hresult("misc", "");
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, LsaOpenPolicy,
PLSA_UNICODE_STRING SystemName,
PVOID ObjectAttributes,
ACCESS_MASK DesiredAccess,
PVOID PolicyHandle
) {
NTSTATUS ret = Old_LsaOpenPolicy(SystemName, ObjectAttributes, DesiredAccess, PolicyHandle);
LOQ_ntstatus("misc", "");
return ret;
}
HOOKDEF(DWORD, WINAPI, WNetGetProviderNameW,
__in DWORD dwNetType,
__out LPWSTR lpProviderName,
__inout LPDWORD lpBufferSize
) {
DWORD ret;
WCHAR *tmp = calloc(1, (*lpBufferSize + 1) * sizeof(wchar_t));
if (tmp == NULL)
return Old_WNetGetProviderNameW(dwNetType, lpProviderName, lpBufferSize);
ret = Old_WNetGetProviderNameW(dwNetType, tmp, lpBufferSize);
LOQ_zero("misc", "iu", "NetType", dwNetType, "ProviderName", ret == NO_ERROR ? tmp : L"");
// WNNC_NET_RDR2SAMPLE, used for vbox detection
if (!g_config.no_stealth && ret && dwNetType == 0x250000) {
lasterror_t lasterrors;
ret = ERROR_NO_NETWORK;
lasterrors.Win32Error = ERROR_NO_NETWORK;
lasterrors.NtstatusError = STATUS_ENTRYPOINT_NOT_FOUND;
}
else if (ret == NO_ERROR && lpProviderName) {
wcscpy(lpProviderName, tmp);
}
free(tmp);
return ret;
}
HOOKDEF(DWORD, WINAPI, RasValidateEntryNameW,
_In_ LPCWSTR lpszPhonebook,
_In_ LPCWSTR lpszEntry
) {
DWORD ret = Old_RasValidateEntryNameW(lpszPhonebook, lpszEntry);
LOQ_zero("misc", "uu", "Phonebook", lpszPhonebook, "Entry", lpszEntry);
return ret;
}
HOOKDEF(DWORD, WINAPI, RasConnectionNotificationW,
_In_ PVOID hrasconn,
_In_ HANDLE hEvent,
_In_ DWORD dwFlags
) {
DWORD ret = Old_RasConnectionNotificationW(hrasconn, hEvent, dwFlags);
LOQ_zero("misc", "");
return ret;
}
HOOKDEF(BOOL, WINAPI, SystemTimeToTzSpecificLocalTime,
_In_opt_ LPTIME_ZONE_INFORMATION lpTimeZone,
_In_ LPSYSTEMTIME lpUniversalTime,
_Out_ LPSYSTEMTIME lpLocalTime
) {
BOOL ret = SystemTimeToTzSpecificLocalTime(lpTimeZone, lpUniversalTime, lpLocalTime);
LOQ_bool("misc", "");
return ret;
}
HOOKDEF(HRESULT, WINAPI, CLSIDFromProgID,
_In_ LPCOLESTR lpszProgID,
_Out_ LPCLSID lpclsid
) {
HRESULT ret = CLSIDFromProgID(lpszProgID, lpclsid);
LOQ_hresult("misc", "u", "ProgID", lpszProgID);
return ret;
}
HOOKDEF(BOOL, WINAPI, GetCurrentHwProfileW,
_Out_ LPHW_PROFILE_INFO lpHwProfileInfo
) {
BOOL ret = Old_GetCurrentHwProfileW(lpHwProfileInfo);
LOQ_bool("misc", "uu", "ProfileGUID", lpHwProfileInfo->szHwProfileGuid, "ProfileName", lpHwProfileInfo->szHwProfileName);
return ret;
}
HOOKDEF(BOOL, WINAPI, IsUserAdmin,
void
) {
BOOL ret = Old_IsUserAdmin();
LOQ_bool("misc", "");
return ret;
}
HOOKDEF(void, WINAPI, GlobalMemoryStatus,
_Out_ LPMEMORYSTATUS lpBuffer
) {
BOOL ret = TRUE;
Old_GlobalMemoryStatus(lpBuffer);
if (!g_config.no_stealth && lpBuffer->dwTotalPhys < 0x80000000)
lpBuffer->dwTotalPhys = 0x80000000;
LOQ_void("misc", "ii", "MemoryLoad", lpBuffer->dwMemoryLoad, "TotalPhysicalMB", lpBuffer->dwTotalPhys / (1024 * 1024));
}
HOOKDEF(BOOL, WINAPI, GlobalMemoryStatusEx,
_Out_ LPMEMORYSTATUSEX lpBuffer
) {
BOOL ret = Old_GlobalMemoryStatusEx(lpBuffer);
if (ret && !g_config.no_stealth && lpBuffer->ullTotalPhys < 0x80000000)
lpBuffer->ullTotalPhys = 0x80000000;
LOQ_void("misc", "ii", "MemoryLoad", lpBuffer->dwMemoryLoad, "TotalPhysicalMB", lpBuffer->ullTotalPhys / (1024 * 1024));
return ret;
}
HOOKDEF(BOOL, WINAPI, SystemParametersInfoA,
_In_ UINT uiAction,
_In_ UINT uiParam,
_Inout_ PVOID pvParam,
_In_ UINT fWinIni
) {
BOOL ret = Old_SystemParametersInfoA(uiAction, uiParam, pvParam, fWinIni);
if (ret && (uiAction == SPI_SETDESKWALLPAPER || uiAction == SPI_GETDESKWALLPAPER))
LOQ_bool("misc", "hhs", "Action", uiAction, "uiParam", uiParam, "pvParam", pvParam);
else
LOQ_bool("misc", "hh", "Action", uiAction, "uiParam", uiParam);
return ret;
}
HOOKDEF(BOOL, WINAPI, SystemParametersInfoW,
_In_ UINT uiAction,
_In_ UINT uiParam,
_Inout_ PVOID pvParam,
_In_ UINT fWinIni