-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOTXv2.py
More file actions
484 lines (424 loc) · 20.6 KB
/
OTXv2.py
File metadata and controls
484 lines (424 loc) · 20.6 KB
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
#!/usr/bin/env python
import json
import datetime
import requests
from requests.packages.urllib3.util import Retry
from requests.adapters import HTTPAdapter
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
import IndicatorTypes
# API URLs
API_V1_ROOT = "/api/v1" # API v1 base path
SUBSCRIBED = "{}/pulses/subscribed".format(API_V1_ROOT) # pulse subscriptions
EVENTS = "{}/pulses/events".format(API_V1_ROOT) # events (user actions)
SEARCH_PULSES = "{}/search/pulses".format(API_V1_ROOT) # search pulses
SEARCH_USERS = "{}/search/users".format(API_V1_ROOT) # search users
PULSE_DETAILS = "{}/pulses/".format(API_V1_ROOT) # pulse meta data
PULSE_INDICATORS = PULSE_DETAILS + "indicators" # pulse indicators
PULSE_CREATE = "{}/pulses/create".format(API_V1_ROOT) # create pulse
INDICATOR_DETAILS = "{}/indicators/".format(API_V1_ROOT) # indicator details
VALIDATE_INDICATOR = "{}/pulses/indicators/validate".format(API_V1_ROOT) # indicator details
class InvalidAPIKey(Exception):
def __init__(self, value=None):
self.value = value or "Invalid API Key"
def __str__(self):
return repr(self.value)
class BadRequest(Exception):
def __init__(self, value=None):
self.value = value or "Bad Request"
def __str__(self):
return repr(self.value)
class RetryError(Exception):
def __init__(self, value=None):
self.value = value or "Exceeded maximum number of retries"
def __str__(self):
return repr(self.value)
class OTXv2(object):
"""
Main class to interact with the AlienVault OTX API.
"""
def __init__(self, api_key, proxy=None, server="https://otx.alienvault.com", project="SDK", user_agent=None):
self.key = api_key
self.server = server
self.proxies = {'http': proxy} if proxy else {}
self.request_session = None
self.headers = {
'X-OTX-API-KEY': self.key,
'User-Agent': user_agent or 'OTX Python {}/1.1'.format(project),
'Content-Type': 'application/json'
}
def session(self):
if self.request_session is None:
self.request_session = requests.Session()
# This will allow 5 tries at a url, with an increasing backoff. Only applies to a specific set of codes
self.request_session.mount('https://', HTTPAdapter(
max_retries=Retry(
total=5,
status_forcelist=[429, 500, 502, 503],
backoff_factor=5,
)
))
return self.request_session
@classmethod
def handle_response_errors(cls, response):
def _response_json():
try:
return response.json()
except Exception as e:
return {'internal_error': 'Unable to decode response json: {}'.format(e)}
if response.status_code == 403:
raise InvalidAPIKey()
elif response.status_code == 400:
raise BadRequest(_response_json())
elif str(response.status_code)[0] != "2":
raise Exception("Unexpected http code: %r, response=%r", response.status_code, _response_json())
return response
def get(self, url, **kwargs):
"""
Internal API for GET request on a OTX URL
:param url: URL to retrieve
:return: response in JSON object form
"""
try:
response = self.session().get(
self.create_url(url, **kwargs),
headers=self.headers,
proxies=self.proxies,
)
return self.handle_response_errors(response).json()
except requests.exceptions.RetryError:
raise RetryError()
def patch(self, url, body, **kwargs):
"""
Internal API for POST request on a OTX URL
:param url: URL to retrieve
:param body: HTTP Body to send in request
:return: response as dict
"""
response = self.session().patch(
self.create_url(url, **kwargs),
data=json.dumps(body),
headers=self.headers,
proxies=self.proxies,
)
return self.handle_response_errors(response).json()
def post(self, url, body, **kwargs):
"""
Internal API for POST request on a OTX URL
:param url: URL to retrieve
:param body: HTTP Body to send in request
:return: response as dict
"""
response = self.session().post(
self.create_url(url, **kwargs),
data=json.dumps(body),
headers=self.headers,
proxies=self.proxies,
)
return self.handle_response_errors(response).json()
def create_pulse(self, **kwargs):
"""
Create a pulse via HTTP Post (Content Type: application/json).
Notes:
If `TLP` is one of: ['red', 'amber'], `public` must be false.
`name` field is required
Default values (unless specified):
- public: True
- TLP: 'green'
:param kwargs containing pulse to submit
:param name(string, required) pulse name
:param public(boolean, required) long form description of threat
:param description(string) long form description of threat
:param tlp(string) Traffic Light Protocol level for threat sharing
:param tags(list of strings) short keywords to associate with your pulse
:param references(list of strings, preferably URLs) external references for this threat
:param indicators(list of objects) IOCs to include in pulse
:return: request body response
:raises BadRequest (400) On failure, BadRequest will be raised containing the invalid fields.
Examples:
Python kwargs can be used in two ways. You can call create_pulse passing a dict, or named arguments.
With a dict:
otx = OTXv2("mysecretkey") # replace with your api key
body = {'name': pulse_name, 'public': False, 'indicators': indicator_list, 'TLP': 'green', ...}
otx.create_pulse(**body) # the dict will be expanded into the args.
Or with named args:
otx = OTXv2("mysecretkey") # replace with your api key
otx.create_pulse(name=pulse_name, public=False, indicators=indicator_list, TLP='green')
"""
body = {
'name': kwargs.get('name', ''),
'description': kwargs.get('description', ''),
'public': kwargs.get('public', True),
'TLP': kwargs.get('TLP', kwargs.get('tlp', 'green')),
'tags': kwargs.get('tags', []),
'references': kwargs.get('references', []),
'indicators': kwargs.get('indicators', [])
}
# name is required. Public is too but will be set True if not specified.
if not body.get('name'):
raise ValueError('Name required. Please resubmit your pulse with a name (string, 5-64 chars).')
return self.post(self.create_url(PULSE_CREATE), body=body)
def validate_indicator(self, indicator_type, indicator, description=""):
"""
The goal of validate_indicator is to aid you in pulse creation. Use this method on each indicator before
calling create_pulse to ensure success in the create call. If you supply invalid indicators in a create call,
the pulse will not be created.
:param indicator: indicator value (string)
:param indicator_type: an IndicatorTypes object (i.e. IndicatorTypes.DOMAIN)
:param description: a short descriptive string can be sent to the validator for length checking
:return:
"""
if not indicator:
raise ValueError("please supply `indicator` when calling validate_indicator")
if not indicator_type:
raise ValueError("please supply `indicator` when calling validate_indicator")
# if caller supplied object instance, use name field
if isinstance(indicator_type, IndicatorTypes.IndicatorTypes):
indicator_type = indicator_type.name
elif indicator_type not in IndicatorTypes.to_name_list(IndicatorTypes.all_types):
raise ValueError("Indicator type: {} is not a valid type.".format(indicator_type))
# indicator type is valid, let's valdate against the otx api
body = {
'indicator': indicator,
'type': indicator_type,
'description': description
}
response = self.post(self.create_url(VALIDATE_INDICATOR), body=body)
return response
def create_url(self, url_path, **kwargs):
""" Turn a path into a valid fully formatted URL. Supports query parameter formatting as well.
:param url_path: Request path (i.e. "/search/pulses")
:param kwargs: key value pairs to be added as query parameters (i.e. limit=10, page=5)
:return: a formatted url (i.e. "/search/pulses")
"""
uri = url_path.format(self.server)
uri = uri if uri.startswith("http") else self.server + uri
if kwargs:
uri += "?" + urlencode(kwargs)
# print uri
return uri
def create_indicator_detail_url(self, indicator_type, indicator, section='general'):
""" Build a valid indicator detail url. This api contains all data we have about indicators.
Only indicators with IndicatorTypes.api_support = True should be used.
:param indicator_type: IndicatorType instance
:param indicator: String indicator (i.e. "69.73.130.198", "mail.vspcord.com")
:param section: Section from IndicatorTypes.section. Default is general info
:return: formatted URL string
"""
indicator_url = self.create_url(INDICATOR_DETAILS)
indicator_url += "{indicator_type}/{indicator}/{section}".format(
indicator_type=indicator_type.slug,
indicator=indicator,
section=section
)
return indicator_url
def walkapi_iter(self, url, max_page=None):
next_page_url = url
count = 0
while next_page_url:
count += 1
if max_page and count > max_page:
break
data = self.get(next_page_url)
for el in data['results']:
yield el
next_page_url = data["next"]
def walkapi(self, url, iter=False, max_page=None):
if iter:
return self.walkapi_iter(url, max_page=max_page)
else:
return list(self.walkapi_iter(url, max_page=max_page))
def getall(self, modified_since=None, author_name=None, limit=20, max_page=None, iter=False):
"""
Get all pulses user is subscribed to.
:param modified_since: datetime object representing earliest date you want returned in results
:param author_name: Name of pulse author to limit results to
:param limit: The page size to retrieve in a single request
:return: the consolidated set of pulses for the user
"""
args = {'limit': limit}
if modified_since is not None:
args['modified_since'] = modified_since
if author_name is not None:
args['author_name'] = author_name
return self.walkapi(self.create_url(SUBSCRIBED, **args), iter=iter, max_page=max_page)
def getall_iter(self, author_name=None, modified_since=None, limit=20, max_page=None):
"""
Get all pulses user is subscribed to, yield results.
:param modified_since: datetime object representing earliest date you want returned in results
:param author_name: Name of pulse author to limit results to
:param limit: The page size to retrieve in a single request
:param max_page: if set, limits number of pages returned to 'max_page'
:return: the consolidated set of pulses for the user
"""
return self.getall(modified_since=modified_since, author_name=author_name, limit=limit, max_page=max_page, iter=True)
def getsince(self, timestamp, limit=20, max_page=None):
"""
Get all pulses modified since a particular time.
:param timestamp: iso formatted date time string
:param limit: Maximum number of results to return in a single request
:return: the consolidated set of pulses for the user
"""
return self.getall(limit=limit, modified_since=timestamp, max_page=max_page, iter=False)
def getsince_iter(self, timestamp, limit=20, max_page=None):
"""
Get all pulses modified since a particular time, yield results.
:param timestamp: iso formatted date time string
:param limit: Maximum number of results to return in a single request
:return: the consolidated set of pulses for the user
"""
return self.getall(limit=limit, modified_since=timestamp, max_page=max_page, iter=True)
def search_pulses(self, query, max_results=25):
"""
Get all pulses with text matching `query`.
:param query: The text to search for
:param max_results: Limit the number of pulses returned in response
:return: All pulses matching `query`
"""
search_pulses_url = self.create_url(SEARCH_PULSES, q=query, page=1, limit=20)
return self._get_paginated_resource(search_pulses_url, max_results=max_results)
def search_users(self, query, max_results=25):
"""
Get all pulses with text matching `query`.
:param query: The text to search for
:param max_results: Limit the number of users returned in response
:return: List of users with username matching `query`
"""
search_users_url = self.create_url(SEARCH_USERS, q=query, limit=20, page=1)
return self._get_paginated_resource(search_users_url, max_results=max_results)
def _get_paginated_resource(self, url=SUBSCRIBED, max_results=25):
"""
Get all pages of a particular API resource, and retain additional fields.
:param url: URL for first page of a paginated list api. Default is list subscribed pulses.
:param max_results: Limit the number of objects returned.
:return: results and additional fields as dict
"""
results = []
next_page_url = url
additional_fields = {}
while next_page_url and len(results) < max_results:
json_data = self.get(next_page_url)
max_results -= len(json_data.get('results'))
for r in json_data.pop("results"):
results.append(r)
next_page_url = json_data.pop("next")
json_data.pop('previous', '')
if json_data.items():
additional_fields.update(json_data)
resource = {"results": results[:max_results]}
resource.update(additional_fields)
return resource
def get_all_indicators(self, indicator_types=IndicatorTypes.all_types, max_page=None):
"""
Get all the indicators contained within your pulses of the IndicatorTypes passed.
By default returns all IndicatorTypes.
:param indicator_types: IndicatorTypes to return
:return: yields the indicator object for use
"""
name_list = IndicatorTypes.to_name_list(indicator_types)
for pulse in self.getall_iter(max_page=max_page):
for indicator in pulse["indicators"]:
if indicator["type"] in name_list:
yield indicator
def getevents_since(self, timestamp, limit=20):
"""
Get all events (activity) created or updated since a timestamp
:param timestamp: ISO formatted datetime string to restrict results (not older than timestamp).
:param limit: The page size to retrieve in a single request
:return: the consolidated set of pulses for the user
"""
return self.walkapi(self.create_url(EVENTS, limit=limit, since=timestamp))
def get_pulse_details(self, pulse_id):
"""
For a given pulse_id, get the details of an arbitrary pulse.0
:param pulse_id: object id for pulse
:return: Pulse as dict
"""
pulse_url = self.create_url(PULSE_DETAILS + str(pulse_id))
meta_data = self.get(pulse_url)
return meta_data
def get_pulse_indicators(self, pulse_id, limit=20):
"""
For a given pulse_id, get list of indicators (IOCs)
:param pulse_id: Object ID specify which pulse to get indicators from
:return: Indicator list
"""
return self.walkapi(self.create_url(PULSE_DETAILS + str(pulse_id) + "/indicators", limit=limit))
def edit_pulse(self, pulse_id, body):
"""
Edits a pulse
:param pulse_id: The pulse you are editing the indicators in
:param body: The complete set of indicators this pulse will now contain
eg; body: {
"description": "New Description",
"tags": {"add": ["addtag1", "addtag2"], "remove": ["remtag1"]}
}
:return: Return the new pulse
"""
response = self.patch(self.create_url(PULSE_DETAILS + str(pulse_id)), body=body)
return response
def replace_pulse_indicators(self, pulse_id, new_indicators):
"""
Replaces indicators in a pulse - new indicators are added, those that are no longer present are set to expire
:param pulse_id: The pulse you are replacing the indicators with
:param new_indicators: The complete set of indicators this pulse will now contain
:return: Return the new pulse
"""
current_indicators = self.get_pulse_indicators(pulse_id)
current_indicator_values = []
current_indicator_indicators = []
for indicator in current_indicators:
current_indicator_values.append(indicator["indicator"])
current_indicator_indicators.append(indicator)
new_indicator_values = []
indicators_to_add = []
for indicator in new_indicators:
new_indicator_value = indicator["indicator"]
new_indicator_values.append(new_indicator_value)
if new_indicator_value not in current_indicator_values:
indicators_to_add.append(indicator)
indicators_to_amend = []
for indicator in current_indicator_indicators:
if indicator["indicator"] not in new_indicator_values:
yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
indicators_to_amend.append({"id": indicator["id"], "expiration": yesterday.strftime("%Y-%m-%d"), "title": "Expired"})
else:
# Need this else statement, to cover indicators that appear, then go, then re-appear
indicators_to_amend.append({"id": indicator["id"], "expiration": "", "title": "", "is_active": 1})
body = {
'indicators': {
'add': indicators_to_add,
'edit': indicators_to_amend
}
}
response = self.patch(self.create_url(PULSE_DETAILS + str(pulse_id)), body=body)
return response
def get_indicator_details_by_section(self, indicator_type, indicator, section='general'):
"""
The Indicator details endpoints are split into sections. Obtain a specific section for an indicator.
:param indicator_type: IndicatorType instance
:param indicator: String indicator (i.e. "69.73.130.198", "mail.vspcord.com")
:param section: Section from IndicatorTypes.section. Default is general info
:return: Return indicator details as dict
"""
if not indicator_type.api_support:
raise TypeError("IndicatorType {0} is not currently supported.".format(indicator_type))
if section not in indicator_type.sections:
raise TypeError("Section {0} is not currently supported for indicator type: {0}")
indicator_url = self.create_indicator_detail_url(indicator_type, indicator, section)
indicator_details = self.get(indicator_url)
return indicator_details
def get_indicator_details_full(self, indicator_type, indicator):
"""
Obtain all sections for an indicator.
:param indicator_type: IndicatorType instance
:param indicator: String indicator (i.e. "69.73.130.198", "mail.vspcord.com")
:return: dict with sections as keys and results for each call as values.
"""
indicator_dict = {}
for section in indicator_type.sections:
indicator_url = self.create_indicator_detail_url(indicator_type, indicator, section)
indicator_dict[section] = self.get(indicator_url)
return indicator_dict