forked from cuckoosandbox/cuckoomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook_network.c
328 lines (293 loc) · 9.92 KB
/
hook_network.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
/*
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 <windns.h>
#include <wininet.h>
#include "hooking.h"
#include "ntapi.h"
#include "log.h"
#include "pipe.h"
static IS_SUCCESS_HINTERNET();
HOOKDEF(HRESULT, WINAPI, URLDownloadToFileW,
LPUNKNOWN pCaller,
LPWSTR szURL,
LPWSTR szFileName,
DWORD dwReserved,
LPVOID lpfnCB
) {
IS_SUCCESS_HRESULT();
HRESULT ret = Old_URLDownloadToFileW(pCaller, szURL, szFileName,
dwReserved, lpfnCB);
LOQ("uu", "URL", szURL, "FileName", szFileName);
if(ret == S_OK) {
pipe("FILE_NEW:%S", szFileName);
}
return ret;
}
HOOKDEF(HINTERNET, WINAPI, InternetOpenA,
_In_ LPCTSTR lpszAgent,
_In_ DWORD dwAccessType,
_In_ LPCTSTR lpszProxyName,
_In_ LPCTSTR lpszProxyBypass,
_In_ DWORD dwFlags
) {
HINTERNET ret = Old_InternetOpenA(lpszAgent, dwAccessType, lpszProxyName,
lpszProxyBypass, dwFlags);
LOQ("spssp", "Agent", lpszAgent, "AccessType", dwAccessType,
"ProxyName", lpszProxyName, "ProxyBypass", lpszProxyBypass,
"Flags", dwFlags);
return ret;
}
HOOKDEF(HINTERNET, WINAPI, InternetOpenW,
_In_ LPWSTR lpszAgent,
_In_ DWORD dwAccessType,
_In_ LPWSTR lpszProxyName,
_In_ LPWSTR lpszProxyBypass,
_In_ DWORD dwFlags
) {
HINTERNET ret = Old_InternetOpenW(lpszAgent, dwAccessType, lpszProxyName,
lpszProxyBypass, dwFlags);
LOQ("upuup", "Agent", lpszAgent, "AccessType", dwAccessType,
"ProxyName", lpszProxyName, "ProxyBypass", lpszProxyBypass,
"Flags", dwFlags);
return ret;
}
HOOKDEF(HINTERNET, WINAPI, InternetConnectA,
_In_ HINTERNET hInternet,
_In_ LPCTSTR lpszServerName,
_In_ INTERNET_PORT nServerPort,
_In_ LPCTSTR lpszUsername,
_In_ LPCTSTR lpszPassword,
_In_ DWORD dwService,
_In_ DWORD dwFlags,
_In_ DWORD_PTR dwContext
) {
HINTERNET ret = Old_InternetConnectA(hInternet, lpszServerName,
nServerPort, lpszUsername, lpszPassword, dwService, dwFlags,
dwContext);
LOQ("pslsslp", "InternetHandle", hInternet, "ServerName", lpszServerName,
"ServerPort", nServerPort, "Username", lpszUsername,
"Password", lpszPassword, "Service", dwService, "Flags", dwFlags);
return ret;
}
HOOKDEF(HINTERNET, WINAPI, InternetConnectW,
_In_ HINTERNET hInternet,
_In_ LPWSTR lpszServerName,
_In_ INTERNET_PORT nServerPort,
_In_ LPWSTR lpszUsername,
_In_ LPWSTR lpszPassword,
_In_ DWORD dwService,
_In_ DWORD dwFlags,
_In_ DWORD_PTR dwContext
) {
HINTERNET ret = Old_InternetConnectW(hInternet, lpszServerName,
nServerPort, lpszUsername, lpszPassword, dwService, dwFlags,
dwContext);
LOQ("puluulp", "InternetHandle", hInternet, "ServerName", lpszServerName,
"ServerPort", nServerPort, "Username", lpszUsername,
"Password", lpszPassword, "Service", dwService, "Flags", dwFlags);
return ret;
}
HOOKDEF(HINTERNET, WINAPI, InternetOpenUrlA,
__in HINTERNET hInternet,
__in LPCTSTR lpszUrl,
__in LPCTSTR lpszHeaders,
__in DWORD dwHeadersLength,
__in DWORD dwFlags,
__in DWORD_PTR dwContext
) {
HINTERNET ret = Old_InternetOpenUrlA(hInternet, lpszUrl, lpszHeaders,
dwHeadersLength, dwFlags, dwContext);
if(dwHeadersLength == (DWORD) -1) dwHeadersLength = strlen(lpszHeaders);
LOQ("psSp", "ConnectionHandle", hInternet, "URL", lpszUrl,
"Headers", dwHeadersLength, lpszHeaders, "Flags", dwFlags);
return ret;
}
HOOKDEF(HINTERNET, WINAPI, InternetOpenUrlW,
__in HINTERNET hInternet,
__in LPWSTR lpszUrl,
__in LPWSTR lpszHeaders,
__in DWORD dwHeadersLength,
__in DWORD dwFlags,
__in DWORD_PTR dwContext
) {
HINTERNET ret = Old_InternetOpenUrlW(hInternet, lpszUrl, lpszHeaders,
dwHeadersLength, dwFlags, dwContext);
LOQ("puUp", "ConnectionHandle", hInternet, "URL", lpszUrl,
"Headers", dwHeadersLength, lpszHeaders, "Flags", dwFlags);
return ret;
}
HOOKDEF(HINTERNET, WINAPI, HttpOpenRequestA,
__in HINTERNET hConnect,
__in LPCTSTR lpszVerb,
__in LPCTSTR lpszObjectName,
__in LPCTSTR lpszVersion,
__in LPCTSTR lpszReferer,
__in LPCTSTR *lplpszAcceptTypes,
__in DWORD dwFlags,
__in DWORD_PTR dwContext
) {
HINTERNET ret = Old_HttpOpenRequestA(hConnect, lpszVerb, lpszObjectName,
lpszVersion, lpszReferer, lplpszAcceptTypes, dwFlags, dwContext);
LOQ("psl", "InternetHandle", hConnect, "Path", lpszObjectName,
"Flags", dwFlags);
return ret;
}
HOOKDEF(HINTERNET, WINAPI, HttpOpenRequestW,
__in HINTERNET hConnect,
__in LPWSTR lpszVerb,
__in LPWSTR lpszObjectName,
__in LPWSTR lpszVersion,
__in LPWSTR lpszReferer,
__in LPWSTR *lplpszAcceptTypes,
__in DWORD dwFlags,
__in DWORD_PTR dwContext
) {
HINTERNET ret = Old_HttpOpenRequestW(hConnect, lpszVerb, lpszObjectName,
lpszVersion, lpszReferer, lplpszAcceptTypes, dwFlags, dwContext);
LOQ("pul", "InternetHandle", hConnect, "Path", lpszObjectName,
"Flags", dwFlags);
return ret;
}
HOOKDEF(BOOL, WINAPI, HttpSendRequestA,
__in HINTERNET hRequest,
__in LPCTSTR lpszHeaders,
__in DWORD dwHeadersLength,
__in LPVOID lpOptional,
__in DWORD dwOptionalLength
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength,
lpOptional, dwOptionalLength);
if(dwHeadersLength == (DWORD) -1) dwHeadersLength = strlen(lpszHeaders);
LOQ("pSb", "RequestHandle", hRequest,
"Headers", dwHeadersLength, lpszHeaders,
"PostData", dwOptionalLength, lpOptional);
return ret;
}
HOOKDEF(BOOL, WINAPI, HttpSendRequestW,
__in HINTERNET hRequest,
__in LPWSTR lpszHeaders,
__in DWORD dwHeadersLength,
__in LPVOID lpOptional,
__in DWORD dwOptionalLength
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength,
lpOptional, dwOptionalLength);
LOQ("pUb", "RequestHandle", hRequest,
"Headers", dwHeadersLength, lpszHeaders,
"PostData", dwOptionalLength, lpOptional);
return ret;
}
HOOKDEF(BOOL, WINAPI, InternetReadFile,
_In_ HINTERNET hFile,
_Out_ LPVOID lpBuffer,
_In_ DWORD dwNumberOfBytesToRead,
_Out_ LPDWORD lpdwNumberOfBytesRead
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_InternetReadFile(hFile, lpBuffer, dwNumberOfBytesToRead,
lpdwNumberOfBytesRead);
LOQ("pB", "InternetHandle", hFile,
"Buffer", lpdwNumberOfBytesRead, lpBuffer);
return ret;
}
HOOKDEF(BOOL, WINAPI, InternetWriteFile,
_In_ HINTERNET hFile,
_In_ LPCVOID lpBuffer,
_In_ DWORD dwNumberOfBytesToWrite,
_Out_ LPDWORD lpdwNumberOfBytesWritten
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_InternetWriteFile(hFile, lpBuffer, dwNumberOfBytesToWrite,
lpdwNumberOfBytesWritten);
LOQ("pB", "InternetHandle", hFile,
"Buffer", lpdwNumberOfBytesWritten, lpBuffer);
return ret;
}
HOOKDEF(BOOL, WINAPI, InternetCloseHandle,
_In_ HINTERNET hInternet
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_InternetCloseHandle(hInternet);
LOQ("p", "InternetHandle", hInternet);
return ret;
}
HOOKDEF(DNS_STATUS, WINAPI, DnsQuery_A,
__in PCSTR lpstrName,
__in WORD wType,
__in DWORD Options,
__inout_opt PVOID pExtra,
__out_opt PDNS_RECORD *ppQueryResultsSet,
__out_opt PVOID *pReserved
) {
IS_SUCCESS_ZERO();
DNS_STATUS ret = Old_DnsQuery_A(lpstrName, wType, Options, pExtra,
ppQueryResultsSet, pReserved);
LOQ("sil", "Name", lpstrName, "Type", wType, "Options", Options);
return ret;
}
HOOKDEF(DNS_STATUS, WINAPI, DnsQuery_UTF8,
__in LPBYTE lpstrName,
__in WORD wType,
__in DWORD Options,
__inout_opt PVOID pExtra,
__out_opt PDNS_RECORD *ppQueryResultsSet,
__out_opt PVOID *pReserved
) {
IS_SUCCESS_ZERO();
DNS_STATUS ret = Old_DnsQuery_UTF8(lpstrName, wType, Options, pExtra,
ppQueryResultsSet, pReserved);
LOQ("sil", "Name", lpstrName, "Type", wType, "Options", Options);
return ret;
}
HOOKDEF(DNS_STATUS, WINAPI, DnsQuery_W,
__in PWSTR lpstrName,
__in WORD wType,
__in DWORD Options,
__inout_opt PVOID pExtra,
__out_opt PDNS_RECORD *ppQueryResultsSet,
__out_opt PVOID *pReserved
) {
IS_SUCCESS_ZERO();
DNS_STATUS ret = Old_DnsQuery_W(lpstrName, wType, Options, pExtra,
ppQueryResultsSet, pReserved);
LOQ("uil", "Name", lpstrName, "Type", wType, "Options", Options);
return ret;
}
HOOKDEF(int, WSAAPI, getaddrinfo,
_In_opt_ PCSTR pNodeName,
_In_opt_ PCSTR pServiceName,
_In_opt_ const ADDRINFOA *pHints,
_Out_ PADDRINFOA *ppResult
) {
IS_SUCCESS_ZERO();
BOOL ret = Old_getaddrinfo(pNodeName, pServiceName, pHints, ppResult);
LOQ("ss", "NodeName", pNodeName, "ServiceName", pServiceName);
return ret;
}
HOOKDEF(int, WSAAPI, GetAddrInfoW,
_In_opt_ PCWSTR pNodeName,
_In_opt_ PCWSTR pServiceName,
_In_opt_ const ADDRINFOW *pHints,
_Out_ PADDRINFOW *ppResult
) {
IS_SUCCESS_ZERO();
BOOL ret = Old_GetAddrInfoW(pNodeName, pServiceName, pHints, ppResult);
LOQ("uu", "NodeName", pNodeName, "ServiceName", pServiceName);
return ret;
}