forked from spender-sandbox/cuckoomon-modified
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook_socket.c
578 lines (517 loc) · 16.2 KB
/
hook_socket.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
/*
Cuckoo Sandbox - Automated Malware Analysis
Copyright (C) 2010-2016 Cuckoo Sandbox Developers, Optiv, Inc. ([email protected]), Brad Spengler
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 <mswsock.h>
#include "hooking.h"
#include "log.h"
#include "config.h"
#include "misc.h"
static PVOID alloc_combined_wsabuf(LPWSABUF buf, DWORD count, DWORD *outlen)
{
DWORD i;
DWORD size = 0;
PUCHAR retbuf;
for (i = 0; i < count; i++) {
size += buf->len;
}
retbuf = malloc(size);
if (retbuf == NULL) {
*outlen = 0;
return retbuf;
}
size = 0;
for (i = 0; i < count; i++) {
memcpy(&retbuf[size], buf->buf, buf->len);
size += buf->len;
}
*outlen = size;
return retbuf;
}
static BOOLEAN get_ip_port(const struct sockaddr *addr,
char *ip, int *port)
{
lasterror_t lasterror;
BOOLEAN ret = TRUE;
if (addr == NULL)
return FALSE;
get_lasterrors(&lasterror);
__try {
// TODO IPv6 support.
if (addr->sa_family == AF_INET) {
const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
addr_to_string(addr4->sin_addr, ip);
*port = our_htons(addr4->sin_port);
}
}
__except (EXCEPTION_EXECUTE_HANDLER) {
ret = FALSE;
}
set_lasterrors(&lasterror);
return ret;
}
HOOKDEF(int, WINAPI, WSAStartup,
_In_ WORD wVersionRequested,
_Out_ LPWSADATA lpWSAData
) {
int ret = Old_WSAStartup(wVersionRequested, lpWSAData);
LOQ_zero("network", "h", "VersionRequested", wVersionRequested);
return ret;
}
HOOKDEF(struct hostent *, WSAAPI, gethostbyname,
__in const char *name
) {
struct hostent *ret = Old_gethostbyname(name);
if (g_config.url_of_interest && g_config.suspend_logging)
g_config.suspend_logging = FALSE;
LOQ_nonnull("network", "s", "Name", name);
return ret;
}
HOOKDEF(SOCKET, WSAAPI, socket,
__in int af,
__in int type,
__in int protocol
) {
SOCKET ret = Old_socket(af, type, protocol);
LOQ_sock("network", "iiii", "af", af, "type", type, "protocol", protocol, "socket", ret);
return ret;
}
HOOKDEF(int, WSAAPI, connect,
__in SOCKET s,
__in const struct sockaddr *name,
__in int namelen
) {
int ret = Old_connect(s, name, namelen);
char ip[16]; int port = 0;
get_ip_port(name, ip, &port);
LOQ_sockerr("network", "isi", "socket", s, "ip", ip, "port", port);
return ret;
}
HOOKDEF(int, WSAAPI, send,
__in SOCKET s,
__in const char *buf,
__in int len,
__in int flags
) {
int ret = Old_send(s, buf, len, flags);
LOQ_sockerr("network", "ib", "socket", s, "buffer", ret < 1 ? len : ret, buf);
return ret;
}
HOOKDEF(int, WSAAPI, sendto,
__in SOCKET s,
__in const char *buf,
__in int len,
__in int flags,
__in const struct sockaddr *to,
__in int tolen
) {
int ret = Old_sendto(s, buf, len, flags, to, tolen);
char ip[16] = { 0 };
int port = 0;
get_ip_port(to, ip, &port);
LOQ_sockerr("network", "ibsi", "socket", s, "buffer", ret < 1 ? len : ret, buf,
"ip", ip, "port", port);
return ret;
}
HOOKDEF(int, WSAAPI, recv,
__in SOCKET s,
__out char *buf,
__in int len,
__in int flags
) {
int ret = Old_recv(s, buf, len, flags);
LOQ_sockerr("network", "ib", "socket", s, "buffer", ret < 1 ? 0 : ret, buf);
return ret;
}
HOOKDEF(int, WSAAPI, recvfrom,
__in SOCKET s,
__out char *buf,
__in int len,
__in int flags,
__out struct sockaddr *from,
__inout_opt int *fromlen
) {
int ret = Old_recvfrom(s, buf, len, flags, from, fromlen);
char ip[16] = { 0 };
int port = 0;
if(ret > 0) {
get_ip_port(from, ip, &port);
}
LOQ_sockerr("network", "ibsi", "socket", s, "buffer", ret < 1 ? 0 : ret, buf,
"ip", ip, "port", port);
return ret;
}
typedef int (WSAAPI * __getpeername)(
__in SOCKET s,
__out_bcount_part(*namelen, *namelen) struct sockaddr FAR * name,
__inout int FAR * namelen
);
static __getpeername _getpeername;
int WSAAPI our_getpeername(__in SOCKET s,
__out_bcount_part(*namelen, *namelen) struct sockaddr FAR * name,
__inout int FAR * namelen
) {
lasterror_t lasterror;
int ret = 1;
get_lasterrors(&lasterror);
if (s != INVALID_SOCKET) {
if (!_getpeername) {
_getpeername = (__getpeername)GetProcAddress(LoadLibraryA("ws2_32"), "getpeername");
}
ret = _getpeername(s, name, namelen);
}
set_lasterrors(&lasterror);
return ret;
}
HOOKDEF(SOCKET, WSAAPI, accept,
__in SOCKET s,
__out struct sockaddr *addr,
__inout int *addrlen
) {
SOCKET ret = Old_accept(s, addr, addrlen);
char ip_s[16] = { 0 };
char ip_c[16] = { 0 };
int port_s = 0, port_c = 0;
struct sockaddr addr_c; int addr_c_len = sizeof(addr_c);
get_ip_port(addr, ip_s, &port_s);
if(our_getpeername(ret, &addr_c, &addr_c_len) == 0) {
get_ip_port(&addr_c, ip_c, &port_c);
}
LOQ_sockerr("network", "iisisi", "socket", s, "ClientSocket", ret,
"ip_accept", ip_s, "port_accept", port_s,
"ip_client", ip_c, "port_client", port_c);
return ret;
}
HOOKDEF(int, WSAAPI, bind,
__in SOCKET s,
__in const struct sockaddr *name,
__in int namelen
) {
int ret = Old_bind(s, name, namelen);
char ip[16] = { 0 };
int port = 0;
get_ip_port(name, ip, &port);
LOQ_sockerr("network", "isi", "socket", s, "ip", ip, "port", port);
return ret;
}
HOOKDEF(int, WSAAPI, listen,
__in SOCKET s,
__in int backlog
) {
int ret = Old_listen(s, backlog);
LOQ_sockerr("network", "i", "socket", s);
return ret;
}
HOOKDEF(int, WSAAPI, select,
__in SOCKET s,
__inout fd_set *readfds,
__inout fd_set *writefds,
__inout fd_set *exceptfds,
__in const struct timeval *timeout
) {
int ret = Old_select(s, readfds, writefds, exceptfds, timeout);
LOQ_sockerr("network", "i", "socket", s);
return ret;
}
HOOKDEF(int, WSAAPI, setsockopt,
__in SOCKET s,
__in int level,
__in int optname,
__in const char *optval,
__in int optlen
) {
int ret = Old_setsockopt(s, level, optname, optval, optlen);
LOQ_sockerr("network", "ippb", "socket", s, "level", level, "optname", optname,
"optval", optlen, optval);
return ret;
}
HOOKDEF(int, WSAAPI, ioctlsocket,
__in SOCKET s,
__in long cmd,
__inout u_long *argp
) {
int ret = Old_ioctlsocket(s, cmd, argp);
LOQ_sockerr("network", "ip", "socket", s, "command", cmd);
return ret;
}
HOOKDEF(int, WSAAPI, closesocket,
__in SOCKET s
) {
int ret = Old_closesocket(s);
LOQ_sockerr("network", "i", "socket", s);
return ret;
}
HOOKDEF(int, WSAAPI, shutdown,
__in SOCKET s,
__in int how
) {
int ret = Old_shutdown(s, how);
LOQ_sockerr("network", "ii", "socket", s, "how", how);
return ret;
}
HOOKDEF(SOCKET, WSAAPI, WSAAccept,
__in SOCKET s,
__out struct sockaddr *addr,
__inout LPINT addrlen,
__in LPCONDITIONPROC lpfnCondition,
__in DWORD_PTR dwCallbackData
) {
SOCKET ret = Old_WSAAccept(s, addr, addrlen, lpfnCondition,
dwCallbackData);
char ip_s[16] = {0};
char ip_c[16] = {0};
int port_s = 0, port_c = 0;
struct sockaddr addr_c; int addr_c_len = sizeof(addr_c);
get_ip_port(addr, ip_s, &port_s);
if(our_getpeername(ret, &addr_c, &addr_c_len) == 0) {
get_ip_port(&addr_c, ip_c, &port_c);
}
LOQ_sockerr("network", "iisisi", "socket", s, "ClientSocket", ret,
"ip_accept", ip_s, "port_accept", port_s,
"ip_client", ip_c, "port_client", port_c);
return ret;
}
HOOKDEF(int, WSAAPI, WSARecv,
__in SOCKET s,
__inout LPWSABUF lpBuffers,
__in DWORD dwBufferCount,
__out LPDWORD lpNumberOfBytesRecvd,
__inout LPDWORD lpFlags,
__in LPWSAOVERLAPPED lpOverlapped,
__in LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
) {
int ret = Old_WSARecv(s, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd,
lpFlags, lpOverlapped, lpCompletionRoutine);
if (lpOverlapped == NULL && lpCompletionRoutine == NULL) {
DWORD outlen;
PVOID buf = alloc_combined_wsabuf(lpBuffers, dwBufferCount, &outlen);
LOQ_sockerr("network", "iBI", "socket", s, "Buffer", lpNumberOfBytesRecvd, buf, "NumberOfBytesReceived", lpNumberOfBytesRecvd);
if (buf)
free(buf);
}
else {
// TODO: handle completion routine case
LOQ_sockerr("network", "i", "socket", s);
}
return ret;
}
HOOKDEF(int, WSAAPI, WSARecvFrom,
__in SOCKET s,
__inout LPWSABUF lpBuffers,
__in DWORD dwBufferCount,
__out LPDWORD lpNumberOfBytesRecvd,
__inout LPDWORD lpFlags,
__out struct sockaddr *lpFrom,
__inout LPINT lpFromlen,
__in LPWSAOVERLAPPED lpOverlapped,
__in LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
) {
int ret = Old_WSARecvFrom(s, lpBuffers, dwBufferCount,
lpNumberOfBytesRecvd, lpFlags, lpFrom, lpFromlen, lpOverlapped,
lpCompletionRoutine);
char ip[16] = { 0 };
int port = 0;
get_ip_port(lpFrom, ip, &port);
if (lpOverlapped == NULL && lpCompletionRoutine == NULL) {
DWORD outlen;
PVOID buf = alloc_combined_wsabuf(lpBuffers, dwBufferCount, &outlen);
LOQ_sockerr("network", "isiBI", "socket", s, "ip", ip, "port", port, "Buffer", lpNumberOfBytesRecvd, buf, "NumberOfBytesReceived", lpNumberOfBytesRecvd);
if (buf)
free(buf);
}
else {
// TODO: handle completion routine case
LOQ_sockerr("network", "isi", "socket", s, "ip", ip, "port", port);
}
return ret;
}
HOOKDEF(int, WSAAPI, WSASend,
__in SOCKET s,
__in LPWSABUF lpBuffers,
__in DWORD dwBufferCount,
__out LPDWORD lpNumberOfBytesSent,
__in DWORD dwFlags,
__in LPWSAOVERLAPPED lpOverlapped,
__in LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
) {
// TODO: handle completion routine case
int ret = Old_WSASend(s, lpBuffers, dwBufferCount, lpNumberOfBytesSent,
dwFlags, lpOverlapped, lpCompletionRoutine);
DWORD outlen;
PVOID buf = alloc_combined_wsabuf(lpBuffers, dwBufferCount, &outlen);
LOQ_sockerr("network", "ib", "Socket", s, "Buffer", outlen, buf);
if (buf)
free(buf);
return ret;
}
HOOKDEF(int, WSAAPI, WSASendTo,
__in SOCKET s,
__in LPWSABUF lpBuffers,
__in DWORD dwBufferCount,
__out LPDWORD lpNumberOfBytesSent,
__in DWORD dwFlags,
__in const struct sockaddr *lpTo,
__in int iToLen,
__in LPWSAOVERLAPPED lpOverlapped,
__in LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
) {
// TODO: handle completion routine case
char ip[16] = { 0 };
int port = 0;
BOOL ret;
DWORD outlen;
PVOID buf;
get_ip_port(lpTo, ip, &port);
ret = Old_WSASendTo(s, lpBuffers, dwBufferCount, lpNumberOfBytesSent,
dwFlags, lpTo, iToLen, lpOverlapped, lpCompletionRoutine);
buf = alloc_combined_wsabuf(lpBuffers, dwBufferCount, &outlen);
LOQ_sockerr("network", "isib", "socket", s, "ip", ip, "port", port, "Buffer", outlen, buf);
if (buf)
free(buf);
return ret;
}
HOOKDEF(SOCKET, WSAAPI, WSASocketA,
__in int af,
__in int type,
__in int protocol,
__in LPWSAPROTOCOL_INFO lpProtocolInfo,
__in GROUP g,
__in DWORD dwFlags
) {
SOCKET ret = Old_WSASocketA(af, type, protocol, lpProtocolInfo, g, dwFlags);
LOQ_sock("network", "iiii", "af", af, "type", type, "protocol", protocol, "socket", ret);
return ret;
}
HOOKDEF(SOCKET, WSAAPI, WSASocketW,
__in int af,
__in int type,
__in int protocol,
__in LPWSAPROTOCOL_INFO lpProtocolInfo,
__in GROUP g,
__in DWORD dwFlags
) {
SOCKET ret = Old_WSASocketW(af, type, protocol, lpProtocolInfo, g, dwFlags);
LOQ_sock("network", "iiii", "af", af, "type", type, "protocol", protocol, "socket", ret);
return ret;
}
HOOKDEF(int, WSAAPI, WSAConnect,
__in SOCKET s,
__in const struct sockaddr *name,
__in int namelen,
__in LPWSABUF lpCallerData,
__out LPWSABUF lpCalleeData,
__in LPQOS lpSQOS,
__in LPQOS lpGQOS
) {
int ret = Old_WSAConnect(s, name, namelen, lpCallerData, lpCalleeData, lpSQOS, lpGQOS);
char ip[16] = { 0 };
int port = 0;
get_ip_port(name, ip, &port);
LOQ_sockerr("network", "isi", "socket", s, "ip", ip, "port", port);
return ret;
}
HOOKDEF(BOOL, PASCAL, ConnectEx,
_In_ SOCKET s,
_In_ const struct sockaddr *name,
_In_ int namelen,
_In_opt_ PVOID lpSendBuffer,
_In_ DWORD dwSendDataLength,
_Out_ LPDWORD lpdwBytesSent,
_In_ LPOVERLAPPED lpOverlapped
) {
BOOL ret = Old_ConnectEx(s, name, namelen, lpSendBuffer, dwSendDataLength,
lpdwBytesSent, lpOverlapped);
char ip[16] = { 0 };
int port = 0;
get_ip_port(name, ip, &port);
LOQ_bool("network", "iBsi", "socket", s, "SendBuffer", lpdwBytesSent, lpSendBuffer,
"ip", ip, "port", port);
return ret;
}
HOOKDEF(BOOL, PASCAL, TransmitFile,
SOCKET hSocket,
HANDLE hFile,
DWORD nNumberOfBytesToWrite,
DWORD nNumberOfBytesPerSend,
LPOVERLAPPED lpOverlapped,
LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers,
DWORD dwFlags
) {
BOOL ret = Old_TransmitFile(hSocket, hFile, nNumberOfBytesToWrite,
nNumberOfBytesPerSend, lpOverlapped, lpTransmitBuffers, dwFlags);
LOQ_bool("network", "ipii", "socket", hSocket, "FileHandle", hFile,
"NumberOfBytesToWrite", nNumberOfBytesToWrite,
"NumberOfBytesPerSend", nNumberOfBytesPerSend);
return ret;
}
HOOKDEF(int, WSAAPI, gethostname,
_Out_ char *name,
_In_ int namelen
) {
int ret = Old_gethostname(name, namelen);
LOQ_sockerr("network", "s", "HostName", ret != SOCKET_ERROR && namelen ? name : NULL);
return ret;
}
HOOKDEF(BOOL, PASCAL, WSAConnectByList,
_In_ SOCKET s,
_In_ PSOCKET_ADDRESS_LIST SocketAddressList,
_Inout_ LPDWORD LocalAddressLength,
_Out_ LPSOCKADDR LocalAddress,
_Inout_ LPDWORD RemoteAddressLength,
_Out_ LPSOCKADDR RemoteAddress,
_In_ PVOID timeout,
_In_ LPWSAOVERLAPPED Reserved
) {
char ip[16] = { 0 };
int port = 0;
BOOL ret = Old_WSAConnectByList(s, SocketAddressList, LocalAddressLength, LocalAddress, RemoteAddressLength, RemoteAddress, timeout, Reserved);
if (SocketAddressList->iAddressCount > 0)
get_ip_port(SocketAddressList->Address[0].lpSockaddr, ip, &port);
LOQ_bool("network", "isi", "NumberOfAddresses", SocketAddressList->iAddressCount, "ip", ip, "port", port);
return ret;
}
HOOKDEF(BOOL, PASCAL, WSAConnectByNameW,
_In_ SOCKET s,
_In_ LPWSTR nodename,
_In_ LPWSTR servicename,
_Inout_ LPDWORD LocalAddressLength,
_Out_ LPSOCKADDR LocalAddress,
_Inout_ LPDWORD RemoteAddressLength,
_Out_ LPSOCKADDR RemoteAddress,
_In_ PVOID timeout,
LPWSAOVERLAPPED Reserved
) {
BOOL ret = Old_WSAConnectByNameW(s, nodename, servicename, LocalAddressLength, LocalAddress, RemoteAddressLength, RemoteAddress, timeout, Reserved);
LOQ_bool("network", "uu", "NodeName", nodename, "ServiceName", servicename);
return ret;
}
HOOKDEF(int, WSAAPI, WSASendMsg,
_In_ SOCKET s,
_In_ LPWSAMSG lpMsg,
_In_ DWORD dwFlags,
_Out_ LPDWORD lpNumberOfBytesSent,
_In_ LPWSAOVERLAPPED lpOverlapped,
_In_ LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
) {
char ip[16] = { 0 };
int port = 0;
PVOID buf;
DWORD outlen = 0;
int ret = Old_WSASendMsg(s, lpMsg, dwFlags, lpNumberOfBytesSent, lpOverlapped, lpCompletionRoutine);
buf = alloc_combined_wsabuf(lpMsg->lpBuffers, lpMsg->dwBufferCount, &outlen);
get_ip_port(lpMsg->name, ip, &port);
LOQ_sockerr("network", "Bsi", "MsgBuffer", lpNumberOfBytesSent, buf, "ip", ip, "port", port);
return ret;
}