Country Blocking

Post new topic   Reply to topic    DD-WRT Forum Index -> Broadcom SoC based Hardware
Goto page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
Author Message
badmoon
DD-WRT Novice


Joined: 22 Jul 2014
Posts: 41

PostPosted: Thu Apr 09, 2015 16:28    Post subject: Country Blocking Reply with quote
This may have been talked about before, but I figured I would share anyways.

To block countries from your network.

ipblock.sh

Code:

#!/bin/sh

#set -x

### Block all traffic from listed. Use ISO code ###
ISO="cn-aggregated tw-aggregated kp-aggregated kr-aggregated ru-aggregated ir-aggregated"

#Testing
#ISO="kr-aggregated"

### Set PATH ###
IPT=/usr/sbin/iptables
WGET=/usr/bin/wget
EGREP=/bin/egrep
LOCKFILE=/tmp/ipblocklock.txt

### No editing below ###
inSPAMLIST="countrydropin"
outSPAMLIST="countrydropout"
ZONEROOT="/opt/ipblock/zones"
DLROOT="http://www.ipdeny.com/ipblocks/data/aggregated"
iBL="/opt/ipblock/zones/ipblockin.sh"
oBL="/opt/ipblock/zones/ipblockout.sh"

if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
    echo "Lock file exist.. exiting"
    exit
fi

# make sure the lockfile is removed when we exit and then claim it
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}

cleanOldRules(){
$IPT -F countrydropin
$IPT -F countrydropout
}

# create a dir
[ ! -d $ZONEROOT ] && /bin/mkdir -p $ZONEROOT

# clean old rules
cleanOldRules
rm -f $iBL
rm -f $oBL

echo '*filter' > $iBL
echo '*filter' > $oBL

for c in $ISO
do
        # local zone file
        tDB=$ZONEROOT/$c.zone

        # get fresh zone file
        $WGET -T 30 -O $tDB $DLROOT/$c.zone

        awk -v inSPAMLIST=$inSPAMLIST '{print "-A "inSPAMLIST" -s "$1" -j DROP"}' $tDB >> $iBL
        awk -v outSPAMLIST=$outSPAMLIST '{print "-A "outSPAMLIST" -d "$1" -j REJECT"}' $tDB >> $oBL
done

echo 'COMMIT' >> $iBL
echo 'COMMIT' >> $oBL

iptables-restore -n < $iBL
iptables-restore -n < $oBL

rm -f ${LOCKFILE}


Add to the bottom of your firewall commands

Code:

iptables -N countrydropin
iptables -N countrydropout
iptables -I INPUT 2 -i vlan2 -j countrydropin
iptables -I FORWARD 2 -i vlan2 -j countrydropin
iptables -I FORWARD 3 -o vlan2 -j countrydropout
sh /opt/ipblock/ipblock.sh &


The INPUT 6 and FORWARDS depends on where the following rule is located in your 2 tables.

ACCEPT 0 -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

The -j countrydropin and countrydropout should be below the above rule so that only NEW connections are sent through the 1000's of rulesets.

The vlan2 is whatever your wan (public IP) interface is. For most it is probably vlan2, but can change to ppp0 for some PPPoE users.

Add to your cron

Code:

@weekly /opt/ipblock/ipblock.sh


Takes a few seconds to finish on the R7000, but it doesn't delay start-up. It shouldn't run more than once at a time because of the lock file. Hope someone finds this useful. Thanks goes to JAMESMTL for helping to polish this script.


Last edited by badmoon on Sat Apr 18, 2015 15:18; edited 10 times in total
Sponsor
JAMESMTL
DD-WRT Guru


Joined: 13 Mar 2014
Posts: 856
Location: Montreal, QC

PostPosted: Thu Apr 09, 2015 19:34    Post subject: Reply with quote
I run similar geo & TOR blocking scripts and you can significantly reduce processing time and router cpu utilization by simply using iptables restore for your block chain(s). In my case I whitelist countries and blacklist tor exit nodes and it literally takes only a few seconds to process and load some 25K IPv4 and IPv6 rules. Also running on an R7000.
slidermike
DD-WRT Guru


