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
|
#include <stdio.h>
#include <winSock2.h>
#include <mswsock.h>
#include <detours.h>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"detours.lib")
#pragma warning(disable:4996)
LPFN_GETACCEPTEXSOCKADDRS lpfnGetAcceptExSockAddress = NULL;
LPFN_ACCEPTEX lpfnAcceptEx = NULL;
VOID(WSAAPI * pOldGetAcceptExSockAddress)(
__in PVOID lpOutputBuffer,
__in DWORD dwReceiveDataLength,
__in DWORD dwLocalAddressLength,
__in DWORD dwRemoteAddressLength,
__deref_out_bcount(*LocalSockaddrLength) struct sockaddr **LocalSockaddr,
__out LPINT LocalSockaddrLength,
__deref_out_bcount(*RemoteSockaddrLength) struct sockaddr **RemoteSockaddr,
__out LPINT RemoteSockaddrLength
);
BOOL(WSAAPI *pOldAcceptEx)(SOCKET sListenSocket, SOCKET sAcceptSocket, PVOID lpOutputBuffer, DWORD dwReceiveDataLength,
DWORD dwLocalAddresssLength, DWORD dwRemoteAddressLength, LPDWORD lpdwBytesReceived, LPOVERLAPPED lpOverlapped);
BOOL WSAAPI MyAcceptEx(SOCKET sListenSocket, SOCKET sAcceptSocket, PVOID lpOutputBuffer, DWORD dwReceiveDataLength,
DWORD dwLocalAddresssLength, DWORD dwRemoteAddressLength, LPDWORD lpdwBytesReceived, LPOVERLAPPED lpOverlapped){
MessageBox(NULL, "This is AcceptEx", "Inject", MB_OK);
return pOldAcceptEx(sListenSocket, sAcceptSocket, lpOutputBuffer, dwReceiveDataLength, dwLocalAddresssLength, dwRemoteAddressLength,
lpdwBytesReceived, lpOverlapped);
}
VOID WSAAPI MyGetAcceptExSockAddress(
__in PVOID lpOutputBuffer,
__in DWORD dwReceiveDataLength,
__in DWORD dwLocalAddressLength,
__in DWORD dwRemoteAddressLength,
__deref_out_bcount(*LocalSockaddrLength) struct sockaddr **LocalSockaddr,
__out LPINT LocalSockaddrLength,
__deref_out_bcount(*RemoteSockaddrLength) struct sockaddr **RemoteSockaddr,
__out LPINT RemoteSockaddrLength
){
MessageBox(NULL, "This is GetAcceptExSockAddress", "Inject", MB_OK);
pOldGetAcceptExSockAddress(lpOutputBuffer, dwReceiveDataLength, dwLocalAddressLength, dwRemoteAddressLength, LocalSockaddr, LocalSockaddrLength, RemoteSockaddr,
RemoteSockaddrLength);
}
BOOL APIENTRY InstallHook(){
char err_buf[100];
GUID GuidGetAcceptExSockAddrs = WSAID_GETACCEPTEXSOCKADDRS;
GUID GuidAcceptEx = WSAID_ACCEPTEX;
DWORD dwBytes = 0;
int err = WSAIoctl(socket(AF_INET, SOCK_STREAM, 0), SIO_GET_EXTENSION_FUNCTION_POINTER, &GuidGetAcceptExSockAddrs,
sizeof(GuidGetAcceptExSockAddrs), &lpfnGetAcceptExSockAddress, sizeof(lpfnGetAcceptExSockAddress),
&dwBytes, NULL, NULL);
WSAIoctl(socket(AF_INET, SOCK_STREAM, 0), SIO_GET_EXTENSION_FUNCTION_POINTER, &GuidAcceptEx, sizeof(GuidAcceptEx),
&lpfnAcceptEx, sizeof(lpfnAcceptEx), &dwBytes, NULL, NULL);
if (err == SOCKET_ERROR){
sprintf(err_buf, "The WSAIoctl is error with code:%d", WSAGetLastError());
MessageBox(NULL, err_buf, "Error", MB_OK);
}
pOldGetAcceptExSockAddress = lpfnGetAcceptExSockAddress;
pOldAcceptEx = lpfnAcceptEx;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(LPVOID&)pOldGetAcceptExSockAddress, MyGetAcceptExSockAddress);
DetourAttach(&(LPVOID&)pOldAcceptEx, MyAcceptEx);
DetourTransactionCommit();
return TRUE;
}
BOOL APIENTRY UnInstallHook(){
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(LPVOID&)pOldGetAcceptExSockAddress, MyGetAcceptExSockAddress);
DetourDetach(&(LPVOID&)pOldAcceptEx, MyAcceptEx);
DetourTransactionCommit();
return TRUE;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason_call, LPVOID lpReserved){
switch (reason_call){
case DLL_PROCESS_ATTACH:
InstallHook();
break;
case DLL_PROCESS_DETACH:
UnInstallHook();
break;
default:
break;
}
return TRUE;
}
|