forked from spender-sandbox/cuckoomon-modified
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook_sleep.c
624 lines (517 loc) · 16.6 KB
/
hook_sleep.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
/*
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 "config.h"
#include "misc.h"
// only skip Sleep()'s the first five seconds
#define MAX_SLEEP_SKIP_DIFF 5000
// skipping sleep calls is done while this variable is set to true
static int sleep_skip_active = 1;
// the amount of time skipped, in 100-nanosecond
LARGE_INTEGER time_skipped;
static LARGE_INTEGER time_start;
static int num_skipped = 0;
static int num_small = 0;
static int num_msg_small = 0;
static int num_wait_skipped = 0;
static int num_wait_small = 0;
void disable_sleep_skip()
{
if (sleep_skip_active && g_config.force_sleepskip < 1) {
pipe("INFO:Disabling sleep skipping.");
sleep_skip_active = 0;
}
}
HOOKDEF(NTSTATUS, WINAPI, NtWaitForSingleObject,
__in HANDLE Handle,
__in BOOLEAN Alertable,
__in_opt PLARGE_INTEGER Timeout
) {
NTSTATUS ret = 0;
LONGLONG interval;
LARGE_INTEGER newint;
LARGE_INTEGER li;
unsigned long milli;
FILETIME ft;
lasterror_t lasterror;
get_lasterrors(&lasterror);
// handle INFINITE wait
if (Timeout == NULL || Timeout->QuadPart == 0x8000000000000000ULL) {
// only log potentially interesting cases
if (hook_info()->main_caller_retaddr)
LOQ_ntstatus("system", "pis", "Handle", Handle, "Milliseconds", -1, "Status", "Infinite");
set_lasterrors(&lasterror);
return Old_NtWaitForSingleObject(Handle, Alertable, Timeout);
}
newint.QuadPart = Timeout->QuadPart;
if (newint.QuadPart > 0LL) {
/* convert absolute time to relative time */
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
newint.HighPart = ft.dwHighDateTime;
newint.LowPart = ft.dwLowDateTime;
newint.QuadPart += time_skipped.QuadPart;
newint.QuadPart -= Timeout->QuadPart;
if (newint.QuadPart > 0LL)
newint.QuadPart = 0LL;
}
interval = -newint.QuadPart;
milli = (unsigned long)(interval / 10000);
GetSystemTimeAsFileTime(&ft);
li.HighPart = ft.dwHighDateTime;
li.LowPart = ft.dwLowDateTime;
/* clamp sleeps between 30 seconds and 1 hour down to 10 seconds as long as we didn't force off sleep skipping */
if (milli >= 30000 && milli <= 3600000 && g_config.force_sleepskip != 0) {
newint.QuadPart = -(10000 * 10000);
time_skipped.QuadPart += interval - (10000 * 10000);
LOQ_ntstatus("system", "pis", "Handle", Handle, "Milliseconds", milli, "Status", "Skipped");
goto docall;
}
else if (g_config.force_sleepskip > 0) {
time_skipped.QuadPart += interval;
LOQ_ntstatus("system", "pis", "Handle", Handle, "Milliseconds", milli, "Status", "Skipped");
newint.QuadPart = 0;
goto docall;
}
else {
disable_sleep_skip();
}
if (milli <= 10) {
if (num_wait_small < 20) {
LOQ_ntstatus("system", "pi", "Handle", Handle, "Milliseconds", milli);
num_wait_small++;
}
else if (num_wait_small == 20) {
LOQ_ntstatus("system", "s", "Status", "Small log limit reached");
num_wait_small++;
}
else if (num_wait_small > 20) {
// likely using a bunch of tiny sleeps to delay execution, so let's suddenly mimic high load and give our
// fake passage of time the impression of longer delays to return from sleep
time_skipped.QuadPart += (randint(500, 1000) * 10000);
}
}
else {
LOQ_ntstatus("system", "pi", "Handle", Handle, "Milliseconds", milli);
}
docall:
set_lasterrors(&lasterror);
return Old_NtWaitForSingleObject(Handle, Alertable, &newint);
}
HOOKDEF(NTSTATUS, WINAPI, NtDelayExecution,
__in BOOLEAN Alertable,
__in PLARGE_INTEGER DelayInterval
) {
NTSTATUS ret = 0;
LONGLONG interval;
FILETIME ft;
LARGE_INTEGER li;
LARGE_INTEGER newint;
unsigned long milli;
lasterror_t lasterror;
get_lasterrors(&lasterror);
newint.QuadPart = DelayInterval->QuadPart;
// handle INFINITE sleep
if (newint.QuadPart == 0x8000000000000000ULL) {
LOQ_ntstatus("system", "is", "Milliseconds", -1, "Status", "Infinite");
goto docall;
}
if (newint.QuadPart > 0LL) {
/* convert absolute time to relative time */
GetSystemTimeAsFileTime(&ft);
newint.HighPart = ft.dwHighDateTime;
newint.LowPart = ft.dwLowDateTime;
newint.QuadPart += time_skipped.QuadPart;
newint.QuadPart -= DelayInterval->QuadPart;
if (newint.QuadPart > 0LL)
newint.QuadPart = 0LL;
}
interval = -newint.QuadPart;
milli = (unsigned long)(interval / 10000);
GetSystemTimeAsFileTime(&ft);
li.HighPart = ft.dwHighDateTime;
li.LowPart = ft.dwLowDateTime;
// check if we're still within the hardcoded limit
if(sleep_skip_active && (li.QuadPart < time_start.QuadPart + MAX_SLEEP_SKIP_DIFF * 10000)) {
time_skipped.QuadPart += interval;
if (num_skipped < 20) {
// notify how much we've skipped
LOQ_ntstatus("system", "is", "Milliseconds", milli, "Status", "Skipped");
num_skipped++;
}
else if (num_skipped == 20) {
LOQ_ntstatus("system", "s", "Status", "Skipped log limit reached");
num_skipped++;
}
goto skipcall;
}
/* clamp sleeps between 30 seconds and 1 hour down to 10 seconds as long as we didn't force off sleep skipping */
else if (milli >= 30000 && milli <= 3600000 && g_config.force_sleepskip != 0) {
newint.QuadPart = -(10000 * 10000);
time_skipped.QuadPart += interval - (10000 * 10000);
LOQ_ntstatus("system", "is", "Milliseconds", milli, "Status", "Skipped");
goto docall;
}
else if (g_config.force_sleepskip > 0) {
time_skipped.QuadPart += interval;
LOQ_ntstatus("system", "is", "Milliseconds", milli, "Status", "Skipped");
newint.QuadPart = 0;
goto docall;
}
else {
disable_sleep_skip();
}
if (milli <= 10) {
if (num_small < 20) {
LOQ_ntstatus("system", "i", "Milliseconds", milli);
num_small++;
}
else if (num_small == 20) {
LOQ_ntstatus("system", "s", "Status", "Small log limit reached");
num_small++;
}
else if (num_small > 20) {
// likely using a bunch of tiny sleeps to delay execution, so let's suddenly mimic high load and give our
// fake passage of time the impression of longer delays to return from sleep
time_skipped.QuadPart += (randint(500, 1000) * 10000);
}
}
else {
LOQ_ntstatus("system", "i", "Milliseconds", milli);
}
docall:
set_lasterrors(&lasterror);
return Old_NtDelayExecution(Alertable, &newint);
skipcall:
set_lasterrors(&lasterror);
return ret;
}
HOOKDEF(DWORD, WINAPI, MsgWaitForMultipleObjectsEx,
_In_ DWORD nCount,
_In_ const HANDLE *pHandles,
_In_ DWORD dwMilliseconds,
_In_ DWORD dwWakeMask,
_In_ DWORD dwFlags
) {
DWORD ret = 0;
if (dwMilliseconds == INFINITE || nCount)
goto docall;
/* clamp sleeps between 30 seconds and 1 hour down to 10 seconds as long as we didn't force off sleep skipping */
else if (dwMilliseconds >= 30000 && dwMilliseconds <= 3600000 && g_config.force_sleepskip != 0) {
time_skipped.QuadPart += (dwMilliseconds - 10000) * 10000;
LOQ_msgwait("system", "is", "Milliseconds", dwMilliseconds, "Status", "Skipped");
dwMilliseconds = 10000;
goto docall;
}
else if (g_config.force_sleepskip > 0) {
LOQ_msgwait("system", "is", "Milliseconds", dwMilliseconds, "Status", "Skipped");
dwMilliseconds = 0;
goto docall;
}
else {
disable_sleep_skip();
}
if (dwMilliseconds <= 10) {
if (num_msg_small < 20) {
LOQ_msgwait("system", "i", "Milliseconds", dwMilliseconds);
num_msg_small++;
}
else if (num_msg_small == 20) {
LOQ_msgwait("system", "s", "Status", "Small log limit reached");
num_msg_small++;
}
else if (num_msg_small > 20) {
// likely using a bunch of tiny sleeps to delay execution, so let's suddenly mimic high load and give our
// fake passage of time the impression of longer delays to return from sleep
time_skipped.QuadPart += (randint(500, 1000) * 10000);
}
}
else {
LOQ_msgwait("system", "i", "Milliseconds", dwMilliseconds);
}
docall:
ret = Old_MsgWaitForMultipleObjectsEx(nCount, pHandles, dwMilliseconds, dwWakeMask, dwFlags);
disable_tail_call_optimization();
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtSetTimer,
IN HANDLE TimerHandle,
IN PLARGE_INTEGER DueTime,
IN PVOID TimerApcRoutine OPTIONAL,
IN PVOID TimerContext OPTIONAL,
IN BOOLEAN ResumeTimer,
IN LONG Period OPTIONAL,
OUT PBOOLEAN PreviousState OPTIONAL
) {
NTSTATUS ret = 0;
LONGLONG interval;
FILETIME ft;
LARGE_INTEGER newint;
unsigned long milli;
lasterror_t lasterror;
get_lasterrors(&lasterror);
newint.QuadPart = DueTime->QuadPart;
// handle INFINITE sleep
if (newint.QuadPart == 0x8000000000000000ULL) {
LOQ_ntstatus("system", "is", "Milliseconds", -1, "Status", "Infinite");
goto docall;
}
if (newint.QuadPart > 0LL) {
/* convert absolute time to relative time */
GetSystemTimeAsFileTime(&ft);
newint.HighPart = ft.dwHighDateTime;
newint.LowPart = ft.dwLowDateTime;
newint.QuadPart += time_skipped.QuadPart;
newint.QuadPart -= DueTime->QuadPart;
if (newint.QuadPart > 0LL)
newint.QuadPart = 0LL;
}
interval = -newint.QuadPart;
milli = (unsigned long)(interval / 10000);
/* clamp sleeps between 30 seconds and 1 hour down to 10 seconds as long as we didn't force off sleep skipping */
if (milli >= 30000 && milli <= 3600000 && g_config.force_sleepskip != 0) {
newint.QuadPart = -(10000 * 10000);
time_skipped.QuadPart += interval - (10000 * 10000);
LOQ_ntstatus("system", "is", "Milliseconds", milli, "Status", "Skipped");
goto docall;
}
else if (g_config.force_sleepskip > 0) {
time_skipped.QuadPart += interval;
LOQ_ntstatus("system", "is", "Milliseconds", milli, "Status", "Skipped");
newint.QuadPart = 0;
goto docall;
}
docall:
set_lasterrors(&lasterror);
ret = Old_NtSetTimer(TimerHandle, &newint, TimerApcRoutine, TimerContext, ResumeTimer, Period, PreviousState);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtSetTimerEx,
IN HANDLE TimerHandle,
IN int TimerSetInformationClass,
__inout PVOID TimerSetInformation,
IN ULONG TimerSetInformationLength
) {
NTSTATUS ret = 0;
LONGLONG interval;
FILETIME ft;
LARGE_INTEGER newint;
LARGE_INTEGER origdue;
unsigned long milli;
lasterror_t lasterror;
BOOL modified_delay = FALSE;
PTIMER_SET_COALESCABLE_TIMER_INFO timerinfo;
get_lasterrors(&lasterror);
if (TimerSetInformationClass)
goto docall;
timerinfo = (PTIMER_SET_COALESCABLE_TIMER_INFO)TimerSetInformation;
origdue = timerinfo->DueTime;
newint.QuadPart = origdue.QuadPart;
// handle INFINITE sleep
if (newint.QuadPart == 0x8000000000000000ULL) {
LOQ_ntstatus("system", "is", "Milliseconds", -1, "Status", "Infinite");
goto docall;
}
if (newint.QuadPart > 0LL) {
/* convert absolute time to relative time */
GetSystemTimeAsFileTime(&ft);
newint.HighPart = ft.dwHighDateTime;
newint.LowPart = ft.dwLowDateTime;
newint.QuadPart += time_skipped.QuadPart;
newint.QuadPart -= origdue.QuadPart;
if (newint.QuadPart > 0LL)
newint.QuadPart = 0LL;
timerinfo->DueTime.QuadPart = newint.QuadPart;
modified_delay = TRUE;
}
interval = -newint.QuadPart;
milli = (unsigned long)(interval / 10000);
/* clamp sleeps between 30 seconds and 1 hour down to 10 seconds as long as we didn't force off sleep skipping */
if (milli >= 30000 && milli <= 3600000 && g_config.force_sleepskip != 0) {
timerinfo->DueTime.QuadPart = -(10000 * 10000);
time_skipped.QuadPart += interval - (10000 * 10000);
LOQ_ntstatus("system", "is", "Milliseconds", milli, "Status", "Skipped");
modified_delay = TRUE;
goto docall;
}
else if (g_config.force_sleepskip > 0) {
time_skipped.QuadPart += interval;
LOQ_ntstatus("system", "is", "Milliseconds", milli, "Status", "Skipped");
timerinfo->DueTime.QuadPart = 0;
modified_delay = TRUE;
goto docall;
}
docall:
set_lasterrors(&lasterror);
ret = Old_NtSetTimerEx(TimerHandle, TimerSetInformationClass, TimerSetInformation, TimerSetInformationLength);
if (modified_delay) {
timerinfo->DueTime = origdue;
}
return ret;
}
static LARGE_INTEGER perf_multiplier;
HOOKDEF(NTSTATUS, WINAPI, NtQueryPerformanceCounter,
_Out_ PLARGE_INTEGER PerformanceCounter,
_Out_opt_ PLARGE_INTEGER PerformanceFrequency
) {
NTSTATUS ret;
ENSURE_LARGE_INTEGER(PerformanceFrequency);
ret = Old_NtQueryPerformanceCounter(PerformanceCounter, PerformanceFrequency);
if (NT_SUCCESS(ret)) {
if (!perf_multiplier.QuadPart)
perf_multiplier.QuadPart = PerformanceFrequency->QuadPart / 1000;
PerformanceCounter->QuadPart += (time_skipped.QuadPart / 10000) * perf_multiplier.QuadPart;
}
return ret;
}
HOOKDEF(void, WINAPI, GetLocalTime,
__out LPSYSTEMTIME lpSystemTime
) {
lasterror_t lasterror;
LARGE_INTEGER li; FILETIME ft;
DWORD ret = 0;
Old_GetLocalTime(lpSystemTime);
get_lasterrors(&lasterror);
SystemTimeToFileTime(lpSystemTime, &ft);
li.HighPart = ft.dwHighDateTime;
li.LowPart = ft.dwLowDateTime;
li.QuadPart += time_skipped.QuadPart;
ft.dwHighDateTime = li.HighPart;
ft.dwLowDateTime = li.LowPart;
FileTimeToSystemTime(&ft, lpSystemTime);
set_lasterrors(&lasterror);
LOQ_void("system", "");
}
HOOKDEF(void, WINAPI, GetSystemTime,
__out LPSYSTEMTIME lpSystemTime
) {
lasterror_t lasterror;
LARGE_INTEGER li; FILETIME ft;
DWORD ret = 0;
Old_GetSystemTime(lpSystemTime);
get_lasterrors(&lasterror);
SystemTimeToFileTime(lpSystemTime, &ft);
li.HighPart = ft.dwHighDateTime;
li.LowPart = ft.dwLowDateTime;
li.QuadPart += time_skipped.QuadPart;
ft.dwHighDateTime = li.HighPart;
ft.dwLowDateTime = li.LowPart;
FileTimeToSystemTime(&ft, lpSystemTime);
set_lasterrors(&lasterror);
LOQ_void("system", "");
}
DWORD raw_gettickcount(void)
{
if (g_osverinfo.dwMajorVersion >= 6)
return (DWORD)((*(ULONGLONG *)0x7ffe0320 * *(DWORD *)0x7ffe0004) >> 24);
else
return (DWORD)(((ULONGLONG)*(DWORD *)0x7ffe0000 * *(DWORD *)0x7ffe0004) >> 24);
}
ULONGLONG raw_gettickcount64(void)
{
ULARGE_INTEGER tickcount64;
DWORD multiplier = *(DWORD *)0x7ffe0004;
tickcount64.LowPart = *(DWORD *)0x7ffe0320;
tickcount64.HighPart = *(DWORD *)0x7ffe0324;
return (((ULONGLONG)tickcount64.LowPart * multiplier) >> 24) +
(((ULONGLONG)tickcount64.HighPart * multiplier) << 8);
}
HOOKDEF(DWORD, WINAPI, GetTickCount,
void
) {
DWORD ret;
FORCE_FRAME_PTR_USE();
ret = raw_gettickcount();
// add the time we've skipped
ret += (DWORD)(time_skipped.QuadPart / 10000);
return ret;
}
HOOKDEF(ULONGLONG, WINAPI, GetTickCount64,
void
) {
ULONGLONG ret;
FORCE_FRAME_PTR_USE();
ret = raw_gettickcount64();
// add the time we've skipped
ret += (time_skipped.QuadPart / 10000);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtQuerySystemTime,
_Out_ PLARGE_INTEGER SystemTime
) {
NTSTATUS ret = Old_NtQuerySystemTime(SystemTime);
LOQ_ntstatus("system", "");
if(NT_SUCCESS(ret)) {
SystemTime->QuadPart += time_skipped.QuadPart;
}
return 0;
}
HOOKDEF(DWORD, WINAPI, timeGetTime,
void
) {
DWORD ret;
FORCE_FRAME_PTR_USE();
ret = Old_timeGetTime();
// add the time we've skipped
ret += (DWORD)(time_skipped.QuadPart / 10000);
return ret;
}
HOOKDEF(void, WINAPI, GetSystemTimeAsFileTime,
_Out_ LPFILETIME lpSystemTimeAsFileTime
) {
LARGE_INTEGER li;
FILETIME ft;
DWORD ret = 0;
Old_GetSystemTimeAsFileTime(&ft);
li.HighPart = ft.dwHighDateTime;
li.LowPart = ft.dwLowDateTime;
li.QuadPart += time_skipped.QuadPart;
ft.dwHighDateTime = li.HighPart;
ft.dwLowDateTime = li.LowPart;
memcpy(lpSystemTimeAsFileTime, &ft, sizeof(ft));
LOQ_void("system", "");
return;
}
static int lastinput_called;
HOOKDEF(BOOL, WINAPI, GetLastInputInfo,
_Out_ PLASTINPUTINFO plii
) {
BOOL ret = Old_GetLastInputInfo(plii);
LOQ_bool("system", "");
lastinput_called++;
/* fake recent user activity */
if (lastinput_called > 2 && plii && plii->cbSize == 8)
plii->dwTime = raw_gettickcount() + (DWORD)(time_skipped.QuadPart / 10000);
return ret;
}
void init_sleep_skip(int first_process)
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
time_start.HighPart = ft.dwHighDateTime;
time_start.LowPart = ft.dwLowDateTime;
// we don't want to skip sleep calls in child processes
if(first_process == 0) {
disable_sleep_skip();
}
}
void init_startup_time(unsigned int startup_time)
{
time_skipped.QuadPart += (unsigned __int64) startup_time * 10000;
}