Path: blob/master/tutorials-and-examples/feature-tutorials/GeoIPLookups.ipynb
3253 views
Title: msticpy - GeoIP Lookup
Introduction
This module contains two classes that allow you to look up the Geolocation of IP Addresses.
You must have msticpy installed to run this notebook:
MaxMind GeoIPLite
This product includes GeoLite2 data created by MaxMind, available from https://www.maxmind.com.
This uses a local database which is downloaded first time when class object is instantiated. It gives very fast lookups but you need to download updates regularly. Maxmind offers a free tier of this database, updated monthly. For greater accuracy and more detailed information they have varying levels of paid service. Please check out their site for more details.
The geoip module uses official maxmind pypi package - geoip2 and also has options to customize the behavior of local maxmind database.
db_folder: Specify custom path containing local maxmind city database. If not specified, download to .msticpy dir under user`s home dir.force_update: can be set to True/False to issue force update despite of age check.Check age of maxmind city database based on database info and download new if it is not updated in last 30 days.
``auto_update``` : can be set to True/False Allow option to override auto update database if user is desired not to update database older than 30 days.
IPStack
This library uses services provided by ipstack. https://ipstack.com
IPStack is an online service and also offers a free tier of their service. Again, the paid tiers offer greater accuracy, more detailed information and higher throughput. Please check out their site for more details.
IPStack Geo-lookup Class
Class Initialization
Note - requires IPStack API Key, Optional parameter bulk_lookup allows multiple IPs in a single request. This is only available with the paid Professional tier and above.
lookup_ip method
You will need a IPStack API key
You will get more detailed results and a higher throughput allowance if you have a paid tier. See IPStack website for more details
Taking input from a pandas DataFrame
The base class for both implementations has a method that sources the ip addresses from a dataframe column and returns a new dataframe with the location information merged with the input frame
Creating a Custom GeopIP Lookup Class
You can derive a class that implements the same operations to use with a different GeoIP service.
The class signature is as follows:
You should override the lookup_ip method implementing your own method of geoip lookup.
Calculating Geographical Distances
Use the geo_distance function from msticpy.sectools.geoip to calculated distances between two locations. I am indebted to Martin Thoma who posted this solution (which I've modified slightly) on Stackoverflow.
Or where you have source and destination IpAddress entities, you can use the wrapper entity_distance.