2017-09-26 18:47:23 +00:00
|
|
|
import logging
|
2017-09-26 20:08:50 +00:00
|
|
|
import json
|
2017-09-27 20:39:47 +00:00
|
|
|
import requests
|
2017-09-26 18:47:23 +00:00
|
|
|
|
2017-10-17 18:29:40 +00:00
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
from six import add_metaclass
|
|
|
|
|
2017-09-26 20:08:50 +00:00
|
|
|
from cachetools import ttl_cache, lru_cache
|
2017-09-26 18:47:23 +00:00
|
|
|
from collections import namedtuple, defaultdict
|
|
|
|
from netaddr import IPNetwork, IPAddress, IPSet, AddrFormatError
|
|
|
|
|
|
|
|
import geoip2.database
|
|
|
|
import geoip2.errors
|
|
|
|
|
2017-10-17 18:29:40 +00:00
|
|
|
from util.abchelpers import nooper
|
|
|
|
|
2017-09-26 18:47:23 +00:00
|
|
|
ResolvedLocation = namedtuple('ResolvedLocation', ['provider', 'region', 'service', 'sync_token'])
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-09-27 20:39:47 +00:00
|
|
|
_DATA_FILES = {'aws-ip-ranges.json': 'https://ip-ranges.amazonaws.com/ip-ranges.json'}
|
|
|
|
|
|
|
|
def update_resolver_datafiles():
|
|
|
|
""" Performs an update of the data file(s) used by the IP Resolver. """
|
|
|
|
for filename, url in _DATA_FILES.iteritems():
|
|
|
|
logger.debug('Updating IP resolver data file "%s" from URL "%s"', filename, url)
|
|
|
|
with open('util/ipresolver/%s' % filename, 'w') as f:
|
|
|
|
response = requests.get(url)
|
|
|
|
logger.debug('Got %s response for URL %s', response.status_code, url)
|
|
|
|
if response.status_code / 2 != 100:
|
|
|
|
raise Exception('Got non-2XX status code for URL %s: %s' % (url, response.status_code))
|
|
|
|
|
|
|
|
f.write(response.text)
|
|
|
|
logger.debug('Successfully wrote %s', filename)
|
|
|
|
|
2017-10-17 18:29:40 +00:00
|
|
|
|
|
|
|
@add_metaclass(ABCMeta)
|
|
|
|
class IPResolverInterface(object):
|
|
|
|
""" Helper class for resolving information about an IP address. """
|
|
|
|
@abstractmethod
|
|
|
|
def resolve_ip(self, ip_address):
|
|
|
|
""" Attempts to return resolved information about the specified IP Address. If such an attempt
|
|
|
|
fails, returns None.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@nooper
|
|
|
|
class NoopIPResolver(IPResolverInterface):
|
|
|
|
""" No-op version of the security scanner API. """
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class IPResolver(IPResolverInterface):
|
2017-09-27 21:14:04 +00:00
|
|
|
def __init__(self, app):
|
2017-09-26 18:47:23 +00:00
|
|
|
self.app = app
|
|
|
|
self.geoip_db = geoip2.database.Reader('util/ipresolver/GeoLite2-Country.mmdb')
|
|
|
|
|
|
|
|
def resolve_ip(self, ip_address):
|
|
|
|
""" Attempts to return resolved information about the specified IP Address. If such an attempt fails,
|
|
|
|
returns None.
|
|
|
|
"""
|
2017-09-26 20:08:50 +00:00
|
|
|
location_function = self._get_location_function()
|
2017-09-26 18:47:23 +00:00
|
|
|
if not ip_address or not location_function:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return location_function(ip_address)
|
|
|
|
|
2017-09-27 21:14:04 +00:00
|
|
|
def _get_aws_ip_ranges(self):
|
2017-09-26 18:47:23 +00:00
|
|
|
try:
|
2017-09-27 20:39:47 +00:00
|
|
|
with open('util/ipresolver/aws-ip-ranges.json', 'r') as f:
|
2017-09-27 21:14:04 +00:00
|
|
|
return json.loads(f.read())
|
2017-09-26 20:08:50 +00:00
|
|
|
except IOError:
|
2017-09-27 20:39:47 +00:00
|
|
|
logger.exception('Could not load AWS IP Ranges')
|
2017-09-26 20:08:50 +00:00
|
|
|
return None
|
|
|
|
except ValueError:
|
2017-09-27 20:39:47 +00:00
|
|
|
logger.exception('Could not load AWS IP Ranges')
|
2017-09-26 20:08:50 +00:00
|
|
|
return None
|
|
|
|
except TypeError:
|
2017-09-27 20:39:47 +00:00
|
|
|
logger.exception('Could not load AWS IP Ranges')
|
2017-09-26 20:08:50 +00:00
|
|
|
return None
|
2017-09-27 21:14:04 +00:00
|
|
|
|
|
|
|
@ttl_cache(maxsize=1, ttl=600)
|
|
|
|
def _get_location_function(self):
|
|
|
|
aws_ip_range_json = self._get_aws_ip_ranges()
|
|
|
|
if aws_ip_range_json is None:
|
|
|
|
return None
|
2017-09-26 20:08:50 +00:00
|
|
|
|
2017-09-27 20:39:47 +00:00
|
|
|
sync_token = aws_ip_range_json['syncToken']
|
|
|
|
all_amazon, regions, services = IPResolver._parse_amazon_ranges(aws_ip_range_json)
|
2017-09-26 20:08:50 +00:00
|
|
|
return IPResolver._build_location_function(sync_token, all_amazon, regions, services, self.geoip_db)
|
2017-09-26 18:47:23 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _build_location_function(sync_token, all_amazon, regions, country, country_db):
|
|
|
|
@lru_cache(maxsize=4096)
|
|
|
|
def _get_location(ip_address):
|
|
|
|
try:
|
|
|
|
parsed_ip = IPAddress(ip_address)
|
|
|
|
except AddrFormatError:
|
|
|
|
return ResolvedLocation('invalid_ip', None, None, sync_token)
|
|
|
|
|
|
|
|
if parsed_ip not in all_amazon:
|
|
|
|
# Try geoip classification
|
|
|
|
try:
|
|
|
|
found = country_db.country(parsed_ip)
|
|
|
|
return ResolvedLocation(
|
|
|
|
'internet',
|
|
|
|
found.continent.code,
|
|
|
|
found.country.iso_code,
|
|
|
|
sync_token,
|
|
|
|
)
|
|
|
|
except geoip2.errors.AddressNotFoundError:
|
|
|
|
return ResolvedLocation('internet', None, None, sync_token)
|
|
|
|
|
|
|
|
region = None
|
|
|
|
|
|
|
|
for region_name, region_set in regions.items():
|
|
|
|
if parsed_ip in region_set:
|
|
|
|
region = region_name
|
|
|
|
break
|
|
|
|
|
|
|
|
return ResolvedLocation('aws', region, None, sync_token)
|
|
|
|
return _get_location
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _parse_amazon_ranges(ranges):
|
|
|
|
all_amazon = IPSet()
|
|
|
|
regions = defaultdict(IPSet)
|
|
|
|
services = defaultdict(IPSet)
|
|
|
|
|
|
|
|
for service_description in ranges['prefixes']:
|
|
|
|
cidr = IPNetwork(service_description['ip_prefix'])
|
|
|
|
service = service_description['service']
|
|
|
|
region = service_description['region']
|
|
|
|
|
|
|
|
all_amazon.add(cidr)
|
|
|
|
regions[region].add(cidr)
|
|
|
|
services[service].add(cidr)
|
|
|
|
|
|
|
|
return all_amazon, regions, services
|