#!/bin/bash REV="$Revision: 4 $" VERSION="0.1" RELEASE=`echo ${REV} | awk '{ print $2}'` # Arg. check if [ "${1}" = "" ]; then echo `basename ${0}`," v.${VERSION}.${RELEASE}" echo "Usage Error: missing arguments" echo "" echo " Usage: ${0} " echo " Country codes available at http://www.maxmind.com/app/iso3166" echo "" exit else # too lazy to work on a script that will download from the right server regarding what has been passed at the prompt :p # http://en.wikipedia.org/wiki/RIPE_Network_Coordination_Centre echo "The country specified belongs to :" echo "1. Africa" echo "2. Asia/Pacific" echo "3. America" echo "4. Latin America/Caribbean" echo "5. Europe" echo "" echo -n "Your zone : " read ZONE echo "" echo "What kind of list do you want to generate ?" echo "1. A list of blocks : simple list, 1 block per line" echo "2. Iptables rulesets : you can set what comes before and after the blocks" echo "" echo -n "Please make your choice [1/2] : " read LISTRULE if [ "${LISTRULE}" = "2" ]; then echo "Please specify the prefix rule (e.g. : iptables -A INPUT -s)" read PRERULE echo "Specify the postfix rule (e.g. : -j DROP)" read POSTRULE fi # Afrinic (afrinic.net) : ftp://ftp.apnic.net/public/stats/afrinic/delegated-afrinic-latest # Apnic (apnic.net) : ftp://ftp.apnic.net/public/stats/apnic/assigned-apnic-latest # Arin (arin.net) : ftp://ftp.apnic.net/public/stats/arin/delegated-arin-latest # Lacnic (lacnic.net) : ftp://ftp.apnic.net/public/stats/lacnic/delegated-lacnic-latest # Ripe (ripe.net) : ftp://ftp.apnic.net/public/stats/ripe-ncc/delegated-ripencc-latest LINK="ftp://ftp.apnic.net/public/stats/" if [ "${ZONE}" = "1" ]; then FILE="afrinic/delegated-afrinic-latest" elif [ "${ZONE}" = "2" ]; then FILE="apnic/assigned-apnic-latest" elif [ "${ZONE}" = "3" ]; then FILE="arin/delegated-arin-latest" elif [ "${ZONE}" = "4" ]; then FILE="lacnic/delegated-lacnic-latest" elif [ "${ZONE}" = "5" ]; then FILE="ripe-ncc/delegated-ripencc-latest" fi TMP="/tmp" COUNTRY=${1} OUT="${TMP}/blocks-${COUNTRY}" cd ${TMP} echo "" echo "Download :" /usr/bin/wget -v --progress=bar ${LINK}/${FILE} -O ${TMP}/db_${COUNTRY} echo "" rm -f ${OUT} for country in ${COUNTRY} do IPS=`cat ${TMP}/db_${COUNTRY} | grep "${country}" | egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | sed -re "s/(ripencc\||${country}|\|ipv(4|6)\||\|allocated|\|assigned|\|(199|200)[0-9]{5})//g;s/\|128/\/25/;s/\|256/\/24/;s/\|512/\/23/;s/\|1024/\/22/;s/\|2048/\/21/;s/\|4096/\/20/;s/\|8192/\/19/;s/\|16384/\/18/;s/\|32768/\/17/;s/\|65536/\/16/;s/\|131072/\/15/;s/\|262144/\/14/;s/\|524288/\/13/;s/\|1048576/\/12/;s/\|2097152/\/11/;s/\|4194304/\/10/;s/\|8388608/\/9/;s/\|16777216/\/8/"` for ips in ${IPS} do if [ "${LISTRULE}" = "2" ]; then echo "${PRERULE} ${ips} ${POSTRULE}" >> ${OUT} else echo "${ips}" >> ${OUT} fi done done echo "Block list saved as ${OUT}" echo "" rm -f ${FILE} fi