GithubHelp home page GithubHelp logo

geoiplookup's Introduction

GeoIP Protection Python Script

Description

This is a Python script to check an IP address against MaxMind free GeoIP database. The builtin geoip features of iptables use geoip v1 which is now depreciated. This uses the current geoip v2 database.

This script is designed to be used as part of connection verification for any linux service that supports tcp wrappers. To find out if your service does perform the following command

ldd $(which service) | grep libwrap.so

The script provides a debug option to display the country codes from the DB lookup.

Installation

Please make sure the requirments are met below.
Copy script to /usr/local/bin and update the permissions.

sudo cp geoiplookup /usr/local/bin
chmod 755 /usr/local/bin/geoiplookup

Update the file /etc/hosts.deny
sshd: ALL
Update the file /etc/hosts.allow
sshd: ALL: aclexec python3 /usr/local/bin/geoiplookup %a

Usage

Update the ALLOWED_COUNTRIES variable at top of script. Format is 2 letter country code.

ALLOWED_COUNTRIES="AU NZ US GB PL SE"

usage: geoiplookup [-h] [-d] address

positional arguments:
  address      check the address against the geoip db

optional arguments:
  -h, --help   show this help message and exit
  -d, --debug  display debug information

Returns exit code of 0 on allowed country otherwise 1. The -d provides additional debug information which should not be used in production. Don't use this option in the hosts.allow file.

Testing

Allowed Region

$ geoiplookup 8.8.8.8

Return code: 0

Denied Region

$ geoiplookup 1.2.3.4

Return code: 1

Allowed Region - Debug Mode

$ geoiplookup -d 8.8.8.8
Allowed Countries = AU NZ US GB SE PL
ISO name found as US. Country name found as United States.
{"geoname_id": 6252001, "iso_code": "US", "names": {"de": "USA", "en": "United States", "es": "Estados Unidos", "fr": "\u00c9tats-Unis", "ja": "\u30a2\u30e1\u30ea\u30ab\u5408\u8846\u56fd", "pt-BR": "Estados Unidos", "ru": "\u0421\u0428\u0410", "zh-CN": "\u7f8e\u56fd"}}
Access ALLOWED from 8.8.8.8 from country United States

Return code: 0

Requirements

This code requires Python 3.5+. Older versions are not supported.

You must first install the maxmind DB. Register a user on the maxmind website and install the free database into your system using one of the provided deb packages from https://github.com/maxmind/geoipupdate/releases/latest

sudo dpkg -i geoipupdate_4.3.0_linux_amd64.deb
Once installed update the /etc/GeoIP.conf with your free license from maxmind.com. Look for My License Key on the navigation menus. Typically it wont exist so you need select generate new license key.
One you have updated the config file, run the update to check its all working. sudo geoipupdate -v

Now install the maxmind C library for faster lookups and a compiler.
sudo apt install libmaxminddb0 libmaxminddb-dev build-essentials

Finally you have to install the maxmind Python module
https://github.com/maxmind/MaxMind-DB-Reader-python/archive/master.zip
Extract the zip
Change to the directory and then run
python3 setup.py build
To make sure it builds with the C library above. It will tell you if it fails.

Once it builds without problems
sudo python3 setup.py install

Support

Please report all issues with this code using the GitHub issue tracker

Please note, support is limited. I figured this out after a beer one night.

geoiplookup's People

Contributors

rtfmoz2 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.