background image

iptablesrocks.org - Deploying a Safetynet

home

 | 

syntax & structure

 | 

examples

 | 

faq

 | 

contact

 | 

links

The iptablesrocks.org iptables firewall setup guide

Deploying a Safetynet:

This next step is of critical importance. Whenever you start toying around with the firewall settings of any server, you will want to deploy a means for 
you to regain access to the server should you accidentally lock yourself out. This may sound silly at first, but it can be very easy to lock yourself out of 
your server when making firewall adjustments, no matter how skilled you are. It only takes a tiny syntax error or a missed flag somewhere in the 
firewall script to lock your server down to a point where you cannot access it. While locking yourself out of a server that you have physical access to is 
not that big of a deal, it can be a nightmare if you lock yourself out of a server that you don't have physical access to. This is certainly the case if your 
server is being hosted somewhere. If you don't believe me, try skipping this step and you may end up learning your lesson the hard way. 

Fortunately, safeguarding against an accidental server lock down is not difficult. The procedure only takes a couple of minutes and can be a lifesaver. 
So here's how you do it:

Our safety net is simply going to be a firewall reset script that runs of our server's crontab. We will set the script to run at regular intervals. In the way, 
should you lock yourself out of your server, all you'll have to do is sit back and wait for the script to execute and for the firewall to reset. 

So let's create the reset script...

vi /root/firewall_reset

# Iptables firewall reset script
*filter
:INPUT ACCEPT [164:15203]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [147:63028]
COMMIT

*mangle
:PREROUTING ACCEPT [164:15203]
:INPUT ACCEPT [164:15203]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [147:63028]
:POSTROUTING ACCEPT [147:63028]
COMMIT

*nat
:PREROUTING ACCEPT [14:672]
:POSTROUTING ACCEPT [9:684]
:OUTPUT ACCEPT [9:684]
COMMIT

Save and exit the file

Now let's test the reset script. The following command will utilize the "iptables-import" tool, which is used to import an iptables config script directly into 
the iptables ruleset. When we import the above iptables configuration into the ruleset, it will completely reset your firewall and put your server is a 
totally "open" state where all packets are allowed to freely move in and out of your server. If you've locked yourself out of your server, this will drop any 
mistakes and let you back in.

iptables-restore < /root/firewall_reset

If you don't get an error, the script worked. The last thing you will want to do is to actually look at the iptables status just to make sure that your server 
is completely open as it should be. The following command will display your current iptables status:

iptables -L

An open firewall should look similar to this:

http://www.iptablesrocks.org/guide/safetynet.php (1 of 2) [2/13/2004 8:04:53 PM]

Summary of Contents for iptables

Page 1: ...vers the installation of iptables in a Redhat environment the syntax and structure associated with iptables and a collection a pre configured iptables configurations for a variety of applications This...

Page 2: ...Replace an existing rule from a desired chain in the current configuration iptables I chain that s a capital I as in Insert Insert a new rule into a desired chain of the current configuration iptables...

Page 3: ...Iptablesrocks org Syntax Structure Site last modified February 13 2004 15 27 51 http www iptablesrocks org syntax 2 of 2 2 13 2004 8 04 43 PM...

Page 4: ...warded packets COMPLETELY 3 With all incoming packets dropped by default it then grants incoming access to a select number of ports These ports are ports that a typical web server might allow Open inb...

Page 5: ...Iptablesrocks org Examples http www iptablesrocks org examples 2 of 2 2 13 2004 8 04 44 PM...

Page 6: ...lat file iptables script into the server s ruleset Q How do I export my server s iptables rulset to flat iptables script A The command iptables save path to firewall_script will export the current ipt...

Page 7: ...esrocks org If you would like to contact me please fill out the form below and then click Send It Your name Your e mail Comments Questions home syntax structure examples faq contact links Site last mo...

Page 8: ...www netfilter org Linuxguruz Iptables Tutorial http www linuxguruz com iptables howto iptables HOWTO html Qmailrocks org My qmail installation guide Djbdnsrocks org My djbdns installation guide home...

