-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_axonius_helpers.py
418 lines (367 loc) · 17 KB
/
custom_axonius_helpers.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import requests
from custom_aws_secrets_helper import get_secret
# Axonius Secrets:
AX_API_SECRET_NAME = "<AX_API_SECRET_NAME>" # Replace <AX_API_SECRET_NAME> with the name of the secret as showed in AWS Secrets Manager
AX_ADDITIONAL_HEADERS = {} # optional
def build_request():
"""
Fetches the secret and insert the API to the relevant headers.
Returns:
url: the URL stored in the secret for Axonius tenant.
headers: the required headers for sending the request to Axonius.
"""
try:
ax_secrets = get_secret(AX_API_SECRET_NAME)
url = ax_secrets.get("url")
headers = {
"api-key": ax_secrets.get("api_key"),
"api-secret": ax_secrets.get("api_secret"),
}
headers.update(AX_ADDITIONAL_HEADERS)
except Exception as error: # pylint: disable=broad-except
return {"ERROR": f"Failed to retreive secrets {error}"}
return url, headers
def find_username(username: str) -> dict:
"""
Gets a username/user email and return details fetched from Axonius.
Args:
username: username/user email of the desired user.
Returns:
Json with information about the user fetched from Axonius.
"""
try:
client = build_request()
if isinstance(client, dict):
return {"ERROR": f'Error getting secrets: {client.get("ERROR")}'}
url = client[0]
headers = client[1]
query = f'("specific_data.data.username" == regex("{username}", "i"))'
fields = [
"specific_data.data.display_name",
"specific_data.data.mail",
"specific_data.data.employee_id",
"specific_data.data.user_status",
"specific_data.data.user_manager",
"specific_data.data.user_title",
"specific_data.data.user_department",
"specific_data.data.user_country",
"specific_data.data.user_telephone_number",
"specific_data.data.user_created",
"specific_data.data.last_password_change",
"specific_data.data.last_logon",
"specific_data.data.associated_devices",
]
params = {"filter": query, "fields": {"users": fields}}
try:
res = requests.get(url=f"{url}/api/users", headers=headers, json=params, timeout=180)
except requests.exceptions.ConnectionError as error:
return {"ERROR": f"Error connecting to {url} - {error}"}
if res.status_code == 200:
res = res.json()
if res.get("data"):
res = res.get("data")[0].get("attributes")
out = {
"user_name": res.get("specific_data.data.display_name"), # list
"user_email": res.get("specific_data.data.mail"), # list
"employee_number": res.get("specific_data.data.employee_id"), # list
"user_status": res.get("specific_data.data.user_status"), # list
"employee_manager": res.get("specific_data.data.user_manager"), # list
"title": res.get("specific_data.data.user_title"), # list
"department": res.get("specific_data.data.user_department"), # list
"location": res.get("specific_data.data.user_country"), # list
"mobilePhone": res.get("specific_data.data.user_telephone_number"), # list
"user_creation_date": res.get("specific_data.data.user_created"), # list
"last_password_change": res.get(
"specific_data.data.last_password_change"
), # str
"last_logon": res.get("specific_data.data.last_logon"), # str
"associated_devices": res.get("specific_data.data.associated_devices"), # list
}
return out
return {"ERROR": f"{username} was not found."}
return {"ERROR": f"{url}/api/users response is {res.status_code} {res.content}"}
except Exception as error: # pylint: disable=broad-except
return {"ERROR": str(error)}
def find_hostname(hostname: str) -> dict:
"""
Gets a device name and return details fetched from Axonius.
Args:
hostname: name of the desired device.
Returns:
Json with information about the device fetched from Axonius.
"""
try:
client = build_request()
if isinstance(client, dict):
return {"ERROR": f'Error getting secrets: {client.get("ERROR")}'}
url = client[0]
headers = client[1]
query = f'("specific_data.data.name" == "{hostname}")'
fields = [
"specific_data.data.os.type_distribution_preferred",
"specific_data.data.serial_number_preferred",
"specific_data.data.last_seen",
"specific_data.data.public_ips",
"specific_data.data.users",
"specific_data.data.last_used_users_mail_association",
]
params = {"filter": query, "fields": {"devices": fields}}
try:
res = requests.get(url=f"{url}/api/devices", headers=headers, json=params, timeout=180)
except requests.exceptions.ConnectionError as error:
return {"ERROR": f"Error connecting to {url} - {error}"}
if res.status_code == 200:
res = res.json()
if res.get("data"):
res = res.get("data")[0].get("attributes")
out = {
"device_name": hostname, # str
"os": res.get("specific_data.data.os.type_distribution_preferred"), # str
"serial_number": res.get("specific_data.data.serial_number_preferred"), # str
"last_seen": res.get("specific_data.data.last_seen"), # str
"public_ips": res.get("specific_data.data.public_ips")
if res.get("specific_data.data.public_ips")
else res.get("specific_data.data.network_interfaces.ips"), # list
"users": res.get("specific_data.data.users")
if res.get("specific_data.data.users")
else res.get("specific_data.data.last_used_users_mail_association"), # list
}
return out
return {"ERROR": f"{hostname} was not found."}
return {"ERROR": f"{url}/api/devices response is {res.status_code} {res.content}"}
except Exception as error: # pylint: disable=broad-except
return {"ERROR": str(error)}
def find_ip(ip_address: str) -> dict: # pylint: disable=R1260,R0911
"""
Gets an IP address and return details fetched from Axonius.
Args:
ip_address: The desired IP address.
Returns:
Json with information about the IP address fetched from Axonius.
"""
try:
client = build_request()
if isinstance(client, dict):
return {"ERROR": f'Error getting secrets: {client.get("ERROR")}'}
url = client[0]
headers = client[1]
query = f'("specific_data.data.last_known_ips" == regex("{ip_address}", "i"))'
fields = [
"specific_data.data.username",
"specific_data.data.last_seen",
]
params = {"filter": query, "fields": {"users": fields}}
try:
res = requests.get(url=f"{url}/api/users", headers=headers, json=params, timeout=180)
except requests.exceptions.ConnectionError as error:
return {"ERROR": f"Error connecting to {url} - {error}"}
if res.status_code == 200:
res = res.json()
if res.get("data"):
res = res.get("data")
ip_users = []
for record in res:
ip_users.append(record.get("attributes").get("specific_data.data.username"))
res = res[0].get("attributes")
out = {
"ip_address": ip_address,
"office_ip_address": len(ip_users) >= 10,
"last_seen": res.get("specific_data.data.last_seen"),
"users_count": len(ip_users),
"users": ip_users,
}
return out
query = f'("specific_data.data.public_ips" == regex("{ip_address}", "i"))'
fields = [
"specific_data.data.last_seen",
"specific_data.data.last_used_users_mail_association",
]
params = {"filter": query, "fields": {"devices": fields}}
try:
res = requests.get(url=f"{url}/api/devices", headers=headers, json=params, timeout=180)
except requests.exceptions.ConnectionError as error:
return {"ERROR": f"Error connecting to {url} - {error}"}
if res.status_code == 200:
res = res.json()
if res.get("data"):
res = res.get("data")
ip_users = []
for record in res:
ip_users.append(
record.get("attributes").get(
"specific_data.data.last_used_users_mail_association"
)
)
res = res[0].get("attributes")
out = {
"ip_address": ip_address,
"office_ip_address": len(ip_users) >= 10,
"last_seen:": res.get("specific_data.data.last_seen"),
"users_count": len(ip_users),
"users": ip_users,
}
return out
return {"ERROR": f"{ip_address} was not found."}
return {"ERROR": f"{url}/api/users response is {res.status_code} {res.content}"}
except Exception as error: # pylint: disable=broad-except
return {"ERROR": str(error)}
def find_cloud_id(cloud_id: str) -> dict:
"""
Gets a cloud ID and return details fetched from Axonius.
Args:
cloud_id: The ID of the desired cloud instance.
Returns:
Json with information about the cloud instance address fetched from Axonius.
"""
try:
client = build_request()
if isinstance(client, dict):
return {"ERROR": f'Error getting secrets: {client.get("ERROR")}'}
url = client[0]
headers = client[1]
query = f'("specific_data.data.cloud_id" == "{cloud_id}")'
fields = [
"specific_data.data.name",
"specific_data.data.cloud_id.cloud_id",
"specific_data.data.cloud_provider_account_id",
"specific_data.data.cloud_provider_account_name",
"specific_data.data.network_interfaces.ips",
"specific_data.data.os.type_distribution_preferred",
"specific_data.data.last_seen",
"specific_data.data.power_state",
]
params = {"filter": query, "fields": {"devices": fields}}
try:
res = requests.get(url=f"{url}/api/devices", headers=headers, json=params, timeout=180)
except requests.exceptions.ConnectionError as error:
return {"ERROR": f"Error connecting to {url} - {error}"}
if res.status_code == 200:
res = res.json()
if res.get("data"):
res = res.get("data")[0].get("attributes")
out = {
"cloud_id": cloud_id,
"instance_name": res.get("specific_data.data.name"),
"account_id": res.get("specific_data.data.cloud_provider_account_id"),
"account_name": res.get("specific_data.data.cloud_provider_account_name"),
"ip_addresses": res.get("specific_data.data.network_interfaces.ips"),
"os": res.get("specific_data.data.os.type_distribution_preferred"),
"last_seen": res.get("specific_data.data.last_seen"),
"status": res.get("specific_data.data.power_state"),
}
return out
return {"ERROR": f"{cloud_id} was not found"}
return {"ERROR": f"{url}/api/devices response is {res.status_code} {res.content}"}
except Exception as error: # pylint: disable=broad-except
return {"ERROR": str(error)}
def user_ip_association(ip_address: str, username: str) -> bool:
"""
Checks if a given user is related to a given IP address.
Args:
ip_address: An IP address of a user.
username: A username/user email.
Returns:
True of this IP address is related to the user else it returns False.
"""
ax_ip = find_ip(ip_address)
if not ax_ip.get("ERROR"):
for sublist in ax_ip.get("users"):
if isinstance(sublist, list):
for _sub_item in sublist:
if username in _sub_item:
return True
else:
if username in sublist:
return True
return False
def get_hostname_vulnerabilities(hostname: str) -> dict:
"""
Gets a device name and return all existing vulnerabilities on this device fetched from Axonius.
Args:
hostname: name of the desired device.
Returns:
Json with information about the device's vulnerabilities fetched from Axonius.
"""
try:
client = build_request()
if isinstance(client, dict):
return {"ERROR": f'Error getting secrets: {client.get("ERROR")}'}
url = client[0]
headers = client[1]
query = f'("specific_data.data.name" == "{hostname}")'
fields = [
"specific_data.data.cisa_vulnerabilities",
"specific_data.data.software_cves",
]
params = {"filter": query, "fields": {"devices": fields}}
try:
res = requests.get(url=f"{url}/api/devices", headers=headers, json=params, timeout=180)
except requests.exceptions.ConnectionError as error:
return {"ERROR": f"Error connecting to {url} - {error}"}
if res.status_code == 200:
res = res.json()
if res.get("data"):
res = res.get("data")[0].get("attributes")
out = {
"cisa_vulnerabilities": res.get(
"specific_data.data.cisa_vulnerabilities"
), # list
"software_vulnerabilities": res.get("specific_data.data.software_cves"), # list
}
return out
return {"ERROR": f"{hostname} was not found."}
return {"ERROR": f"{url}/api/devices response is {res.status_code} {res.content}"}
except Exception as error: # pylint: disable=broad-except
return {"ERROR": str(error)}
def find_cs_aid(aid: str) -> dict:
"""
Translate Crowdstrike AID to a device name.
Args:
aid: Crowdstrike AID of the desired device.
Returns:
Json with details about the device found based on the provided CS AID.
"""
try:
client = build_request()
if isinstance(client, dict):
return {"ERROR": f'Error getting secrets: {client.get("ERROR")}'}
url = client[0]
headers = client[1]
query = f'("adapters_data.crowd_strike_adapter.device_id" == "{aid}")'
fields = [
"specific_data.data.os.type_distribution_preferred",
"specific_data.data.serial_number_preferred",
"specific_data.data.last_seen",
"specific_data.data.public_ips",
"specific_data.data.users",
"specific_data.data.last_used_users_mail_association",
"adapters_data.crowd_strike_adapter.external_ip",
"adapters_data.crowd_strike_adapter.hostname",
]
params = {"filter": query, "fields": {"devices": fields}}
try:
res = requests.get(url=f"{url}/api/devices", headers=headers, json=params, timeout=180)
if res.status_code == 200:
res = res.json()
if res.get("data"):
res = res.get("data")[0].get("attributes")
out = {
"hostname": res.get("adapters_data.crowd_strike_adapter.hostname"),
"cs_device_aid": aid, # str
"os": res.get("specific_data.data.os.type_distribution_preferred"), # str
"serial_number": res.get(
"specific_data.data.serial_number_preferred"
), # str
"last_seen": res.get("specific_data.data.last_seen"), # str
"external_ip": res.get("adapters_data.crowd_strike_adapter.external_ip"),
"users": res.get("specific_data.data.users")
if res.get("specific_data.data.users")
else res.get("specific_data.data.last_used_users_mail_association"), # list
}
return out
return {"ERROR": f"{aid} was not found"}
return {"ERROR": f"{url}/api/devices response is {res.status_code} {res.content}"}
except requests.exceptions.ConnectionError as error:
return {"ERROR": f"Error connecting to {url} - {error}"}
except Exception as error: # pylint: disable=broad-except
return {"ERROR": str(error)}