Joined: 11 Nov 2013
Posts: 1487
Location: USA

PostPosted: Thu Apr 09, 2015 20:34    Post subject: Reply with quote
James,
thank you for adding.
Care to explain the process a little more on adding this to reduce the cpu load?

I have added this thread to the suggested list of URLs for users in the sticky.

_________________
Router currently owned:
Netgear R7800 - Router
Netgear R7000 - AP mode

R7000 specific Tips/Tricks.
http://www.dd-wrt.com/phpBB2/viewtopic.php?t=264152
JAMESMTL
DD-WRT Guru


Joined: 13 Mar 2014
Posts: 856
Location: Montreal, QC

PostPosted: Thu Apr 09, 2015 21:00    Post subject: Reply with quote
http://www.iptables.info/en/iptables-save-restore-rules.html

Quote:

Speed considerations

One of the largest reasons for using the iptables-save and iptables-restore commands is that they will speed up the loading and saving of larger rule-sets considerably. The main problem with running a shell script that contains iptables rules is that each invocation of iptables within the script will first extract the whole rule-set from the Netfilter kernel space, and after this, it will insert or append rules, or do whatever change to the rule-set that is needed by this specific command. Finally, it will insert the new rule-set from its own memory into kernel space. Using a shell script, this is done for each and every rule that we want to insert, and for each time we do this, it takes more time to extract and insert the rule-set.

To solve this problem, there is the iptables-save and restore commands. The iptables-save command is used to save the rule-set into a specially formatted text-file, and the iptables-restore command is used to load this text-file into kernel again. The best parts of these commands is that they will load and save the rule-set in one single request. iptables-save will grab the whole rule-set from kernel and save it to a file in one single movement. iptables-restore will upload that specific rule-set to kernel in a single movement for each table. In other words, instead of dropping the rule-set out of kernel some 30,000 times, for really large rule-sets, and then upload it to kernel again that many times, we can now save the whole thing into a file in one movement and then upload the whole thing in as little as three movements depending on how many tables you use.


Ideally for these types of large firewall rules you would use ipsets instead of iptables chains as it is the fastest way of handling large rulesets but that is not supported natively by dd-wrt at this time.

I'll attach an example of my script in a bit as I'm on my iPad.
JAMESMTL
DD-WRT Guru


Joined: 13 Mar 2014
Posts: 856
Location: Montreal, QC

PostPosted: Thu Apr 09, 2015 22:44    Post subject: Reply with quote
Here are examples of my IPv4/IPv6 geo-blocking and TOR exit node blocking scripts.

Note there are additional lines in the scripts used for reporting and are not needed for simple blocking. I have additional scripts to block static IPs and report blocked packets and bytes which I have not included.

Pay attention to where the chains are inserted into the iptables rules as you want them to be inserted after the established & related state rule so that they are not processed needlessly and to allow LAN access to blocked IP addresses.

For IPv6 you would also want to ensure the rules are placed after the accept echo request rules

Execution times for these scripts are measured in seconds


**** sample scripts deleted to avoid confusion


Last edited by JAMESMTL on Mon Dec 14, 2015 5:47; edited 1 time in total
badmoon
DD-WRT Novice


Joined: 22 Jul 2014
Posts: 41

PostPosted: Fri Apr 10, 2015 2:01    Post subject: Reply with quote
Nice addition jamesmtl. I'll take a look at your layout. One thing I have noticed so far is 14k rulesets in iptables runs terrible on the R7000 once you start putting any kind of load through it. 1400 seems to run just fine. I'll have to find that "sweet spot".
JAMESMTL
DD-WRT Guru


Joined: 13 Mar 2014
Posts: 856
Location: Montreal, QC

PostPosted: Fri Apr 10, 2015 2:31    Post subject: Reply with quote
Take a closer look at my code. I run 25K rules with no lag whatsoever. You need to ensure your chains are only processed when needed

