forked from bitspalter/DNS-Proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_Net.cpp
343 lines (275 loc) · 11.1 KB
/
C_Net.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
//////////////////////////////////////////////////////////////////////////////////
// [ Net_Class_Source ]
//////////////////////////////////////////////////////////////////////////////////
#include "C_Net.hpp"
//////////////////////////////////////////////////////////////////////////////////
// [ start ]
//////////////////////////////////////////////////////////////////////////////////
int C_Net::start(C_DArray<char>* pCDA_WList, C_DArray<char>* pCDA_BList){
if(!pCDA_WList || !pCDA_BList) return(C_NET_ERROR);
this->pCDA_WhiteList = pCDA_WList;
this->pCDA_BlackList = pCDA_BList;
if(CNInterface.create() != C_NET_INTERFACE_READY){
cout << "ERROR: CNInterface.create" << endl;
return(C_NET_ERROR);
}
return(C_NET_READY);
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_udp_data ]
//////////////////////////////////////////////////////////////////////////////////
void C_Net::on_udp_data(int id, int cData){
if(id == C_NET_ID_CLIENT) on_client_data(&CAClient[0], (DWORD)cData);
else
if(id == C_NET_ID_SERVER) on_server_data(&CAServer[0], (DWORD)cData);
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_client_data ]
//////////////////////////////////////////////////////////////////////////////////
void C_Net::on_client_data(UCHAR* pBuffer, DWORD cBuffer){
if(cBuffer < sizeof(DNS_HEADER)) return;
// GET DNS FLAG
WORD _DNS_Flag = ntohs(((DNS_HEADER*)pBuffer)->DNS_Flags);
if(_DNS_Flag & DNS_FLAG_TYP_RESPONSE){
// CHECK ALL REQUESTS
for(int n = 0; n < cda_request.getnItems(); n++){
// GET REQUEST
C_Array<Dns_Request>* pRequest = cda_request.getpItempData(n);
if(!pRequest) return;
// GET DATA
Dns_Request* pDns = pRequest->getpBuffer();
if(!pDns) return;
// CHECK DNS ID
if(pDns->ID == ((DNS_HEADER*)pBuffer)->DNS_ID){
// SET DESTINATON
in_addr in;
in.s_addr = pDns->Client_IP;
CNUdpServer.connect(inet_ntoa(in), pDns->Client_Port);
// SEND DATA
CNUdpServer.send(pBuffer, cBuffer);
// SHOW REQUEST
get_request(pDns->Client_IP, pBuffer, cBuffer);
// DELETE REQUEST
cda_request.delItem(n);
// READY
return;
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_server_data ]
//////////////////////////////////////////////////////////////////////////////////
void C_Net::on_server_data(UCHAR* pBuffer, DWORD cBuffer){
if(cBuffer < sizeof(DNS_HEADER)) return;
WORD _DNS_Flag = ntohs(((DNS_HEADER*)pBuffer)->DNS_Flags);
if(_DNS_Flag & DNS_FLAG_TYP_RESPONSE){ // Response
//message( "DNS Response" );
}else{ // Request wirklich ?!?
sockaddr_in sock;
CNUdpServer.getAddressRemote(&sock);
////////////////////////////////////////////////////////////////////////
// Test gegen Black | White List
if(this->NetModus == C_NET_MODUS_WHITELIST || this->NetModus == C_NET_MODUS_BLACKLIST){
DWORD off = sizeof(DNS_HEADER);
DWORD offDomain = 0;
UCHAR pDomain[1024];
///////////////////////////////////////////////////////////////////////
// Mach Aus pBuffer[] = "[3][G][M][X][2][D][E]"
// pDomain[] = "[G][M][X][.][D][E][0]"
while(pBuffer[off]){
memcpy(&pDomain[offDomain], &pBuffer[off + 1], pBuffer[off]);
offDomain += pBuffer[off];
if(offDomain > 1023){
offDomain = 1023;
break;
}
pDomain[offDomain] = 46;
offDomain++;
off += pBuffer[off] + 1;
}
if(!offDomain) return;
pDomain[offDomain - 1] = 0;
///////////////////////////////////////////////////////////////////
// Mach aus aaa.bbbb.cc.de
// pTLD->"cc.de"
long cfff = strlen((char*)pDomain);
long pPoint[2];
long cPoint = 0;
memset(pPoint, 0, sizeof(pPoint));
for(long n = cfff; n > 0; n--){
switch(pDomain[n]){
case 46:{
pPoint[cPoint++] = n; break;
}
}
if(cPoint == 2) break;
}
char* pTLD = (char*)&pDomain;
if(cPoint > 1)
pTLD = (char*)&pDomain[pPoint[1]] + 1;
////////////////////////////////////////////////////////////////////
// Test gegen White | Black List
bool bLegal = false;
if(this->NetModus == C_NET_MODUS_WHITELIST){
for(int nWL = 0; nWL < pCDA_WhiteList->getnItems(); nWL++){
if(C_Array<char>* pData = pCDA_WhiteList->getpItempData(nWL)){
if(char* pTB = pData->getpBuffer()){
if(!strcmp(pTB, pTLD)){
bLegal = true;
break;
}
}
}
}
}else
if(this->NetModus == C_NET_MODUS_BLACKLIST){
bLegal = true;
for(int nBL = 0; nBL < pCDA_BlackList->getnItems(); nBL++){
if(C_Array<char>* pData = pCDA_BlackList->getpItempData(nBL)){
if(char* pTB = pData->getpBuffer()){
if(!strcmp(pTB, pTLD)){
bLegal = false;
break;
}
}
}
}
}
////////////////////////////////////////////////////////77
if(bLegal == false){
///////////////////////////////////////////////////////////////////////////
// Packet hat Black | White List nicht bestanden
// Sende [Not Exist Error]-->Client
((DNS_HEADER*)pBuffer)->DNS_Flags = 0x8381;
//////////////////////////////////////////////////
// SET MAC:IP:PORT FOR UDP SOCKET
CNUdpServer.connect(inet_ntoa(sock.sin_addr), ntohs(sock.sin_port));
// SEND DATA UDP SOCKET
CNUdpServer.send(pBuffer, cBuffer);
//////////////////////////////////////////////////////////////////////////
// Ausgabe
UCHAR NETBCIP[4] = {0xFF,0xFF,0xFF,0xFF};
dns_result.pIP = (DWORD)sock.sin_addr.s_addr;
dns_result.psName = (char*)pBuffer + sizeof(DNS_HEADER);
dns_result.pResult[0] = NETBCIP[0];
dns_result.pResult[1] = NETBCIP[1];
dns_result.pResult[2] = NETBCIP[2];
dns_result.pResult[3] = NETBCIP[3];
dns_result.pName = 0;
dns_result.bBlock = true;
sig_dns_data();
return; // FERTIG
}
}
///////////////////////////////////////////////////////////////////////////
// Packet hat Black | White List bestanden
// Speicher Request, Sende Request an Original DNS Server
C_Array<Dns_Request>* pRequest = cda_request.addItem(1);
if(!pRequest) return;
//////////////////////////////////////////////////
Dns_Request dnsRequest;
// REQUEST ID
dnsRequest.ID = ((DNS_HEADER*)pBuffer)->DNS_ID;
// REQUEST SIZE
dnsRequest.cData = cBuffer;
// REQUEST IP
dnsRequest.Client_IP = sock.sin_addr.s_addr;
// REQUEST Port
dnsRequest.Client_Port = htons(sock.sin_port);
// SAVE REQUEST
memcpy(pRequest->getpBuffer(), &dnsRequest, sizeof(Dns_Request));
//////////////////////////////////////////////////
// SEND DATA UDP SOCKET
CNUdpClient.send(pBuffer, cBuffer);
}
}
//////////////////////////////////////////////////////////////////////////////////
// [ get_request ]
//////////////////////////////////////////////////////////////////////////////////
void C_Net::get_request(DWORD pIP, UCHAR* pDNS_Packet, DWORD cDNS_Packet){
UCHAR* pData = pDNS_Packet;
char psDomain[1024];
size_t csDomain = 0;
size_t csName = 0;
if(cDNS_Packet < sizeof(DNS_HEADER)) return;
WORD cQuest = ntohs(((DNS_HEADER*)pDNS_Packet)->DNS_cQuestion);
WORD cAnswer = ntohs(((DNS_HEADER*)pDNS_Packet)->DNS_cAnswer);
pData += sizeof(DNS_HEADER);
//////////////////////////////////////////////////////////////
// DNS_cQuestion
if(cQuest){
for(DWORD nQuest = 0; nQuest < cQuest; nQuest++){
csName = strlen((char*)pData);
for(DWORD n = 0; n < csName; n++){
char cPart = pData[n];
memcpy(&psDomain[csDomain], &pData[n + 1], cPart);
csDomain += cPart;
psDomain[csDomain++] = 46;
n += cPart;
}
if(csDomain) psDomain[--csDomain] = 0x00;
pData += csDomain;
pData += 6;
}
}
//////////////////////////////////////////////////////////////
// DNS_cAnswer
if(cAnswer){
for(DWORD nAnswer = 0; nAnswer < cAnswer; nAnswer++){
DNS_ANSWER* pAnswer = (DNS_ANSWER*)pData;
pData += sizeof(DNS_ANSWER);
// DATA = ADDRESS LIKE 190.37.233.45
if(pAnswer->DNS_Type == DNS_QUEST_TYP_ADDRESS){
dns_result.pIP = pIP;
dns_result.psName = (char*)pDNS_Packet + sizeof(DNS_HEADER);
dns_result.pResult[0] = pData[0];
dns_result.pResult[1] = pData[1];
dns_result.pResult[2] = pData[2];
dns_result.pResult[3] = pData[3];
dns_result.pName = 0;
dns_result.bBlock = false;
sig_dns_data();
pData += 4;
}else
// DATA = ADDRESS LIKE www3.L.[0xC0][POINTER ON STRING]
if(pAnswer->DNS_Type == DNS_QUEST_TYP_CNAME){
memset(psDomain, 0, sizeof(psDomain));
csDomain = 0;
csName = 0;
csName = ntohs(pAnswer->DNS_cData);
for(DWORD n = 0; n < csName; n++){
UCHAR cPart = pData[n];
if(!cPart) break; // STRING END
else
if(cPart == 0xC0){ // pData[n + 1] == OFFSET
UCHAR* pTemp = pDNS_Packet;
// pTemp -> STRING
pTemp += pData[n + 1];
size_t cTemp = strlen((char*)pTemp);
for(DWORD nTemp = 0; nTemp < cTemp; nTemp++){
UCHAR __cPart = pTemp[nTemp];
memcpy(&psDomain[csDomain], &pTemp[nTemp + 1], __cPart);
csDomain += __cPart;
// Appand "."
psDomain[csDomain++] = 46;
nTemp += __cPart;
}
// [0xC0][0xOFFSET]
n += 2;
}else{ // pData[n] == SIZEOF(pData[n + 1])
memcpy(&psDomain[csDomain], &pData[n + 1], cPart);
csDomain += cPart;
// Appand "."
psDomain[csDomain++] = 46;
n += cPart;
}
}
// Delete "."
if(csDomain) psDomain[ --csDomain ] = 0x00;
pData += csName;
}
}
}
return;
}