-
Notifications
You must be signed in to change notification settings - Fork 15
/
hook_sleep.c
396 lines (333 loc) · 10.5 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
/*
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_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 = STATUS_TIMEOUT;
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);
// only handle lame cases
if (Handle != GetCurrentProcess()) {
LOQ_ntstatus("system", "pi", "Handle", Handle, "Milliseconds", milli);
goto docall;
}
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_wait_skipped < 20) {
// notify how much we've skipped
LOQ_ntstatus("system", "pis", "Handle", Handle, "Milliseconds", milli, "Status", "Skipped");
num_wait_skipped++;
}
else if (num_wait_skipped == 20) {
LOQ_ntstatus("system", "s", "Status", "Skipped log limit reached");
num_wait_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) {
LARGE_INTEGER newint;
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);
skipcall:
set_lasterrors(&lasterror);
return ret;
}
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 */
FILETIME ft;
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) {
LARGE_INTEGER newint;
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(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", "");
}
HOOKDEF(DWORD, WINAPI, GetTickCount,
void
) {
DWORD ret = Old_GetTickCount();
// add the time we've skipped
ret += (DWORD)(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 = 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 = 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;
}