2016-12-16 15:14:48 +01:00
|
|
|
import json
|
|
|
|
import pygeoip
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import logging
|
2016-12-07 15:30:49 +01:00
|
|
|
import configparser
|
2016-09-28 14:05:43 +02:00
|
|
|
|
|
|
|
log = logging.getLogger('geoip_country')
|
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
|
|
|
ch.setLevel(logging.DEBUG)
|
|
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
log.addHandler(ch)
|
|
|
|
|
|
|
|
misperrors = {'error': 'Error'}
|
|
|
|
mispattributes = {'input': ['ip-src', 'ip-dst', 'domain|ip'], 'output': ['freetext']}
|
|
|
|
|
|
|
|
# possible module-types: 'expansion', 'hover' or both
|
|
|
|
moduleinfo = {'version': '0.1', 'author': 'Andreas Muehlemann',
|
|
|
|
'description': 'Query a local copy of Maxminds Geolite database',
|
|
|
|
'module-type': ['expansion', 'hover']}
|
|
|
|
|
2016-12-16 15:22:16 +01:00
|
|
|
try:
|
|
|
|
# get current db from http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'geoip_country.cfg'))
|
|
|
|
gi = pygeoip.GeoIP(config.get('GEOIP', 'database'))
|
|
|
|
enabled = True
|
|
|
|
except:
|
|
|
|
enabled = False
|
2016-09-28 14:05:43 +02:00
|
|
|
|
2016-12-16 15:14:48 +01:00
|
|
|
|
2016-09-28 14:05:43 +02:00
|
|
|
def handler(q=False):
|
|
|
|
if q is False:
|
|
|
|
return False
|
|
|
|
request = json.loads(q)
|
|
|
|
|
|
|
|
if request.get('ip-dst'):
|
|
|
|
toquery = request['ip-dst']
|
|
|
|
elif request.get('ip-src'):
|
|
|
|
toquery = request['ip-src']
|
|
|
|
elif request.get('domain|ip'):
|
|
|
|
toquery = request['domain|ip'].split('|')[1]
|
|
|
|
else:
|
2016-12-16 15:14:48 +01:00
|
|
|
return False
|
2016-09-28 14:05:43 +02:00
|
|
|
|
|
|
|
log.debug(toquery)
|
|
|
|
|
|
|
|
try:
|
|
|
|
answer = gi.country_code_by_addr(toquery)
|
|
|
|
except:
|
|
|
|
misperrors['error'] = "GeoIP resolving error"
|
|
|
|
return misperrors
|
|
|
|
|
2016-12-16 15:14:48 +01:00
|
|
|
r = {'results': [{'types': mispattributes['output'], 'values': [str(answer)]}]}
|
2016-12-07 14:18:21 +01:00
|
|
|
|
2016-09-28 14:05:43 +02:00
|
|
|
return r
|
|
|
|
|
2016-12-16 15:14:48 +01:00
|
|
|
|
2016-09-28 14:05:43 +02:00
|
|
|
def introspection():
|
|
|
|
return mispattributes
|
|
|
|
|
2016-12-16 15:14:48 +01:00
|
|
|
|
2016-09-28 14:05:43 +02:00
|
|
|
def version():
|
2016-12-16 15:14:48 +01:00
|
|
|
# moduleinfo['config'] = moduleconfig
|
2016-09-28 14:05:43 +02:00
|
|
|
return moduleinfo
|