-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.py
More file actions
748 lines (702 loc) · 30.3 KB
/
process.py
File metadata and controls
748 lines (702 loc) · 30.3 KB
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
import socket, struct, os, array
from scapy.all import *
#from scapy.all import ETH_P_ALL
#from scapy.all import select
#from scapy.all import MTU
import psutil
import time
import threading
from string import *
import binascii
import linecache
import psutil
import time
import multiprocessing
from cryptography import x509
from cryptography.hazmat.backends import default_backend
import base64
from OpenSSL import crypto
import sys
import os
start_time=time.time()
ipv4 = os.popen('ip addr show enp3s0 | grep "\<inet\>" | awk \'{ print $2 }\' | awk -F "/" \'{ print $1 }\'').read().strip()
vettore=['F','S','R','P','A','U']
open('/nf/access.log','w').close()
open('/nf/refuse.log','w').close()
class IPSniff:
def __init__(self, interface_name, on_ip_incoming, on_ip_outgoing):
self.interface_name = interface_name
self.on_ip_incoming = on_ip_incoming
self.on_ip_outgoing = on_ip_outgoing
# The raw in (listen) socket is a L2 raw socket that listens
# for all packets going through a specific interface.
self.ins = socket.socket(
socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**30)
self.ins.bind((self.interface_name, ETH_P_ALL))
def __process_ipframe(self, pkt_type, ip_header, payload):
# Extract the 20 bytes IP header, ignoring the IP options
fields = struct.unpack("!BBHHHBBHII", ip_header)
dummy_hdrlen = fields[0] & 0xf
iplen = fields[2]
ip_src = payload[12:16]
ip_dst = payload[16:20]
ip_frame = payload[0:14]
if pkt_type == socket.PACKET_OUTGOING:
if self.on_ip_outgoing is not None:
self.on_ip_outgoing(ip_src, ip_dst, ip_frame,payload)
else:
if self.on_ip_incoming is not None:
self.on_ip_incoming(ip_src, ip_dst, ip_frame,payload)
def recv(self):
while True:
pkt, sa_ll = self.ins.recvfrom(MTU)
if type == socket.PACKET_OUTGOING and self.on_ip_outgoing is None:
continue
elif self.on_ip_outgoing is None:
continue
if len(pkt) <= 0:
break
eth_header = struct.unpack("!6s6sH", pkt[0:14])
dummy_eth_protocol = socket.ntohs(eth_header[2])
if eth_header[2] != 0x800 :
continue
ip_header = pkt[14:34]
payload = pkt[14:]
self.__process_ipframe(sa_ll[2], ip_header, payload)
def PrintException():
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
def obtain_flag(p):
binary=str(bin(int(p)))
V=0
flag=''
for j in reversed(binary):
if j=='1':
flag=flag+vettore[V]
V=V+1
return flag
def control():
while True:
print psutil.cpu_percent(interval=1), psutil.phymem_usage().percent,(time.time()-start_time)
print count
print
time.sleep(10)
def pulizia_memoria():
while True:
j=0
try:
tempo=memoria[8]
except:
tempo=-1
while int(float(tempo))+20<(time.time()-start_time) and tempo!=-1:
#print memoria[j*9:j*9+4],memoria[j*9+6:j*9+9]
memoria[j*9:j*9+9]=[]
j=j+1
try:
tempo=memoria[8+j*9]
except:
tempo=-1
j=0
try:
tempo1=connection[7]
except:
tempo1=-1
while int(float(tempo1))+100<(time.time()-start_time) and tempo1!=-1:
x=0
while x<len(name_org):
try:
punt_namex=name_org.index(connection[j*8],x,len(name_org))
except:
punt_namex=-1
#f=open('/nf/access.log','a+')
control=0
try:
if punt_namex!=-1 and name_org[punt_namex+1]==connection[j*8+1] and name_org[punt_namex+2]==connection[j*8+2] and name_org[punt_namex+3]==connection[j*8+3]:
#f.write(str(connection[j*8:j*8+7]).strip('[]\'')+' '+'NF'+' '+str(name_org[punt_namex+4]).strip('[]\'')+' '+str(name_org[punt_namex+5]).strip('[]\'')+' '+'\n')
w.put([str(connection[j*8:j*8+7]).strip('[]\''),'NF',str(name_org[punt_namex+4]).strip('[]\''),str(name_org[punt_namex+5]).strip('[]\''),1])
x=len(name_org)
name_org[punt_namex:punt_namex+8]=[]
elif punt_namex!=-1 and name_org[punt_namex-1]==connection[j*8+1] and name_org[punt_namex+1]==connection[j*8+2] and name_org[punt_namex+2]==connection[j*8+3]:
#f.write(str(connection[j*8:j*8+7]).strip('[]\'')+' '+'NF'+' '+str(name_org[punt_namex+3]).strip('[]\'')+' '+str(name_org[punt_namex+4]).strip('[]\'')+' '+'\n')
w.put([str(connection[j*8:j*8+7]).strip('[]\''),'NF',str(name_org[punt_namex+3]).strip('[]\''),str(name_org[punt_namex+4]).strip('[]\''),1])
x=len(name_org)
name_org[punt_name-1:punt_name+7]=[]
else:
control=-1
except:
control=-1
if punt_namex!=-1 and control==-1:
x=punt_namex+5
else:
x=len(name_org)
#fr=open('/nf/refuse.log','a+')
#fr.write(str(connection[j*8])+' '+str(connection[j*8+1])+' '+str(connection[j*8+2])+' '+str(connection[j*8+3])+'\n')
w.put[str(connection[j*8]),str(connection[j*8+1]),str(connection[j*8+2]),str(connection[j*8+3]),2]
#fr.close
#f.close()
connection[j*8:j*8+8]=[]
j=j+1
try:
tempo1=connection[j*8+7]
except:
tempo1=-1
j=0
try:
tempo2=name_org[7]
except:
tempo2=-1
while int(float(tempo2))+10<(time.time()-start_time) and tempo2!=-1:
name_org[j*8:j*8+8]=[]
j=j+1
try:
tempo2=name_org[j*8+7]
except:
tempo2=-1
j=0
try:
tempo3=retrasmission[6]
except:
tempo3=-1
try:
while int(float(tempo3))+30<(time.time()-start_time) and tempo3!=-1:
j=j+1
retrasmission[j*7:j*7+7]=[]
try:
tempo3=restrasmission[j*7+6]
except:
tempo3=-1
except:
print 'Error tempo3'
time.sleep(5)
def hashing(ips,ipd,sp,dp):
list=ips.split('.',4)
x1=list[-1]
list=ipd.split('.',4)
x2=list[-1]
hash=int(x1)*100+int(x2)*100+int(sp)+int(dp)
if hash>=1668 and hash<30495:
return 'a'
elif hash>=30495 and hash<45322:
return 'b'
elif hash>=45322 and hash<59149:
return 'c'
elif hash>=59149 and hash<116978:
return 'd'
else:
print
print 'None',hash,ips,ipd,sp,dp
print
def find_certificate(pkt,name_org,retrasmission,memoria):
ips=pkt[0]
ipd=pkt[1]
sp=pkt[2]
dp=pkt[3]
stringa=pkt[4]
hx=hex(int(str(sp))).lstrip("0x")
inizio=stringa.index(str(hx),0,len(stringa))
posizione=inizio+len(str(hx))+20
lenght_TCP_hx=stringa[posizione]
lenght=int(str(lenght_TCP_hx),16)*4
posizione_data=lenght*2+inizio-1
lenght_p=len(stringa)-posizione_data
lenght_TLS=0
total_lenght=len(stringa)-posizione_data
seq_num_hx=stringa[inizio+7:inizio+15]
seq_num=int(seq_num_hx,16)
next_seq_num=int(seq_num+total_lenght/2)
next_seq_num_hx=hex(next_seq_num).lstrip("0x").zfill(8)
next_seq_num_hx=next_seq_num_hx[:8]
type=0
index=stringa.find('160303',posizione_data,len(stringa))
lenght_TLS_hx=stringa[index+6:index+10]
lenght_TLS=int(str(lenght_TLS_hx),16)*2
index_c=index+10
count=0
type=' '
marked=0
while index_c<len(stringa) and index!=-1:
type=stringa[index_c:index_c+2]
lenght_TLS_c=int(stringa[index_c+2:index_c+8],16)*2
if len(stringa)-index_c<lenght_TLS_c and type=='0b':
marked=1
lenght_p=len(stringa)-index_c
time_t=time.time()-start_time
memoria.extend([ips,sp,ipd,dp,lenght_TLS_c+8,stringa[index_c:],lenght_p,next_seq_num_hx,time_t])
index_c=len(stringa)
elif type=='0b':
certificate=stringa[index_c:index_c+8+lenght_TLS_c]
index_c=len(stringa)
try:
certificate=certificate[20:]
result='-----BEGIN CERTIFICATE-----\n'+base64.encodestring(binascii.unhexlify(certificate))+'-----END CERTIFICATE-----'
cert=x509.load_pem_x509_certificate(result,default_backend())
certs=crypto.load_certificate(crypto.FILETYPE_PEM,result)
certs2=certs.get_subject()
name=certs2.organizationName
l=0
try:
index_cert_org=name_org.index(ipd,0,len(name_org))
except:
index_cert_org=-1
while l<len(name_org) and index_cert_org!=-1:
if name_org[index_cert_org+1]==ips and name_org[index_cert_org+2]==dp and name_org[index_cert_org+3==sp]:
l=len(name_org)
name_org[index_cert_org+4]=name
name_org[index_cert_org+6]=time.time()-start_time
else:
l=index_cert_org+4
if l<len(name_org):
try:
index_cert_org=name_org.index(ipd,l,len(name_org))
except:
index_cert_org=-1
if index_cert_org==-1:
name_org.extend([ipd,ips,dp,sp,name,'0','0',(time.time()-start_time)])
except:
print certificate
elif type!='0b':
count=count+lenght_TLS_c+8
if lenght_TLS<=count:
index=stringa.find('160303',count,len(stringa))
index_c=index+10
lenght_TLS_hx=stringa[index+6:index+10]
try:
lenght_TLS=int(str(lenght_TLS_hx),16)*2
except:
index_c=len(stringa)
else:
index_c=index_c+8+lenght_TLS_c
try:
index_mem=memoria.index(ips,0,len(memoria))
except:
index_mem=-1
while index_mem!=-1:
if index_mem!=-1 and memoria[index_mem+1]==sp and memoria[index_mem+2]==ipd and memoria[index_mem+3]==dp:
var=memoria[index_mem+5]
if type!='0b':
marked=1
if str(seq_num_hx)==str(memoria[index_mem+7]):
try:
index_retr=retrasmission.index(ips,0,len(retrasmission))
except:
index_retr=-1
while index_retr!=-1:
if retrasmission[index_retr+1]==dp and retrasmission[index_retr+4]==next_seq_num_hx:
stringa=stringa+retrasmission[index_retr+5]
lenght_p=lenght_p+retrasmission[index_retr+2]
next_seq_num_hx=next_seq_num_hx+hex(int(retrasmission[index_retr+3]/2))
retrasmission[index_retr+6]=[]
index_retr=index_retr-5
try:
index_retr=retrasmission.index(ips,index_retr+5,len(retrasmission))
except:
index_retr=-1
if memoria[index_mem+4]-memoria[index_mem+6]<=len(stringa)-posizione_data:
payload=var+stringa[posizione_data:posizione_data+memoria[index_mem+4]-memoria[index_mem+6]]
certificate=payload
index_mem=-1
try:
certificate=certificate[20:]
result='-----BEGIN CERTIFICATE-----\n'+base64.encodestring(binascii.unhexlify(certificate))+'-----END CERTIFICATE-----'
cert=x509.load_pem_x509_certificate(result,default_backend())
certs=crypto.load_certificate(crypto.FILETYPE_PEM,result)
certs2=certs.get_subject()
name=certs2.organizationName
l=0
try:
index_cert_org=name_org.index(ipd,0,len(name_org))
except:
index_cert_org=-1
while l<len(name_org) and index_cert_org!=-1:
if name_org[index_cert_org+1]==ips and name_org[index_cert_org+2]==dp and name_org[index_cert_org+3==sp]:
l=len(name_org)
name_org[index_cert_org+4]=name
name_org[index_cert_org+6]=time.time()-start_time
else:
l=index_cert_org+4
if l<len(name_org):
try:
index_cert_org=name_org.index(ipd,l,len(name_org))
except:
index_cert_org=-1
if index_cert_org==-1:
name_org.extend([ipd,ips,dp,sp,name,'0','0',(time.time()-start_time)])
except:
print certificate
else:
payload=var+stringa[posizione_data:]
memoria[index_mem+7]=next_seq_num_hx
lenght_pN=memoria[index_mem+6]+lenght_p
memoria[index_mem+5]=payload
memoria[index_mem+6]=lenght_pN
memoria[index_mem+8]=time.time()-start_time
else:
retrasmission.extend([ips,dp,lenght_p,total_lenght,seq_num_hx,stringa[posizione_data:],(time.time()-start_time)])
if index_mem!=-1:
try:
index_mem=memoria.index(ips,index_mem+8,len(memoria))
except:
index_mem=-1
def find_e_name(pkt,name_org):
ips=pkt[0]
ipd=pkt[1]
sp=pkt[2]
dp=pkt[3]
stringa=pkt[4]
hx=hex(int(str(sp))).lstrip("0x")
inizio=stringa.index(str(hx),0,len(stringa))
posizione=inizio+len(str(hx))+20
lenght_TCP_hx=stringa[posizione]
lenght=int(str(lenght_TCP_hx),16)*4
posizione_data=lenght*2+inizio-1
lenght_p=len(stringa)-posizione_data
lenght_TLS=0
total_lenght=len(stringa)-posizione_data
index_ch=stringa.find('160301',posizione_data,len(stringa))
t=posizione_data
e_name='0'
while t<len(stringa) and index_ch!=-1:
tot_ch=int(stringa[index_ch+6:index_ch+10],16)
if stringa[index_ch+10:index_ch+12]=='01' and stringa[index_ch+18:index_ch+22]=='0303':
t=len(stringa)
len_sid=int(stringa[index_ch+86:index_ch+88],16)*2
len_cs=int(stringa[index_ch+len_sid+88:index_ch+len_sid+92],16)*2
len_cm=int(stringa[index_ch+len_sid+len_cs+92:index_ch+len_sid+len_cs+94],16)*2
accu=index_ch+len_sid+len_cs+94+len_cm
types=stringa[accu+4:accu+8]
if types=='0015':
padd=int(stringa[accu+8:accu+12],16)
else:
padd=0
if padd==0:
m=0
else:
m=8
while m<len(stringa):
type=stringa[accu+4+padd*2+m:accu+8+padd*2+m]
len_me=stringa[accu+8+padd*2+m:accu+12+padd*2+m]
if type=='0000':
len_server_name=int(stringa[accu+18+padd*2+m:accu+padd*2+m+22],16)
e_name=binascii.unhexlify(stringa[accu+22+padd*2+m:accu+22+padd*2+len_server_name*2+m])
m=len(stringa)
else:
try:
m=int(str(len_me),16)*2+m+8
except:
print len_me
sys.exit(0)
index_ch=-1
else:
t=index_ch+6
index_ch=stringa.find('160301',index_ch+tot_ch+10,len(stringa))
if e_name!='0':
l=0
try:
index_e_name=name_org.index(ips,0,len(name_org))
except:
index_e_name=-1
while l<len(name_org) and index_e_name!=-1:
if name_org[index_e_name+1]==ipd and name_org[index_e_name+2]==sp and name_org[index_e_name+3]==dp:
name_org[index_e_name+5]=e_name
#name_org[index_e_name+6]=time.time()-start_time
l=len(name_org)
else:
l=index_e_name+5
if l<len(name_org):
try:
index_e_name=name_org.index(ips,l,len(name_org))
except:
index_e_name=-1
if index_e_name==-1:
name_org.extend([ips,ipd,sp,dp,'0',e_name,(time.time()-start_time)])
def analize_packet(pkt,connection,name_org):
ips=pkt[0]
ipd=pkt[1]
sp=pkt[2]
dp=pkt[3]
flag=pkt[5]
lenght=pkt[6]
if flag.find('SA',0,len(flag))!=-1:
try:
puntatore=connection.index(ips,0,len(connection))
if connection[puntatore-1]==ipd and connection[puntatore+1]==dp and connection[puntatore+2]==sp:
connection[puntatore+4]=connection[puntatore+4]+lenght
connection[puntatore+3]=1
except:
pass
elif flag.find('S',0,len(flag))!=-1:
try:
puntatore=connection.index(ipd,0,len(connection))
except:
puntatore=-1
if puntatore!=-1 and connection[puntatore+1]==sp and connection[puntatore+2]==dp:
connection[puntatore+5]=int(connection[puntatore+5])+lenght
else:
connection.extend([ips,ipd,sp,dp,0,0,lenght,str(time.time()-start_time)])
elif flag.find('R',0,len(flag))!=-1:
var=0
while var<len(connection):
try:
puntatore=connection.index(ips,var,len(connection))
except:
puntatore=-1
if puntatore!=-1 and connection[puntatore+1]==ipd and connection[puntatore+2]==sp and connection[puntatore+3]==dp:
var=len(connection)
connection[puntatore+6]=connection[puntatore+6]+lenght
x=0
while x<len(name_org):
try:
punt_name=name_org.index(ips,x,len(name_org))
except:
punt_name=-1
#f=open('/nf/access.log','a+')
control=0
try:
if punt_name!=-1 and name_org[punt_name+1]==ipd and name_org[punt_name+2]==sp and name_org[punt_name+3]==dp:
#f.write(str(connection[puntatore:puntatore+7]).strip('[]\'')+' '+'R'+' '+str(name_org[punt_name+4]).strip('[]\'')+' '+str(name_org[punt_name+5]).strip('[]\'')+' '+'\n')
w.put([str(connection[puntatore:puntatore+7]).strip('[]\''),'R',str(name_org[punt_name+4]).strip('[]\''),str(name_org[punt_name+5]).strip('[]\''),1])
x=len(name_org)
name_org[punt_name:punt_name+8]=[]
else:
control=-1
except:
control=-1
if punt_name!=-1 and control==-1:
x=punt_name+4
else:
x=len(name_org)
#fr=open('/nf/refuse.log','a+')
#fr.write(str(ipd)+' '+str(ips)+' '+str(dp)+' '+str(sp)+'\n')
w.put([str(ipd),str(ips),str(dp),str(sp),2])
#fr.close
#f.close()
connection[puntatore:puntatore+8]=[]
elif puntatore!=-1 and connection[puntatore-1]==ipd and connection[puntatore+1]==dp and connection[puntatore+2]==sp:
var=len(connection)
connection[puntatore+5]=connection[puntatore+5]+lenght
x=0
#print 'R DOWN', ipd,ips,dp,sp
while x<len(name_org):
try:
punt_name=name_org.index(ips,x,len(name_org))
except:
punt_name=-1
#f=open('/nf/access.log','a+')
control=0
try:
if punt_name!=-1 and name_org[punt_name-1]==ipd and name_org[punt_name+1]==dp and name_org[punt_name+2]==sp:
#f.write(str(connection[puntatore-1:puntatore+6]).strip('[]\'')+' '+'R'+' '+str(name_org[punt_name+3]).strip('[]\'')+' '+str(name_org[punt_name+4]).strip('[]\'')+' '+'\n')
w.put([str(connection[puntatore-1:puntatore+6]).strip('[]\''),'R',str(name_org[punt_name+3]).strip('[]\''),str(name_org[punt_name+4]).strip('[]\''),1])
x=len(name_org)
name_org[punt_name-1:punt_name+7]=[]
else:
control=-1
except:
control=-1
if punt_name!=-1 and control==-1:
x=punt_name+3
else:
x=len(name_org)
#fr=open('/nf/refuse.log','a+')
#fr.write(str(ipd)+' '+str(ips)+' '+str(dp)+' '+str(sp)+'\n')
w.put([str(ipd),str(ips),str(dp),str(sp),2])
#fr.close
#f.close()
connection[puntatore-1:puntatore+7]=[]
else:
var=var+5
elif flag.find('F',0,len(flag))!=-1:
var=0
while var<len(connection):
try:
puntatore=connection.index(ips,var,len(connection))
except:
puntatore=-1
if puntatore!=-1 and connection[puntatore+1]==ipd and connection[puntatore+2]==sp and connection[puntatore+3]==dp:
var=len(connection)
connection[puntatore+6]=connection[puntatore+6]+lenght
if connection[puntatore+4]==3:
x=0
while x<len(name_org):
try:
punt_name=name_org.index(ips,x,len(name_org))
except:
punt_name=-1
#f=open('/nf/access.log','a+')
control=0
try:
if punt_name!=-1 and name_org[punt_name+1]==ipd and name_org[punt_name+2]==sp and name_org[punt_name+3]==dp:
#f.write(str(connection[puntatore:puntatore+7]).strip('[]\'')+' '+'F'+' '+str(name_org[punt_name+4]).strip('[]\'')+' '+str(name_org[punt_name+5]).strip('[]\'')+' '+'\n')
w.put([str(connection[puntatore:puntatore+7]).strip('[]\''),'F',str(name_org[punt_name+4]).strip('[]\''),str(name_org[punt_name+5]).strip('[]\''),1])
x=len(name_org)
name_org[punt_name:punt_name+8]=[]
else:
control=-1
except:
control=1
if punt_name!=-1 and control==-1:
x=punt_name+4
else:
x=len(name_org)
#fr=open('/nf/refuse.log','a+')
#fr.write(str(ips)+' '+str(ipd)+' '+str(sp)+' '+str(dp)+'\n')
w.put([str(ips),str(ipd),str(sp),str(dp),2])
#fr.close
#f.close()
connection[puntatore:puntatore+8]=[]
else:
connection[puntatore+4]=2
elif puntatore!=-1 and connection[puntatore-1]==ipd and connection[puntatore+1]==dp and connection[puntatore+2]==sp:
var=len(connection)
connection[puntatore+5]=connection[puntatore+5]+lenght
if connection[puntatore+3]==2:
x=0
while x<len(name_org):
try:
punt_name=name_org.index(ips,x,len(name_org))
except:
punt_name=-1
#f=open('/nf/access.log','a+')
control=0
try:
if punt_name!=-1 and name_org[punt_name-1]==ipd and name_org[punt_name+1]==dp and name_org[punt_name+2]==sp:
#f.write(str(connection[puntatore-1:puntatore+6]).strip('[]\'')+' '+'F'+' '+str(name_org[punt_name+3]).strip('[]\'')+' '+str(name_org[punt_name+4]).strip('[]\'')+' '+'\n')
w.put([str(connection[puntatore-1:puntatore+6]).strip('[]\''),'F',str(name_org[punt_name+3]).strip('[]\''),str(name_org[punt_name+4]).strip('[]\''),1])
x=len(name_org)
name_org[punt_name-1:punt_name+7]=[]
else:
control=-1
except:
control=-1
if punt_name!=-1 and control==-1:
x=punt_name+3
else:
x=len(name_org)
#fr=open('/nf/refuse.log','a+')
#fr.write(str(ipd)+' '+str(ips)+' '+str(dp)+' '+str(sp)+'\n')
w.put([str(ipd),str(ips),str(dp),str(sp),2])
#fr.close
#f.close()
connection[puntatore-1:puntatore+7]=[]
else:
connection[puntatore+3]=3
else:
var=var+7
else:
var=0
while var<len(connection):
try:
puntatore_1=connection.index(ips,var,len(connection))
except:
puntatore_1=-1
if puntatore_1!=-1 and connection[puntatore_1+1]==ipd and connection[puntatore_1+2]==sp and connection[puntatore_1+3]==dp:
connection[puntatore_1+6]=connection[puntatore_1+6]+lenght
connection[puntatore_1+7]=str(time.time()-start_time)
var=len(connection)
elif puntatore_1!=-1 and connection[puntatore_1-1]==ipd and connection[puntatore_1+1]==dp and connection[puntatore_1+2]==sp:
connection[puntatore_1+4]=connection[puntatore_1+4]+lenght
connection[puntatore_1+6]=str(time.time()-start_time)
var=len(connection)
else:
var=var+7
a=multiprocessing.Queue()
b=multiprocessing.Queue()
c=multiprocessing.Queue()
d=multiprocessing.Queue()
w=multiprocessing.Queue()
def process(x):
p=threading.Thread(target=pulizia_memoria)
p.start()
connection=[] #nei vettori in connection l'ultima cifra indica se la connessione si e' gia' instaurata(0 no, 1 si, 2 fin in una direzione
memoria=[]
name_org=[]
retrasmission=[]
while True:
if x=='a':
pkt=a.get()
elif x=='b':
pkt=b.get()
elif x=='c':
pkt=c.get()
elif x=='d':
pkt=d.get()
if pkt[2]==443:
find_certificate(pkt,name_org,retrasmission,memoria)
if pkt[3]==443:
find_e_name(pkt,name_org)
analize_packet(pkt,connection,name_org)
def write_process():
fr=open('/nf/refuse.log','a+')
f=open('/nf/access.log','a+')
while True:
dati=w.get(block=True)
if dati[4]==2:
fr.write(str(dati[0])+' '+str(dati[1])+' '+str(dati[2])+' '+str(dati[3])+'\n')
else:
f.write(str(dati[0]).strip('[]\'')+' '+str(dati[1]).strip('[]\'')+' '+str(dati[2]).strip('[]\'')+' '+str(dati[3]).strip('[]\'')+'\n')
fr.close()
f.close()
t=threading.Thread(target=control)
p1=multiprocessing.Process(target=process,args=('a',))
p2=multiprocessing.Process(target=process,args=('b',))
p3=multiprocessing.Process(target=process,args=('c',))
p4=multiprocessing.Process(target=process,args=('d',))
wp=multiprocessing.Process(target=write_process)
wp.start()
print wp.is_alive()
p1.start()
print p1.is_alive()
p2.start()
print p2.is_alive()
p3.start()
print p3.is_alive()
p4.start()
print p4.is_alive()
t.start()
print t.isAlive()
count=[0,0,0,0]
def create_process(ips,ipd,sp,dp,flag,stringa,lenght):
if sp==443 or dp==443:
x=hashing(ips,ipd,sp,dp)
if x=='a':
count[0]=count[0]+1
a.put([ips,ipd,sp,dp,stringa,flag,lenght])
elif x=='b':
count[1]=count[1]+1
b.put([ips,ipd,sp,dp,stringa,flag,lenght])
elif x=='c':
count[2]=count[2]+1
c.put([ips,ipd,sp,dp,stringa,flag,lenght])
elif x=='d':
count[3]=count[3]+1
d.put([ips,ipd,sp,dp,stringa,flag,lenght])
def test_incoming_callback(src, dst, frame,payload):
ips=socket.inet_ntoa(src)
ipd=socket.inet_ntoa(dst)
lenght=len(payload)
data=str(payload).encode("HEX")
lenght=int(str(data[1]),16)*4*2
sp=int(data[lenght:lenght+4],16)
dp=int(data[lenght+4:lenght+8],16)
p=int(data[lenght+12:lenght+13],16)
flag=obtain_flag(p)
create_process(ips,ipd,sp,dp,flag,data,lenght)
def test_outgoing_callback(src, dst, frame,payload):
ips=socket.inet_ntoa(src)
ipd=socket.inet_ntoa(dst)
lenght=len(payload)
data=str(payload).encode("HEX")
lenght=int(str(data[1]),16)*4*2
sp=int(data[lenght:lenght+4],16)
dp=int(data[lenght+4:lenght+8],16)
p=int(data[lenght+12:lenght+13],16)
flag=obtain_flag(p)
create_process(ips,ipd,sp,dp,flag,data,lenght)
ip_sniff = IPSniff('br0', test_incoming_callback, test_outgoing_callback).recv()
ip_sniff.recv()