forked from MISP/misp-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
countrycode.py
executable file
·59 lines (45 loc) · 1.85 KB
/
countrycode.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
import json
import requests
misperrors = {'error': 'Error'}
mispattributes = {'input': ['hostname', 'domain']}
# possible module-types: 'expansion', 'hover' or both
moduleinfo = {'version': '1', 'author': 'Hannah Ward',
'description': 'Expand Country Codes',
'module-type': ['hover']}
# config fields that your code expects from the site admin
moduleconfig = []
common_tlds = {"com": "Commercial (Worldwide)",
"org": "Organisation (Worldwide)",
"net": "Network (Worldwide)",
"int": "International (Worldwide)",
"edu": "Education (Usually USA)",
"gov": "Government (USA)"
}
def parse_country_code(extension):
# Retrieve a json full of country info
try:
codes = requests.get("http://www.geognos.com/api/en/countries/info/all.json").json()
except Exception:
return "http://www.geognos.com/api/en/countries/info/all.json not reachable"
if not codes.get('StatusMsg') or not codes["StatusMsg"] == "OK":
return 'Not able to get the countrycode references from http://www.geognos.com/api/en/countries/info/all.json'
for country in codes['Results'].values():
if country['CountryCodes']['tld'] == extension:
return country['Name']
return "Unknown"
def handler(q=False):
if q is False:
return False
request = json.loads(q)
domain = request["domain"] if "domain" in request else request["hostname"]
# Get the extension
ext = domain.split(".")[-1]
# Check if it's a common, non country one
val = common_tlds[ext] if ext in common_tlds.keys() else parse_country_code(ext)
r = {'results': [{'types': ['text'], 'values':[val]}]}
return r
def introspection():
return mispattributes
def version():
moduleinfo['config'] = moduleconfig
return moduleinfo