forked from cuckoosandbox/cuckoomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook_reg.c
370 lines (343 loc) · 11.7 KB
/
hook_reg.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
/*
Cuckoo Sandbox - Automated Malware Analysis
Copyright (C) 2010-2014 Cuckoo Sandbox Developers
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <windows.h>
#include "hooking.h"
#include "ntapi.h"
#include "log.h"
static IS_SUCCESS_LONGREG();
HOOKDEF(LONG, WINAPI, RegOpenKeyExA,
__in HKEY hKey,
__in_opt LPCTSTR lpSubKey,
__reserved DWORD ulOptions,
__in REGSAM samDesired,
__out PHKEY phkResult
) {
LONG ret = Old_RegOpenKeyExA(hKey, lpSubKey, ulOptions, samDesired,
phkResult);
LOQ("psP", "Registry", hKey, "SubKey", lpSubKey, "Handle", phkResult);
return ret;
}
HOOKDEF(LONG, WINAPI, RegOpenKeyExW,
__in HKEY hKey,
__in_opt LPWSTR lpSubKey,
__reserved DWORD ulOptions,
__in REGSAM samDesired,
__out PHKEY phkResult
) {
LONG ret = Old_RegOpenKeyExW(hKey, lpSubKey, ulOptions, samDesired,
phkResult);
LOQ("puP", "Registry", hKey, "SubKey", lpSubKey, "Handle", phkResult);
return ret;
}
HOOKDEF(LONG, WINAPI, RegCreateKeyExA,
__in HKEY hKey,
__in LPCTSTR lpSubKey,
__reserved DWORD Reserved,
__in_opt LPTSTR lpClass,
__in DWORD dwOptions,
__in REGSAM samDesired,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__out PHKEY phkResult,
__out_opt LPDWORD lpdwDisposition
) {
LONG ret = Old_RegCreateKeyExA(hKey, lpSubKey, Reserved, lpClass,
dwOptions, samDesired, lpSecurityAttributes, phkResult,
lpdwDisposition);
LOQ("psslP", "Registry", hKey, "SubKey", lpSubKey, "Class", lpClass,
"Access", samDesired, "Handle", phkResult);
return ret;
}
HOOKDEF(LONG, WINAPI, RegCreateKeyExW,
__in HKEY hKey,
__in LPWSTR lpSubKey,
__reserved DWORD Reserved,
__in_opt LPWSTR lpClass,
__in DWORD dwOptions,
__in REGSAM samDesired,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__out PHKEY phkResult,
__out_opt LPDWORD lpdwDisposition
) {
LONG ret = Old_RegCreateKeyExW(hKey, lpSubKey, Reserved, lpClass,
dwOptions, samDesired, lpSecurityAttributes, phkResult,
lpdwDisposition);
LOQ("puulP", "Registry", hKey, "SubKey", lpSubKey, "Class", lpClass,
"Access", samDesired, "Handle", phkResult);
return ret;
}
HOOKDEF(LONG, WINAPI, RegDeleteKeyA,
__in HKEY hKey,
__in LPCTSTR lpSubKey
) {
LONG ret = Old_RegDeleteKeyA(hKey, lpSubKey);
LOQ("ps", "Handle", hKey, "SubKey", lpSubKey);
return ret;
}
HOOKDEF(LONG, WINAPI, RegDeleteKeyW,
__in HKEY hKey,
__in LPWSTR lpSubKey
) {
LONG ret = Old_RegDeleteKeyW(hKey, lpSubKey);
LOQ("pu", "Handle", hKey, "SubKey", lpSubKey);
return ret;
}
HOOKDEF(LONG, WINAPI, RegEnumKeyW,
__in HKEY hKey,
__in DWORD dwIndex,
__out LPWSTR lpName,
__in DWORD cchName
) {
LONG ret = Old_RegEnumKeyW(hKey, dwIndex, lpName, cchName);
LOQ("plu", "Handle", hKey, "Index", dwIndex, "Name", lpName);
return ret;
}
HOOKDEF(LONG, WINAPI, RegEnumKeyExA,
__in HKEY hKey,
__in DWORD dwIndex,
__out LPTSTR lpName,
__inout LPDWORD lpcName,
__reserved LPDWORD lpReserved,
__inout LPTSTR lpClass,
__inout_opt LPDWORD lpcClass,
__out_opt PFILETIME lpftLastWriteTime
) {
LONG ret = Old_RegEnumKeyExA(hKey, dwIndex, lpName, lpcName, lpReserved,
lpClass, lpcClass, lpftLastWriteTime);
LOQ("plss", "Handle", hKey, "Index", dwIndex, "Name", lpName,
"Class", lpClass);
return ret;
}
HOOKDEF(LONG, WINAPI, RegEnumKeyExW,
__in HKEY hKey,
__in DWORD dwIndex,
__out LPWSTR lpName,
__inout LPDWORD lpcName,
__reserved LPDWORD lpReserved,
__inout LPWSTR lpClass,
__inout_opt LPDWORD lpcClass,
__out_opt PFILETIME lpftLastWriteTime
) {
LONG ret = Old_RegEnumKeyExW(hKey, dwIndex, lpName, lpcName, lpReserved,
lpClass, lpcClass, lpftLastWriteTime);
LOQ("pluu", "Handle", hKey, "Index", dwIndex, "Name", lpName,
"Class", lpClass);
return ret;
}
HOOKDEF(LONG, WINAPI, RegEnumValueA,
__in HKEY hKey,
__in DWORD dwIndex,
__out LPTSTR lpValueName,
__inout LPDWORD lpcchValueName,
__reserved LPDWORD lpReserved,
__out_opt LPDWORD lpType,
__out_opt LPBYTE lpData,
__inout_opt LPDWORD lpcbData
) {
ENSURE_DWORD(lpType);
LONG ret = Old_RegEnumValueA(hKey, dwIndex, lpValueName, lpcchValueName,
lpReserved, lpType, lpData, lpcbData);
if(ret == ERROR_SUCCESS && lpType != NULL && lpData != NULL &&
lpcbData != NULL) {
LOQ("plsr", "Handle", hKey, "Index", dwIndex,
"ValueName", lpValueName, "Data", *lpType, *lpcbData, lpData);
}
else {
LOQ2("plsLL", "Handle", hKey, "Index", dwIndex,
"ValueName", lpValueName, "Type", lpType, "DataLength", lpcbData);
}
return ret;
}
HOOKDEF(LONG, WINAPI, RegEnumValueW,
__in HKEY hKey,
__in DWORD dwIndex,
__out LPWSTR lpValueName,
__inout LPDWORD lpcchValueName,
__reserved LPDWORD lpReserved,
__out_opt LPDWORD lpType,
__out_opt LPBYTE lpData,
__inout_opt LPDWORD lpcbData
) {
ENSURE_DWORD(lpType);
LONG ret = Old_RegEnumValueW(hKey, dwIndex, lpValueName, lpcchValueName,
lpReserved, lpType, lpData, lpcbData);
if(ret == ERROR_SUCCESS && lpType != NULL && lpData != NULL &&
lpcbData != NULL) {
LOQ("pluR", "Handle", hKey, "Index", dwIndex,
"ValueName", lpValueName, "Data", *lpType, *lpcbData, lpData);
}
else {
LOQ2("pluLL", "Handle", hKey, "Index", dwIndex,
"ValueName", lpValueName, "Type", lpType, "DataLength", lpcbData);
}
return ret;
}
HOOKDEF(LONG, WINAPI, RegSetValueExA,
__in HKEY hKey,
__in_opt LPCTSTR lpValueName,
__reserved DWORD Reserved,
__in DWORD dwType,
__in const BYTE *lpData,
__in DWORD cbData
) {
LONG ret = Old_RegSetValueExA(hKey, lpValueName, Reserved, dwType, lpData,
cbData);
if(ret == ERROR_SUCCESS) {
LOQ("pslr", "Handle", hKey, "ValueName", lpValueName, "Type", dwType,
"Buffer", dwType, cbData, lpData);
}
else {
LOQ2("psl", "Handle", hKey, "ValueName", lpValueName, "Type", dwType);
}
return ret;
}
HOOKDEF(LONG, WINAPI, RegSetValueExW,
__in HKEY hKey,
__in_opt LPWSTR lpValueName,
__reserved DWORD Reserved,
__in DWORD dwType,
__in const BYTE *lpData,
__in DWORD cbData
) {
LONG ret = Old_RegSetValueExW(hKey, lpValueName, Reserved, dwType, lpData,
cbData);
if(ret == ERROR_SUCCESS) {
LOQ("pulR", "Handle", hKey, "ValueName", lpValueName, "Type", dwType,
"Buffer", dwType, cbData, lpData);
}
else {
LOQ2("pul", "Handle", hKey, "ValueName", lpValueName, "Type", dwType);
}
return ret;
}
HOOKDEF(LONG, WINAPI, RegQueryValueExA,
__in HKEY hKey,
__in_opt LPCTSTR lpValueName,
__reserved LPDWORD lpReserved,
__out_opt LPDWORD lpType,
__out_opt LPBYTE lpData,
__inout_opt LPDWORD lpcbData
) {
ENSURE_DWORD(lpType);
LONG ret = Old_RegQueryValueExA(hKey, lpValueName, lpReserved, lpType,
lpData, lpcbData);
if(ret == ERROR_SUCCESS && lpType != NULL && lpData != NULL &&
lpcbData != NULL) {
LOQ("psr", "Handle", hKey, "ValueName", lpValueName,
"Data", *lpType, *lpcbData, lpData);
}
else {
LOQ2("psLL", "Handle", hKey, "ValueName", lpValueName,
"Type", lpType, "DataLength", lpcbData);
}
return ret;
}
HOOKDEF(LONG, WINAPI, RegQueryValueExW,
__in HKEY hKey,
__in_opt LPWSTR lpValueName,
__reserved LPDWORD lpReserved,
__out_opt LPDWORD lpType,
__out_opt LPBYTE lpData,
__inout_opt LPDWORD lpcbData
) {
ENSURE_DWORD(lpType);
LONG ret = Old_RegQueryValueExW(hKey, lpValueName, lpReserved, lpType,
lpData, lpcbData);
if(ret == ERROR_SUCCESS && lpType != NULL && lpData != NULL &&
lpcbData != NULL) {
LOQ("puR", "Handle", hKey, "ValueName", lpValueName,
"Data", *lpType, *lpcbData, lpData);
}
else {
LOQ2("puLL", "Handle", hKey, "ValueName", lpValueName,
"Type", lpType, "DataLength", lpcbData);
}
return ret;
}
HOOKDEF(LONG, WINAPI, RegDeleteValueA,
__in HKEY hKey,
__in_opt LPCTSTR lpValueName
) {
LONG ret = Old_RegDeleteValueA(hKey, lpValueName);
LOQ("ps", "Handle", hKey, "ValueName", lpValueName);
return ret;
}
HOOKDEF(LONG, WINAPI, RegDeleteValueW,
__in HKEY hKey,
__in_opt LPWSTR lpValueName
) {
LONG ret = Old_RegDeleteValueW(hKey, lpValueName);
LOQ("pu", "Handle", hKey, "ValueName", lpValueName);
return ret;
}
HOOKDEF(LONG, WINAPI, RegQueryInfoKeyA,
_In_ HKEY hKey,
_Out_opt_ LPTSTR lpClass,
_Inout_opt_ LPDWORD lpcClass,
_Reserved_ LPDWORD lpReserved,
_Out_opt_ LPDWORD lpcSubKeys,
_Out_opt_ LPDWORD lpcMaxSubKeyLen,
_Out_opt_ LPDWORD lpcMaxClassLen,
_Out_opt_ LPDWORD lpcValues,
_Out_opt_ LPDWORD lpcMaxValueNameLen,
_Out_opt_ LPDWORD lpcMaxValueLen,
_Out_opt_ LPDWORD lpcbSecurityDescriptor,
_Out_opt_ PFILETIME lpftLastWriteTime
) {
IS_SUCCESS_LONGREG();
LONG ret = Old_RegQueryInfoKeyA(hKey, lpClass, lpcClass, lpReserved,
lpcSubKeys, lpcMaxSubKeyLen, lpcMaxClassLen, lpcValues,
lpcMaxValueNameLen, lpcMaxValueLen, lpcbSecurityDescriptor,
lpftLastWriteTime);
LOQ("pS6L", "KeyHandle", hKey, "Class", lpcClass ? *lpcClass : 0, lpClass,
"SubKeyCount", lpcSubKeys, "MaxSubKeyLength", lpcMaxSubKeyLen,
"MaxClassLength", lpcMaxClassLen, "ValueCount", lpcValues,
"MaxValueNameLength", lpcMaxValueNameLen,
"MaxValueLength", lpcMaxValueLen);
return ret;
}
HOOKDEF(LONG, WINAPI, RegQueryInfoKeyW,
_In_ HKEY hKey,
_Out_opt_ LPWSTR lpClass,
_Inout_opt_ LPDWORD lpcClass,
_Reserved_ LPDWORD lpReserved,
_Out_opt_ LPDWORD lpcSubKeys,
_Out_opt_ LPDWORD lpcMaxSubKeyLen,
_Out_opt_ LPDWORD lpcMaxClassLen,
_Out_opt_ LPDWORD lpcValues,
_Out_opt_ LPDWORD lpcMaxValueNameLen,
_Out_opt_ LPDWORD lpcMaxValueLen,
_Out_opt_ LPDWORD lpcbSecurityDescriptor,
_Out_opt_ PFILETIME lpftLastWriteTime
) {
IS_SUCCESS_LONGREG();
LONG ret = Old_RegQueryInfoKeyW(hKey, lpClass, lpcClass, lpReserved,
lpcSubKeys, lpcMaxSubKeyLen, lpcMaxClassLen, lpcValues,
lpcMaxValueNameLen, lpcMaxValueLen, lpcbSecurityDescriptor,
lpftLastWriteTime);
LOQ("pU6L", "KeyHandle", hKey, "Class", lpcClass ? *lpcClass : 0, lpClass,
"SubKeyCount", lpcSubKeys, "MaxSubKeyLength", lpcMaxSubKeyLen,
"MaxClassLength", lpcMaxClassLen, "ValueCount", lpcValues,
"MaxValueNameLength", lpcMaxValueNameLen,
"MaxValueLength", lpcMaxValueLen);
return ret;
}
HOOKDEF(LONG, WINAPI, RegCloseKey,
__in HKEY hKey
) {
LONG ret = Old_RegCloseKey(hKey);
LOQ("p", "Handle", hKey);
return ret;
}