-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurityinsights.py
More file actions
233 lines (165 loc) · 8.57 KB
/
securityinsights.py
File metadata and controls
233 lines (165 loc) · 8.57 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
import requests
import xlsxwriter
from pathlib import Path
from datetime import datetime
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from requests import get
center_port = 443
center_base_url = "api/3.0"
def get(center_token, center_ip):
try:
headers = { "x-token-id": center_token }
print("Fetching preset id data...")
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/presets",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
#raw JSON data response
presets = r_get.json()
id = 0
for preset in presets:
if preset['label'] == "All data":
id = preset['id']
print("Fetching DNS data...")
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/presets/{id}/visualisations/dashboard-security/dns/list",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
dns = r_get.json()
print("" + str(len(dns)) + " DNS requests found")
print("Fetching HTTP data...")
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/presets/{id}/visualisations/dashboard-security/http/list",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
http = r_get.json()
for request in http:
loc = get(f'https://ipapi.co/{request["label"]}/json/')
request["name"] = loc["org"]
print("" + str(len(http)) + " HTTP requests found")
print("Fetching SMB data...")
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/presets/{id}/visualisations/dashboard-security/smb/list",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
smb = r_get.json()
print("" + str(len(smb)) + " Server Message Block (SMB) Trees found")
print("Checking Cyber Vision version...")
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/version",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
version = r_get.json()
external = {"devices": [], "components": []}
if version["major"] == 4 and version["minor"] == 3:
print("Version is at least 4.3.x")
print("Fetching device ids...")
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/devices",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
devices = r_get.json()
print("Fetching component ids...")
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/components",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
components = r_get.json()
print("Fetching external communications for devices...")
for device in devices:
if device["externalCommunicationsCount"] > 0:
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/devices/{device['id']}/externalCommunications",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
temp = r_get.json()
device_data = {'label': device['label'], 'timestamp': 0, 'count': 0, 'ip': temp[0]['sourceIP'], 'mac': device['mac'], 'volume': 0}
for activity in temp:
if activity['lastSeen'] > device_data['timestamp']:
device_data['timestamp'] = activity['lastSeen']
device_data['count'] += 1
device_data['volume'] += activity['sentByDevice']
external['devices'].append(device_data)
print("Fetching external communications for components...")
for component in components:
if component["externalCommunicationsCount"] > 0:
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/components/{component['id']}/externalCommunications",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
temp = r_get.json()
component_data = {'label': component['label'], 'timestamp': 0, 'count': 0, 'ip': temp[0]['sourceIP'], 'mac': component['mac'], 'volume': 0}
for activity in temp:
if activity['lastSeen'] > component_data['timestamp']:
component_data['timestamp'] = activity['lastSeen']
component_data['count'] += 1
component_data['volume'] += activity['sentByDevice']
external['components'].append(component_data)
return [dns, http, smb, external]
except Exception as e:
return f"Error when connecting: {e}"
def get_security_insights(center_token, center_ip):
data = get(center_token, center_ip)
print("Finished fetching API data")
now = datetime.now()
current_time = now.strftime("%m-%d-%Y_%H-%M-%S")
file_name = "Cyber Vision Security Insights_" + current_time + ".xlsx"
workbook = xlsxwriter.Workbook(file_name)
print("Writing DNS data to Excel")
sheet1 = workbook.add_worksheet("DNS")
sheet1.write(0, 0, 'DNS')
sheet1.write(0, 1, 'Request Count')
sheet1.write(0, 2, 'Component Count')
index = 1
for dns in data[0]:
sheet1.write(index, 0, dns['label'])
sheet1.write(index, 1, dns['value'])
sheet1.write(index, 2, dns['componentsCount'])
index += 1
print("Writing HTTP data to Excel")
sheet2 = workbook.add_worksheet("HTTP")
sheet2.write(0, 0, 'HTTP')
sheet2.write(0, 1, 'Request Count')
sheet2.write(0, 2, 'Component Count')
sheet2.write(0, 3, 'Name')
index = 1
for http in data[1]:
sheet2.write(index, 0, http['label'])
sheet2.write(index, 1, http['value'])
sheet2.write(index, 2, http['componentsCount'])
sheet2.write(index, 3, http['name'])
index += 1
print("Writing SMB data to Excel")
sheet3 = workbook.add_worksheet("SMB")
sheet3.write(0, 0, 'SMB')
sheet3.write(0, 1, 'Request Count')
sheet3.write(0, 2, 'Component Count')
index = 1
for smb in data[2]:
sheet3.write(index, 0, smb['label'])
sheet3.write(index, 1, smb['value'])
sheet3.write(index, 2, smb['componentsCount'])
index += 1
print("Writing External Communication data to Excel")
sheet4 = workbook.add_worksheet("External Communication")
index = 1
if len(data[3]["devices"]) > 0 or len(data[3]["components"]) > 0:
if len(data[3]['devices']) > 0:
sheet4.write(0, 0, 'Device')
sheet4.write(0, 1, 'IP')
sheet4.write(0, 2, 'MAC')
sheet4.write(0, 3, 'Count')
sheet4.write(0, 4, "Last Activity")
sheet4.write(0, 5, "Data Volume")
for device in data[3]['devices']:
sheet4.write(index, 0, device['label'])
sheet4.write(index, 1, device['ip'])
sheet4.write(index, 2, device['mac'])
sheet4.write(index, 3, device['count'])
sheet4.write(index, 4, datetime.fromtimestamp(device['timestamp']//1000).strftime("%b %d, %Y %I:%M:%S %p"))
sheet4.write(index, 5, device['volume'])
index += 1
index += 1
else:
index = 0
if len(data[3]["components"]) > 0:
sheet4.write(index, 0, 'Component')
sheet4.write(index, 1, 'IP')
sheet4.write(index, 2, 'MAC')
sheet4.write(index, 3, 'Count')
sheet4.write(index, 4, "Last Activity")
sheet4.write(index, 5, "Data Volume")
index += 1
for component in data[3]["components"]:
sheet4.write(index, 0, component['label'])
sheet4.write(index, 1, component['ip'])
sheet4.write(index, 2, component['mac'])
sheet4.write(index, 3, component['count'])
sheet4.write(index, 4, datetime.fromtimestamp(component['timestamp']//1000).strftime("%b %d, %Y %I:%M:%S %p"))
sheet4.write(index, 5, component['volume'])
index += 1
workbook.close()
print("Security Insight Report in this directory: " + str(Path.cwd()))