-
Notifications
You must be signed in to change notification settings - Fork 77
/
d4iot_dumper.py
229 lines (186 loc) · 10.6 KB
/
d4iot_dumper.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Untitled Goose Tool: d4iot_dumper!
This module has all the telemetry pulls for Defender for IoT.
"""
import getpass
import json
import os
from goosey.datadumper import DataDumper
from goosey.utils import *
__author__ = "Claire Casalnova, Jordan Eberst, Wellington Lee, Victoria Wallace"
__version__ = "1.2.5"
class DefenderIoTDumper(DataDumper):
def __init__(self, output_dir, reports_dir, session, csrftoken, sessionid, config, auth_un_pw, debug):
super().__init__(f'{output_dir}{os.path.sep}d4iot', reports_dir, csrftoken, sessionid, session, debug)
self.logger = setup_logger(__name__, debug)
if auth_un_pw is not None:
if auth_un_pw['auth']['d4iot_sensor_token']:
self.sensor_token = auth_un_pw['auth']['d4iot_sensor_token']
else:
self.sensor_token = getpass.getpass("Please type your D4IOT sensor token. If you don't have a D4IOT sensor, you can leave it blank (hit Enter to continue): ")
if auth_un_pw['auth']['d4iot_mgmt_token']:
self.mgmt_token = auth_un_pw['auth']['d4iot_mgmt_token']
else:
self.mgmt_token = getpass.getpass("Please type your D4IOT management console token. If you don't have a D4IOT management console, you can leave it blank (hit Enter to continue): ")
else:
self.sensor_token = getpass.getpass("Please type your D4IOT sensor token. If you don't have a D4IOT sensor, you can leave it blank (hit Enter to continue): ")
self.mgmt_token = getpass.getpass("Please type your D4IOT management console token. If you don't have a D4IOT management console, you can leave it blank (hit Enter to continue): ")
self.sensor_ip = config_get(config, 'config', 'd4iot_sensor_ip', self.logger)
self.mgmt_ip = config_get(config, 'config', 'd4iot_mgmt_ip', self.logger)
self.csrftoken = csrftoken
self.sessionid = sessionid
async def helper_multiple_object_sensor(self, parent, child, identifier='id'):
if not self.csrftoken or not self.sessionid:
self.logger.error(f"Missing csrftoken and sessionid from auth. Did you auth correctly?")
return
base_url = "https://" + str(self.sensor_ip)
parent_url = base_url + parent
parent_header = {'Authorization': '%s' % (self.sensor_token)}
self.logger.info('Dumping alert information...')
parent_list = []
async with self.ahsession.request('GET', parent_url, headers=parent_header, ssl=False) as r:
result = await r.json()
if not result:
self.logger.debug("Error with result. Please check your auth: {}".format(str(result)))
return
for entry in result:
parent_list.append(entry[identifier])
child_header = {"Cookie": "csrftoken=" + self.csrftoken + "; sessionid=" + self.sessionid}
outpath = os.path.join(self.output_dir, 'sensor_alert_pcaps')
check_output_dir(outpath, self.logger)
for id in parent_list:
child_url = base_url + child + str(id)
outfile = os.path.join(outpath, "alert_" + str(id) + ".pcap")
async with self.ahsession.request('GET', child_url, headers=child_header, ssl=False, allow_redirects=True) as r:
output = await r.read()
with open(outfile, 'wb') as f:
f.write(output)
async def helper_single_object_sensor(self, url, object) -> None:
if not self.sensor_token:
self.logger.error(f"Missing sensor api token. Acquire the token from the portal to proceed")
return
header = {'Authorization': '%s' % (self.sensor_token)}
self.logger.info('Dumping %s information...' % (object))
sensor_output_dir = os.path.join(self.output_dir, 'sensor')
check_output_dir(sensor_output_dir, self.logger)
async with self.ahsession.request('GET', url, headers=header, ssl=False) as r:
result = await r.json()
outfile = os.path.join(sensor_output_dir, object + '.json')
if not result:
self.logger.debug("Error with result. Please check your auth: {}".format(str(result)))
return
if type(result) ==list:
with open(outfile, 'w+', encoding='utf-8') as f:
for entry in result:
f.write(json.dumps(entry) + "\n")
elif type(result) == dict:
with open(outfile, 'w+', encoding='utf-8') as f:
f.write(json.dumps(result) + "\n")
self.logger.info("Finished dumping %s information." % (object))
async def dump_sensor_devices(self) -> None:
url = "https://" + str(self.sensor_ip) + "/api/v1/devices"
object = 'devices'
await self.helper_single_object_sensor(url, object)
async def dump_sensor_alerts(self) -> None:
url = "https://" + str(self.sensor_ip) + "/api/v1/alerts"
object = 'alerts'
await self.helper_single_object_sensor(url, object)
async def dump_sensor_device_connections(self) -> None:
url = "https://" + str(self.sensor_ip) + "/api/v1/devices/connections"
object = 'device_connections'
await self.helper_single_object_sensor(url, object)
async def dump_sensor_device_cves(self) -> None:
url = "https://" + str(self.sensor_ip) + "/api/v1/devices/cves"
object = 'devices_cves'
await self.helper_single_object_sensor(url, object)
async def dump_sensor_events(self) -> None:
url = "https://" + str(self.sensor_ip) + "/api/v1/events"
object = 'events'
await self.helper_single_object_sensor(url, object)
async def dump_sensor_device_vuln(self) -> None:
url = "https://" + str(self.sensor_ip) + "/api/v1/reports/vulnerabilities/devices"
object = 'device_vulnerabilities'
await self.helper_single_object_sensor(url, object)
async def dump_sensor_security_vuln(self) -> None:
url = "https://" + str(self.sensor_ip) + "/api/v1/reports/vulnerabilities/security"
object = "security_vulnerabilities"
await self.helper_single_object_sensor(url, object)
async def dump_sensor_operational_vuln(self) -> None:
url = "https://" + str(self.sensor_ip) + "/api/v1/reports/vulnerabilities/operational"
object = 'operational_vulnerabilities'
await self.helper_single_object_sensor(url, object)
async def dump_sensor_pcap(self) -> None:
parent = "/api/v1/alerts"
child = "/api/alert/filtered-pcap/"
await self.helper_multiple_object_sensor(parent, child)
async def helper_single_object_mgmt(self, url, object) -> None:
if not self.mgmt_token:
self.logger.error(f"Missing management console api token. Acquire the token from the portal to proceed")
return
header = {'Authorization': '%s' % (self.mgmt_token)}
mgmt_output_dir = os.path.join(self.output_dir, 'mgmt_console')
check_output_dir(mgmt_output_dir, self.logger)
self.logger.info('Dumping %s from the management console...' % (object))
async with self.ahsession.request('GET', url, headers=header, ssl=False) as r:
result = await r.json()
if not result:
self.logger.debug("Error with result. Please check your auth: {}".format(str(result)))
return
outfile = os.path.join(mgmt_output_dir, object + '.json')
with open(outfile, 'w+', encoding='utf-8') as f:
for entry in result:
f.write(json.dumps(entry) + "\n")
async def dump_mgmt_devices(self) -> None:
url = "https://" + self.mgmt_ip + "/external/v1/devices"
object = "mgmt_devices"
await self.helper_single_object_mgmt(url,object)
async def dump_mgmt_alerts(self) -> None:
url = "https://" + self.mgmt_ip + "/external/v1/alerts"
object = "mgmt_alerts"
await self.helper_single_object_mgmt(url,object)
async def dump_mgmt_sensor_info(self) -> None:
url = "https://" + self.mgmt_ip + "/external/v3/integration/sensors"
object = "sensor_info"
await self.helper_single_object_mgmt(url,object)
async def dump_mgmt_pcap(self) -> None:
parent = "v1/alerts"
child = 'v2/alerts/pcap'
await self.helper_multiple_object_mgmt(parent,child)
async def helper_multiple_object_mgmt(self, parent, child,identifier='id'):
url_parent ="https://" + self.mgmt_ip + "/external/"
if not self.mgmt_token:
self.logger.error(f"Missing management console api token. Acquire the token from the portal to proceed")
return
parent_list = []
header = {'Authorization': '%s' % (self.mgmt_token)}
parent_url = url_parent + parent
outpath = os.path.join(self.output_dir, 'mgmt_console_alert_pcaps')
check_output_dir(outpath, self.logger)
async with self.ahsession.request('GET', parent_url, headers=header, ssl=False) as r:
result = await r.json()
if not result:
self.logger.debug("Error with result. Please check your auth: {}".format(str(result)))
return
for entry in result:
parent_list.append(entry[identifier])
self.logger.info('Dumping %s %s information...' % (parent, child))
for parent_id in parent_list:
url2 = url_parent + child + "/" + str(parent_id)
async with self.ahsession.request('GET', url2, headers=header, ssl=False) as r:
result = await r.json()
if not result or 'error' in result:
self.logger.debug("Error with result. Please check your auth: {}".format(str(result)))
return
download_url = result['downloadUrl']
token = result['token']
outfile = os.path.join(outpath, "alert_" + str(parent_id) + ".pcap")
pcap_token_header = {'Authorization': '%s' % (token)}
splits = download_url.split('/')
splits[2] = self.sensor_ip
download_url_ip = "/".join(splits)
async with self.ahsession.request('GET', download_url_ip, headers=pcap_token_header, ssl=False, allow_redirects=True) as r:
output = await r.read()
with open(outfile, 'wb') as f:
f.write(output)
self.logger.info('Finished dumping %s %s information.' % (parent, child))