{"id":3477,"date":"2014-08-07T10:41:31","date_gmt":"2014-08-07T15:41:31","guid":{"rendered":"https:\/\/secure.rosehosting.com\/blog\/?p=3477"},"modified":"2022-12-19T06:38:05","modified_gmt":"2022-12-19T12:38:05","slug":"securing-your-ubuntudebian-based-vps-using-iptablesnetfilter-firewall","status":"publish","type":"post","link":"https:\/\/www.rosehosting.com\/blog\/securing-your-ubuntudebian-based-vps-using-iptablesnetfilter-firewall\/","title":{"rendered":"Securing your Ubuntu\/Debian based VPS using IPTABLES\/Netfilter firewall"},"content":{"rendered":"
In the following article we will show you how you can secure and protect your Ubuntu or Debian based virtual server<\/a> using a firewall<\/strong> application, called iptables<\/a>.<\/p>\n What is iptables?<\/strong><\/p>\n It is is a user space application program that allows a system administrator to configure the tables provided by the Linux kernel firewall (implemented as different Netfilter modules<\/em>) and the chains and rules it stores.<\/p>\n In order to keep down the number of attempted break-ins and to filter the ports opened on your virtual server<\/a>, it is necessary to properly configure your firewall<\/strong>.<\/p>\n <\/p>\n To make the management of your server’s firewall rules easier, we will provide a simple shell script that will help with the management of the firewall rules. Basically, whenever you need to change your firewall rules, you would use this script to add\/remove the desired rule(s).<\/p>\n Before wrapping the rules in a script, let’s split it in sections so you can know the design of the firewall and what rules will be in place.<\/p>\n The firewall rules can be flushed using the following commands:<\/p>\n You have to know what services you have exposed to the public so you can set appropriate rules for them. One way to find what services are listening on what ports is to use the for example, we are using one of our Ubuntu 12.04 LTS virtual servers<\/a> and the following is the output of the so this means that we have the following ports opened to the public:<\/p>\n ok, so now we can set-up our firewall’s policies to DROP<\/b> everything in the INPUT<\/b> and FORWARD<\/b> chains and allow traffic in the OUTPUT<\/b> chain.<\/p>\n the default table check default policies can be applied using the following commands:<\/p>\n Next thing to do is to actually set-up the firewall rules for our services. As the INPUT chain is DROPPED, we will need to white-list the services which are listening on public ports, so:<\/p>\n the first thing is to enable free use of the next, let’s open our service ports by using firewall rules like:<\/p>\n and so on. Once all the services are white-listed, it is a good idea to allow ICMP packets and to LOG and DROP everything else in the INPUT chain.<\/p>\n Firewall rules are not persistent by default, which means they will be lost if you reboot your virtual server<\/a> for example. To save your current firewall in and to restore the saved rules, you would use something like:<\/p>\n to make the rules automatically restored on system start-up, you would either have to use a package named save the script in to activate the rules you’ve prepared in the script, just execute<\/p>\n from the command line. To add or remove a rule, you just need to to open the script, add or remove your desired rule, save and run it again, for example:<\/p>\n Of course you don\u2019t have to do any of this if you use one of our Linux VPS hosting<\/a> services, in which case you can simply ask our expert linux admins to do this for you. They are available 24\u00d77 and will take care of your request immediately.<\/p>\n PS.<\/strong> <\/span>If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":" In the following article we will show you how you can secure and protect your Ubuntu or Debian based virtual … <\/p>\nSET-UP FIREWALL RULES<\/strong><\/h3>\n
FLUSH OLD RULES<\/strong><\/h4>\n
## iptables --flush\r\n## iptables --delete-chain\r\n## iptables --table nat --flush\r\n## iptables --table nat --delete-chain<\/pre>\n
DETERMINE SERVICE PORTS<\/strong><\/h4>\n
netstat<\/code> or
ss<\/code> commands as in:<\/p>\n
## netstat -tunlp\r\n\r\nor\r\n\r\n## ss -tunlp<\/pre>\n
netstat<\/code> command:<\/p>\n
## netstat -tunlp\r\nActive Internet connections (only servers)\r\nProto Recv-Q Send-Q Local Address Foreign Address State PID\/Program name\r\ntcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 296\/mysqld\r\ntcp 0 0 0.0.0.0:587 0.0.0.0:* LISTEN 449\/sendmail: MTA:\r\ntcp 0 0 0.0.0.0:110 0.0.0.0:* LISTEN 261\/dovecot\r\ntcp 0 0 0.0.0.0:143 0.0.0.0:* LISTEN 261\/dovecot\r\ntcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 590\/apache2\r\ntcp 0 0 209.135.140.77:53 0.0.0.0:* LISTEN 353\/named\r\ntcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 353\/named\r\ntcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 173\/vsftpd\r\ntcp 0 0 0.0.0.0:5622 0.0.0.0:* LISTEN 630\/sshd\r\ntcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 449\/sendmail: MTA:\r\ntcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN 353\/named\r\ntcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 590\/apache2\r\ntcp 0 0 0.0.0.0:993 0.0.0.0:* LISTEN 261\/dovecot\r\ntcp 0 0 0.0.0.0:995 0.0.0.0:* LISTEN 261\/dovecot\r\nudp 0 0 209.135.140.77:53 0.0.0.0:* 353\/named\r\nudp 0 0 127.0.0.1:53 0.0.0.0:* 353\/named<\/pre>\n
\n
SET-UP DEFAULT POLICIES<\/strong><\/h4>\n
filter<\/code> contains three built-in chain: <\/i><\/p>\n
\n
man 8 iptables<\/code> for more information on this<\/i><\/p>\n
## iptables -P INPUT DROP # <- do not run this over ssh as it will lock you out\r\n## iptables -P FORWARD DROP\r\n## iptables -P OUTPUT ACCEPT<\/pre>\n
SET-UP FIREWALL RULES<\/strong><\/h4>\n
loopback interfaces<\/code>, to ensure all TCP sessions should begin with SYN and to allow established and related packets:<\/p>\n
## iptables -A INPUT -i lo -j ACCEPT\r\n## iptables -A OUTPUT -o lo -j ACCEPT\r\n## iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP\r\n## iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT<\/pre>\n
### SSH\r\n## iptables -A INPUT -p tcp --dport 5622 -m state --state NEW -j ACCEPT\r\n\r\n### HTTP\r\n## iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT\r\n\r\n### SMTP\r\n## iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT<\/pre>\n
## iptables -A INPUT -p ICMP --icmp-type 8 -j ACCEPT\r\n## iptables -A INPUT -j LOG\r\n## iptables -A INPUT -j DROP<\/pre>\n
SAVE\/RESTORE THE FIREWALL RULES<\/strong><\/h4>\n
\/etc\/fwall.rules<\/code> you would use something like:<\/p>\n
## iptables-save > \/etc\/fwall.rules<\/pre>\n
## iptables-restore -c \/etc\/fwall.rules<\/pre>\n
iptables-persistent<\/code> or you can simply create a script in
\/etc\/network\/if-pre-up.d<\/code> which will load the rules saved in
\/etc\/fwall.rules<\/code>, for example:<\/p>\n
## vim \/etc\/network\/if-pre-up.d\/iptables\r\n\r\n#!\/bin\/bash\r\ntest -e \/etc\/fwall.rules && iptables-restore -c \/etc\/fwall.rules\r\n\r\n## chmod +x \/etc\/network\/if-pre-up.d\/iptables<\/pre>\n
\nFIREWALL SCRIPT<\/strong><\/h3>\n
\/usr\/local\/bin\/fwall-rules<\/code> and make it executable<\/p>\n
#!\/bin\/bash\r\n\r\nIPTABLES=\/sbin\/iptables\r\n\r\necho \" * flushing old rules\"\r\n${IPTABLES} --flush\r\n${IPTABLES} --delete-chain\r\n${IPTABLES} --table nat --flush\r\n${IPTABLES} --table nat --delete-chain\r\n\r\necho \" * setting default policies\"\r\n${IPTABLES} -P INPUT DROP\r\n${IPTABLES} -P FORWARD DROP\r\n${IPTABLES} -P OUTPUT ACCEPT\r\n\r\necho \" * allowing loopback devices\"\r\n${IPTABLES} -A INPUT -i lo -j ACCEPT\r\n${IPTABLES} -A OUTPUT -o lo -j ACCEPT\r\n\r\n${IPTABLES} -A INPUT -p tcp ! --syn -m state --state NEW -j DROP\r\n${IPTABLES} -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT\r\n\r\n## BLOCK ABUSING IPs HERE ##\r\n#echo \" * BLACKLIST\"\r\n#${IPTABLES} -A INPUT -s _ABUSIVE_IP_ -j DROP\r\n#${IPTABLES} -A INPUT -s _ABUSIVE_IP2_ -j DROP\r\n\r\necho \" * allowing ssh on port 5622\"\r\n${IPTABLES} -A INPUT -p tcp --dport 5622 -m state --state NEW -j ACCEPT\r\n\r\necho \" * allowing ftp on port 21\"\r\n${IPTABLES} -A INPUT -p tcp --dport 21 -m state --state NEW -j ACCEPT\r\n\r\necho \" * allowing dns on port 53 udp\"\r\n${IPTABLES} -A INPUT -p udp -m udp --dport 53 -j ACCEPT\r\n\r\necho \" * allowing dns on port 53 tcp\"\r\n${IPTABLES} -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT\r\n\r\necho \" * allowing http on port 80\"\r\n${IPTABLES} -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT\r\n\r\necho \" * allowing https on port 443\"\r\n${IPTABLES} -A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT\r\n\r\necho \" * allowing smtp on port 25\"\r\n${IPTABLES} -A INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT\r\n\r\necho \" * allowing submission on port 587\"\r\n${IPTABLES} -A INPUT -p tcp -m state --state NEW -m tcp --dport 587 -j ACCEPT\r\n\r\necho \" * allowing imaps on port 993\"\r\n${IPTABLES} -A INPUT -p tcp -m state --state NEW -m tcp --dport 993 -j ACCEPT\r\n\r\necho \" * allowing pop3s on port 995\"\r\n${IPTABLES} -A INPUT -p tcp -m state --state NEW -m tcp --dport 995 -j ACCEPT\r\n\r\necho \" * allowing imap on port 143\"\r\n${IPTABLES} -A INPUT -p tcp -m state --state NEW -m tcp --dport 143 -j ACCEPT\r\n\r\necho \" * allowing pop3 on port 110\"\r\n${IPTABLES} -A INPUT -p tcp -m state --state NEW -m tcp --dport 110 -j ACCEPT\r\n\r\necho \" * allowing ping responses\"\r\n${IPTABLES} -A INPUT -p ICMP --icmp-type 8 -j ACCEPT\r\n\r\n# DROP everything else and Log it\r\n${IPTABLES} -A INPUT -j LOG\r\n${IPTABLES} -A INPUT -j DROP\r\n\r\n#\r\n# Save settings\r\n#\r\necho \" * SAVING RULES\"\r\n\r\nif [[ -d \/etc\/network\/if-pre-up.d ]]; then\r\n if [[ ! -f \/etc\/network\/if-pre-up.d\/iptables ]]; then\r\n echo -e \"#!\/bin\/bash\" > \/etc\/network\/if-pre-up.d\/iptables\r\n echo -e \"test -e \/etc\/iptables.rules && iptables-restore -c \/etc\/iptables.rules\" >> \/etc\/network\/if-pre-up.d\/iptables\r\n chmod +x \/etc\/network\/if-pre-up.d\/iptables\r\n fi\r\nfi\r\n\r\niptables-save > \/etc\/fwall.rules\r\niptables-restore -c \/etc\/fwall.rules<\/pre>\n
## chmod +x \/usr\/local\/bin\/fwall-rules<\/pre>\n
## fwall-rules<\/pre>\n
## fwall-rules\r\n * flushing old rules\r\n * setting default policies\r\n * allowing loopback devices\r\n * allowing ssh on port 5622\r\n * allowing ftp on port 21\r\n * allowing dns on port 53 udp\r\n * allowing dns on port 53 tcp\r\n * allowing http on port 80\r\n * allowing https on port 443\r\n * allowing smtp on port 25\r\n * allowing submission on port 587\r\n * allowing imaps on port 993\r\n * allowing pop3s on port 995\r\n * allowing imap on port 143\r\n * allowing pop3 on port 110\r\n * allowing ping responses\r\n * SAVING RULES<\/pre>\n
\n