-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_collection.py
159 lines (134 loc) · 6.34 KB
/
data_collection.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
from zulu_time import ZuluTime
class DataCollection():
def __init__(self):
self.collect = {}
def get_local_networks(self):
networks = list(self.collect['local_networks'].keys()) if 'local_networks' in self.collect else []
return networks
def get_local_network_devices(self, network_id):
#make a copy so we don't iterate over data as it is being collected
data = self.collect.copy()
time_stamp = ZuluTime.get_timestamp()
if network_id in data['local_networks']:
devices = data['local_networks'][network_id]['devices'].items()
device_list = sorted([
[x[1]['ip_address'],
x[1]['mac_address'],
x[1]['manufacturer'],
x[1]['host_name'],
'Yes' if x[1]['is_you'] else 'No',
self._get_time_diff(time_stamp, x[1]['last_seen'])
] for x in devices], key=lambda x: self.ip_to_int(x[0]))
return device_list
else:
return []
def get_local_device_tcp_ports(self, network_id, mac_address):
try:
device = self.collect['local_networks'][network_id]['devices'][mac_address]
tcp_ports = sorted([x for x in device['open_tcp_ports'] if x is not None])
tcp_ports_string = ', '.join(str(x) for x in tcp_ports)
return tcp_ports_string
except:
return 'tcp collection error'
def get_local_device_udp_ports(self, network_id, mac_address):
try:
device = self.collect['local_networks'][network_id]['devices'][mac_address]
udp_ports = sorted([x for x in device['open_udp_ports'] if x is not None])
udp_ports_string = ', '.join(str(x) for x in udp_ports)
return udp_ports_string
except:
return 'udp collection error'
def get_local_device_protocols(self, network_id, mac_address):
try:
device = self.collect['local_networks'][network_id]['devices'][mac_address]
protocols = ', '.join(sorted([str(x) for x in device['protocols_seen']]))
return protocols
except Exception as ex:
return 'protocol collection error'
def get_local_device_data(self, network_id, mac_address):
try:
device = self.collect['local_networks'][network_id]['devices'][mac_address]
data_collect = '; '.join(device['data_collect'])
return data_collect
except:
return ''
def get_local_device_summary(self, network_id, mac_address):
protocols = self.get_local_device_protocols(network_id, mac_address)
tcp_ports = self.get_local_device_tcp_ports(network_id, mac_address)
udp_ports = self.get_local_device_udp_ports(network_id, mac_address)
device_data = self.get_local_device_data(network_id, mac_address)
summary = f'Protocols: {protocols}\nTCP Ports: {tcp_ports}\nUDP Ports: {udp_ports}\nDevice Data: {device_data}'
return summary
def get_external_devices(self):
#make a copy so we don't iterate over data as it is being collected
data = self.collect.copy()
time_stamp = ZuluTime.get_timestamp()
devices = data['external_devices'].items()
device_list = sorted([
[x[1]['ip_address'],
x[1]['mac_address'],
x[1]['manufacturer'],
x[1]['host_name'],
'Yes' if x[1]['is_you'] else 'No',
self._get_time_diff(time_stamp, x[1]['last_seen'])
] for x in devices], key=lambda x: self.ip_to_int(x[0]))
return device_list
def get_external_device_tcp_ports(self, ip_address):
try:
device = self.collect['external_devices'][ip_address]
tcp_ports = sorted([x for x in device['open_tcp_ports'] if x is not None])
tcp_ports_string = ', '.join(str(x) for x in tcp_ports)
return tcp_ports_string
except:
return 'tcp collection error'
def get_external_device_udp_ports(self, ip_address):
try:
device = self.collect['external_devices'][ip_address]
udp_ports = sorted([x for x in device['open_udp_ports'] if x is not None])
udp_ports_string = ', '.join(str(x) for x in udp_ports)
return udp_ports_string
except:
return 'udp collection error'
def get_external_device_protocols(self, ip_address):
try:
device = self.collect['external_devices'][ip_address]
protocols = ', '.join(sorted([str(x) for x in device['protocols_seen']]))
return protocols
except Exception as ex:
return 'protocol collection error'
def get_external_device_data(self, ip_address):
try:
device = self.collect['external_devices'][ip_address]
data_collect = '; '.join(device['data_collect'])
return data_collect
except:
return ''
def get_external_device_summary(self, ip_address):
protocols = self.get_external_device_protocols(ip_address)
tcp_ports = self.get_external_device_tcp_ports(ip_address)
udp_ports = self.get_external_device_udp_ports(ip_address)
device_data = self.get_external_device_data(ip_address)
summary = f'Protocols: {protocols}\nTCP Ports: {tcp_ports}\nUDP Ports: {udp_ports}\nDevice Data: {device_data}'
return summary
@staticmethod
def _get_time_diff(start_time, stop_time):
try:
time_diff = str(start_time - stop_time).split('.')[0]
val = int(str(time_diff.replace(':', '')))
if val < 100:
return str(time_diff).split(':')[-1] + 's'
elif val < 10000:
return str(time_diff).split(':')[-2] + 'm'
else:
return str(time_diff).split(':')[-3] + 'h'
except Exception as ex:
return '????'
@staticmethod
#https://stackoverflow.com/questions/5619685/conversion-from-ip-string-to-integer-and-backward-in-python
def ip_to_int(ip):
if ip is not None:
o = list(map(int, ip.split('.')))
ip_int = (16777216 * o[0]) + (65536 * o[1]) + (256 * o[2]) + o[3]
return ip_int
else:
return 0