-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcs2th.py
356 lines (298 loc) · 11.2 KB
/
cs2th.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import getopt
import argparse
import datetime
from io import BytesIO
import base64
import logging
import json
from CrowdStrike.api import CrowdStrikeApi
from thehive4py.api import TheHiveApi
from thehive4py.models import Alert, AlertArtifact
from config import CrowdStrike, TheHive
def th_severity(max_severity_displayname):
"""
convert CrowdStrike severity into TheHive severity
:param max_severity_displayname: CrowdStrike severity
:type max_severity_displayname: string
:return TheHive severity
:rtype: int
"""
severities = {
'Low': 1,
'Medium': 2,
'High': 3,
'Critical': 3,
}
return severities[max_severity_displayname]
def th_datatype(type):
"""
Convert CrowdStrike IOCs to TH dataType
:param type: CS type
:type type: string
:return: TH dataType
:rtype: str
"""
types = {
'parent_md5': 'hash',
'parent_sha256': 'hash',
'filename': 'filename',
'sha256': 'hash',
'md5': 'hash',
'cmdline': 'other',
'local_ip': 'ip',
'external_ip': 'ip'
}
if isinstance(type, dict):
return "other"
if type in types:
return types[type]
else:
return "other"
def add_alert_artefact(artefacts, dataType, data, tags, tlp):
"""
:type artefacts: array
:type dataType: string
:type data: string
:type tags: array
:type tlp: int
:rtype: array
"""
return artefacts.append(AlertArtifact(tags=tags,
dataType=dataType,
data=data,
message="From CrowdStrike",
tlp=tlp))
def build_observables(observables, device_id):
"""
Convert CrowdStrike behaviors into TheHive observables
:param observables: behaviors from CrowdStrike
:type observables: dict
:return: AlertArtifact
:rtype: thehive4py.models AlertArtifact
"""
observables = flatten_dict(observables)
artefacts = []
for observable_key, observable_value in observables.items():
# print(observable_key, 'corresponds to', observable_value)
a = AlertArtifact(
data=observable_value,
dataType=th_datatype(observable_value),
message="Observable from CrowdStrike. \
Device ID: {}".format(device_id),
tlp=2,
tags=["src:CrowdStrike"]
)
return a
def build_alert(detection, id):
"""
Convert CrowdStrike Falcon alert into TheHive Alerts
:param detection: Detection from CrowdStrike Alert
:type detection: dict
:param observables: observables from CrowdStrike Alert
:type observables: dict
:return: TheHive alert
:rtype: thehive4py.models Alerts
"""
meta = detection['data']['meta']
resources = detection['data']['resources'][0]
status = detection['status']
behaviors = resources['behaviors'][0]
device = resources['device']
detection_title = '{} {} {} {}'.format(
'CROWDSTRIKE ALERT - ', resources.get('max_severity_displayname'),
'Severity', behaviors['scenario'].replace("_", " "))
tlp = 2
artifacts = [
AlertArtifact(dataType='filename',
data=behaviors.get('filename'),
message="File name of the triggering process"),
AlertArtifact(dataType='hash',
data=behaviors.get('sha256'),
tags=['sha256'],
message="SHA256 of the triggering process",
ioc=True),
AlertArtifact(dataType='other',
data=behaviors.get('user_name'),
tags=['target-user'],
message="The user's name",
tlp=3),
AlertArtifact(dataType='other',
data=behaviors.get('user_id'),
message="Users SID in Windows",
tlp=3),
AlertArtifact(dataType='hash',
data=behaviors.get('md5'),
tags=['md5'],
message="MD5 of the triggering process",
ioc=True),
AlertArtifact(dataType='other',
data=behaviors.get('cmdline'),
message="Command Line of the triggering process"),
AlertArtifact(dataType='other',
data=behaviors.get('ioc_source'),
message="Source that triggered an IOC detection"),
AlertArtifact(dataType=th_datatype(behaviors.get('ioc_type')),
data=behaviors.get('ioc_value'),
message="ioc_value",
ioc=True),
AlertArtifact(dataType='other',
data=device.get('hostname'),
tags=['target-machine'],
message="Device hostname"),
AlertArtifact(dataType='ip',
data=device.get('local_ip'),
tags=['host-ip'],
message="Hosts local ip.",
tlp=3),
AlertArtifact(dataType='ip',
data=device.get('external_ip'),
tags=['external-ip'],
message="Hosts external ip.")
]
a = Alert(
title='{} {} {}'.format(
detection_title,
'on',
device.get('hostname')),
tlp=2,
severity=th_severity(resources.get(
'max_severity_displayname')),
type="FalconHost Alert",
tags=[
"CrowdStrike:Scenario={}".format(
behaviors['scenario'].replace("_", " ").title()),
"CrowdStrike:Max Confidence={}".format(
resources.get('max_confidence')),
"CrowdStrike:Max Severity={}".format(
resources.get('max_severity')),
device.get('machine_domain'),
device.get('hostname'),
device.get('local_ip'),
behaviors.get('user_name'),
behaviors.get('tactic'),
behaviors.get('technique')
],
caseTemplate=TheHive['template'],
source="CrowdStrike",
sourceRef=id,
artifacts=artifacts,
# Switch description to named parameters?
description="{0} {1} {2} {3} {4} {5} {6}".format(
"#### **" + detection_title + 'on ' + device.get('hostname') + "**",
"\n\n --- \n",
"#### **SUMMARY**\n\n",
"{0} {1} {2} {3} {4} {5} {6} {7}".format(
"- **SCENARIO: **" + behaviors.get('scenario') + "\n",
"- **SEVERITY: **" + resources.get('max_severity_displayname') + "\n",
"- **DETECT TIME: **" + behaviors.get('timestamp') + "\n",
"- **HOST: **" + device.get('hostname') + "\n",
"- **HOST IP: **" + device.get('local_ip') + "\n",
"- **USERNAME: **" + behaviors.get('user_name') + "\n",
"- **TACTIC: **" + behaviors.get('tactic') + "\n",
"- **TECHNIQUE: **" + behaviors.get('technique') + "\n",
),
"\n\n --- \n",
"#### **DETECTION DETAILS**\n\n",
"{0} {1} {2}".format(
"```\n",
json.dumps(detection, indent=4, sort_keys=True),
"\n```",
),
)
)
return a
def find_detections(csapi, since):
"""
:param csapi: CrowdStrike.api.CrowdStrikeApi
:param since: number of minutes
:type since: int
:return: list of thehive4py.models Alerts
:rtype: array
"""
detections = csapi.find_detection(UtcNow(since))
if detections.get('status') == 'success':
event_count = detections['data']['meta']['pagination']['total']
if event_count > 0:
detection_ids = detections['data']['resources']
for id in detection_ids:
x = '{{"ids":["{}"]}}'.format(id)
detection_details = csapi.find_detection_details(x)
a = build_alert(detection_details, id)
create_thehive_alerts(TheHive, a)
def create_thehive_alerts(config, alerts):
"""
:param config: TheHive config
:type config: dict
:param alerts: List of alerts
:type alerts: list
:return: create TH alert
"""
thapi = TheHiveApi(config.get('url', None), config.get('key'), config.get('password', None),
config.get('proxies'))
response = thapi.create_alert(alerts)
if response.status_code == 201:
print(json.dumps(response.json(), indent=4, sort_keys=True))
else:
print('ko: {}/{}'.format(response.status_code, response.text))
def UtcNow(delta):
"""
:param delta: timesince delta
:type delta: int
:return: delta
"""
delta = datetime.datetime.utcnow() - datetime.timedelta(minutes=delta)
return (repr(delta.strftime("%Y-%m-%dT%H:%M")))
def run():
"""
Download CrowdStrike incident and create a new alert in TheHive
"""
def find(args):
if 'last' in args and args.last is not None:
last = args.last.pop()
if (not args.i ^ args.I) or args.i:
alerts = find_detections(csapi, last)
if args.monitor:
mon = monitoring("{}/cs2th.status".format(
os.path.dirname(os.path.realpath(__file__))))
mon.touch()
parser = argparse.ArgumentParser(
description="Get CrowdStrike FalconHost alerts and create a case in \
TheHive.")
parser.add_argument("-d", "--debug",
action='store_true',
default=False,
help="generate a log file and active debug logging")
subparsers = parser.add_subparsers(help="subcommand help")
parser_find = subparsers.add_parser('find',
help="find alerts in time")
parser_find.add_argument("-l", "--last",
metavar="M",
nargs=1,
type=int, required=True,
help="get all alerts published during\
the last [M] minutes")
parser_find.add_argument("-m", "--monitor",
action='store_true',
default=False,
help="active monitoring")
parser_find.add_argument("-i",
action='store_true',
default=False,
help="get intel incidents only")
parser_find.add_argument("-I",
action='store_true',
default=False,
help="get high and critical incidents only")
parser_find.set_defaults(func=find)
if len(sys.argv[1:]) == 0:
parser.print_help()
parser.exit()
args = parser.parse_args()
csapi = CrowdStrikeApi(CrowdStrike)
args.func(args)
if __name__ == '__main__':
run()