-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththreatconnect-report.py
executable file
·281 lines (225 loc) · 10.4 KB
/
threatconnect-report.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
# Copyright 2016 ThreatConnect, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
ThreatConnect reporting module for Cuckoo version 1.2.
This module creates an incident in ThreatConnect representing the analysis, and then imports all
network indicators found by Cuckoo and associates those indicators with the analysis.
"""
import datetime
import re
import ipaddress
import threatconnect
from lib.cuckoo.common.abstracts import Report
from lib.cuckoo.common.exceptions import CuckooReportError
def ip(indicator):
"""Check if an indicator is an IP address or not."""
try:
ipaddress.ip_address(indicator)
except ValueError:
return False
else:
return True
class ThreatConnectReport(Report):
"""Reports indicators from analysis results to an instance of ThreatConnect."""
def create_incident(self):
"""Create an incident to represent the analysis in ThreatConnect.
@raise CuckooReportError: if fails to write report.
"""
# Instantiate an incidents object
incidents = self.tc.incidents()
# Get todays date and the filename of the analysis target
date_today = datetime.date.today().strftime('%Y%m%d')
if self.results.get('target').get('file').get('name'):
filename = self.results['target']['file']['name']
# Build a title for the incident
title = 'Cuckoo Analysis {}: {}'.format(date_today, filename)
# Add the title to the object
incident = incidents.add(title, self.target_source)
# Get the full timestamp for the current time and set the event date
date_today_iso = datetime.datetime.now().isoformat()
incident.set_event_date(date_today_iso)
# Add the analysis ID to an attribute
if self.results.get('info').get('id'):
analysis_id = self.results.get('info').get('id')
incident.add_attribute('Analysis ID', analysis_id)
# Build a report link and record it in the Source attribute
report_link = self.report_link_template.format(analysis_id)
incident.add_attribute('Source', report_link)
# Commit the changes to ThreatConnect
try:
incident.commit()
except RuntimeError as e:
raise CuckooReportError('Failed to commit incident: {}'.format(e))
else:
# Load the attributes into the incident object
incident.load_attributes()
# Mark all Cuckoo attributes with DO NOT SHARE security label
for attribute in incident.attributes:
if attribute.type == 'Analysis ID' or attribute.type == 'Source':
attribute.add_security_label('DO NOT SHARE')
# Commit the changes to ThreatConnect
try:
incident.commit()
except RuntimeError as e:
raise CuckooReportError('Failed to commit incident: {}'.format(e))
else:
return incident.id
def upload_indicator(self, raw_indicator):
"""Upload one indicator to ThreatConnect."""
indicators = self.tc.indicators()
indicator = indicators.add(raw_indicator, self.target_source)
indicator.associate_group(threatconnect.ResourceType.INCIDENTS, self.incident_id)
# Commit the changes to ThreatConnect
try:
indicator.commit()
except RuntimeError as e:
if not re.search('exclusion list', e):
raise CuckooReportError('Failed to commit indicator: {}'.format(e))
def import_network(self, type):
"""Loop through all connections and import all source and destination indicators.
@param incident_id: Analysis incident ID.
@param type: protocol, tcp or udp
@raise CuckooReportError: if fails to write indicator.
"""
for conn in self.results.get('network', dict()).get(type, dict()):
# Import the source
try:
self.upload_indicator(conn.get('src'))
except (CuckooReportError, RuntimeError):
pass
# Import the destination
try:
self.upload_indicator(conn.get('dst'))
except (CuckooReportError, RuntimeError):
pass
def import_network_http(self):
"""Loop through all HTTP network connections and import all HTTP indicators.
@param incident_id: Analysis incident ID.
@raise CuckooReportError: if fails to write indicator.
"""
# Loop through all HTTP network connections
for conn in self.results.get('network', dict()).get('http', dict()):
# Remove port number from host
host = re.sub(':\d+', '', conn.get('host'))
# Check if the host is an IP address
if ip(host):
try:
self.upload_indicator(host)
except (CuckooReportError, RuntimeError):
pass
# Import the URL indicator
if conn.get('uri'):
try:
self.upload_indicator(conn.get('uri'))
except (CuckooReportError, RuntimeError):
pass
def import_network_hosts(self):
"""Loop through all network hosts and import all network host indicators.
@param incident_id: Analysis incident ID.
@raise CuckooReportError: if fails to write indicator.
"""
for host in self.results.get('network', dict()).get('hosts', dict()):
# Check if the host is an IP address
if ip(host):
try:
self.upload_indicator(host)
except (CuckooReportError, RuntimeError):
pass
else:
try:
self.upload_indicator(host)
except (CuckooReportError, RuntimeError):
pass
def import_network_dns(self):
"""Loop through all DNS connections and import all request and answer indicators.
@param incident_id: Analysis incident ID.
@raise CuckooReportError: if fails to write indicator.
"""
# Loop through all DNS request connections
for conn in self.results.get('network', dict()).get('dns', dict()):
# Record the DNS request
try:
self.upload_indicator(conn.get('request'))
except (CuckooReportError, RuntimeError):
pass
# Record all the answers
for answer in conn.get('answers', list()):
try:
self.upload_indicator(answer)
except (CuckooReportError, RuntimeError):
pass
def import_network_domains(self):
"""Loop through all domains and import everything as host and address indicators.
@param incident_id: Analysis incident ID.
@raise CuckooReportError: if fails to write indicator.
"""
for domain in self.results.get('network', dict()).get('domains', dict()):
# If an IP is available, import it
if domain.get('ip'):
try:
self.upload_indicator(domain.get('ip'))
except (CuckooReportError, RuntimeError):
pass
# If domain is available, import it
if domain.get('domain'):
try:
self.upload_indicator(domain.get('domain'))
except (CuckooReportError, RuntimeError):
pass
def import_file(self):
"""Import file indicator.
@param incident_id: Analysis incident ID.
@raise CuckooReportError: if fails to write indicator.
"""
if self.results.get('target').get('category') == 'file':
if self.results.get('target').get('file'):
indicators = self.tc.indicators()
file_data = self.results.get('target').get('file')
# Import all the hashes
indicator = indicators.add(file_data.get('md5'), self.target_source)
indicator.set_indicator(file_data.get('sha1'))
indicator.set_indicator(file_data.get('sha256'))
# Set the file size
indicator.set_size(file_data.get('size'))
# If there is a started time, set this as a file occurrence along with the filename
if self.results.get('info').get('started'):
fo_date = self.results.get('info').get('started')[:10]
indicator.add_file_occurrence(file_data.get('name'), fo_date=fo_date)
indicator.associate_group(threatconnect.ResourceType.INCIDENTS, self.incident_id)
# Commit the changes to ThreatConnect
try:
indicator.commit()
except RuntimeError as e:
if not re.search('exclusion list', e):
raise CuckooReportError('Failed to commit indicator: {}'.format(e))
def run(self, results):
"""Upload indicators and incident via ThreatConnect SDK.
@param results: Cuckoo results dict.
"""
api_access_id = self.options.api_access_id
api_secret_key = self.options.api_secret_key
api_base_url = self.options.api_base_url
self.target_source = self.options.target_source
self.tc = threatconnect.ThreatConnect(api_access_id, api_secret_key,
self.options.target_source, api_base_url)
self.report_link_template = self.options.report_link_template
self.results = results
self.incident_id = self.create_incident()
self.import_network('udp')
self.import_network('tcp')
self.import_network_http()
self.import_network_hosts()
self.import_network_dns()
self.import_network_domains()
try:
self.import_file()
except (CuckooReportError, RuntimeError):
pass