-
Notifications
You must be signed in to change notification settings - Fork 11
/
otx.py
168 lines (154 loc) · 6.08 KB
/
otx.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
import requests
from MaltegoTransform import *
import json
import sys
import math
import datetime
import re
base_url = 'https://otx.alienvault.com/api/v1/indicators/'
pulse_url = 'https://otx.alienvault.com/api/v1/pulses/'
headers = {'X-OTX-API-KEY':'<insert API key here>'}
me = MaltegoTransform()
me.parseArguments(sys.argv)
# Parse arguments
entity = sys.argv[2]
section = sys.argv[1]
entity_type = sys.argv[3]
# Determine entity type to use in API calls
if 'pulseid' in entity_type:
entity_type = entity_type
elif 'ipaddress' in entity_type:
entity_type = 'IPv4'
elif 'fqdn' in entity_type:
# Check if it's a hostname or domain name
if re.match(r'.*\..*\..*', entity_type.split("=")[1]):
entity_type='hostname'
else:
entity_type='domain'
elif 'hash' in entity_type:
entity_type = 'file'
elif 'URL' in entity_type:
entity_type = 'url'
else:
entity_type = entity_type
def adddatefield_indicators(indicator):
try:
dt = datetime.datetime.strptime(str(indicator['created']),'%Y-%m-%dT%H:%M:%S.%f')
datetime = str(dt.date())
except:
datetime = ''
ent.addAdditionalFields('link#maltego.link.label','Label','',str(datetime))
if section == 'indicators':
pulse_id = entity_type.split('#')[0].split('=')[1]
r = requests.get(pulse_url+pulse_id+'/indicators', headers=headers)
response = r.json()
for indicator in response['results']:
if indicator['type'] == 'IPv4':
ent = me.addEntity("maltego.IPv4Address",indicator['indicator'])
if indicator['type'] == 'domain':
ent = me.addEntity("maltego.Domain",indicator['indicator'])
if indicator['type'] == 'URL':
ent = me.addEntity("URL",indicator['indicator'])
ent.addAdditionalFields('theurl','URL','',indicator['indicator'])
if indicator['type'] == 'FileHash-MD5':
ent = me.addEntity("maltego.Hash",indicator['indicator'])
if indicator['type'] == 'FileHash-SHA1':
ent = me.addEntity("maltego.Hash",indicator['indicator'])
if indicator['type'] == 'hostname':
ent = me.addEntity("maltego.DNSName",indicator['indicator'])
if indicator['type'] == "FileHash-SHA256":
ent = me.addEntity("maltego.Hash",indicator['indicator'])
else:
ent = me.addEntity("maltego.unknown",indicator['indicator'])
adddatefield_indicators(indicator)
if section == 'analysis':
try:
r = requests.get(base_url+entity_type+'/'+entity+'/analysis', headers=headers)
response = r.json()
ent = me.addEntity("otx.OTXPulse",str(response['analysis']['info']['results']['md5']))
ent.addAdditionalFields('Website', 'Website','','https://otx.alienvault.com/indicator/file/'+str(response['analysis']['info']['results']['md5']))
dt = datetime.datetime.strptime(str(response['analysis']['datetime_int']),'%Y-%m-%dT%H:%M:%S')
ent.addAdditionalFields('link#maltego.link.label','Label','',str(dt.date()))
except:
pass
if section == 'pulses':
try:
r = requests.get(base_url+entity_type+'/'+entity+'/general', headers=headers)
response = r.json()
for pulse in response['pulse_info']['pulses']:
ent = me.addEntity("otx.OTXPulse",str(pulse['name']))
ent.addAdditionalFields('Website', 'Website','','https://otx.alienvault.com/pulse/'+str(pulse['id']))
dt = datetime.datetime.strptime(str(pulse['modified']),'%Y-%m-%dT%H:%M:%S.%f')
ent.addAdditionalFields('link#maltego.link.label','Label','',str(dt.date()))
ent.addAdditionalFields('pulseid','pulseid','',str(pulse['id']))
except:
pass
if section == 'geo':
r = requests.get(base_url+entity_type+'/'+entity+'/geo', headers=headers)
response = r.json()
if response == {}:
pass
else:
asn = response.get('asn')
region = response.get('region')
country_name = response.get('country_name')
ent = me.addEntity("maltego.Location",str(asn) + '\n' + str(region) + str(country_name))
ent.addAdditionalFields('link#maltego.link.label','Label','',' ')
if section == 'malware':
try:
r = requests.get(base_url+entity_type+'/'+entity+'/malware', headers=headers)
response = r.json()
for sample in response['data']:
ent = me.addEntity("maltego.Hash",sample['hash'])
dt = datetime.datetime.fromtimestamp(float(str(sample['datetime_int']))).strftime('%Y-%m-%d')
ent.addAdditionalFields('link#maltego.link.label','Label','',str(dt))
except:
pass
if section == 'url_list':
try:
r = requests.get(base_url+entity_type+'/'+entity+'/url_list', headers=headers)
response = r.json()
records = response['full_size']
pages = int(records) / 10.0
pages = math.ceil(float(pages))
current_page = 1
while current_page <= int(pages):
params = {'page':str(current_page)}
r = requests.get(base_url+entity_type+'/'+entity+'/url_list', headers=headers, params=params)
response = r.json()
current_page += 1
ent = me.addEntity("maltego.URL",response['url_list'][0]['url'])
ent.addAdditionalFields('url','URL','',str(response['url_list'][0]['url']))
dt = datetime.datetime.strptime(str(response['url_list'][0]['date']),'%Y-%m-%dT%H:%M:%S')
ent.addAdditionalFields('link#maltego.link.label','Label','',str(dt.date()))
except:
pass
if section == 'domain_list':
try:
r = requests.get(base_url+entity_type+'/'+entity+'/url_list', headers=headers)
response = r.json()
records = response['full_size']
pages = int(records) / 10.0
pages = math.ceil(float(pages))
current_page = 1
while current_page <= int(pages):
params = {'page':str(current_page)}
r = requests.get(base_url+entity_type+'/'+entity+'/url_list', headers=headers, params=params)
response = r.json()
current_page += 1
ent = me.addEntity("maltego.Domain",response['url_list'][0]['domain'])
dt = datetime.datetime.strptime(str(response['url_list'][0]['date']),'%Y-%m-%dT%H:%M:%S.%f')
ent.addAdditionalFields('link#maltego.link.label','Label','',str(dt.date()))
except:
pass
if section == 'passive_dns':
try:
r = requests.get(base_url+entity_type+'/'+entity+'/passive_dns', headers=headers)
response = r.json()
for domain in response['passive_dns']:
ent = me.addEntity("maltego.Domain",domain['hostname'])
dt = datetime.datetime.strptime(str(domain['last']),'%Y-%m-%d %H:%M:%S')
ent.addAdditionalFields('link#maltego.link.label','Label','',str(dt.date()))
except:
pass
me.returnOutput()