-
Notifications
You must be signed in to change notification settings - Fork 5
/
mydll.cpp
361 lines (268 loc) · 8.2 KB
/
mydll.cpp
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
// mydll.cpp : Defines the entry point for the DLL application.
//
#include <windows.h>
#include <string>
#define Append(text) AppendLog(text, strlen(text))
#define LogFile "C:\\tensaix2j\\cpp\\logs\\log.txt"
HINSTANCE g_hInst=0;
HANDLE hLogFile=0;
HANDLE OpenLog(char *Filename);
BOOL CloseLog(HANDLE h=hLogFile);
DWORD AppendLog(char *str, DWORD uSize, HANDLE h=hLogFile);
int HookWinsockProcs();
BOOL IsLogging=false;
PROC WINAPI HookImportedFunction(
HMODULE hFromModule, // Module to intercept calls from
PSTR pszFunctionModule, // Module to intercept calls to
PSTR pszFunctionName, // Function to intercept calls to
PROC pfnNewProc // New function (replaces old function)
);
extern BOOL IsLogging;
extern HANDLE hLogFile;
BOOL Hooked=false;
typedef int(WINAPI *SENDPROC)(SOCKET, const char FAR*, int, int); //SENDPROC
typedef int(WINAPI *RECVPROC)(SOCKET, char FAR *, int, int); //RECVPROC
typedef int(WINAPI *BINDPROC)(SOCKET, const struct sockaddr FAR *, int); //BINDPROC
typedef int(WINAPI *CLOSESOCKETPROC)(SOCKET); //CLOSESOCKETPROC
typedef int(WINAPI *CONNECTPROC)(SOCKET, const struct sockaddr FAR *, int); //CONNECTPROC
typedef int(WINAPI *LISTENPROC)(SOCKET, int); //LISTENPROC
typedef SOCKET(WINAPI *ACCEPTPROC)(SOCKET, struct sockaddr FAR *, int FAR *); //ACCEPTPROC
typedef SOCKET(WINAPI *SOCKETPROC)(int, int, int); //SOCKETPROC
typedef int(WINAPI *WSAGetLastErrorProc)(void); //WSAGetLastErrorProc
SENDPROC Osend;
RECVPROC Orecv;
ACCEPTPROC Oaccept;
BINDPROC Obind;
CLOSESOCKETPROC Oclosesocket;
CONNECTPROC Oconnect;
LISTENPROC Olisten;
SOCKETPROC Osocket;
WSAGetLastErrorProc OWSAGetLastError;
int WINAPI _send
(SOCKET s, const char FAR *buf, int len, int flags );
int WINAPI _recv
(SOCKET s, char FAR *buf, int len, int flags);
int WINAPI _listen
(SOCKET s, int backlog );
SOCKET WINAPI _accept
(SOCKET s, struct sockaddr FAR *addr, int FAR *addrlen);
int WINAPI _bind
(SOCKET s, const struct sockaddr FAR *name, int namelen);
int WINAPI _closesocket
(SOCKET s);
int WINAPI _connect
(SOCKET s, const struct sockaddr FAR *name, int namelen);
SOCKET WINAPI _socket
(int af, int type, int protocol);
//============================
// Hook functions:
//
//#define WinsockDll "WSock32.dll"
#define WinsockDll "WS2_32.dll"
#define Hook(dllfuncname, proc) HookImportedFunction(GetModuleHandle(0), WinsockDll, dllfuncname, (PROC)proc)
#define IfCantHook(returnV, cast, dllfuncname, proc) if( (returnV = (cast)Hook(dllfuncname, (PROC)proc))==0 )
#define CheckForHook if(!Hooked) return
#define SendCheck Append("error"); else Append("OK")
static char logbuffer[1024];
//-------
BOOL APIENTRY DllMain( HINSTANCE hInstance,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_hInst = hInstance;
hLogFile = OpenLog(LogFile);
Append("\r\n************************\r\nDLL_PROCESS_ATTACH \r\n");
HookWinsockProcs();
return true;
break;
case DLL_THREAD_ATTACH:
Append("DLL_THREAD_ATTACH\r\n");
break;
case DLL_THREAD_DETACH:
Append("DLL_THREAD_DETACH\r\n");
break;
case DLL_PROCESS_DETACH:
Append("DLL_PROCESS_DETACH\r\n********************\r\n\r\n");
CloseLog();
return true;
break;
}
return TRUE;
}
//---------------------------
HANDLE OpenLog(char *Filename)
{
HANDLE hLogFile;
hLogFile = CreateFile( Filename, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_ALWAYS,0,0);
if(hLogFile!=INVALID_HANDLE_VALUE)
IsLogging = true;
return hLogFile;
}
BOOL CloseLog(HANDLE h)
{
IsLogging = false;
return CloseHandle(h);
}
//returns written bytes
DWORD AppendLog(char *str, DWORD uSize, HANDLE h)
{
DWORD written;
if(!IsLogging) return 0;
SetFilePointer( h, 0, 0, FILE_END );
WriteFile(h, str, uSize, &written, 0);
FlushFileBuffers(h);
return written;
}
//--------
int HookWinsockProcs()
{
Hooked=false;
Append("Hooking...");
Append("\r\n\tsocket() -> ");
IfCantHook(Osocket, SOCKETPROC, "socket", _socket)
SendCheck;
Append("\r\n\tsend() -> ");
IfCantHook(Osend, SENDPROC, "send", _send)
SendCheck;
Append("\r\n\trecv() ->");
IfCantHook(Orecv, RECVPROC, "recv", _recv)
SendCheck;
Append("\r\n\tlisten() -> ");
IfCantHook(Olisten, LISTENPROC, "listen", _listen)
SendCheck;
Append("\r\n\taccept() -> ");
IfCantHook(Oaccept, ACCEPTPROC, "accept", _accept)
SendCheck;
Append("\r\n\tbind() -> ");
IfCantHook(Obind, BINDPROC, "bind", _bind)
SendCheck;
Append("\r\n\tclosesocket() -> ");
IfCantHook(Oclosesocket, CLOSESOCKETPROC, "closesocket", _closesocket)
SendCheck;
Append("\r\n\tconnect() -> ");
IfCantHook(Oconnect, CONNECTPROC, "connect", _connect)
SendCheck;
Append("\r\nWSAGetLastError() ->");
OWSAGetLastError = GetProcAddress(GetModuleHandle(WinsockDll), "WSAGetLastError");
if(OWSAGetLastError==0)
SendCheck;
Append("\r\n\r\n");
Hooked = true;
return 0;
}
//==================================================
int WINAPI _send(SOCKET s, const char FAR *buff, int len, int flags )
{
/////////////////////////
char buf[1024];
int ret = Osend(s, buff, len, flags);
sprintf(buf, "send(SOCKET=%d, size=%d) - ret=%d \r\n{", s, len, ret);
Append(buf);
AppendLog((char*)buff, len);
Append("}\r\n");
return ret;
}
int WINAPI _recv(SOCKET s, char FAR *buff, int len, int flags)
{
char buf[1024];
int ret = Orecv(s, buff, len, flags);
int gle;
if(ret==-1)//SOCKET_ERROR
{
if(OWSAGetLastError!=0)
{
gle = OWSAGetLastError();
if(gle==WSAEWOULDBLOCK)
sprintf(buf, "recv(SOCKET=%d, size=%d) - No data in queue (nonblocking socket)\r\n", s, len, ret);
else
sprintf(buf, "recv(SOCKET=%d, size=%d) - ret=%d - SOCKET_ERROR - WSAGetLastError=%d\r\n", s, len, ret, gle);
} else
sprintf(buf, "recv(SOCKET=%d, size=%d) - ret=%d - SOCKET_ERROR - WSAGetLastError=???\r\n", s, len, ret);
Append(buf);
}else if(ret==0)
{
sprintf(buf, "recv(SOCKET=%d, size=%d) - ret=%d If the connection has been gracefully closed!\r\n", s, len, ret);
Append(buf);
}else
{
sprintf(buf, "recv(SOCKET=%d, size=%d) - ret=%d(bytes recv'ed) \r\n{", s, len, ret);
Append(buf);
AppendLog(buff, ret);
Append("}\r\n");
}
return ret;
}
int WINAPI _listen(SOCKET s, int backlog )
{
char buf[1024];
int ret = Olisten(s, backlog);
sprintf(buf, "listen(SOCKET=%d, backlog=%d) - ret=%d\r\n", s, backlog, ret);
Append(buf);
return ret;
}
SOCKET WINAPI _accept(SOCKET s, struct sockaddr FAR *addr, int FAR *addrlen)
{
char buf[1024];
int ret= Oaccept(s, addr, addrlen);
sprintf(buf, "accept(SOCKET=%d) - ret=(SOCKET)%d//New socket descriptor\r\n", s, ret);
Append(buf);
return ret;
}
int WINAPI _bind(SOCKET s, const struct sockaddr FAR *name, int namelen)
{
char buf[1024];
int ret = Obind(s, name, namelen);
sprintf(buf, "bind(SOCKET=%d, sockaddr=%x) - ret=%d\r\n", s, (DWORD)name, ret);
Append(buf);
return ret;
}
int WINAPI _closesocket(SOCKET s)
{
char buf[1024];
int ret = Oclosesocket(s);
sprintf(buf, "closesocket(SOCKET=%d) - ret=%d\r\n", s, ret);
Append(buf);
return ret;
}
int WINAPI _connect( SOCKET s, const struct sockaddr FAR *name, int namelen)
{
char buf[1024];
int ret = Oconnect(s, name, namelen);
sprintf(buf, "connect(SOCKET=%d, sockaddr=%x) - ret=%d", s, (DWORD)name, ret);
Append(buf);
return ret;
}
SOCKET WINAPI _socket(int af, int type, int protocol)
{
char buf[1024], buf2[1024];
SOCKET ret=Osocket(af, type, protocol);
sprintf(buf, "socket(af=");
if(af==AF_INET)
sprintf(buf, "%s%s, type=", buf, "AF_INET");
else
sprintf(buf, "%s%d, type=", buf, af);
if(type==SOCK_STREAM)
strcpy(buf2, "SOCK_STREAM");
else if(type==SOCK_DGRAM)
strcpy(buf2, "SOCK_DGRAM");
else
itoa(type, buf2, 10);
sprintf(buf, "%s%s, protocol=%d) - ", buf, buf2, protocol);
if(ret!=-1)
sprintf(buf, "%sret=(SOCKET)%d //New socket descriptor\r\n", buf, ret);
else
{
sprintf(buf, "%sret==SOCKET_ERROR(-1) -> WSAGetLastError()=", buf);
if(OWSAGetLastError)
sprintf(buf, "%s%d\r\n", OWSAGetLastError());
else
strcat(buf, "???\r\n");
}
Append(buf);
return ret;
}
#include "hookapi.h"