| Sunday November 23rd 2014

Feedburner

Subscribe by email:

We promise not to spam/sell you.


Search Amazon deals:

Iptables country blocklist API


hot female plumberThis tool will automatically create iptables rules on Linux system to block all connection from one country or more. The blocklist is created with an API that fetch data in our SQL database based on IP address country location.

How often is data updated?
During the first week of each month.

How accurate is the data?
This is a somehow complex grouping of IP by country and CIDR of our database. The data has over 99.9% accuracy on a country basis. You can try a few traceroute.


API Usage
First you need to know the code (ISO 3166 format) of the country you would like to block. The full list is available here Once you have the country code, you can now get the list at the following url (Afghanistan and Argentina in this example). If your browser show the data on a single line, simply view the page source code :

http://iplocationtools.com/country_query.php?country=AF,AR

This will output something like this :

58.147.128.0/19
67.212.160.0/24
80.247.139.0/24
82.205.190.0/21
82.205.198.0/23
82.205.202.0/22
[...]

Automatic bash script
The following script will fetch the right IP addresses of the country you would like to block from our API and then add these rules in iptables.

#!/bin/bash
###PUT HERE COMA SEPARATED LIST OF COUNTRY CODE###
COUNTRIES="AK,AR"
WORKDIR="/home/someuser"
#######################################
cd $WORKDIR
wget -c --output-document=iptables-blocklist.txt http://iplocationtools.com/country_query.php?country=$COUNTRIES
if [ -f iptables-blocklist.txt ]; then
iptables -F
BLOCKDB="iptables-blocklist.txt"
IPS=$(grep -Ev "^#" $BLOCKDB)
for i in $IPS
do
iptables -A INPUT -s $i -j DROP
iptables -A OUTPUT -d $i -j DROP
done
fi
rm -f $WORKDIR/iptables-blocklist.txt

Source

Related Posts: On this day...

Leave a Reply

You must be logged in to post a comment.