001
002
#include "stdafx.h"
003
#include "mhook-lib/mhook.h"
004
#include <WinSock2.h>
005
006
007
static
void
GT_WriteReleaseLog(
char
* str,
char
* path=
"C:\\mk.log"
)
008
{
009
HANDLE
hFile = CreateFileA(path, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
010
if
(hFile == NULL)
011
return
;
012
013
_llseek((
HFILE
)hFile,0, SEEK_END);
014
DWORD
dw;
015
WriteFile(hFile,str,
strlen
(str),&dw,NULL);
016
_lclose((
HFILE
)hFile);
017
}
018
HMODULE
hMod = LoadLibraryA(
"Ws2_32"
);
019
020
typedef
int
(WINAPI *_send)(SOCKET s,
const
char
*buf,
int
len,
int
flags);
021
typedef
int
(WINAPI *_recv)(SOCKET s,
char
*buf,
int
len,
int
flags);
022
_send g_trueSend = (_send)GetProcAddress(hMod,
"send"
);
023
_recv g_trueRecv = (_recv)GetProcAddress(hMod,
"recv"
);
024
025
typedef
int
(WINAPI *_wsend)(SOCKET s, LPWSABUF lpBuffers,
DWORD
dwBufferCount,
026
LPDWORD
lpNumberOfBytesSent,
DWORD
dwFlags, LPWSAOVERLAPPED lpOverlapped,
027
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
028
typedef
int
(WINAPI *_wrecv)(SOCKET s, LPWSABUF lpBuffers,
DWORD
dwBufferCount,
029
LPDWORD
lpNumberOfBytesRecvd,
LPDWORD
lpFlags, LPWSAOVERLAPPED lpOverlapped,
030
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
031
_wsend g_trueWSend = (_wsend)GetProcAddress(hMod,
"WSASend"
);
032
_wrecv g_trueWRecv = (_wrecv)GetProcAddress(hMod,
"WSARecv"
);
033
034
mhook_func _msend = NULL;
035
mhook_func _mrecv = NULL;
036
mhook_func _mwsend = NULL;
037
mhook_func _mwrecv = NULL;
038
039
040
041
042
043
044
045
046
047
int
WINAPI hook_send(SOCKET s,
const
char
*buf,
int
len,
int
flags)
048
{
049
int
ret = g_trueSend(s,buf,len,flags);
050
if
(ret > 0)
051
{
052
char
*temp =
new
char
[ret];
053
memcpy_s(temp,ret,buf,ret);
054
if
(_msend != NULL)
055
_msend(s,temp,ret);
056
delete
temp;
057
}
058
return
ret;
059
}
060
061
062
063
064
065
066
067
068
069
int
WINAPI hook_recv(SOCKET s,
char
*buf,
int
len,
int
flags)
070
{
071
int
ret = g_trueRecv(s,buf,len,flags);
072
if
(ret > 0)
073
{
074
char
*temp =
new
char
[ret];
075
memcpy_s(temp,ret,buf,ret);
076
if
(_msend != NULL)
077
_mrecv(s,temp,ret);
078
delete
temp;
079
}
080
return
ret;
081
}
082
083
084
085
086
087
088
089
090
091
092
093
int
WINAPI hook_wsend(SOCKET s, LPWSABUF lpBuffers,
DWORD
dwBufferCount,
094
LPDWORD
lpNumberOfBytesSent,
DWORD
dwFlags, LPWSAOVERLAPPED lpOverlapped,
095
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
096
{
097
int
ret = g_trueWSend(s,lpBuffers,dwBufferCount,lpNumberOfBytesSent
098
,dwFlags,lpOverlapped,lpCompletionRoutine);
099
DWORD
len = *lpNumberOfBytesSent;
100
if
(len > 0)
101
{
102
char
*temp =
new
char
[len];
103
memcpy_s(temp,len,lpBuffers->buf,len);
104
if
(_mwsend != NULL)
105
_mwsend(s,temp,len);
106
delete
temp;
107
}
108
return
ret;
109
}
110
111
int
WINAPI hook_wrecv(SOCKET s, LPWSABUF lpBuffers,
DWORD
dwBufferCount,
112
LPDWORD
lpNumberOfBytesRecvd,
LPDWORD
lpFlags, LPWSAOVERLAPPED lpOverlapped,
113
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
114
{
115
int
ret = g_trueWRecv(s,lpBuffers,dwBufferCount,lpNumberOfBytesRecvd,lpFlags
116
,lpOverlapped,lpCompletionRoutine);
117
DWORD
len = *lpNumberOfBytesRecvd;
118
if
(len > 0)
119
{
120
char
*temp =
new
char
[len];
121
memcpy_s(temp,len,lpBuffers->buf,len);
122
if
(_mwrecv != NULL)
123
_mwrecv(s,temp,len);
124
delete
temp;
125
}
126
return
ret;
127
}
128
129
BOOL
APIENTRY DllMain(
HMODULE
hModule,
DWORD
ul_reason_for_call,
LPVOID
lpReserved)
130
{
131
switch
(ul_reason_for_call)
132
{
133
case
DLL_PROCESS_ATTACH:
134
135
Mhook_SetHook((
LPVOID
*)&g_trueSend,hook_send);
136
Mhook_SetHook((
LPVOID
*)&g_trueRecv,hook_recv);
137
Mhook_SetHook((
LPVOID
*)&g_trueWSend,hook_wsend);
138
Mhook_SetHook((
LPVOID
*)&g_trueWRecv,hook_wrecv);
139
break
;
140
case
DLL_THREAD_ATTACH:
141
break
;
142
case
DLL_THREAD_DETACH:
143
break
;
144
case
DLL_PROCESS_DETACH:
145
146
Mhook_Unhook((
LPVOID
*)&g_trueSend);
147
Mhook_Unhook((
LPVOID
*)&g_trueRecv);
148
Mhook_Unhook((
LPVOID
*)&g_trueWSend);
149
Mhook_Unhook((
LPVOID
*)&g_trueWRecv);
150
if
(hModule != NULL)
151
FreeLibrary(hModule);
152
break
;
153
}
154
return
TRUE;
155
}