forked from netchx/netch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedirector.cpp
340 lines (299 loc) · 8.1 KB
/
Redirector.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
#include "Based.h"
#include "EventHandler.h"
#include "IPEventHandler.h"
#include "Utils.h"
extern bool filterLoopback;
extern bool filterIntranet;
extern bool filterParent;
extern bool filterICMP;
extern bool filterTCP;
extern bool filterUDP;
extern bool filterDNS;
extern DWORD icmping;
extern bool dnsOnly;
extern bool dnsProx;
extern string dnsHost;
extern USHORT dnsPort;
extern wstring tgtHost;
extern wstring tgtPort;
extern string tgtUsername;
extern string tgtPassword;
extern vector<wstring> bypassList;
extern vector<wstring> handleList;
extern atomic_ullong UP;
extern atomic_ullong DL;
NF_EventHandler EventHandler = {
threadStart,
threadEnd,
tcpConnectRequest,
tcpConnected,
tcpClosed,
tcpReceive,
tcpSend,
tcpCanReceive,
tcpCanSend,
udpCreated,
udpConnectRequest,
udpClosed,
udpReceive,
udpSend,
udpCanReceive,
udpCanSend
};
NF_IPEventHandler IPEventHandler = {
ipReceive,
ipSend
};
BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
UNREFERENCED_PARAMETER(hModule);
UNREFERENCED_PARAMETER(dwReason);
UNREFERENCED_PARAMETER(lpReserved);
return TRUE;
}
extern "C" {
__declspec(dllexport) BOOL __cdecl aio_register(LPWSTR value)
{
auto status = nf_registerDriver(ws2s(value).c_str());
if (status != NF_STATUS_SUCCESS)
{
printf("[Redirector][aio_register] nf_registerDriver: %d\n", status);
return FALSE;
}
return TRUE;
}
__declspec(dllexport) BOOL __cdecl aio_unregister(LPWSTR value)
{
auto status = nf_unRegisterDriver(ws2s(value).c_str());
if (status != NF_STATUS_SUCCESS)
{
printf("[Redirector][aio_unregister] nf_unRegisterDriver: %d\n", status);
return FALSE;
}
return TRUE;
}
__declspec(dllexport) BOOL __cdecl aio_dial(int name, LPWSTR value)
{
switch (name)
{
case AIO_FILTERLOOPBACK:
filterLoopback = (wstring(value).find(L"false") == string::npos);
break;
case AIO_FILTERINTRANET:
filterIntranet = (wstring(value).find(L"false") == string::npos);
break;
case AIO_FILTERPARENT:
filterParent = (wstring(value).find(L"false") == string::npos);
break;
case AIO_FILTERICMP:
filterICMP = (wstring(value).find(L"false") == string::npos);
break;
case AIO_FILTERTCP:
filterTCP = (wstring(value).find(L"false") == string::npos);
break;
case AIO_FILTERUDP:
filterUDP = (wstring(value).find(L"false") == string::npos);
break;
case AIO_FILTERDNS:
filterDNS = (wstring(value).find(L"false") == string::npos);
break;
case AIO_ICMPING:
icmping = atoi(ws2s(value).c_str());
break;
case AIO_DNSONLY:
dnsOnly = (wstring(value).find(L"false") == string::npos);
break;
case AIO_DNSPROX:
dnsProx = (wstring(value).find(L"false") == string::npos);
break;
case AIO_DNSHOST:
dnsHost = ws2s(value);
break;
case AIO_DNSPORT:
dnsPort = static_cast<USHORT>(atoi(ws2s(value).c_str()));
break;
case AIO_TGTHOST:
tgtHost = wstring(value);
break;
case AIO_TGTPORT:
tgtPort = wstring(value);
break;
case AIO_TGTUSER:
tgtUsername = ws2s(value);
break;
case AIO_TGTPASS:
tgtPassword = ws2s(value);
break;
case AIO_CLRNAME:
bypassList.clear();
handleList.clear();
break;
case AIO_BYPNAME:
try
{
std::wregex checker(value);
}
catch (regex_error) {
return FALSE;
}
bypassList.emplace_back(value);
break;
case AIO_ADDNAME:
try
{
std::wregex checker(value);
}
catch (regex_error) {
return FALSE;
}
handleList.emplace_back(value);
break;
default:
return FALSE;
}
return TRUE;
}
__declspec(dllexport) BOOL __cdecl aio_init()
{
WSADATA data;
if (WSAStartup(MAKEWORD(2, 2), &data) != NO_ERROR)
{
puts("[Redirector][aio_init] WSAStartup != NO_ERROR");
return FALSE;
}
nf_adjustProcessPriviledges();
if (!eh_init())
{
puts("[Redirector][aio_init] !eh_init");
return FALSE;
}
if (nf_init("netfilter2", &EventHandler) != NF_STATUS_SUCCESS)
{
puts("[Redirector][aio_init] nf_init != NF_STATUS_SUCCESS");
return FALSE;
}
NF_RULE rule;
if (!filterLoopback)
{
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", rule.remoteIpAddress);
inet_pton(AF_INET, "255.0.0.0", rule.remoteIpAddressMask);
rule.filteringFlag = NF_ALLOW;
nf_addRule(&rule, FALSE);
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET6;
rule.remoteIpAddress[15] = 1;
memset(rule.remoteIpAddressMask, 0xff, sizeof(rule.remoteIpAddressMask));
rule.filteringFlag = NF_ALLOW;
nf_addRule(&rule, FALSE);
}
if (!filterIntranet)
{
/* 10.0.0.0/8 */
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
inet_pton(AF_INET, "10.0.0.0", rule.remoteIpAddress);
inet_pton(AF_INET, "255.0.0.0", rule.remoteIpAddressMask);
rule.filteringFlag = NF_ALLOW;
nf_addRule(&rule, FALSE);
/* 100.64.0.0/10 */
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
inet_pton(AF_INET, "100.64.0.0", rule.remoteIpAddress);
inet_pton(AF_INET, "255.192.0.0", rule.remoteIpAddressMask);
rule.filteringFlag = NF_ALLOW;
nf_addRule(&rule, FALSE);
/* 169.254.0.0/16 */
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
inet_pton(AF_INET, "169.254.0.0", rule.remoteIpAddress);
inet_pton(AF_INET, "255.255.0.0", rule.remoteIpAddressMask);
rule.filteringFlag = NF_ALLOW;
nf_addRule(&rule, FALSE);
/* 172.16.0.0/12 */
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
inet_pton(AF_INET, "100.64.0.0", rule.remoteIpAddress);
inet_pton(AF_INET, "255.240.0.0", rule.remoteIpAddressMask);
rule.filteringFlag = NF_ALLOW;
nf_addRule(&rule, FALSE);
/* 192.0.0.0/24 */
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
inet_pton(AF_INET, "192.0.0.0", rule.remoteIpAddress);
inet_pton(AF_INET, "255.255.255.0", rule.remoteIpAddressMask);
rule.filteringFlag = NF_ALLOW;
nf_addRule(&rule, FALSE);
/* 192.168.0.0/16 */
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
inet_pton(AF_INET, "192.168.0.0", rule.remoteIpAddress);
inet_pton(AF_INET, "255.255.0.0", rule.remoteIpAddressMask);
rule.filteringFlag = NF_ALLOW;
nf_addRule(&rule, FALSE);
/* 198.18.0.0/15 */
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
inet_pton(AF_INET, "198.18.0.0", rule.remoteIpAddress);
inet_pton(AF_INET, "255.254.0.0", rule.remoteIpAddressMask);
rule.filteringFlag = NF_ALLOW;
nf_addRule(&rule, FALSE);
}
if (filterICMP)
{
nf_setIPEventHandler(&IPEventHandler);
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
rule.protocol = IPPROTO_ICMP;
rule.direction = NF_D_OUT;
rule.filteringFlag = NF_FILTER_AS_IP_PACKETS;
nf_addRule(&rule, FALSE);
}
if (filterTCP)
{
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
rule.protocol = IPPROTO_TCP;
rule.direction = NF_D_OUT;
rule.filteringFlag = NF_INDICATE_CONNECT_REQUESTS;
nf_addRule(&rule, FALSE);
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET6;
rule.protocol = IPPROTO_TCP;
rule.direction = NF_D_OUT;
rule.filteringFlag = NF_INDICATE_CONNECT_REQUESTS;
nf_addRule(&rule, FALSE);
}
if (filterUDP || filterDNS)
{
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET;
rule.protocol = IPPROTO_UDP;
rule.filteringFlag = NF_FILTER;
nf_addRule(&rule, FALSE);
memset(&rule, 0, sizeof(NF_RULE));
rule.ip_family = AF_INET6;
rule.protocol = IPPROTO_UDP;
rule.filteringFlag = NF_FILTER;
nf_addRule(&rule, FALSE);
}
return TRUE;
}
__declspec(dllexport) void __cdecl aio_free()
{
nf_deleteRules();
nf_free();
eh_free();
WSACleanup();
return;
}
__declspec(dllexport) ULONG64 __cdecl aio_getUP()
{
return UP;
}
__declspec(dllexport) ULONG64 __cdecl aio_getDL()
{
return DL;
}
}