Shell Blocking Scripts:
Both scripts require SuperUser to be activated and run from BASH and IPTables must be implemented as well. I make NO Claims to the use of these or how you implement them on your system. Nor do I warranty or will I support them in any way. Use them at YOUR OWN RISK, and by all means if you don’t understand the code. Don’t Use them.
blockingscript.sh
#!/bin/bash # Originally from http://www.cyberciti.biz/faq/block-entier-country-using-iptablesif [ “$(id -u)” != “0” ]; then echo “This Script must be run as root” 1>&2 exit 1 fi### Block all traffic from AFGHANISTAN (af) and CHINA (CN). Use ISO code ### ISO=”af ar cn kr ru hu id ir iq pk” ### Set PATH ### IPT=/sbin/iptables WGET=/usr/bin/wget EGREP=/bin/egrep### No editing below ### CBLIST=”countrydrop” ZONEROOT=”/var/iptables” IPTCBRESTORE=”/etc/sysconfig/iptables.cb” IPTCBDEVICE=eth0 ALLOWPORTS=80,443 ALLOWSUBNET=192.168.0.0/255.255.0.0 MAXZONEAGE=7 DLROOT=”http://www.ipdeny.com/ipblocks/data/countries”cleanOldRules(){ $IPT -L $CBLIST > /dev/null 2>&1 if [ $? = 0 ] ; then $IPT -D INPUT ${IPTCBDEVICE:+-i }${IPTCBDEVICE} -j $CBLIST $IPT -D OUTPUT ${IPTCBDEVICE:+-o }${IPTCBDEVICE} -j $CBLIST $IPT -D FORWARD ${IPTCBDEVICE:+-i }${IPTCBDEVICE} -j $CBLIST fi $IPT -F $CBLIST $IPT -X $CBLIST for i in `$IPT -L -n | grep Chain | cut -f 2 -d ‘ ‘ | grep ‘\-$CBLIST’` do $IPT -F ${i} $IPT -X ${i} done }updateZoneFiles() { ZONEARCH=${ZONEROOT}/arch mkdir -p ${ZONEARCH} find ${ZONEROOT} -maxdepth 1 -mindepth 1 -ctime +${MAXZONEAGE} -exec mv {} ${ZONEARCH} \; for c in $ISO do # local zone file tDB=$ZONEROOT/$c.zone if [ -f $tDB ] ; then createIPTLoadFile() { if [ “Z${ALLOWPORTS}” = “Z” ] ; then if [ “Z${ALLOWSUBNET}” = “Z” ] ; then for c in $ISO # country specific log message # Create drop chain for identified packets # Load IP ranges into chains correlating to first octet directLoadTables() { if [ “Z${ALLOWPORTS}” = “Z” ] ; then if [ “Z${ALLOWSUBNET}” = “Z” ] ; then for c in $ISO # country specific log message # Create drop chain for identified packets # Load IP ranges into chains correlating to first octet loadTables() { # create a dir # clean old rules # update zone files as needed # create a new iptables list exit 0 |
UPDATE 01.02.12 Added removal of some more Replicated lines near end of script. Also creation of DenyHosts.ip file that can be used in your .htaccess file.
IPBlock.sh
#!/bin/bashif [ “$(id -u)” != “0” ]; then echo “This Script must be run as root” 1>&2 exit 1 fi### Preserve Former Blacklist.txt echo ‘Backing up Blacklist Data’ cp Blacklist.ip Blacklist.`date +”%Y%m%d%H%M%S”`.bak### Setting up for Blacklist – Removal and Creation of tmp echo ‘Removing old datafiles’ if [ -f /tmp/Blacklist*.txt ] then echo ‘Removing old datafiles’ rm tmp/Blacklist*.txt rm -r tmp rm DenyHosts.ip else echo ‘Success, no old datafiles found’ fi echo ‘Creating tmp directory’ ### Grab IP from Apache2 error.log ### Grap IP from Apache2 ARCHIVED error.log ### Combine Blacklists into newfile Blacklist3.txt ### Remove any reference to localhost 127.0.0.1 and [LOCAL IP] and [YOUR IP HERE] and unknown statements ### Sort IP addresses output to Blacklist7.txt ### Remove any Duplicates and output to Blacklist.ip ### Create Deny Hosts File echo ‘Banning…’ echo ‘Backing up Rules…’ ### Remove old files |