Page 9: ...ain unused port What if you want to block someone from your server Get the answers right here What if I want to open another port While it is possible to append rules to your live iptables configurati...

Page 10: ...ou take a look at the firewall script you will notice that TCP port 3389 is not mentioned anywhere in the script and to be more specific it is not mentioned in the INPUT portion of the script Well sin...

Page 11: ...make sure you enable your iptables safetynet before you start playing with your firewall setup You don t want to accdientally lock yourself out of your server Starting your fiewall on boot The final p...

Page 12: ...stions May we post your comments on this website Yes No your name e mail address will not be posted A note on privacy Some people worry about providing their e mail address on the form You don t need...

Page 13: ...ck myself out of my server I will only need to wait a maximum of 10 minutes before the bad firewall rules will be dropped and I ll be able to shell into my server again If you are a newbie at iptables...

Page 14: ...up and lock yourself out of your server while you are implementing or testing new iptables configurations With this crontab running should you lock yourself out you will only have to wait a maximum o...

Page 15: ...TING p tcp m tcp tcp flags FIN SYN FIN SYN j DROP A PREROUTING p tcp m tcp tcp flags FIN SYN RST PSH ACK URG FIN PSH URG j DROP A PREROUTING p tcp m tcp tcp flags FIN SYN RST PSH ACK URG NONE j DROP A...

Page 16: ...tcp dport 783 j ACCEPT A OUTPUT p tcp m tcp dport 993 j ACCEPT A OUTPUT p tcp m tcp dport 3306 j ACCEPT A OUTPUT p tcp m tcp dport 12000 j ACCEPT A OUTPUT p tcp m tcp dport 15000 j ACCEPT A OUTPUT p t...

Page 17: ...hain iptables t table X chain iptables t table P chain target options iptables t table E old chain name new chain name DESCRIPTION Iptables is used to set up maintain and inspect the tables of IP pack...

Page 18: ...is configured with automatic module loading an attempt will be made to load the appropriate module for that table if it is not already there The tables are as follows filter This is the default table...

Page 19: ...les are inserted at the head of the chain This is also the default if no rule number is specified R replace chain rulenum rule specification Replace a rule in the selected chain If the source and or d...

Page 20: ...representing one of these protocols or a different one A protocol name from etc protocols is also allowed A argument before the protocol inverts the test The number zero is equivalent to all Protocol...

Page 21: ...When the argument precedes the f flag the rule will only match head fragments or unfragmented packets c set counters PKTS BYTES This enables the administrator to initialize the packet and byte counte...

Page 22: ...fied using the format port port If the first port is omitted 0 is assumed if the last is omitted 65535 is assumed If the second port greater then the first they will be swapped The flag sport is a con...

Page 23: ...ort port port Destination port or port range specification See the description of the destination port option of the TCP extension for details icmp This extension is loaded if protocol icmp is specifi...

Page 24: ...source or destination ports Up to 15 ports can be specified It can only be used in conjunction with p tcp or p udp source ports port port port Match if the source port is one of the given ports The f...

Page 25: ...ted by a process with the given command name this option is present only if iptables was compiled under a kernel supporting this feature state This module when combined with connection tracking allows...

Page 26: ...original source address differs from the reply destination DNAT A virtual state matching if the original destination differs from the reply source ctproto proto Protocol to match by number or name ct...

Page 27: ...ie including the precedence bits tos tos The argument is either a standard name use iptables m tos h to see the list or a numeric value to match ah This module matches the SPIs in AH header of IPSec...

Page 28: ...he Linux kernel will print some information on all matching packets like most IP header fields via the kernel log where it can be read with dmesg or syslogd 8 This is a non terminating target i e rule...

Page 29: ...trols the nature of the error packet returned reject with type The type given can be icmp net unreachable icmp host unreachable icmp port unreachable icmp proto unreachable icmp net prohibited or icmp...

Page 30: ...rce ports below 512 will be mapped to other ports below 512 those between 512 and 1023 inclusive will be mapped to ports below 1024 and other ports will be mapped to 1024 or above Where possible no po...

