Skip to content

Commit 34a5a7a

Browse files
authored
Merge pull request #112 from dev195/master
Switch to geoip2
2 parents 55f8a06 + f13a53c commit 34a5a7a

4 files changed

Lines changed: 67 additions & 65 deletions

File tree

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Key features:
1414

1515
* Linux (developed on Ubuntu 12.04)
1616
* Python 2.7
17-
* [pygeoip](https://github.com/appliedsec/pygeoip), GNU Lesser GPL
18-
* [MaxMind GeoIP Legacy datasets](http://dev.maxmind.com/geoip/legacy/geolite/)
17+
* [geoip2](https://github.com/maxmind/GeoIP2-python), Apache License, Version 2.0
18+
* [MaxMind GeoIP datasets](https://dev.maxmind.com/geoip/geoip2/geolite2/)
1919
* [PyCrypto](https://pypi.python.org/pypi/pycrypto), custom license
2020
* [dpkt](https://code.google.com/p/dpkt/), New BSD License
2121
* [IPy](https://github.com/haypo/python-ipy), BSD 2-Clause License
@@ -24,13 +24,11 @@ Key features:
2424

2525
## Installation
2626

27-
1. Install all of the necessary Python modules listed above. Many of them are available via pip and/or apt-get. Pygeoip is not yet available as a package and must be installed with pip or manually.
27+
1. Install all of the necessary Python modules listed above. Many of them are available via pip and/or apt-get.
2828

29-
1. `sudo apt-get install python-crypto python-dpkt python-ipy python-pypcap`
29+
* `sudo pip install geoip2 pycrypto dpkt IPy pypcap`
3030

31-
2. `sudo pip install pygeoip`
32-
33-
2. Configure pygeoip by moving the MaxMind data files (GeoIP.dat, GeoIPv6.dat, GeoIPASNum.dat, GeoIPASNumv6.dat) to <install-location>/share/GeoIP/
31+
2. Configure GeoIP by moving the MaxMind data files (GeoLite2-Country.mmdb, GeoLite2-ASN.mmdb) to <install-location>/share/GeoIP/
3432

3533
2. Run `make`. This will build Dshell.
3634

@@ -50,6 +48,10 @@ Key features:
5048
## Development
5149
* [Using Dshell With PyCharm](doc/UsingDshellWithPyCharm.md)
5250

51+
## Recent Major Updates
52+
53+
* Feb 2019 - Removed deprecated pygeoip dependency, and replaced it with geoip2. This requires the use of new GeoIP data files, listed in the Prerequisites and Installation sections above.
54+
5355
## Partners
5456

5557
Below are repositories from partners Dshell has worked together with.

docker/Dockerfile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ FROM ubuntu:14.04
44
RUN apt-get update && apt-get install -y \
55
python-crypto \
66
python-dpkt \
7-
python-ipy \
7+
python-ipy \
88
python-pypcap \
99
python-pip \
10+
python-geoip2 \
1011
wget \
1112
git
1213

13-
RUN pip install pygeoip
14-
1514
# Download the latest version of the code from GitHub
1615
WORKDIR /opt/
1716
RUN git clone https://github.com/USArmyResearchLab/Dshell.git
1817

19-
# download and gunzip GeoIP files
18+
# download and untar GeoIP files
2019
WORKDIR /opt/Dshell/share/GeoIP/
21-
RUN wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
22-
RUN wget http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz
23-
RUN wget http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
24-
RUN wget http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNumv6.dat.gz
25-
RUN gunzip *.gz
20+
RUN wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz
21+
RUN wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN.tar.gz
22+
RUN tar -zxf GeoLite2-Country.tar.gz
23+
RUN tar -zxf GeoLite2-ASN.tar.gz
24+
RUN GeoLite2-Country*/GeoLite2-Country.mmdb .
25+
RUN GeoLite2-ASN*/GeoLite2-ASN.mmdb .
2626

2727
# make Dshell
2828
WORKDIR /opt/Dshell/

lib/dshell.py

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# For IP lookups
1616
try:
17-
import pygeoip
17+
import geoip2.database
1818
except:
1919
pass
2020

@@ -106,25 +106,18 @@ def __init__(self, **kwargs):
106106
self.cleanupts = 0
107107

108108
# instantiate and save references to lookup function
109+
geoip_dir = os.path.join(os.environ['DATAPATH'], "GeoIP")
109110
try:
110-
self.geoccdb = [pygeoip.GeoIP(os.environ[
111-
'DATAPATH'] + '/GeoIP/GeoIP.dat', pygeoip.MEMORY_CACHE).country_code_by_addr]
112-
try:
113-
self.geoccdb.append(pygeoip.GeoIP(os.environ[
114-
'DATAPATH'] + '/GeoIP/GeoIPv6.dat', pygeoip.MEMORY_CACHE).country_code_by_addr)
115-
except:
116-
pass
111+
self.geoccdb = geoip2.database.Reader(
112+
os.path.join(geoip_dir, "GeoLite2-Country.mmdb")
113+
).country
117114
except:
118115
self.geoccdb = None
119116

120117
try:
121-
self.geoasndb = [pygeoip.GeoIP(
122-
os.environ['DATAPATH'] + '/GeoIP/GeoIPASNum.dat', pygeoip.MEMORY_CACHE).org_by_addr]
123-
try:
124-
self.geoasndb.append(pygeoip.GeoIP(os.environ[
125-
'DATAPATH'] + '/GeoIP/GeoIPASNumv6.dat', pygeoip.MEMORY_CACHE).org_by_addr)
126-
except:
127-
pass
118+
self.geoasndb = geoip2.database.Reader(
119+
os.path.join(geoip_dir, "GeoLite2-ASN.mmdb")
120+
).asn
128121
except:
129122
self.geoasndb = None
130123

@@ -250,41 +243,48 @@ class members will be updated from value'''
250243
if self.name in options:
251244
self.parseOptions(options[self.name])
252245

253-
def getGeoIP(self, ip, db=[], notfound='--'):
246+
def getGeoIP(self, ip, db=None, notfound='--'):
254247
"""
255-
Get record associated with an IP
256-
requires GeoIP
248+
Get country code associated with an IP.
249+
Requires GeoIP library (geoip2) and data files.
257250
"""
258-
o = None
259-
if db == []:
260-
db = self.geoccdb # default to self.geoccdb
261-
for d in db:
262-
try:
263-
o = d(ip)
264-
except:
265-
# traceback.print_exc() # removed by bg on 20121203
266-
continue # passing ipv6 address to ipv4 lookup or v/v
267-
if o:
268-
return o # stop when we get a result
269-
return notfound
270-
271-
def getASN(self, ip, db=[], notfound='--'):
251+
if not db:
252+
db = self.geoccdb
253+
try:
254+
# Get country code based on order of importance
255+
# 1st: Country that owns an IP address registered in another
256+
# location (e.g. military bases in foreign countries)
257+
# 2nd: Country in which the IP address is registered
258+
# 3rd: Physical country where IP address is located
259+
# https://dev.maxmind.com/geoip/geoip2/whats-new-in-geoip2/#Country_Registered_Country_and_Represented_Country
260+
location = db(ip)
261+
country = (
262+
location.represented_country.iso_code or
263+
location.registered_country.iso_code or
264+
location.country.iso_code or
265+
notfound
266+
)
267+
return country
268+
except Exception:
269+
# Many expected exceptions can occur here. Ignore them all and
270+
# return default value.
271+
return notfound
272+
273+
def getASN(self, ip, db=None, notfound='--'):
272274
"""
273-
Get record associated with an IP
274-
requires GeoIP
275+
Get ASN associated with an IP.
276+
Requires GeoIP library (geoip2) and data files.
275277
"""
276-
o = None
277-
if db == []:
278-
db = self.geoasndb # default to self.geoccdb
279-
for d in db:
280-
try:
281-
o = d(ip)
282-
except:
283-
# traceback.print_exc() # removed by bg on 20121203
284-
continue # passing ipv6 address to ipv4 lookup or v/v
285-
if o:
286-
return o # stop when we get a result
287-
return notfound
278+
if not db:
279+
db = self.geoasndb
280+
try:
281+
template = "AS{0.autonomous_system_number} {0.autonomous_system_organization}"
282+
asn = template.format( db(ip) )
283+
return asn
284+
except Exception:
285+
# Many expected exceptions can occur here. Ignore them all and
286+
# return default value.
287+
return notfound
288288

289289
def close(self, conn, ts=None):
290290
'''for connection based decoders
@@ -851,9 +851,9 @@ def __init__(self, decoder, addr, ts=None, pkt=None, **kwargs):
851851
# cache
852852
try:
853853
self.info(sipcc=decoder.getGeoIP(self.sip, db=decoder.geoccdb),
854-
sipasn=decoder.getGeoIP(self.sip, db=decoder.geoasndb),
854+
sipasn=decoder.getASN(self.sip, db=decoder.geoasndb),
855855
dipcc=decoder.getGeoIP(self.dip, db=decoder.geoccdb),
856-
dipasn=decoder.getGeoIP(self.dip, db=decoder.geoasndb))
856+
dipasn=decoder.getASN(self.dip, db=decoder.geoasndb))
857857
except:
858858
self.sipcc, self.sipasn, self.dipcc, self.dipasn = None, None, None, None
859859

share/GeoIP/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
GeoIP Legacy data sets go here.
1+
GeoIP data sets go here.

0 commit comments

Comments
 (0)