Code:
iptables -vnL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
 8440 1230K ACCEPT     0    --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       66.220.2.74          0.0.0.0/0
   64 16586 ipv4block  0    --  vlan2  *       0.0.0.0/0            0.0.0.0/0
   60 16372 torblock   0    --  vlan2  *       0.0.0.0/0            0.0.0.0/0
   60 16372 blocklist  0    --  vlan2  *       0.0.0.0/0            0.0.0.0/0
....
ip6tables -vnL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
  503 50094 ACCEPT     0        *      *       ::/0                 ::/0               state RELATED,ESTABLISHED
  120  9168 ACCEPT     icmpv6    *      *       ::/0                 ::/0
    0     0 ipv6block  0        ip6tun *       ::/0                 ::/0
....
ip6tables -vnL FORWARD
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
   22  1716 TCPMSS     tcp      *      *       ::/0                 ::/0               tcp flags:0x06/0x02 TCPMSS clamp to PMTU
  526  165K ACCEPT     0        *      *       ::/0                 ::/0               state RELATED,ESTABLISHED
   12  1248 icmpv6-filter  icmpv6    *      *       ::/0                 ::/0
   14 10504 ipv6block  0        ip6tun *       ::/0                 ::/0
   12   972 ACCEPT     0        *      ip6tun  ::/0                 ::/0
...
/jffs/etc/config/viewblocklist.sh

----------------------------------------
Ruleset           Rules  Packets  Bytes
---------------- ------- ------- -------
Blocklist              9     141     10K
TOR Block          2,765       0       0
IPv4 Geoblock     19,683      4K    293K
IPv6 Geoblock      4,288     147      9K
----------------------------------------
Uptime: 6 days, 21 hours, 5 minutes
----------------------------------------

Bird333
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 809

PostPosted: Fri Apr 10, 2015 12:45    Post subject: Reply with quote
JAMESMTL wrote:
Here are examples of my IPv4/IPv6 geo-blocking and TOR exit node blocking scripts.

Note there are additional lines in the scripts used for reporting and are not needed for simple blocking. I have additional scripts to block static IPs and report blocked packets and bytes which I have not included.

Pay attention to where the chains are inserted into the iptables rules as you want them to be inserted after the established & related state rule so that they are not processed needlessly and to allow LAN access to blocked IP addresses.

For IPv6 you would also want to ensure the rules are placed after the accept echo request rules

Execution times for these scripts are measured in seconds


Thanks for posting. Could you post a stripped down version of your scripts just for blocking? Also, what purpose does the TOR script serve? If I am not running IPv6, can I just comment out the IPv6 stuff? Could you also post your counter script?
badmoon
DD-WRT Novice


Joined: 22 Jul 2014
Posts: 41

PostPosted: Fri Apr 10, 2015 15:43    Post subject: Reply with quote
JamesMTL, I made the changes you suggested and it works great. iptables-restore loads really fast (0.77 seconds) for 14k rows. I had not moved the -j below the state RELATED,ESTABLISHED. Moving that took care of my speed issue.

Thanks again!!
badmoon
DD-WRT Novice


Joined: 22 Jul 2014
Posts: 41

PostPosted: Fri Apr 10, 2015 15:52    Post subject: Reply with quote
Updated ipblock.sh

Code:

#!/bin/bash

#set -x

### Block all traffic from listed. Use ISO code ###
ISO="cn tw kp kr ru ir"
#ISO="kp kr"

#Testing
#ISO="kr"

### Set PATH ###
IPT=/usr/sbin/iptables
WGET=/usr/bin/wget
EGREP=/bin/egrep
LOCKFILE=/tmp/ipblocklock.txt

### No editing below ###
SPAMLIST="countrydrop"
ZONEROOT="/opt/ipblock/zones"
DLROOT="http://www.ipdeny.com/ipblocks/data/countries"
BL="/opt/ipblock/zones/ipblock.sh"

if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
    echo "Lock file exist.. exiting"
    exit
fi

# make sure the lockfile is removed when we exit and then claim it
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}

