forked from MISP/misp-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threatcrowd.py
160 lines (115 loc) · 4.44 KB
/
threatcrowd.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
import json
import requests
import re
misperrors = {'error': 'Error'}
mispattributes = {'input': ["hostname", "domain", "ip-src", "ip-dst", "md5", "sha1", "sha256", "sha512", "whois-registrant-email"],
'output': ["domain", "ip-src", "ip-dst", "text", "md5", "sha1", "sha256", "sha512", "hostname", "whois-registrant-email"]
}
# possible module-types: 'expansion', 'hover' or both
moduleinfo = {'version': '1', 'author': 'chrisdoman',
'description': 'Get information from ThreatCrowd',
'module-type': ['expansion']}
moduleconfig = []
# Avoid adding windows update to enrichment etc.
def isBlacklisted(value):
blacklist = ['8.8.8.8', '255.255.255.255', '192.168.56.' , 'time.windows.com']
for b in blacklist:
if value in b:
return True
return False
def valid_ip(ip):
m = re.match(r"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$", ip)
return bool(m) and all(map(lambda n: 0 <= int(n) <= 255, m.groups()))
def valid_domain(hostname):
if len(hostname) > 255:
return False
if hostname[-1] == ".":
hostname = hostname[:-1] # strip exactly one dot from the right, if present
allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split("."))
def valid_email(email):
return bool(re.search(r"^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$", email))
def handler(q=False):
if q is False:
return False
q = json.loads(q)
r = {"results": []}
if "ip-src" in q:
r["results"] += getIP(q["ip-src"])
if "ip-dst" in q:
r["results"] += getIP(q["ip-dst"])
if "domain" in q:
r["results"] += getDomain(q["domain"])
if 'hostname' in q:
r["results"] += getDomain(q['hostname'])
if 'md5' in q:
r["results"] += getHash(q['md5'])
if 'sha1' in q:
r["results"] += getHash(q['sha1'])
if 'sha256' in q:
r["results"] += getHash(q['sha256'])
if 'sha512' in q:
r["results"] += getHash(q['sha512'])
if 'whois-registrant-email' in q:
r["results"] += getEmail(q['whois-registrant-email'])
uniq = []
for res in r["results"]:
if res not in uniq:
uniq.append(res)
r["results"] = uniq
return r
def getHash(hash):
ret = []
req = json.loads(requests.get("https://www.threatcrowd.org/searchApi/v2/file/report/?resource=" + hash).text)
if "domains" in req:
domains = req["domains"]
for domain in domains:
if not isBlacklisted(domain) and valid_domain(domain):
ret.append({"types": ["hostname"], "values": [domain]})
if "ips" in req:
ips = req["ips"]
for ip in ips:
if not isBlacklisted(ip):
ret.append({"types": ["ip-dst"], "values": [ip]})
return ret
def getIP(ip):
ret = []
req = json.loads( requests.get("https://www.threatcrowd.org/searchApi/v2/ip/report/?ip=" + ip).text )
if "resolutions" in req:
for dns in req["resolutions"]:
if "domain" in dns:
if valid_domain(dns["domain"]):
ret.append({"types": ["hostname"], "values": [dns["domain"]]})
if "hashes" in req:
for hash in req["hashes"]:
ret.append({"types": ["md5"], "values": [hash]})
return ret
def getEmail(email):
ret = []
j = requests.get("https://www.threatcrowd.org/searchApi/v2/email/report/?email=" + email).text
req = json.loads(j)
if "domains" in req:
domains = req["domains"]
for domain in domains:
if not isBlacklisted(domain) and valid_domain(domain):
ret.append({"types": ["hostname"], "values": [domain]})
return ret
def getDomain(domain):
ret = []
req = json.loads( requests.get("https://www.threatcrowd.org/searchApi/v2/domain/report/?domain=" + domain).text )
if "resolutions" in req:
for dns in req["resolutions"]:
if "ip_address" in dns:
ret.append({"types": ["ip-dst"], "values": [dns["ip_address"]]})
if "emails" in req:
for email in req["emails"]:
ret.append({"types": ["whois-registrant-email"], "values": [email]})
if "hashes" in req:
for hash in req["hashes"]:
ret.append({"types": ["md5"], "values": [hash]})
return ret
def introspection():
return mispattributes
def version():
moduleinfo['config'] = moduleconfig
return moduleinfo