forked from cuckoosandbox/cuckoomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook_socket.c
336 lines (303 loc) · 8.69 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
/*
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 <winsock2.h>
#include "hooking.h"
#include "ntapi.h"
#include "log.h"
static IS_SUCCESS_INTM1();
HOOKDEF(int, WINAPI, WSAStartup,
_In_ WORD wVersionRequested,
_Out_ LPWSADATA lpWSAData
) {
IS_SUCCESS_ZERO();
int ret = Old_WSAStartup(wVersionRequested, lpWSAData);
LOQ("p", "VersionRequested", wVersionRequested);
return ret;
}
HOOKDEF(struct hostent *, WSAAPI, gethostbyname,
__in const char *name
) {
IS_SUCCESS_HANDLE();
struct hostent *ret = Old_gethostbyname(name);
LOQ("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("lll", "af", af, "type", type, "protocol", protocol);
return ret;
}
HOOKDEF(int, WSAAPI, connect,
__in SOCKET s,
__in const struct sockaddr *name,
__in int namelen
) {
int ret = Old_connect(s, name, namelen);
LOQ("p", "socket", s);
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("pb", "socket", s, "buffer", ret < 1 ? 0 : 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);
LOQ("pb", "socket", s, "buffer", ret < 1 ? 0 : ret, buf);
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("pb", "socket", s, "buffer", ret < 1 ? 0 : len, 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);
LOQ("pb", "socket", s, "buffer", ret < 1 ? 0 : ret, buf);
return ret;
}
HOOKDEF(SOCKET, WSAAPI, accept,
__in SOCKET s,
__out struct sockaddr *addr,
__inout int *addrlen
) {
SOCKET ret = Old_accept(s, addr, addrlen);
LOQ("pp", "socket", s, "ClientSocket", ret);
return ret;
}
HOOKDEF(int, WSAAPI, bind,
__in SOCKET s,
__in const struct sockaddr *name,
__in int namelen
) {
int ret = Old_bind(s, name, namelen);
if(ret == 0) {
LOQ("psl", "socket", s,
"ip", inet_ntoa(((struct sockaddr_in *) name)->sin_addr),
"port", htons(((struct sockaddr_in *) name)->sin_port));
}
else {
LOQ2("p", "socket", s);
}
return ret;
}
HOOKDEF(int, WSAAPI, listen,
__in SOCKET s,
__in int backlog
) {
int ret = Old_listen(s, backlog);
LOQ("p", "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("p", "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("pllb", "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("pl", "socket", s, "command", cmd);
return ret;
}
HOOKDEF(int, WSAAPI, closesocket,
__in SOCKET s
) {
int ret = Old_closesocket(s);
LOQ("p", "socket", s);
return ret;
}
HOOKDEF(int, WSAAPI, shutdown,
__in SOCKET s,
__in int how
) {
int ret = Old_shutdown(s, how);
LOQ("pl", "socket", s, "how", how);
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
) {
BOOL ret = Old_WSARecv(s, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd,
lpFlags, lpOverlapped, lpCompletionRoutine);
// TODO dump buffers
LOQ("p", "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
) {
BOOL ret = Old_WSARecvFrom(s, lpBuffers, dwBufferCount,
lpNumberOfBytesRecvd, lpFlags, lpFrom, lpFromlen, lpOverlapped,
lpCompletionRoutine);
// TODO dump buffer
LOQ("p", "socket", s);
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
) {
BOOL ret = Old_WSASend(s, lpBuffers, dwBufferCount, lpNumberOfBytesSent,
dwFlags, lpOverlapped, lpCompletionRoutine);
// TODO dump buffers
LOQ("p", "Socket", s);
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
) {
BOOL ret = Old_WSASendTo(s, lpBuffers, dwBufferCount, lpNumberOfBytesSent,
dwFlags, lpTo, iToLen, lpOverlapped, lpCompletionRoutine);
// TODO dump buffers
LOQ("p", "Socket", s);
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("lll", "af", af, "type", type, "protocol", protocol);
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("lll", "af", af, "type", type, "protocol", protocol);
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
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_ConnectEx(s, name, namelen, lpSendBuffer, dwSendDataLength,
lpdwBytesSent, lpOverlapped);
LOQ("pB", "socket", s, "SendBuffer", lpdwBytesSent, lpSendBuffer);
return ret;
}
HOOKDEF(BOOL, PASCAL, TransmitFile,
SOCKET hSocket,
HANDLE hFile,
DWORD nNumberOfBytesToWrite,
DWORD nNumberOfBytesPerSend,
LPOVERLAPPED lpOverlapped,
LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers,
DWORD dwFlags
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_TransmitFile(hSocket, hFile, nNumberOfBytesToWrite,
nNumberOfBytesPerSend, lpOverlapped, lpTransmitBuffers, dwFlags);
LOQ("ppll", "socket", hSocket, "FileHandle", hFile,
"NumberOfBytesToWrite", nNumberOfBytesToWrite,
"NumberOfBytesPerSend", nNumberOfBytesPerSend);
return ret;
}