-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
AwakeSecurity.py
486 lines (428 loc) · 15.8 KB
/
AwakeSecurity.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401
''' IMPORTS '''
import base64
import re
import requests
import urllib3
# disable insecure warnings
urllib3.disable_warnings()
''' GLOBALS '''
handle_proxy()
params = demisto.params()
server = params["server"].rstrip('/')
prefix = server + "/awakeapi/v1"
verify = not params.get('unsecure', False)
credentials = params["credentials"]
identifier = credentials["identifier"]
password = credentials["password"]
suspicious_threshold = params["suspicious_threshold"]
malicious_threshold = params["malicious_threshold"]
authTokenRequest = {
"loginUsername": identifier,
"loginPassword": password
}
authTokenResponse = requests.post(prefix + "/authtoken", json=authTokenRequest, verify=verify)
authToken = authTokenResponse.json()["token"]["value"]
headers = {
"Authentication": ("access " + authToken)
}
command = demisto.command()
args = demisto.args()
request = {}
''' HELPERS '''
# Convenient utility to marshal command arguments into the request body
def slurp(fields):
for field in fields:
if field in args:
request[field] = args[field]
# Render a subset of the fields of the Contents as a markdown table
def displayTable(contents, fields):
# We don't use a set() because we want to preserve field order
#
# The fields are ordered to put the most relevant information first
presentFields = [] # type: List[str]
# Omit table columns that are all empty
for content in contents:
for field in fields:
if field in content and content[field] and field not in presentFields:
presentFields.append(field)
line0 = "| "
line1 = "| "
for field in presentFields:
# Translate camel-case field names to title-case space-separated words
tokens = re.findall("[a-zA-Z][A-Z]*[^A-Z]*", field)
name = " ".join(token.title() for token in tokens)
line0 += name + " | "
line1 += "--- | "
line0 += "\n"
line1 += "\n"
body = ""
for content in contents:
body += "| "
for field in presentFields:
if field in content:
value = json.dumps(content[field])
else:
value = ""
body += value + " | "
body += "\n"
if presentFields:
return (line0 + line1 + body)
else:
return "Empty results"
def returnResults(contents, outerKey, innerKey, humanReadable, dbotScore, genericContext=None):
demisto.results(create_result_entry(contents, outerKey, innerKey, humanReadable, dbotScore, genericContext))
def create_result_entry(contents, outerKey, innerKey, humanReadable, dbotScore, genericContext=None):
machineReadable = {
"AwakeSecurity": contents,
}
entryContext = {
("AwakeSecurity." + outerKey + "(val." + innerKey + "== obj." + innerKey + ")"): contents,
}
if dbotScore is not None:
machineReadable["DBotScore"] = dbotScore
entryContext["DBotScore"] = dbotScore
if genericContext:
entryContext.update(genericContext)
return {
"Type": entryTypes['note'],
"ContentsFormat": formats['json'],
"Contents": json.dumps(machineReadable),
"HumanReadable": humanReadable,
"ReadableContentsFormat": formats['markdown'],
"EntryContext": entryContext,
}
def toDBotScore(indicator_type, percentile, lookup_key):
if percentile <= suspicious_threshold:
score = 1
elif percentile <= malicious_threshold:
# Something doing something out of the ordinary
score = 2
else:
# Probably bad or at least not compliant with
# company policy.
score = 3
return {
"Vendor": "Awake Security",
"Type": indicator_type,
"Indicator": lookup_key,
"Score": score,
"Reliability": demisto.params().get('integrationReliability')
}
''' COMMANDS '''
def lookup(lookup_type, lookup_key):
path = "/lookup/" + lookup_type
request["lookup_key"] = lookup_key
# default value of lookback_minutes is 480
if "lookback_minutes" not in args:
args["lookback_minutes"] = 480
request["lookback_minutes"] = int(args["lookback_minutes"])
response = requests.post(prefix + path, json=request, headers=headers, verify=verify)
if response.status_code < 200 or response.status_code >= 300:
return_error(f'Request Failed.\nStatus code: {str(response.status_code)}'
f' with body {str(response.content)} with headers {response.headers}')
return response.json()
def lookupDevice():
lookup_key = args["device"]
contents = lookup("device", lookup_key)
humanReadableFields = [
"deviceScore",
"deviceName",
"deviceType",
"os",
"osVersion",
"commonEmail",
"commonUsername",
"tags",
"recentIP",
"activeIP",
"nSimilarDevices",
"ipCount",
"applicationCount",
# "protocols",
"firstSeen",
"lastSeen",
]
if "deviceScore" in contents:
dbotScore = toDBotScore("device", contents["deviceScore"], lookup_key)
else:
dbotScore = {
"Vendor": "Awake Security",
"Type": 'device',
"Indicator": lookup_key,
"Score": 0,
"Reliability": demisto.params().get('integrationReliability')
}
humanReadable = displayTable([contents], humanReadableFields)
contents["device"] = lookup_key
returnResults(contents, "Devices", "device", humanReadable, dbotScore)
def lookupDomain():
lookup_keys = argToList(args["domain"])
results = []
for lookup_key in lookup_keys:
contents = lookup("domain", lookup_key)
humanReadableFields = [
"notability",
"isAlexaTopOneMillion",
"isDGA",
"intelSources",
"numAssociatedDevices",
"numAssociatedActivities",
"approxBytesTransferred",
"protocols",
"firstSeen",
"lastSeen",
]
if "notability" in contents:
dbotScore = toDBotScore("domain", contents["notability"], lookup_key)
else:
dbotScore = {
"Vendor": "Awake Security",
"Type": 'domain',
"Indicator": lookup_key,
"Score": 0,
"Reliability": demisto.params().get('integrationReliability')
}
humanReadable = displayTable([contents], humanReadableFields)
contents["domain"] = lookup_key
genericContext = {"Domain": {"Name": lookup_key}}
results.append(create_result_entry(contents, "Domains", "domain", humanReadable, dbotScore, genericContext))
demisto.results(results)
def lookupEmail():
lookup_keys = argToList(args["email"])
results = []
for lookup_key in lookup_keys:
contents = lookup("email", lookup_key)
humanReadableFields = [
"notabilityPercentile",
"deviceName",
"os",
"deviceType",
"application",
"numberSimilarDevices",
"numberSessions",
"firstSeen",
"lastSeen",
"duration",
"deviceId",
]
if "notabilityPercentile" in contents:
dbotScore = toDBotScore("email", contents["notabilityPercentile"], lookup_key)
else:
dbotScore = {
"Vendor": "Awake Security",
"Type": 'email',
"Indicator": lookup_key,
"Score": 0,
"Reliability": demisto.params().get('integrationReliability')
}
humanReadable = displayTable(contents, humanReadableFields)
for content in contents:
content["email"] = lookup_key
results.append(create_result_entry(contents, "Emails", "email", humanReadable, dbotScore))
demisto.results(results)
def lookupIp():
lookup_keys = argToList(args["ip"])
results = []
for lookup_key in lookup_keys:
contents = lookup("ip", lookup_key)
humanReadableFields = [
"deviceCount",
"activityCount",
"ipFirstSeen",
"ipLastSeen",
]
dbotScore = {
"Vendor": "Awake Security",
"Type": 'ip',
"Indicator": lookup_key,
"Score": 0,
"Reliability": demisto.params().get('integrationReliability')
}
# Note: No DBotScore for IP addresses as we do not score them.
# Our product scores devices rather than IP addresses.
humanReadable = displayTable([contents], humanReadableFields)
contents["ip"] = lookup_key
genericContext = {"IP": {"Address": lookup_key}}
results.append(create_result_entry(contents, "IPs", "ip", humanReadable, dbotScore, genericContext))
demisto.results(results)
def query(lookup_type):
# Default to an empty query if unset
request["queryExpression"] = ""
slurp(["queryExpression", "startTime", "endTime"])
nameMappings = [
("ipAddress", "device.ip == {}"),
("deviceName", "device.name like r/{}/"),
("domainName", "domain.name like r/{}/"),
("protocol", "activity.protocol == \"{}\""),
("tags", "\"{}\" in device.tags"),
]
for (name, mapping) in nameMappings:
if name in args:
if "queryExpression" in request and request["queryExpression"]:
request["queryExpression"] = request["queryExpression"] + " && " + mapping.format(args[name])
else:
request["queryExpression"] = mapping.format(args[name])
path = "/query/" + lookup_type
response = requests.post(prefix + path, json=request, headers=headers, verify=verify)
if response.status_code < 200 or response.status_code >= 300:
return_error(f'Request Failed.\nStatus code: {str(response.status_code)}'
f' with body {str(response.content)} with headers {response.headers}')
contents = response.json()
return request["queryExpression"], contents
def queryActivities():
q, contents = query("activities")
humanReadableFields = [
"sourceIP",
"sourceHost",
"sourcePort",
"destIP",
"destHost",
"destPort",
"activityDeviceName",
"activityStart",
"activityEnd",
"protocols",
]
humanReadable = displayTable(contents, humanReadableFields)
for content in contents:
content["query"] = q
returnResults(contents, "Activities", "activityId", humanReadable, None)
def queryDevices():
q, contents = query("devices")
humanReadableFields = [
"notabilityPercentile",
"deviceName",
"os",
"deviceType",
"application",
"numberSimilarDevices",
"numberSessions",
"firstSeen",
"lastSeen",
"duration",
"deviceId",
]
humanReadable = displayTable(contents, humanReadableFields)
for content in contents:
content["query"] = q
returnResults(contents, "Devices", "deviceId", humanReadable, None)
def queryDomains():
q, contents = query("domains")
humanReadableFields = [
"name",
"notability",
"created",
"lastUpdated",
"expiration",
"registrantOrg",
"registrantCountry",
"registrarName",
"nameservers",
"deviceCount",
"intelCount",
"lastSeen",
]
humanReadable = displayTable(contents, humanReadableFields)
for content in contents:
content["query"] = q
returnResults(contents, "Domains", "name", humanReadable, None)
def pcapDownload():
slurp(["monitoringPointID"])
session = {}
for field in ["hostA", "hostB", "startTimeRFC3339Nano", "endTimeRFC3339Nano"]:
if field in args:
session[field] = args[field]
if "startTimeRFC3339Nano" in args:
session["startTimeRFC3339Nano"] = args["startTime"]
if "endTimeRFC3339Nano" in args:
session["endTimeRFC3339Nano"] = args["endTime"]
for field in ["protocol", "portA", "portB"]:
if field in args:
session[field] = int(args[field])
request["sessions"] = [session]
path = "/pcap/download"
response = requests.post(prefix + path, json=request, headers=headers, verify=verify)
if response.status_code < 200 or response.status_code >= 300:
return_error(f"Request Failed.\nStatus code: {str(response.status_code)} "
f"with body {str(response.content)} with headers {response.headers}")
b64 = response.json()["pcap"]
bytes = base64.b64decode(b64)
demisto.results(fileResult("download.pcap", bytes))
def fetchIncidents():
threatBehaviorsString = params.get("threat_behaviors") or ""
threatBehaviors = [threatBehavior.strip() for threatBehavior in threatBehaviorsString.split(",")]
if threatBehaviors == [""]:
threatBehaviors = []
lastRun = demisto.getLastRun()
formatString = "%Y-%m-%d %H:%M:%S+0000"
earlyTimeString = "1970-01-01 00:00:00+0000"
startTimeString = lastRun.get("time") or earlyTimeString
startTime = datetime.strptime(startTimeString, formatString)
endTime = datetime.utcnow()
endTimeString = datetime.strftime(endTime, formatString)
if timedelta(minutes=int(params['fetch_interval'])) <= endTime - startTime:
jsonRequest = {
"startTime": startTimeString,
"endTime": endTimeString,
"threatBehaviors": threatBehaviors
}
response = requests.post(prefix + "/threat-behavior/matches", json=jsonRequest, headers=headers, verify=verify)
jsonResponse = response.json()
matchingThreatBehaviors = jsonResponse.get("matchingThreatBehaviors", [])
def toIncident(matchingThreatBehavior):
# Currently the threat behavior API doesn't allow us to retrieve metadata for
# the behaviors that matched, which is why this incident record is mostly empty
#
# However, we can provide the original query that the threat behavior corresponded
# to plus the date range so that a playbook can feed them back into
# `awake-query-{devices,activities}` to retrieving the matching devices or
# activities that triggered the match to the threat behavior.
return {
"Name": matchingThreatBehavior["name"],
"Query": matchingThreatBehavior["query"],
"StartTime": startTimeString,
"EndTime": endTimeString,
"rawJSON": json.dumps(matchingThreatBehavior),
}
demisto.incidents(list(map(toIncident, matchingThreatBehaviors)))
# Don't increase the low-water-mark until we actually find incidents
#
# This is a precaution because incidents sometimes appear in an old time
# bucket after a delay
if len(matchingThreatBehaviors) > 0:
lastRun = {"time": endTimeString}
else:
demisto.incidents([])
demisto.setLastRun(lastRun)
''' EXECUTION '''
LOG('command is %s' % (command))
try:
if command == "test-module":
# If we got this far we already successfully authenticated against the server
demisto.results('ok')
elif command == "fetch-incidents":
fetchIncidents()
elif command == "awake-query-devices":
queryDevices()
elif command == "awake-query-activities":
queryActivities()
elif command == "awake-query-domains":
queryDomains()
elif command == "awake-pcap-download":
pcapDownload()
elif command == "domain":
lookupDomain()
elif command == "email":
lookupEmail()
elif command == "ip":
lookupIp()
elif command == "device":
lookupDevice()
except Exception as e:
if command == "fetch-incidents":
raise
LOG(e)
LOG.print_log()
return_error(e)