zmaster
New Around Here
Hi guys,
I've been a reader of this forum for quite some time and I have a RT-AC68U for almost 2.5 years now but flashed Merlin's firmware last week. My reason was to flash was OpenVPN 2.4.x. My Synology was a bit behind and I don’t like services being exposed to the internet when they are not on a pretty recent release.
So since I knew the existence of custom scripts I started hardening my OpenVPN setup. I’m a Microsoft consultant by nature so bash, iptables and unix commands are completely new to me. The thing that bothered me is that I saw multiple connection attempts on my OpenVPN port (running on tcp/443) from lots of different countries. Lots of them are port scanners, I know but still.. .
With the help of this forum and my friend Google I modified firewall-start scripts to only allow connections from The Netherlands since I only use my VPN connection from my own country. I thought of sharing this with everyone on this forum, in case anyone wants to use it. I have not yet copied it to my RT-AC68U but I’ve tested it on a Centos VM. I would like to hear from you guys if I’m working in the right direction and if the stuff I made, makes any sense.
Like the last line contained -z "$(iptables-save | grep AllowedCountries)", I have changed that to -z "$(iptables -S | grep AllowedCountries)". I saw no reason why it should be saved in order to check if the rule was already there.
I also got two questions:
I've been a reader of this forum for quite some time and I have a RT-AC68U for almost 2.5 years now but flashed Merlin's firmware last week. My reason was to flash was OpenVPN 2.4.x. My Synology was a bit behind and I don’t like services being exposed to the internet when they are not on a pretty recent release.
So since I knew the existence of custom scripts I started hardening my OpenVPN setup. I’m a Microsoft consultant by nature so bash, iptables and unix commands are completely new to me. The thing that bothered me is that I saw multiple connection attempts on my OpenVPN port (running on tcp/443) from lots of different countries. Lots of them are port scanners, I know but still.. .
With the help of this forum and my friend Google I modified firewall-start scripts to only allow connections from The Netherlands since I only use my VPN connection from my own country. I thought of sharing this with everyone on this forum, in case anyone wants to use it. I have not yet copied it to my RT-AC68U but I’ve tested it on a Centos VM. I would like to hear from you guys if I’m working in the right direction and if the stuff I made, makes any sense.
Like the last line contained -z "$(iptables-save | grep AllowedCountries)", I have changed that to -z "$(iptables -S | grep AllowedCountries)". I saw no reason why it should be saved in order to check if the rule was already there.
I also got two questions:
- The zone files which are downloaded to /jffs/ipset_lists, will they be removed after a reboot? Because I want to work with a fresh zone file every reboot.
- The Iptables rule for 443 is ALLOW for the AllowedCountry list. Is that a best practise or is it better to DROP everything except for the AllowedCountry list?
Code:
#!/bin/sh
echo "[firewall-start] Starting custom firewall script to allow OpenVPN from NL"
LINENUMBER=$(iptables -L INPUT --line-numbers | grep "dpt:https" | cut -d" " -f1)
if [ -z "$LINENUMBER" ]
then
echo "[firewall-start] Couldn't find a firewall rule for OpenVPN. Exiting firewall-start script."
exit 1
else
echo "[firewall-start] Found OpenVPN firewall rule at line $LINENUMBER."
fi
# Load ipset modules
ipset -v | grep -i "v4" > /dev/null 2>&1
if [ $? -eq 0 ];
then
# old ipset
ipsetv=4
lsmod | grep "ipt_set" > /dev/null 2>&1 || \
for module in ip_set ip_set_nethash ip_set_iphash ipt_set
do
insmod $module
done
else
# new ipset
ipsetv=6
lsmod | grep "xt_set" > /dev/null 2>&1 || \
for module in ip_set ip_set_hash_net ip_set_hash_ip xt_set
do
insmod $module
done
fi
# Preparing folder to cache downloaded files
IPSET_LISTS_DIR=/jffs/ipset_lists
[ -d "$IPSET_LISTS_DIR" ] || mkdir -p $IPSET_LISTS_DIR
# Different routers got different iptables syntax
case $(uname -m) in
armv7l)
MATCH_SET='--match-set'
;;
mips)
MATCH_SET='--set'
;;
x86_64)
MATCH_SET='--match-set'
;;
esac
# Allow incoming traffic from some countries. nl is for Netherlands. See other countries code at http://www.ipdeny.com/ipblocks/
if [ -z $(ipset list -n | grep "AllowedCountries") ]
then
ipset create AllowedCountries hash:net
for country in nl
do
echo "[firewall-start] Downloading zone file for $country."
[ -e $IPSET_LISTS_DIR/$country-aggregated.zone ] || wget -q -O $IPSET_LISTS_DIR/$country-aggregated.zone http://ipdeny.com/ipblocks/data/aggregated/$country-aggregated.zone
for IP in $(cat $IPSET_LISTS_DIR/$country-aggregated.zone)
do
ipset add AllowedCountries $IP
done
done
fi
if [ -z "$(iptables -S | grep AllowedCountries)" ]
then
echo "[firewall-start] Creating restricted firewall rule."
iptables -R INPUT $LINENUMBER -p tcp -m tcp --dport 443 -m set $MATCH_SET AllowedCountries src -j ACCEPT
else
echo "[firewall-start] Already found a rule with an AllowedCountries list. Skipping creation of firewall rule."
fi