Xtables - GeoIP

IPSET AS Number

(09:30:07) Ian LSLTD: Tak, zainstaluj sobie pakiety xtables z repo Debian'a.  Potem tworz sobie takiego skryptu i odpal z cron'a
(09:30:19) Ian LSLTD: #!/bin/bash
#
# Get GeoIP database, unpack and prepare for iptables
cd /usr/share/xt_geoip/
/usr/lib/xtables-addons/xt_geoip_dl
/usr/lib/xtables-addons/xt_geoip_build *.csv
rm -rf *.zip
rm -rf *.csv# Get IP Block Data for IPSET rules
cd /etc/iptables/geoip
rm -rf gb.zone
rm -rf pl.zone
wget http://ipdeny.com/ipblocks/data/countries/gb.zone
wget http://ipdeny.com/ipblocks/data/countries/pl.zone
for ipgb in `cat gb.zone` ; do /sbin/ipset add IPGB $ipgb ; done
for ippl in `cat pl.zone` ; do /sbin/ipset add IPPL $ippldone
/sbin/ipset save -file /etc/iptables/ipset# Restart iptables and fail2ban
/etc/init.d/netfilter-persistent restart
/etc/init.d/fail2ban restart(09:31:04) Ian LSLTD: Reguly iptables:
-A INPUT -m geoip --src-cc GB,PL -p tcp -m multiport --dports 993,995 -m state --state NEW -j ACCEPT
-A INPUT -m set --set IPGB src -p tcp -m multiport --dports 993,995 -m state --state NEW -j ACCEPT
-A INPUT -m set --set IPPL src -p tcp -m multiport --dports 993,995 -m state --state NEW -j ACCEPT(09:31:57) Ian LSLTD: cat /etc/cron.d/geoip 
# cron to get geoip for iptables
0 1 * * 0    root    /usr/local/bin/get_geoip_db > /tmp/geoip.log 2>&1

--- OLD ---

#!/bin/bash
#
# Get GeoIP database

cd /etc/graylog/server
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz > /dev/null 2>&1
tar zxf GeoLite2-City.tar.gz
cp GeoLite2-City_*/GeoLite2-City.mmdb ./
rm -rf GeoLite2-City.tar.gz
rm -rf GeoLite2-City_*# Restart graylog
/etc/init.d/graylog-server restart > /dev/null 2>&1

czasem potrzeba dpkg-reconfigure xtables-addons-dkms

  1. Instalacja programów potrzebnych do poprawnej instalacji i konfiguracji iptables-geoip.

    apt-get install libtext-csv-xs-perl module-assistant geoip-database libgeoip1

  2. Instalacja modułów xtables-addons.

    module-assistant --verbose --text-mode auto-install xtables-addons

  3. Pobranie skryptów i przetworzenie baz geoip (maxmind).

    mkdir /usr/share/xt_geoip
    cd /usr/share/xt_geoip
    wget http://man.sethuper.com/wp-content/uploads/2013/06/geoip-dl-build.tar.gz
    tar xvf geoip-dl-build.tar.gz
    ./xt_geoip_dl
    ./xt_geoip_build -D . *.csv
    rm -fr geoip-dl-build.tar.gz
  1. Konfiguracja iptables.

    Przykład regółki do iptables. Kraje zablokowane: Chiny (CN), Ukraina (UA), Taiwan (TW).

    iptables -A INPUT -m geoip --src-cc CN,UA,TW -j DROP

Inny opis

http://terminal28.com/how-to-block-countries-using-iptables-debian/

Tested:    Debian 6.0.x,
    Debian 7.x.x.
    Debian 8.x.x
    iptables v1.4.141. Install some needed iptables-geoip dependencies .sudo apt-get install libtext-csv-xs-perl module-assistant geoip-database libgeoip12. Install modules xtables-addons.For Debian 6 Squeeze and Debian 7 Wheezy:sudo module-assistant --verbose --text-mode auto-install xtables-addonsFor Debian 8 Jessie:apt-get install libtext-csv-xs-perl xtables-addons-common3. Download and install database geoip (maxmind).sudo mkdir /usr/share/xt_geoip
cd /usr/share/xt_geoip
sudo wget http://terminal28.com/wp-content/uploads/2013/10/geoip-dl-build.tar.gz
sudo tar xvf geoip-dl-build.tar.gz
sudo ./xt_geoip_dl
sudo ./xt_geoip_build -D . *.csv
sudo rm -fr geoip-dl-build.tar.gz4. Configure iptables.Example.
Blocked countries: China (CN), Ukraine (UA), Taiwan (TW).iptables -A INPUT -m geoip --src-cc CN,UA,TW -j DROP