misp-modules/misp_modules/modules/expansion/countrycode.py

62 lines
1.7 KiB
Python
Raw Normal View History

2016-08-12 15:45:28 +02:00
import json
import requests
misperrors = {'error': 'Error'}
mispattributes = {'input': ['hostname', 'domain']}
2016-08-12 15:45:28 +02:00
# possible module-types: 'expansion', 'hover' or both
moduleinfo = {'version': '1', 'author': 'Hannah Ward',
'description': 'Expand Country Codes',
2016-08-15 12:09:40 +02:00
'module-type': ['hover']}
2016-08-12 15:45:28 +02:00
# config fields that your code expects from the site admin
moduleconfig = []
2018-12-11 15:29:09 +01:00
common_tlds = {"com": "Commercial (Worldwide)",
"org": "Organisation (Worldwide)",
"net": "Network (Worldwide)",
"int": "International (Worldwide)",
"edu": "Education (Usually USA)",
"gov": "Government (USA)"
}
2016-08-12 15:45:28 +02:00
2018-06-06 08:31:41 +02:00
codes = False
2018-12-11 15:29:09 +01:00
2016-08-12 15:45:28 +02:00
def handler(q=False):
global codes
2018-06-06 08:31:41 +02:00
if not codes:
2018-12-11 15:29:09 +01:00
codes = requests.get("http://www.geognos.com/api/en/countries/info/all.json").json()
2016-08-12 15:45:28 +02:00
if q is False:
return False
request = json.loads(q)
domain = request["domain"] if "domain" in request else request["hostname"]
2016-08-12 15:45:28 +02:00
# Get the extension
ext = domain.split(".")[-1]
# Check if it's a common, non country one
2016-08-12 15:45:28 +02:00
if ext in common_tlds.keys():
2018-12-11 15:29:09 +01:00
val = common_tlds[ext]
2016-08-12 15:45:28 +02:00
else:
2018-12-11 15:29:09 +01:00
# Retrieve a json full of country info
if not codes["StatusMsg"] == "OK":
val = "Unknown"
else:
# Find our code based on TLD
codes = codes["Results"]
for code in codes.keys():
if codes[code]["CountryCodes"]["tld"] == ext:
val = codes[code]["Name"]
r = {'results': [{'types': ['text'], 'values':[val]}]}
2016-08-12 15:45:28 +02:00
return r
def introspection():
return mispattributes
def version():
moduleinfo['config'] = moduleconfig
return moduleinfo