forked from wolfpack1/threatrecon
/
threat_recon_request.py
128 lines (115 loc) · 4.04 KB
/
threat_recon_request.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
import socket
from api import get_api_key
from query import search_is_domain, query_threat_recon
api_key = get_api_key() or 'my API key'
search = raw_input("Please Enter an indicator: ")
results = query_threat_recon(search, api_key)
indicator_meta = []
related_indicators = []
#check host IP if no results
if results is None:
if search_is_domain(search):
try: # tries to get IP from domain
iplookup = socket.gethostbyname(search)
print "***** No results found for this domain..."
print "***** checking host IP: %s\n" % iplookup
results = query_threat_recon(iplookup, api_key)
except socket.gaierror as e:
print "***** Lookup failed: %s" % e
#find relationships in JSON results and list out
else:
for item in results:
root_node = item["RootNode"]
rdata = item["Rdata"]
indicator = item["Indicator"]
if item["RootNode"] == '' and search == item["Indicator"]:
indicator_meta.append(
[
item["Reference"],
item["Source"],
item["KillChain"],
item["FirstSeen"],
item["LastSeen"],
item["Attribution"],
item["ProcessType"],
item["Country"],
item["Tags"],
item["Comment"],
str(item["Confidence"])
]
)
if search == item["RootNode"]:
related_indicators.append(
[
item["Indicator"],
item["ProcessType"],
item["Rdata"],
item["Rrname"],
item["RootNode"]
]
)
if item["RootNode"] != '' and search != item["RootNode"]:
related_indicators.append(
[
item["Indicator"],
item["ProcessType"],
item["Rdata"],
item["Rrname"],
item["RootNode"]
]
)
#check to see if search is a derived indicator
if not indicator_meta and related_indicators:
print "COMMENT: %s is a derived indicator..." % search
print "metadata is inherited from the root node"
if indicator_meta:
list_meta = indicator_meta[0]
else:
list_meta = []
if not list_meta and not related_indicators:
print 'sorry, no results.. that might be a good thing'
#list metadata if Direct indicator
if list_meta:
print "Threat Recon has found the following metadata on %s: " % search
tags = [
'Reference',
'Source',
'KillChain',
'First Seen',
'Last Seen',
'Attribution',
'ProcessType',
'Country',
'TAGS',
'Comment',
'Confidence'
]
for item in indicator_meta:
for i in range(11):
if item[i]:
print "%s: %s" % (tags[i], item[i])
print '\n'
#list related indicators
if related_indicators:
print "\n"
print "Threat Recon has found the following indicator(s) "
print "that are related to %s" % search
for item in related_indicators:
if item[0] != search:
print '******************'
print 'Related indicator: %s' % item[0]
print 'Relationship type: %s' % item[1]
if item[2]:
print 'Relationship pivot: %s' % item[2]
if item[3] and item[3] != item[0]:
print 'RRNAME: %s' % item[3]
print '****************** \n'
else:
print '******************'
print 'Related indicator: %s' % item[4]
print 'Relationship type: %s' % item[1]
if item[2]:
print 'Relationship pivot: %s' % item[2]
if item[3] and item[3] != item[4]:
print 'RRNAME: %s' % item[3]
print '****************** \n'