forked from cutaway-security/ICSPcapViz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicsPcapViz.py
executable file
·339 lines (313 loc) · 11.9 KB
/
icsPcapViz.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
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
#!/usr/bin/env python3
import os,sys,re
import configparser
import pyshark
from py2neo import Graph, Node, Relationship
####################
# Globals
####################
MAJOR_VER = '0'
MINOR_VER = '2.0'
VERSION = '.'.join([MAJOR_VER,MINOR_VER])
SEPERATOR = "==================================="
DEBUG = False
INF = False
SERV1 = './ieee_services.ini'
SERV2 = './ics_services.ini'
PACKETS = False
NEOGRAPH = False
TCP = True
UDP = False
ICMP = False
ARP = False
NEO_PASSWD = 'admin'
####################
# FUNCTIONS
####################
def usage():
print("%s: %s"%(sys.argv[0],VERSION))
print("")
print("%s [-h] [-d] [-n int] [-l int] [-s int] [-m] [-M list] [-e] [-z] [-f <binary file>]"%(sys.argv[0]))
print(" -h: This is it.")
print(" -v: version info.")
print(" -d: Turn on debugging. Default: off")
print(" -f <pcap>: pcap file that contains the data. Required")
print(" -p: <passwd>: Neo4J password Default: admin. Yes, this will be in your shell history.")
print(" -t: Process TCP packets. Default: True")
print(" -u: Process UDP packets. Default: False")
print(" -i: Process ICMP packets. Default: False [NOT IMPLEMENTED]")
print(" -a: Process ARP packets. Default: False [NOT IMPLEMENTED]")
print("")
print("Be sure to start your Neo4J database. Read README for guidance.")
print("")
print("Processing PCAPs can take time. Be patient.")
print("Yes, you can write a progress bar and submit a pull request.")
sys.exit()
def version():
print("%s: %s"%(sys.argv[0],VERSION))
sys.exit()
def getKeyByVal(d, val):
keys = [k for k, v in d.items() if v == val]
if keys:
return keys[0]
return None
# Process TCP packets
def processTCP(inHosts,inData,inGraph):
# TCP Process each server application as s
for dst in list(inData.keys()):
if DEBUG: print('IP: %s Eth: %s'%(dst,getKeyByVal(inHosts,dst)))
s = Node("Host", name=str(dst))
s['ethaddr'] = str(getKeyByVal(inHosts,dst))
# Process each client talking to an application as c
for src in list(inData[dst].keys()):
c = Node("Host",name=str(src))
c['ethaddr'] = str(getKeyByVal(inHosts,src))
for conn in inData[dst][src]:
c['vlan'] = str(conn['vlan'])
if DEBUG: print('c[dvlan]: %s'%(c['vlan']))
if conn['vlan']:
SENDtcp = Relationship.type(str(conn['proto']) + " " + str(conn['dstport']) + "/TCP/VLAN:" + str(conn['vlan']))
else:
SENDtcp = Relationship.type(str(conn['proto']) + " " + str(conn['dstport']) + "/TCP")
# client is the source, so it goes first
inGraph.merge(SENDtcp(c, s), "Host", "name")
# Process UDP packets
def processUDP(inHosts,inData,inGraph):
# UDP Process each server application as s
for dst in list(inData.keys()):
s = Node("Host", name=str(dst))
#s['ethaddr'] = getKeyByVal(inHosts,dst)
# Process each client talking to an application as c
for src in list(inData[dst].keys()):
c = Node("Host",name=str(src))
#c['ethaddr'] = getKeyByVal(inHosts,src)
for conn in inData[dst][src]:
SENDudp = Relationship.type(str(conn['proto']) + " " + str(conn['dstport']) + "/UDP")
# client is the source, so it goes first
inGraph.merge(SENDudp(c, s), "Host", "name")
# Process ICMP packets
def processICMP():
print("%s: ICMP is not implemented.")
usage() # NOT IMPLEMENTED
# Process ARP packets
def processARP():
print("%s: ARP is not implemented.")
usage() # NOT IMPLEMENTED
if __name__ == "__main__":
ops = ['-h','-d','-f', '-p', '-t', '-u', '-i', '-a', '-v']
if len(sys.argv) < 2:
usage()
while len(sys.argv) > 1:
op = sys.argv.pop(1)
if op == '-h':
usage()
if op == '-v':
version()
if op == '-d':
DEBUG = True
if op == '-f':
INF = sys.argv.pop(1)
if op == '-p':
INF = sys.argv.pop(1)
if op == '-t':
TCP = False
if op == '-u':
UDP = True
if op == '-i':
ICMP = True
usage() # NOT IMPLEMENTED
if op == '-a':
ARP = True
usage() # NOT IMPLEMENTED
if op not in ops:
usage()
# Test PCAP File
if not INF:
usage()
try:
PACKETS = pyshark.FileCapture(INF)
except:
print("%s: Failed to open PCAP file: %s."%(sys.arv[0],INF))
usage()
# Use services configuration file: TCP, UDP
config = configparser.ConfigParser()
try:
config.read(SERV1)
except:
print("%s: Failed to open services configuration file: %s."%(sys.arv[0],SERV1))
try:
config.read(SERV2)
except:
print("%s: Failed to open services configuration file: %s."%(sys.arv[0],SERV2))
# Connect to Neo4J Database
try:
NEOGRAPH = Graph(password=NEO_PASSWD)
except:
print("%s: Failed to connect to Neo4J database."%(sys.arv[0]))
usage()
# Storage variables
host_addrs = {}
udp_conn_dict = {}
tcp_conn_dict = {}
# Process packets
for p in PACKETS:
# New storage for packet source data
tcp_src_dict = {}
udp_src_dict = {}
# Check for TCP layer or continue
if 'TCP Layer' in str(p.layers):
# Update host_addrs
host_keys = list(host_addrs.keys())
if p.eth.dst not in host_keys: host_addrs[p.eth.dst] = p.ip.dst
if p.eth.src not in host_keys: host_addrs[p.eth.src] = p.ip.src
# Prefer ports that are less than 1024 to identify service
srvport = 0
if str(p.tcp.dstport) in config['TCP']: # could cause false flow direction
srvport = p.tcp.dstport
srchost = p.ip.src
dsthost = p.ip.dst
dsteth = p.eth.dst
srceth = p.eth.src
elif str(p.tcp.srcport) in config['TCP']:
srvport = p.tcp.srcport
srchost = p.ip.dst
dsthost = p.ip.src
dsteth = p.eth.src
srceth = p.eth.dst
elif int(p.tcp.dstport) < 1024:
srvport = p.tcp.dstport
srchost = p.ip.src
dsthost = p.ip.dst
dsteth = p.eth.dst
srceth = p.eth.src
elif int(p.tcp.srcport) < 1024:
srvport = p.tcp.srcport
srchost = p.ip.dst
dsthost = p.ip.src
dsteth = p.eth.src
srceth = p.eth.dst
elif int(p.tcp.dstport) <= int(p.tcp.srcport): # if = then could be false direction
srvport = p.tcp.dstport
srchost = p.ip.src
dsthost = p.ip.dst
dsteth = p.eth.dst
srceth = p.eth.src
elif int(p.tcp.srcport) < int(p.tcp.dstport):
srvport = p.tcp.srcport
srchost = p.ip.dst
dsthost = p.ip.src
dsteth = p.eth.src
srceth = p.eth.dst
# If port number selected, find a service name
if srvport:
if str(srvport) in config['TCP']:
nameport = config['TCP'][str(srvport)]
else:
nameport = p.highest_layer
else:
if DEBUG: print("Packet not selected: %s"%(str(p.layers)))
continue
# Check VLAN tag
dvlan = ''
vid = ''
vtype = ''
if 'VLAN Layer' in str(p.layers):
vdata = p['VLAN']
if 'id' in list(vdata.field_names): vid = str(vdata.id)
if 'etype' in list(vdata.field_names): vtype = str(vdata.etype)
if vid:
dvlan = vid
if vtype: dvlan = dvlan + "/" + vtype
# Process packet
tcp_conn_keys = list(tcp_conn_dict.keys())
# Check for saved conns to destination
if srvport not in tcp_conn_keys:
tcp_conn_dict[dsthost] = {}
src_keys = list(tcp_conn_dict[dsthost].keys())
if srchost not in src_keys:
tcp_conn_dict[dsthost][srchost] = []
# Save source
tcp_src_dict = {'dstport':srvport,'proto':nameport,'vlan':dvlan}
tcp_conn_dict[dsthost][srchost].append(tcp_src_dict)
# Check for UDP layer or continue, check IP later to skip IPv6
if 'UDP Layer' in str(p.layers) and 'IP Layer' in str(p.layers):
# Update host_addrs
host_keys = list(host_addrs.keys())
if p.eth.dst not in host_keys: host_addrs[p.eth.dst] = p.ip.dst
if p.eth.src not in host_keys: host_addrs[p.eth.src] = p.ip.src
# Prefer ports that are less than 1024 to identify service
srvport = 0
if str(p.udp.dstport) in config['UDP']: # could cause false flow direction
srvport = p.udp.dstport
srchost = p.ip.src
dsthost = p.ip.dst
dsteth = p.eth.dst
srceth = p.eth.src
elif str(p.udp.srcport) in config['UDP']:
srvport = p.udp.srcport
srchost = p.ip.dst
dsthost = p.ip.src
dsteth = p.eth.src
srceth = p.eth.dst
elif int(p.udp.dstport) < 1024:
srvport = p.udp.dstport
srchost = p.ip.src
dsthost = p.ip.dst
dsteth = p.eth.dst
srceth = p.eth.src
elif int(p.udp.srcport) < 1024:
srvport = p.udp.srcport
srchost = p.ip.dst
dsthost = p.ip.src
dsteth = p.eth.src
srceth = p.eth.dst
elif int(p.udp.dstport) <= int(p.udp.srcport): # if = then could be false direction
srvport = p.udp.dstport
srchost = p.ip.src
dsthost = p.ip.dst
dsteth = p.eth.dst
srceth = p.eth.src
elif int(p.udp.srcport) < int(p.udp.dstport):
srvport = p.udp.srcport
srchost = p.ip.dst
dsthost = p.ip.src
dsteth = p.eth.src
srceth = p.eth.dst
# If port number selected, find a service name
if srvport:
if str(srvport) in config['UDP']:
nameport = config['UDP'][str(srvport)]
else:
nameport = p.highest_layer
else:
continue
# Check VLAN tag
dvlan = ''
vid = ''
vtype = ''
if 'VLAN Layer' in str(p.layers):
vdata = p['VLAN']
if 'id' in list(vdata.field_names): vid = str(vdata.id)
if 'etype' in list(vdata.field_names): vtype = str(vdata.etype)
if vid:
dvlan = vid
if vtype: dvlan = dvlan + "/" + vtype
# Process packet
udp_conn_keys = list(udp_conn_dict.keys())
# Check for saved conns to destination
if srvport not in udp_conn_keys:
udp_conn_dict[dsthost] = {}
src_keys = list(udp_conn_dict[dsthost].keys())
if srchost not in src_keys:
udp_conn_dict[dsthost][srchost] = []
# Save source
udp_src_dict = {'dstport':srvport,'proto':nameport,'vlan':dvlan}
udp_conn_dict[dsthost][srchost].append(udp_src_dict)
if TCP:
processTCP(host_addrs,tcp_conn_dict,NEOGRAPH)
if UDP:
processUDP(host_addrs,udp_conn_dict,NEOGRAPH)
if ICMP:
processICMP()
if ARP:
processARP()