/
akamai_ioc.py
229 lines (208 loc) · 9.69 KB
/
akamai_ioc.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
#!/usr/bin/env python3
"""
Module (type "expansion") from Akamai to provide IOC analysis.
Date : 12/1/2020
Authors: ["Shiran Guez","Jordan Garzon","Avishai Katz","Asaf Nadler"]
"""
import json
import time
import requests
from urllib.parse import urljoin
from akamai.edgegrid import EdgeGridAuth
from . import check_input_attribute, checking_error, standard_error_message
from pymisp import MISPAttribute, MISPEvent, MISPObject
import logging
import logging.handlers
logging.basicConfig(filename='akamai.log',
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)
log = logging.getLogger("akamai_ioc")
misperrors = {
"error": "Error",
}
mispattributes = {
"input": [
"domain",
"hostname",
],
'format': 'misp_standard'
}
moduleinfo = {
"version": "0.1",
"author": "Akamai Team",
"description": "Get Akamai IOC v1 infomration.",
"module-type": ["expansion"],
}
moduleconfig = ['client_secret', 'apiURL', 'access_token', 'client_token', 'etp_config_id']
class APIAKAOpenParser():
def __init__(self, ctoken, csecret, atoken, configID, baseurl, rrecord):
self.misp_event = MISPEvent()
self.ctoken = ctoken
self.csecret = csecret
self.atoken = atoken
self.baseurl = baseurl
self.configID = configID
self.rrecord = rrecord
def get_results(self):
event = json.loads(self.misp_event.to_json())
results = {key: event[key] for key in ('Attribute', 'Object') if (key in event and event[key])}
return {'results': results}
def parse_domain(self, rrecord):
aka_object = MISPObject('Akamai IOC enrich')
session = requests.Session()
session.auth = EdgeGridAuth(
client_token = self.ctoken,
client_secret = self.csecret,
access_token = self.atoken
)
result = session.get(urljoin(self.baseurl, '/etp-report/v1/ioc/information?record=' + rrecord))
if result.status_code != 200:
raise Exception ("Akamai Open API return error: " + str(result.status_code))
q = result.json()
to_Enrich = ""
whois_info = ""
urlList = ""
incident_flag = "false"
commentval = "Akamai IOC enrich"
self._get_dns_info(rrecord)
try:
if self.incident_flag == "true":
tagval = ["AkamaiETP:incident-classification=incident"]
else:
tagval = ["source:AkamaiETP"]
except:
tagval = ["source:AkamaiETP"]
threatInfo = ""
for (k, v) in q.items():
if k == 'record':
to_Enrich += str(q[k]) + "\n"
if k == 'recordType':
continue
if k == 'description':
to_Enrich += str(q[k]) + "\n"
if k == 'categories':
to_Enrich += str(q[k]) + "\n"
if k == 'registrarName':
whois_info += str(k) + ": " + str(q[k]) + "\n"
if k == 'registrantName':
aka_object.add_attribute('Registrant name', type='whois-registrant-name', value=str(q[k]))
if k == 'strantOrganizatione':
whois_info += str(k) + ": " + str(q[k]) + "\n"
if k == 'registrantCity':
whois_info += str(k) + ": " + str(q[k]) + "\n"
if k == 'registrantState':
whois_info += str(k) + ": " + str(q[k]) + "\n"
if k == 'registrantEmails':
aka_object.add_attribute('Registrant Emails', type='whois-registrant-email', value=str(q[k]))
if k == 'nameServerNames':
whois_info += str(k) + ": " + str(q[k]) + "\n"
if k == 'registrantAddress':
whois_info += str(k) + ": " + str(q[k]) + "\n"
if k == 'registrantCountry':
whois_info += str(k) + ": " + str(q[k]) + "\n"
if k == 'whoisServer':
whois_info += str(k) + ": " + str(q[k]) + "\n"
if k == 'badUrls':
for item in q['badUrls'][0]['badUrls']:
aka_object.add_attribute('PMD', **{'type': 'url', 'value': item['url']})
if k == 'createdDate':
aka_object.add_attribute('first-seen', disable_correlation=True, **{'type': 'datetime', 'value': q[k]})
if k == 'lastModifiedDate':
aka_object.add_attribute('last-seen', disable_correlation=True, **{'type': 'datetime', 'value': q[k]})
if k == 'threatInformation' and threatInfo == "":
threatIN = q['threatInformation']
tmpI = 0
ThreatTag = tagval
for item in threatIN:
if item['threatId'] != tmpI:
addresult = session.get(urljoin(self.baseurl, '/etp-report/v1/configs/' + str(self.configID) + '/threats/' + str(item['threatId'])))
d = addresult.json()
threatInfo = "\nThreat Name: " + str(d['threatName']) + "\nDescription: " + str(d['description'] + " ")
try:
if d['familyName'] != "" and d['threatName'] != "":
NEWTAGAPP="misp-galaxy:"+d['familyName']+'="'+d['threatName']+'"'
else:
NEWTAGAPP="Threat:"+d['threatName']
except:
NEWTAGAPP="Threat:unknown"
ThreatTag.append(NEWTAGAPP)
aka_object.add_attribute('Threat Info', type='text', value=threatInfo, Tag=ThreatTag, disable_correlation=True)
for link in d['externalLinks']:
aka_object.add_attribute('reference', type='link', value=link, Tag=ThreatTag, disable_correlation=True)
tmpI = item['threatId']
if whois_info != "":
to_Enrich += "\nWhois Information: \n" + whois_info + "\n"
if urlList != "":
to_Enrich += "\nURL list: \n" + urlList + "\n"
try:
changes_result = session.get(urljoin(self.baseurl, '/etp-report/v1/ioc/changes?record=' + rrecord))
changes = changes_result.json()
for change in changes:
aka_object.add_attribute('timeline', disable_correlation=True, **{'type': 'datetime', 'value': change['date'], 'comment': str(change["description"])})
except Exception as e:
log.info('Exception in custom info {}'.format(e))
aka_object.add_attribute('Domain Threat Info', type='text', value=to_Enrich, Tag=tagval, disable_correlation=True)
self.misp_event.add_object(**aka_object)
def _get_dns_info(self, rrecord):
aka_cust_object = MISPObject('misc')
tagInfo=["source:AkamaiETP"]
_text = ""
dimensions = ['deviceId','site']
for dimension in dimensions:
#_result = self._run_custom_request(self, rrecord, dimension)
session = requests.Session()
session.auth = EdgeGridAuth(
client_token = self.ctoken,
client_secret = self.csecret,
access_token = self.atoken
)
confID = self.configID
epoch_time = int(time.time())
last_30_days = epoch_time - 3600 * 24 * 30 # last month by default for now
url = f'/etp-report/v2/configs/{str(confID)}' + \
f'/dns-activities/aggregate?cardinality=2500&dimension={dimension}&endTimeSec={epoch_time}&filters' + \
f'=%7B%22domain%22:%7B%22in%22:%5B%22{rrecord}%22%5D%7D%7D&startTimeSec={last_30_days}'
_result = session.get(urljoin(self.baseurl, url)).json()
if _result['dimension']['total'] != 0:
_text += dimension + ' involved\n\n'
if 'aggregations' in _result:
for el in _result['aggregations']:
name = el['name']
_text += f"{name} : {el['total']} connections \n"
aka_cust_object.add_attribute('Customer Attribution', type='text', value=str(_text), Tag=tagInfo, disable_correlation=True)
self.incident_flag = "true"
self.misp_event.add_object(**aka_cust_object)
def introspection():
return mispattributes
def version():
moduleinfo['config'] = moduleconfig
return moduleinfo
def handler(q=False):
if q is False:
return False
request = json.loads(q)
if request.get('config'):
if (request['config']['client_token'] is '') \
or (request['config']['client_secret'] is '') \
or (request['config']['access_token'] is '') \
or (request['config']['etp_config_id'] is '') \
or (request['config']['apiURL'] is ''):
misperrors['error'] = "Akamai Open API credentials are missing"
return misperrors
attribute = request['attribute']
ctoken = str(request['config']['client_token'])
csecret = str(request['config']['client_secret'])
atoken = str(request['config']['access_token'])
configID = str(request['config']['etp_config_id'])
baseurl = str(request['config']['apiURL'])
rrecord = attribute['value'] if 'value' in attribute else attribute['value1']
mapping = {
'domain': 'parse_domain',
'hostname': 'parse_domain'
}
aka_parser = APIAKAOpenParser(ctoken, csecret, atoken, configID, baseurl, rrecord)
attribute_value = attribute['value'] if 'value' in attribute else attribute['value1']
getattr(aka_parser, mapping[attribute['type']])(attribute_value)
return aka_parser.get_results()