-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcap_key_value.py
64 lines (53 loc) · 1.95 KB
/
pcap_key_value.py
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
#python 3.5.2
#将pcap文件转化为key-value文件,并进行保存
import numpy as np
from scapy.all import *
# 对文件夹进行遍历
def eachFile(filepath):
pathDir = os.listdir(filepath)
for allDir in pathDir:
child = os.path.join('%s/%s' % (filepath, allDir))
if os.path.isfile(child):
pcap_key_value(child)
continue
eachFile(child)
# pcap转化为key_value文件
def pcap_key_value(filename):
#loading the pcap file
x=rdpcap(filename)
path = filename.split('.')
w = open(str(path[0])+'.csv', 'w')
keys=['Ethernet_dst', 'Ethernet_src', 'Ethernet_type', 'IP_chksum', 'IP_dst',
'IP_flags', 'IP_frag', 'IP_id', 'IP_ihl', 'IP_len', 'IP_options',
'IP_proto', 'IP_src', 'IP_tos', 'IP_ttl', 'IP_version', 'TCP_ack',
'TCP_chksum', 'TCP_dataofs', 'TCP_dport', 'TCP_flags',
'TCP_reserved', 'TCP_seq', 'TCP_sport', 'TCP_urgptr', 'TCP_window']
for key in keys:
w.write(str(key)+',')
w.write('\n')
for pkt in x:
#packet layernumber
i = 0
while (pkt[i].name != pkt.lastlayer().name):
i += 1
i+=1
#change the dictionary with the keys
for m in range(i):
keylist = list(pkt[m].fields.keys())
for n in keylist:
pkt[m].fields[pkt[m].name + '_' + n] = pkt[m].fields.pop(n, None)
for k in range(i-1):
pkt[0].fields.update(pkt[k+1].fields)
pkt_info=pkt[0].fields
for key in keys:
if key not in pkt_info:
w.write(str(0)+',')
else:
w.write(str(pkt_info[key])+',')
w.write('\n')
w.close()
import csv
if __name__ == '__main__':
pcap_key_value('test_data/A/a+b.pcap')
x=[i for i in csv.reader(open('test_data/A/a+b.csv','r'))]
print(x)