Cheat Sheet : IP Table

Ip tables is a command line firewall that comes with linux. Its essential to learn how to use the different parts of it for the scanning phase:

IPTABLES: iptables has the following 4 built in tables:
 
i.FILTER table: it has 3 chains:
        1.INPUT chain- incoming to the firewall
        2.OUTPUT chain- outgoing from the firewall
        3.FORWARD chain-packets for another NIC on the local server
 
ii.NAT table: it has 3 chains:
        1.PREROUTING- alters packets before routing. (used for DNAT , destination nat)
        2.POSTROUTING – alters packets after routing. (used for SNAT , source nat)
        3.OUTPUT – used for packets generated locally 
 
iii.MANGLE table: specialized packet alteration. It modifys the QOS bit in the TCP header. It has these chains:
        1.PREROUTING
        2.OUTPUT
        3.FORWARD
        4.INPUT
        5.POSTROUTING
 
iv.RAW table: configuration exemptions. chains include:
        1.PREROUTING
        2.OUTPUT
 
* 4 values that iptables will do with a packet:
        i.ACCEPT-firewall accepts packet
        ii.DROP- firewall drops the packet
        iii.QUEUE- firewall will pass the packet to the userspace
        iv.RETURN- firewall will stop executing the next set of rules in the current chain
 
* .Some iptables examples:
 
i.Ok first lets make sure our tables are flushed: iptables –F
 
ii.Now lets check: iptables –L
 
iii.Now lets set a simple rule to block by ip address: iptables –A INPUT  –i eth0 –s “10.10.10.7” –j DROP
 
iv.Now lets do the same for output:  iptables –A OUTPUT –o eth0 –s “10.10.10.7”  -j DROP
now try to ping from bt5 to win7 and vice versa and see that you cannot
 
v.Allow all incoming ssh (need to set an input and output):  iptables –A INPUT –i eth0 –p tcp --dport 22 –m  state  --state NEW, ESTABLISHED  -j ACCEPT   and now for the output:  iptables –A  OUTPUT  -o eth0  -p tcp --sport 22 –m state  --state ESTABLISHED  -j ACCEPT   so what all this means is  -A is for specifying the chain, -i is for the NIC interface, -p is protocol,  dport is destination port, -m state is for the state of the connection, in the first rule it is syn and ack, or NEW and ESTABLISHED. –j is “jump to target”, tells it what to to do with the packet.
 
vi. Now lets allow ssh incoming from only a specific network:  iptables –A INPUT –i eth0  -p tcp  -s  192.168.3.0/24  --dport 22  -m state  --state NEW,ESTABLISHED  -j ACCEPT   now for the output:  iptables –A OUTPUT –o eth0   -p tcp  --sport 22  -m state  --state ESTABLISHED  -j ACCEPT
 
vii.Allow incoming http and https:  iptables –A INPUT  -i eth0 –p tcp  --dport 80 –m state  --state NEW,ESTABLISHED   -j ACCEPT   and now the output :  iptables –A OUTPUT  -o  eth0  -p  tcp --sport 80  -m state  --state ESTABLISHED  -j ACCEPT  . Now for the https just substitute the port 80 with 443
 
viii.Combine multiple rules with multiport:  iptables –A INPUT  -i eth0 –p tcp  -m multiport  --dports 22,80,443  -m state  --state NEW,ESTABLISHED  -j ACCEPT  and now the output : iptables –A OUTPUT  -o eth0 –p –m multiport  --sports 22,80,443  -m state --state ESTABLISHED  -j ACCEPT
 
ix.Allow outgoing ssh:  iptables –A OUTPUT  -o eth0 –p tcp --dport 22 –m state  --state NEW,ESTABLISHED –j ACCEPT  and now the input: iptables –A INPUT –i eth0 –p tcp  --sport 22 –m state  --state ESTABLISHED –j ACCEPT 
 
x.Allow ping from outside to in: iptables –A INPUT –p ICMP --icmp-type  echo-request  -j ACCEPT   and the outgoing: iptables –A OUTPUT –p ICMP  --icmp-type echo-reply  -j ACCEPT
 
xi.Allow internal to external. On a firewall server there would be one nic connected to external and one to internal. This rule allows internal to talk to external (eth1 is connected to external, eth0 is connected to internal) : iptables –A FORWARD –i eth0  -o eth1  -j ACCEPT
 
xii.Allow outbound dns: iptables –A OUTPUT  -p udp  -o eth0  --dport 53 –j ACCEPT  and now the inbound: iptables –A INPUT –p udp –i eth0  --sport 53 –j ACCEPT 
 
xiii.Now to port forward that comes to port 422 to port 22: first we allow 422: iptables –A INPUT –i eth0 –p tcp --dport 422  -m state   --state NEW,ESTABLISHED  -j ACCEPT  and now the output: iptables –A OUTPUT –o eth0 –p tcp  --sport 422  -m state  --state ESTABLISHED  -j ACCEPT  and now the port forward:  iptables  -t nat  -A PREROUTING  -p tcp  -d  192.168.3.6  --dport 422  -j  DNAT  --to 192.168.3.6:22
 
xiv.Iptables –L –n –v  shows the full state of the firewall
 
xv.You can also set a connection limit, lets say allow only two telnet connections per host: iptables –A INPUT  -p  tcp   --syn  --dport 23  -m connlimit  --connlimit-above  2  -j REJECT
 
xvi.Default policy of drop all: iptables  -P FORWARD DROP (do a flush first –F and notice the default policy for FORWARD is accept)
 
xvii.Setting up a basic nat, forward packets from my internal network(eth1) to the external network (eth0) and hide it (masquerade) :  iptables  -t  nat  -A  POSTROUTING  -o  eth0  -j MASQUERADE  then the next line:  iptables  -A FORWARD  -i eth0  -o eth1  -m state  --state RELATED,ESTABLISHED  -j ACCEPT  then the next line: iptables –A FORWARD  -i eth1  -o eth0  -j ACCEPT
 
xviii.(have students do this one on their own) The business objective in this example is to block all traffic except web surfing from the internal network. The internal network is 192.168.3.0/24 :  
1.first we turn on NAT (masquerading) on eth0: iptables –t nat –A POSTROUTING   -o eth0 –j MASQUERADE
2.then we set the default policy (-P) of the forward chain: iptables   -P  FORWARD  DROP
3.then we append (-A) the forward chain with a rule to jump (-j)  to accept when traffic is coming from a source address (-s), to a certain protocol (-p) , to a destination port (--dport ) : iptables –A FORWARD   -j  ACCEPT   -s 192.168.3.0/24  -p udp  --dport 53  then on the next line type : iptables –A FORWARD  -j  ACCEPT  -s 192.168.3.0/24  -p tcp  --dport 80
4.now we need to allow traffic to return to the internal network (-d)  if it is coming from the allowed source ports: iptables  -A  FORWARD  -j  ACCEPT  -d 192.168.3.0/24  -p udp  --sport 53   then on the next line :  iptables –A  FORWARD  -j ACCEPT  -d 192.168.3.0/24  -p tcp  --sport 80.  So basically its saying allow traffic to return only if its coming from ports 80 and 53

Comments

Popular posts from this blog

Cheat Sheet : Wireshark

Monitor and block SSH connection attempts

Cheat Sheet : NetCat