Page 31: ...rts to use overriding the default SNAT source port selection heuristics see above This is only valid if the rule also specifies p tcp or p udp REDIRECT This target is only valid in the nat table in th...

Page 32: ...usually limiting it to your outgoing interface s MTU minus 40 Of course it can only be used in conjunction with p tcp This target is used to overcome criminally braindead ISPs or servers which block I...

Page 33: ...rious error messages are printed to standard error The exit code is 0 for correct functioning Errors which appear to be caused by invalid or abused command line parameters cause an exit code of 2 and...

Page 34: ...NAT HOWTO details NAT the netfilter extensions HOWTO details the extensions that are not in the standard distribution and the netfilter hacking HOWTO details the netfilter internals See http www netfi...

Page 35: ...COMMANDS PARAMETERS OTHER OPTIONS MATCH EXTENSIONS tcp udp icmp mac limit multiport mark owner state conntrack dscp pkttype tos ah esp length ttl unclean TARGET EXTENSIONS LOG MARK REJECT TOS http ww...

Page 36: ...LOG TCPMSS DSCP ECN DIAGNOSTICS BUGS COMPATIBILITY WITH IPCHAINS SEE ALSO AUTHORS This document was created by man2html using the manual pages Time 05 21 18 GMT January 07 2004 http www iptablesrocks...

Page 37: ...by initially blocking all incoming outgoing and forwarded packets COMPLETELY 3 With all incoming packets dropped by default it then grants incoming access to a select number of ports These ports are p...

Page 38: ...rnel logging configuration so that the firewall will log its activites to a custom logfile Here s how you do it vi etc syslog conf Add the following to the syslog conf file IPTables logging kernel mes...

Page 39: ...org Preparation Proceed to the next step home syntax structure examples faq contact links Site last modified February 13 2004 15 27 51 http www iptablesrocks org guide preparation php 2 of 2 2 13 200...

Page 40: ...e will set the script to run at regular intervals In the way should you lock yourself out of your server all you ll have to do is sit back and wait for the script to execute and for the firewall to re...

Page 41: ...irewall every 15 minutes crontab e 0 15 30 45 sbin iptables restore root firewall_reset That s it Now save and exit out of the crontab editor That s it Until the crontab is disabled your server s ipta...

Page 42: ...EROUTING p tcp m tcp tcp flags FIN SYN FIN SYN j DROP A PREROUTING p tcp m tcp tcp flags FIN SYN RST PSH ACK URG FIN PSH URG j DROP A PREROUTING p tcp m tcp tcp flags FIN SYN RST PSH ACK URG NONE j DR...

Page 43: ...m tcp dport 443 j ACCEPT uncomment the next line if you are running Spamassassin on your server A OUTPUT p tcp m tcp dport 783 j ACCEPT A OUTPUT p tcp m tcp dport 993 j ACCEPT A OUTPUT p tcp m tcp dpo...

Page 44: ...p anywhere anywhere tcp dpt http ACCEPT tcp anywhere anywhere tcp dpt pop3 ACCEPT tcp anywhere anywhere tcp dpt imap ACCEPT tcp anywhere anywhere tcp dpt https ACCEPT tcp anywhere anywhere tcp dpt ima...

Page 45: ...ning you will want to disable the safetynet crontab so that your firewall will remain up and running Proceed to the next step home syntax structure examples faq contact links Site last modified Februa...

Page 46: ...L portscans it should block many and make the others a bit more time consuming and troublesome A portion nmap probe s activities should be logged in the var log firewall logfile The log entries will c...

Page 47: ...gz And now let s start the installation cd iptables Log into mysql and do the database work mysql u root p create a database called iptables mysql create database iptables Now create an admin user for...

Page 48: ...the database feeder script vi scripts feed_db pl Make sure the following configuration section is set properly Make sure you enter the iptables_user mysql password where the x s are my dsn DBI mysql...

Page 49: ...our server and then logging in again via SSH The SSH connection will be recorded in the iptables logs and this will appear on the Iptables Log Analyzer screen If you need more help with Iptables Log A...

Reviews: