/
pktanalyzer.cpp
465 lines (425 loc) · 13.9 KB
/
pktanalyzer.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
*
* Copyright (C) Max <max1976@mail.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdint.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <linux/netfilter.h>
#include <memory>
#include "main.h"
#include "AhoCorasickPlus.h"
#include <Poco/Stopwatch.h>
#include <Poco/Net/IPAddress.h>
#include <Poco/URI.h>
#include "sendertask.h"
#include "ndpiwrapper.h"
#include "pktanalyzer.h"
#define iphdr(x) ((struct iphdr *)(x))
#define tcphdr(x) ((struct tcphdr *)(x))
#define __USE_POCO_URI_DECODE
PktAnalyzer::PktAnalyzer(const std::string& name, Poco::NotificationQueue& queue, struct nfqConfig& cfg, nfqThread *parent):
_name(name),
_queue(queue),
_logger(Poco::Logger::get(name)),
_config(cfg),
_parent(parent),
_idleTime(200),
_stopped(false)
{
}
char PktAnalyzer::from_hex(char ch)
{
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}
std::string PktAnalyzer::url_decode(std::string text)
{
char h;
std::string escaped;
escaped.reserve(text.length());
for (auto i = text.begin(), n = text.end(); i != n; ++i)
{
std::string::value_type c = (*i);
if (c == '%')
{
if (i[1] && i[2])
{
h = from_hex(i[1]) << 4 | from_hex(i[2]);
if((h >= '0' && h <= '9') || ( h >= 'a' && h <= 'z') || ( h >= 'A' && h <= 'Z'))
{
escaped += h;
i += 2;
} else {
escaped += c;
}
}
} else {
escaped += c;
}
}
return escaped;
}
void PktAnalyzer::dump_file(unsigned char *buf,uint32_t rv, int pkt_id)
{
std::string s=std::to_string(pkt_id);
Poco::FileOutputStream pdump("/tmp/packet_dump"+s,std::ios::binary);
for(uint32_t i=0; i < rv; i++)
{
pdump << (unsigned char) buf[i];
}
pdump.close();
}
void PktAnalyzer::analyzer(Packet &pkt)
{
Poco::Stopwatch sw;
_logger.debug("Got packet from queue");
unsigned char *full_packet=pkt.get_payload();
int id = pkt.get_id();
uint32_t size = pkt.get_size();
struct nfq_q_handle *qh=pkt.get_qh();
struct ip *iph = (struct ip *)full_packet;
struct ip6_hdr *iph6 = (struct ip6_hdr *)full_packet;
// определяем версию протокола
int ip_version=0;
if(iph->ip_v == 6)
ip_version = 6;
else if (iph->ip_v == 4)
ip_version = 4;
if(!ip_version)
{
_logger.error("Unsupported IP protocol version %d for packet id %d",(int) iph->ip_v, id);
nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
dump_file(full_packet,size,id);
return ;
}
unsigned char *pkt_data_ptr = NULL;
struct tcphdr* tcph;
pkt_data_ptr = full_packet + (ip_version == 4 ? sizeof(struct ip) : sizeof(struct ip6_hdr));
tcph = (struct tcphdr *) pkt_data_ptr;
// длина ip заголовка
int iphlen = iphdr(full_packet)->ihl*4; // ipv4
if(ip_version == 6)
iphlen = sizeof(struct ip6_hdr);
// длина tcp заголовка
int tcphlen = tcphdr(full_packet+iphlen)->doff*4;
// общая длина всех заголовков
uint32_t hlen = iphlen + tcphlen;
_parent->inc_total_bytes_packets(size);
// пропускаем пакет без данных
if(hlen == size)
{
nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
return ;
}
int tcp_src_port=ntohs(tcph->source);
int tcp_dst_port=ntohs(tcph->dest);
std::unique_ptr<Poco::Net::IPAddress> src_ip;
std::unique_ptr<Poco::Net::IPAddress> dst_ip;
if(ip_version == 4)
{
src_ip.reset(new Poco::Net::IPAddress(&iph->ip_src,sizeof(in_addr)));
dst_ip.reset(new Poco::Net::IPAddress(&iph->ip_dst,sizeof(in_addr)));
} else {
src_ip.reset(new Poco::Net::IPAddress(&iph6->ip6_src,sizeof(in6_addr)));
dst_ip.reset(new Poco::Net::IPAddress(&iph6->ip6_dst,sizeof(in6_addr)));
}
uint8_t ip_protocol=(ip_version == 4 ? iph->ip_p : iph6->ip6_ctlun.ip6_un1.ip6_un1_nxt);
{
Poco::ScopedReadRWLock lock(nfqFilter::_ipportMapMutex);
IPPortMap::iterator it_ip=nfqFilter::_ipportMap->find(*dst_ip.get());
if(it_ip != nfqFilter::_ipportMap->end())
{
unsigned short port=tcp_dst_port;
if (it_ip->second.size() == 0 || it_ip->second.find(port) != it_ip->second.end())
{
_parent->inc_matched_ip_port();
if(_config.send_rst)
{
_logger.debug("HostList: Send RST to the client (%s) and server (%s) (packet no %d)",src_ip->toString(),dst_ip->toString(),id);
std::string empty_str;
SenderTask::queue.enqueueNotification(new RedirectNotification(tcp_src_port, tcp_dst_port,src_ip.get(), dst_ip.get(),/*acknum*/ tcph->ack_seq, /*seqnum*/ tcph->seq,/* flag psh */ (tcph->psh ? 1 : 0 ),empty_str,true));
_parent->inc_sended_rst();
nfq_set_verdict(qh,id,NF_DROP,0,NULL);
} else {
_logger.debug("HostList: Set mark %d to packet no %d port %hu",_config.mark_value,id,port);
_parent->inc_marked_hosts();
nfq_set_verdict2(qh,id,NF_ACCEPT,_config.mark_value,0,NULL);
}
return ;
}
}
}
// nDPI usage
sw.reset();
sw.start();
nDPIWrapper nw;
struct ndpi_flow_struct *flow=nw.get_flow();
uint32_t current_tickt = 0;
ndpi_protocol protocol = ndpi_detection_process_packet(nfqFilter::my_ndpi_struct, flow, full_packet, size, current_tickt, nw.get_src(), nw.get_dst());
if(protocol.protocol == NDPI_PROTOCOL_UNKNOWN)
{
_logger.debug("Guessing protocol...");
protocol = ndpi_guess_undetected_protocol(nfqFilter::my_ndpi_struct,
ip_protocol,
0,//ip
tcp_src_port, // sport
0,
tcp_dst_port); // dport
}
_logger.debug("Protocol is %hu/%hu ",protocol.master_protocol,protocol.protocol);
sw.stop();
_logger.debug("nDPI protocol detection occupied %ld us",sw.elapsed());
if(protocol.master_protocol == NDPI_PROTOCOL_SSL || protocol.protocol == NDPI_PROTOCOL_SSL || protocol.protocol == NDPI_PROTOCOL_TOR)
{
if(flow->l4.tcp.ssl_seen_client_cert == 1)
{
std::string ssl_client;
_logger.debug("Analysing SSL protocol");
if(flow->protos.ssl.client_certificate[0] != '\0')
{
ssl_client=flow->protos.ssl.client_certificate;
_logger.debug("SSL client is: %s",ssl_client);
}
if(!ssl_client.empty())
{
sw.reset();
sw.start();
if(_config.lower_host)
std::transform(ssl_client.begin(), ssl_client.end(), ssl_client.begin(), ::tolower);
AhoCorasickPlus::Match match;
std::size_t host_len=ssl_client.length();
bool found=false;
{
Poco::Mutex::ScopedLock lock(nfqFilter::_sslMutex);
nfqFilter::atm_ssl->search(ssl_client,false);
while(nfqFilter::atm_ssl->findNext(match) && !found)
{
if(match.pattern.length != host_len)
{
DomainsMatchType::Iterator it=nfqFilter::_SSLdomainsMatchType->find(match.id);
bool exact_match=false;
if(it != nfqFilter::_SSLdomainsMatchType->end())
exact_match = it->second;
if(exact_match)
continue;
if(ssl_client[host_len-match.pattern.length-1] != '.')
continue;
}
found=true;
}
}
sw.stop();
_logger.debug("SSL Host seek occupied %ld us, host: %s",sw.elapsed(),ssl_client);
if(found)
{
_parent->inc_matched_ssl();
if(_config.send_rst)
{
_logger.debug("SSLHostList: Send RST to the client (%s) and server (%s) (packet no %d)",src_ip->toString(),dst_ip->toString(),id);
std::string empty_str;
SenderTask::queue.enqueueNotification(new RedirectNotification(tcp_src_port, tcp_dst_port,src_ip.get(), dst_ip.get(),/*acknum*/ tcph->ack_seq, /*seqnum*/ tcph->seq,/* flag psh */ (tcph->psh ? 1 : 0 ),empty_str,true));
_parent->inc_sended_rst();
nfq_set_verdict(qh,id,NF_DROP,0,NULL);
} else {
_logger.debug("SSLHostList: Set mark %d to packet no %d, ssl host name: %s",_config.mark_value,id,ssl_client);
_parent->inc_marked_ssl();
nfq_set_verdict2(qh,id,NF_ACCEPT,_config.mark_value,0,NULL);
}
return ;
} else {
nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
return ;
}
} else {
if(_config.block_undetected_ssl)
{
Poco::ScopedReadRWLock lock(nfqFilter::_sslIpsSetMutex);
if(nfqFilter::_sslIps->try_search_exact_ip(*dst_ip.get()))
{
_parent->inc_matched_ssl_ip();
_logger.debug("Blocking/Marking SSL client hello packet from %s:%d to %s:%d", src_ip->toString(),tcp_src_port,dst_ip->toString(),tcp_dst_port);
if(_config.send_rst)
{
_logger.debug("SSLClientHello: Send RST to the client (%s) and server (%s) (packet no %d)",src_ip->toString(),dst_ip->toString(),id);
std::string empty_str;
SenderTask::queue.enqueueNotification(new RedirectNotification(tcp_src_port, tcp_dst_port,src_ip.get(), dst_ip.get(),/*acknum*/ tcph->ack_seq, /*seqnum*/ tcph->seq,/* flag psh */ (tcph->psh ? 1 : 0 ),empty_str,true));
_parent->inc_sended_rst();
nfq_set_verdict(qh,id,NF_DROP,0,NULL);
} else {
_logger.debug("SSLClientHello: Set mark %d to packet no %d",_config.mark_value,id);
_parent->inc_marked_ssl();
nfq_set_verdict2(qh,id,NF_ACCEPT,_config.mark_value,0,NULL);
}
return ;
}
}
_logger.debug("No ssl client certificate found! Accept packet from %s:%d to %s:%d.",src_ip->toString(),tcp_src_port,dst_ip->toString(),tcp_dst_port);
}
}
nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
return ;
}
if(protocol.master_protocol != NDPI_PROTOCOL_HTTP && protocol.protocol != NDPI_PROTOCOL_HTTP && protocol.protocol != NDPI_PROTOCOL_DIRECT_DOWNLOAD_LINK)
{
_logger.debug("Not http protocol. Protocol is %hu/%hu from %s:%d to %s:%d",protocol.master_protocol,protocol.protocol,src_ip->toString(),tcp_src_port,dst_ip->toString(),tcp_dst_port);
nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
return ;
}
_logger.debug("Got HTTP protocol");
std::string host((char *)&flow->host_server_name[0]);
if((flow->http.method == HTTP_METHOD_GET || flow->http.method == HTTP_METHOD_POST || flow->http.method == HTTP_METHOD_HEAD) && !host.empty())
{
int dot_del=0;
if(host[host.length()-1] == '.')
{
dot_del=host.length()-1;
host.erase(dot_del,1);
}
if(_config.lower_host)
std::transform(host.begin(), host.end(), host.begin(), ::tolower);
sw.reset();
sw.start();
AhoCorasickPlus::Match match;
bool found=false;
{
Poco::Mutex::ScopedLock lock(nfqFilter::_domainMapMutex);
nfqFilter::atm_domains->search(host,false);
std::size_t host_len=host.length();
while(nfqFilter::atm_domains->findNext(match) && !found)
{
if(match.pattern.length != host_len)
{
DomainsMatchType::Iterator it=nfqFilter::_domainsMatchType->find(match.id);
bool exact_match=false;
if(it != nfqFilter::_domainsMatchType->end())
exact_match = it->second;
if(exact_match)
continue;
if(host[host_len-match.pattern.length-1] != '.')
continue;
}
found=true;
}
}
sw.stop();
_logger.debug("Host seek occupied %ld us",sw.elapsed());
if(found)
{
_logger.debug("Host %s present in domain (file line %d) list from ip %s", host, match.id, src_ip->toString());
std::string add_param;
switch (_config.add_p_type)
{
case A_TYPE_ID: add_param="id="+std::to_string(match.id);
break;
case A_TYPE_URL: add_param="url="+host;
break;
default: break;
}
SenderTask::queue.enqueueNotification(new RedirectNotification(tcp_src_port, tcp_dst_port, src_ip.get(), dst_ip.get(),/*acknum*/ tcph->ack_seq, /*seqnum*/ tcph->seq,/* flag psh */ (tcph->psh ? 1 : 0 ),add_param));
_parent->inc_redirected_domains();
nfq_set_verdict(qh,id,NF_DROP,0,NULL);
return ;
}
sw.reset();
sw.start();
found=false;
std::string uri_o(flow->http.url ? flow->http.url : "");
if(flow->http.url)
{
std::string uri;
if(dot_del)
uri_o.erase(dot_del+7,1);
try
{
Poco::URI uri_p(uri_o);
uri_p.normalize();
uri.assign(uri_p.toString());
if(_config.url_decode)
{
#ifdef __USE_POCO_URI_DECODE
Poco::URI::decode(uri_p.toString(),uri);
#else
uri=url_decode(uri);
#endif
}
} catch (Poco::SyntaxException &ex)
{
_logger.debug("An SyntaxException occured: '%s' on URI: '%s'",ex.displayText(), uri_o);
uri.assign(flow->http.url);
}
{
Poco::Mutex::ScopedLock lock(nfqFilter::_urlMapMutex);
nfqFilter::atm->search(uri,false);
while(nfqFilter::atm->findNext(match) && !found)
{
if(_config.match_url_exactly && uri.length() != match.pattern.length)
continue;
found=true;
}
}
sw.stop();
_logger.debug("URL seek occupied %ld us for uri %s",sw.elapsed(),uri);
if(found)
{
_logger.debug("URL %s present in url (file pos %u) list from ip %s",uri,match.id,src_ip->toString());
std::string add_param;
switch (_config.add_p_type)
{
case A_TYPE_ID: add_param="id="+std::to_string(match.id);
break;
case A_TYPE_URL: add_param="url="+uri;
break;
default: break;
}
SenderTask::queue.enqueueNotification(new RedirectNotification(tcp_src_port, tcp_dst_port,src_ip.get(),dst_ip.get(),/*acknum*/ tcph->ack_seq, /*seqnum*/ tcph->seq,/* flag psh */ (tcph->psh ? 1 : 0 ),add_param));
_parent->inc_redirected_urls();
nfq_set_verdict(qh,id,NF_DROP,0,NULL);
return ;
}
}
}
nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
}
void PktAnalyzer::run()
{
_logger.debug("Starting thread...");
for (;;)
{
Poco::Notification::Ptr pNf(_queue.waitDequeueNotification(_idleTime));
if (pNf)
{
PktNotification::Ptr pPktNf = pNf.cast<PktNotification>();
if (pPktNf)
{
analyzer(pPktNf->pkt());
}
}
Poco::FastMutex::ScopedLock lock(_mutex);
if(_stopped)
{
break;
}
}
_logger.debug("Stopping thread...");
}
void PktAnalyzer::stop()
{
_stopped=true;
}