cleanOldRules(){
$IPT -F countrydrop
}

# create a dir
[ ! -d $ZONEROOT ] && /bin/mkdir -p $ZONEROOT

# clean old rules
cleanOldRules
rm -f $BL

echo '*filter' > $BL
for c in $ISO
do
        # local zone file
        tDB=$ZONEROOT/$c.zone

        # get fresh zone file
        $WGET -T 30 -O $tDB $DLROOT/$c.zone

        awk -v SPAMLIST=$SPAMLIST '{print "-A "SPAMLIST" -s "$1" -j DROP"}' $tDB >> $BL

done
echo 'COMMIT' >> $BL

iptables-restore -n < $BL

rm -f ${LOCKFILE}



Updated Firewall Commands
Code:

iptables -N countrydrop
iptables -I INPUT 6 -j countrydrop
sh /opt/ipblock/ipblock.sh &


The INPUT 6 depends on where the following rule is located in your INPUT table.

ACCEPT 0 -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

The -j countrydrop should be below the above rule so that only NEW connections are sent through the 1000's of rulesets.
slidermike
DD-WRT Guru


Joined: 11 Nov 2013
Posts: 1487
Location: USA

PostPosted: Fri Apr 10, 2015 15:55    Post subject: Reply with quote
badmoon,
do you mind updating/replacing better scripts in your original post?

That will save everyone from having to potentially read through pages of comments to arrive at the best solution.

Thank you
Mike

_________________
Router currently owned:
Netgear R7800 - Router
Netgear R7000 - AP mode

R7000 specific Tips/Tricks.
http://www.dd-wrt.com/phpBB2/viewtopic.php?t=264152
badmoon
DD-WRT Novice


Joined: 22 Jul 2014
Posts: 41

PostPosted: Fri Apr 10, 2015 16:38    Post subject: Reply with quote
done..
JAMESMTL
DD-WRT Guru


Joined: 13 Mar 2014
Posts: 856
Location: Montreal, QC

PostPosted: Fri Apr 10, 2015 17:31    Post subject: Reply with quote
@badmoon

Your line inserting the block chain into the input table

iptables -I INPUT 6 -j countrydrop

Should really be

iptables -I INPUT 6 -i vlan2 -j countrydrop

Where vlan2 is the WANIF

The reason for doing this is to strictly limit parsing the block chain to incoming requests over the WAN. The way it is now all connections being established from the LAN to the router are forced to be evaluated by the block chain.

Example every time you want to access a website your computer will need to do a DNS lookup and that connection between the computer and router will needlessly be evaluated.
JAMESMTL
DD-WRT Guru


Joined: 13 Mar 2014
Posts: 856
Location: Montreal, QC

PostPosted: Fri Apr 10, 2015 17:44    Post subject: Reply with quote
@bird333

I'll add a stripped down version and my counter reporting script to the examples. However if your just looking for straight IPv4 badmoon's script should work just fine.

The TOR exit mode blocking script is used to block request to your router originating over the TOR network
badmoon
DD-WRT Novice


Joined: 22 Jul 2014
Posts: 41

PostPosted: Fri Apr 10, 2015 18:59    Post subject: Reply with quote
Hmm, would that cover outbound to a blocked country? After you make the outbound connection, wouldn't anything inbound be considered established and therefor bypassed.


JAMESMTL wrote:
@badmoon

Your line inserting the block chain into the input table

iptables -I INPUT 6 -j countrydrop

Should really be

iptables -I INPUT 6 -i vlan2 -j countrydrop

Where vlan2 is the WANIF

The reason for doing this is to strictly limit parsing the block chain to incoming requests over the WAN. The way it is now all connections being established from the LAN to the router are forced to be evaluated by the block chain.

Example every time you want to access a website your computer will need to do a DNS lookup and that connection between the computer and router will needlessly be evaluated.
Goto page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next Display posts from previous:    Page 1 of 10
Post new topic   Reply to topic    DD-WRT Forum Index -> Broadcom SoC based Hardware All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum