Skripts IP adrešu diapazonu bloķēšanai saiknē ar valsts nosaukumiem. Piemēram, iespējams bloķēt visas valstis DDoS uzbrukumu laikā, kā arī aizliegt e-pasta pienākšanu no valstīm, ar kurām sarakstei nav jānotiek.
IP piesaistes saraksts valstu nosaukumam tiek ielādēts no vietnes http://www.ipdeny.com/ipblocks/data/countries
#!/bin/bash
### Block all traffic from AFGHANISTAN (af) and CHINA (CN). Use ISO code ###
ISO="af cn"
### Set PATH ###
IPT=/sbin/iptables
WGET=/usr/bin/wget
EGREP=/bin/egrep
### No editing below ###
SPAMLIST="countrydrop"
ZONEROOT="/root/iptables"
DLROOT="http://www.ipdeny.com/ipblocks/data/countries"
cleanOldRules(){
$IPT -F
$IPT -X
$IPT -t nat -F
$IPT -t nat -X
$IPT -t mangle -F
$IPT -t mangle -X
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT -P FORWARD ACCEPT
}
# create a dir
[ ! -d $ZONEROOT ] && /bin/mkdir -p $ZONEROOT
# clean old rules
cleanOldRules
# create a new iptables list
$IPT -N $SPAMLIST
for c in $ISO
do
# local zone file
tDB=$ZONEROOT/$c.zone
# get fresh zone file
$WGET -O $tDB $DLROOT/$c.zone
# country specific log message
SPAMDROPMSG="$c Country Drop"
# get
BADIPS=$(egrep -v "^#|^$" $tDB)
for ipblock in $BADIPS
do
$IPT -A $SPAMLIST -s $ipblock -j LOG --log-prefix "$SPAMDROPMSG"
$IPT -A $SPAMLIST -s $ipblock -j DROP
done
done
# Drop everything
$IPT -I INPUT -j $SPAMLIST
$IPT -I OUTPUT -j $SPAMLIST
$IPT -I FORWARD -j $SPAMLIST
# call your other iptable script
# /path/to/other/iptables.sh
exit 0
Secīgas nosacījumu pievienošanas vietā ātrdarbības uzlabošanai tiek rekomendēts izmantot sistēmu ipset (http://ipset.netfilter.org/) vai nfqueue (http://nfqueue.sf.net/)
Izmantošana ir apmēram šāda:
#!/bin/sh
iptables -F INPUT
ipset -N spam ipmap
cat block_country_net_list.txt | while read net; do
ipset -A spam --network $net
done
iptables -A INPUT -m set --set spam src -j REJECT
Otrs efektīgs variants – izmanoto iptables moduli geoip (http://people.netfilter.org/peejix/geoip/), kas neietilpst iptables bāzes komplektā, bet jāuzstāda kopā ar patch-o-matic, kā arī jāielādē papildus datu bāze ar valstu piesaistes datiem, piemēram, no vietnes http://www.maxmind.com/
Piemēram, ICMP pieprasījumu bloķēšana no Francijas, Itālijas un Spānijas izskatīsies sekojoši:
iptables -A OUTPUT -p icmp -m geoip --dst-cc FR,IT,ES -j REJECT







