-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocks5udphooker.cpp
executable file
·288 lines (241 loc) · 8.3 KB
/
socks5udphooker.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
// Very bad thing I didn't want to write. Assumes little-endian.
#include <cstdint>
#include <stdio.h>
#include <winsock2.h>
#include <Windows.h>
#include <detours.h>
#define SOCK_BUFLEN 8096
#define LOCALHOST 0x0100007f
// Make sure we hook the functions in winsock2 specifically
HMODULE hLib = LoadLibrary("ws2_32.dll");
typedef int (WSAAPI *RecvFromPtr)(
SOCKET s,
char FAR* buf,
int len,
int flags,
struct sockaddr FAR* from,
int FAR* fromlen
);
static RecvFromPtr sRealRecvFrom = (RecvFromPtr)GetProcAddress(hLib, "recvfrom");
typedef int (WSAAPI* SendToPtr)(
SOCKET s,
const char FAR* buf,
int len,
int flags,
const struct sockaddr FAR* to,
int tolen
);
static SendToPtr sRealSendTo = (SendToPtr)GetProcAddress(hLib, "sendto");
static SOCKET sProxySock = 0;
static uint16_t sProxyUDPPort = 0;
static BOOL sProxyEnabled = FALSE;
// Old pcaps make it seem like sizeof() sometimes includes alignment
// padding at the end of struct. Maybe not.
const size_t SOCKS_DATAGRAM_HEADER_SIZE = 10;
#pragma pack(push, 1)
struct socks5_datagram_header_t {
uint16_t reserved;
uint8_t fragment;
uint8_t address_type;
uint32_t address;
uint16_t port;
};
#pragma pack(pop)
BOOL is_proxied_datagram(SOCKET s, const struct sockaddr* addr) {
int sock_type;
int length = sizeof(int);
// Fail if we can't get socket option
if (getsockopt(s, SOL_SOCKET, SO_TYPE, (char*)&sock_type, &length)) {
return FALSE;
}
// UDP only
if (sock_type != SOCK_DGRAM) {
return FALSE;
}
// Addr has to be provided
if (addr == nullptr) {
return FALSE;
}
// IPv4 only.
if (addr->sa_family != AF_INET) {
return FALSE;
}
sockaddr_in* addr_in = (sockaddr_in*)addr;
// Pass DNS through as-is no matter what.
if (ntohs(addr_in->sin_port) == 53)
return FALSE;
return TRUE;
}
int
WSAAPI
fake_recvfrom(
_In_ SOCKET s,
_Out_writes_bytes_to_(len, return) __out_data_source(NETWORK) char FAR* buf,
_In_ int len,
_In_ int flags,
_Out_writes_bytes_to_opt_(*fromlen, *fromlen) struct sockaddr FAR* from,
_Inout_opt_ int FAR* fromlen
) {
// Pass errors through as-is
int data_len = sRealRecvFrom(s, buf, len, flags, from, fromlen);
if (data_len < 0) {
return data_len;
}
// This isn't something that could even potentially be proxied.
if (!is_proxied_datagram(s, from)) {
return data_len;
}
sockaddr_in* from_in = (sockaddr_in*)from;
// Didn't actually come from the proxy, pass it through
if (from_in->sin_addr.s_addr != LOCALHOST) {
return data_len;
}
// Not long enough to even have a header
if (data_len < SOCKS_DATAGRAM_HEADER_SIZE) {
goto internal_error;
}
const socks5_datagram_header_t socks_header = *((socks5_datagram_header_t *)buf);
// We don't know how to handle either of these cases.
if (socks_header.reserved || socks_header.fragment) {
goto internal_error;
}
// lie about who we got the packet from using the SOCKS5 header
from_in->sin_addr.s_addr = socks_header.address;
from_in->sin_port = socks_header.port;
// Remove the datagram header from the start of the buffer
memmove(buf, buf + SOCKS_DATAGRAM_HEADER_SIZE, data_len - SOCKS_DATAGRAM_HEADER_SIZE);
data_len -= SOCKS_DATAGRAM_HEADER_SIZE;
return data_len;
internal_error:
// Zero is returned for errors on Windows for some reason,
// even though zero-length datagrams are valid.
return 0;
}
int
WSAAPI
fake_sendto(
_In_ SOCKET s,
_In_reads_bytes_(len) const char FAR* buf,
_In_ int len,
_In_ int flags,
_In_reads_bytes_(tolen) const struct sockaddr FAR* to,
_In_ int tolen
) {
if (!is_proxied_datagram(s, to)) {
return sRealSendTo(s, buf, len, flags, to, tolen);
}
// Too large, can't send this
if (len + SOCKS_DATAGRAM_HEADER_SIZE >= SOCK_BUFLEN) {
return -1;
}
// We can't mutate the original because that will mess with resends, make a copy
// is_proxied_datagram() verifies that this is af_inet.
struct sockaddr_in proxy_to = *(sockaddr_in*)to;
char send_buf[SOCK_BUFLEN] = { 0 };
// Write the SOCKS5 header at the start of the new packet
socks5_datagram_header_t* socks_header = (socks5_datagram_header_t*)&send_buf;
socks_header->address = proxy_to.sin_addr.s_addr;
socks_header->port = proxy_to.sin_port;
socks_header->reserved = 0;
socks_header->fragment = 0;
socks_header->address_type = 1;
// rewrite the to address to point to the proxy
proxy_to.sin_port = sProxyUDPPort;
proxy_to.sin_addr.s_addr = LOCALHOST;
// Copy the original data over to the new packet
memcpy(send_buf + SOCKS_DATAGRAM_HEADER_SIZE, buf, len);
return sRealSendTo(s, send_buf, len + SOCKS_DATAGRAM_HEADER_SIZE, flags, (sockaddr*)&proxy_to, tolen);
}
BOOL blocking_socks5_handshake() {
WSADATA wsaData;
char recvbuf[SOCK_BUFLEN] = {0};
int recvbuflen = SOCK_BUFLEN;
int timeout = 1000;
sockaddr_in proxy_addr = { 0 };
// We have to start up winsock ourselves since the parent process won't have by this point
WSAStartup(0x0202, &wsaData);
sProxySock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sProxySock == INVALID_SOCKET) {
MessageBox(NULL, TEXT("Couldn't create a socket"), TEXT("Proxy error"), 0);
return FALSE;
}
setsockopt(sProxySock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout));
setsockopt(sProxySock, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout));
proxy_addr.sin_family = AF_INET;
proxy_addr.sin_addr.s_addr = LOCALHOST;
proxy_addr.sin_port = htons(9061);
if (connect(sProxySock, (const sockaddr*)&proxy_addr, sizeof(sockaddr_in))) {
MessageBox(NULL, TEXT("Couldn't connect to SOCKS proxy, closing!"), TEXT("Proxy error"), 0);
return FALSE;
}
// SOCKS 5 handshake, no auth
if (send(sProxySock, "\x05\x01\x00", 3, 0) != 3) {
goto handshake_failed;
}
// failed to send
if (recv(sProxySock, recvbuf, 2, 0) != 2) {
goto handshake_failed;
}
// not SOCKS or unauthed not allowed.
if (memcmp(recvbuf, "\x05\x00", 2)) {
goto handshake_failed;
}
// ask for a UDP association
if (send(sProxySock, "\x05\x03\x00\x01\x00\x00\x00\x00\x00\x00", 10, 0) != 10) {
MessageBox(NULL, TEXT("Failed to ask for UDP association!"), TEXT("Proxy error"), 0);
goto handshake_failed;
}
if (recv(sProxySock, recvbuf, 10, 0) != 10) {
goto handshake_failed;
}
// Did we fail to get an IPv4 association
if (memcmp(recvbuf, "\x05\x00\x00\x01", 4)) {
MessageBox(NULL, TEXT("Didn't get a UDP association"), TEXT("Proxy error"), 0);
goto handshake_failed;
}
// Don't care about the host. We assume it's localhost. Only get the port, network-endian.
sProxyUDPPort = (*((uint16_t*)&recvbuf[8]));
sProxyEnabled = TRUE;
return TRUE;
handshake_failed:
shutdown(sProxySock, SD_SEND);
closesocket(sProxySock);
MessageBox(NULL, TEXT("SOCKS Proxy handshake failed!"), TEXT("Proxy error"), 0);
return FALSE;
}
//-------------------------------------------------------------------------
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
{
LONG error;
UNREFERENCED_PARAMETER(hinst);
UNREFERENCED_PARAMETER(reserved);
if (DetourIsHelperProcess())
return TRUE;
if (dwReason == DLL_PROCESS_ATTACH)
{
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
if (blocking_socks5_handshake()) {
DetourAttach(&(PVOID&)sRealRecvFrom, fake_recvfrom);
DetourAttach(&(PVOID&)sRealSendTo, fake_sendto);
}
else {
ExitProcess(1);
}
error = DetourTransactionCommit();
}
else if (dwReason == DLL_PROCESS_DETACH)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
if (sProxyEnabled) {
shutdown(sProxySock, SD_SEND);
closesocket(sProxySock);
DetourDetach(&(PVOID&)sRealRecvFrom, fake_recvfrom);
DetourDetach(&(PVOID&)sRealSendTo, fake_sendto);
}
error = DetourTransactionCommit();
}
return TRUE;
}