-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpcap_decode.py
More file actions
177 lines (166 loc) · 7.1 KB
/
pcap_decode.py
File metadata and controls
177 lines (166 loc) · 7.1 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
# coding:UTF-8
from scapy.all import *
from scapy.layers.inet import *
from scapy.layers.inet6 import *
from scapy.layers.l2 import Ether
class PcapDecode:
def __init__(self):
# ETHER:读取以太网层协议配置文件
with open('./protocol/ETHER', 'r', encoding='UTF-8') as f:
ethers = f.readlines()
self.ETHER_DICT = dict()
for ether in ethers:
ether = ether.strip().strip('\n').strip('\r').strip('\r\n')
self.ETHER_DICT[int(ether.split(':')[0])] = ether.split(':')[1]
# IP:读取IP层协议配置文件
with open('./protocol/IP', 'r', encoding='UTF-8') as f:
ips = f.readlines()
self.IP_DICT = dict()
for ip in ips:
ip = ip.strip().strip('\n').strip('\r').strip('\r\n')
self.IP_DICT[int(ip.split(':')[0])] = ip.split(':')[1]
# PORT:读取应用层协议端口配置文件
with open('./protocol/PORT', 'r', encoding='UTF-8') as f:
ports = f.readlines()
self.PORT_DICT = dict()
for port in ports:
port = port.strip().strip('\n').strip('\r').strip('\r\n')
self.PORT_DICT[int(port.split(':')[0])] = port.split(':')[1]
# TCP:读取TCP层协议配置文件
with open('./protocol/TCP', 'r', encoding='UTF-8') as f:
tcps = f.readlines()
self.TCP_DICT = dict()
for tcp in tcps:
tcp = tcp.strip().strip('\n').strip('\r').strip('\r\n')
self.TCP_DICT[int(tcp.split(':')[0])] = tcp.split(':')[1]
# UDP:读取UDP层协议配置文件
with open('./protocol/UDP', 'r', encoding='UTF-8') as f:
udps = f.readlines()
self.UDP_DICT = dict()
for udp in udps:
udp = udp.strip().strip('\n').strip('\r').strip('\r\n')
self.UDP_DICT[int(udp.split(':')[0])] = udp.split(':')[1]
# 解析以太网层协议
def ether_decode(self, p):
data = dict()
if p.haslayer(Ether):
data = self.ip_decode(p)
return data
else:
data['time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.time))
data['Source'] = 'Unknow'
data['Destination'] = 'Unknow'
data['Procotol'] = 'Unknow'
data['len'] = len(corrupt_bytes(p))
data['info'] = p.summary()
return data
# 解析IP层协议
def ip_decode(self, p):
data = dict()
if p.haslayer(IP): # 2048:Internet IP (IPv4)
ip = p.getlayer(IP)
if p.haslayer(TCP): # 6:TCP
data = self.tcp_decode(p, ip)
return data
elif p.haslayer(UDP): # 17:UDP
data = self.udp_decode(p, ip)
return data
else:
if ip.proto in self.IP_DICT:
data['time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.time))
data['Source'] = ip.src
data['Destination'] = ip.dst
data['Procotol'] = self.IP_DICT[ip.proto]
data['len'] = len(corrupt_bytes(p))
data['info'] = p.summary()
return data
else:
data['time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.time))
data['Source'] = ip.src
data['Destination'] = ip.dst
data['Procotol'] = 'IPv4'
data['len'] = len(corrupt_bytes(p))
data['info'] = p.summary()
return data
elif p.haslayer(IPv6): # 34525:IPv6
ipv6 = p.getlayer(IPv6)
if p.haslayer(TCP): # 6:TCP
data = self.tcp_decode(p, ipv6)
return data
elif p.haslayer(UDP): # 17:UDP
data = self.udp_decode(p, ipv6)
return data
else:
if ipv6.nh in self.IP_DICT:
data['time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.time))
data['Source'] = ipv6.src
data['Destination'] = ipv6.dst
data['Procotol'] = self.IP_DICT[ipv6.nh]
data['len'] = len(corrupt_bytes(p))
data['info'] = p.summary()
return data
else:
data['time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.time))
data['Source'] = ipv6.src
data['Destination'] = ipv6.dst
data['Procotol'] = 'IPv6'
data['len'] = len(corrupt_bytes(p))
data['info'] = p.summary()
return data
else:
if p.type in self.ETHER_DICT:
data['time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.time))
data['Source'] = p.src
data['Destination'] = p.dst
data['Procotol'] = self.ETHER_DICT[p.type]
data['len'] = len(corrupt_bytes(p))
data['info'] = p.summary()
return data
else:
data['time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.time))
data['Source'] = p.src
data['Destination'] = p.dst
data['Procotol'] = hex(p.type)
data['len'] = len(corrupt_bytes(p))
data['info'] = p.summary()
return data
# 解析TCP层协议
def tcp_decode(self, p, ip):
data = dict()
tcp = p.getlayer(TCP)
data['time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.time))
data['Source'] = ip.src + ":" + str(ip.sport)
data['Destination'] = ip.dst + ":" + str(ip.dport)
data['len'] = len(corrupt_bytes(p))
data['info'] = p.summary()
if tcp.dport in self.PORT_DICT:
data['Procotol'] = self.PORT_DICT[tcp.dport]
elif tcp.sport in self.PORT_DICT:
data['Procotol'] = self.PORT_DICT[tcp.sport]
elif tcp.dport in self.TCP_DICT:
data['Procotol'] = self.TCP_DICT[tcp.dport]
elif tcp.sport in self.TCP_DICT:
data['Procotol'] = self.TCP_DICT[tcp.sport]
else:
data['Procotol'] = "TCP"
return data
# 解析UDP层协议
def udp_decode(self, p, ip):
data = dict()
udp = p.getlayer(UDP)
data['time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.time))
data['Source'] = ip.src + ":" + str(ip.sport)
data['Destination'] = ip.dst + ":" + str(ip.dport)
data['len'] = len(corrupt_bytes(p))
data['info'] = p.summary()
if udp.dport in self.PORT_DICT:
data['Procotol'] = self.PORT_DICT[udp.dport]
elif udp.sport in self.PORT_DICT:
data['Procotol'] = self.PORT_DICT[udp.sport]
elif udp.dport in self.UDP_DICT:
data['Procotol'] = self.UDP_DICT[udp.dport]
elif udp.sport in self.UDP_DICT:
data['Procotol'] = self.UDP_DICT[udp.sport]
else:
data['Procotol'] = "UDP"
return data