Iptables 2
Iptables 2
Iptables 2
Objectives
to learn the basics of iptables
Contents
Start and stop IPtables Checking IPtables status Input and Output chain Pre and Post routing Forward of address and port Firewall standard rules Lading/Unloading kernel driver modules Connection tracking modules
Practicals
working with iptables
Summary
What Is iptables?
Stateful packet inspection.
The firewall keeps track of each connection passing through it, This is an important feature in the support of active FTP and VoIP.
Filtering packets based the values of the flags in the TCP header
Helpful in preventing attacks using malformed packets and in restricting access.
Packet manipulation (mangling) like altering the TOS/DSCP/ECN bits of the IP header
Mark and classify packets dependent on rules. First step in QoS.
Download from:
http://www.netfilter.org/downloads.html
Documentation:
http://www.netfilter.org/documentation/index.html
iptables itself is a command which we will see soon. To show all current rule chains:
iptables -list
DROP
iptables stops further processing. The packet is blocked.
LOG
The packet information is sent to the syslog daemon for logging. iptables continues processing with the next rule in the table. You can't log and drop at the same time ->use two rules. --log-prefix reason"
REJECT
Works like the DROP target, but will also return an error message to the host sending the packet that the packet was blocked --reject-with qualifier Qualifier is an ICMP message
DNAT
Used to do destination network address translation. ie. rewriting the destination IP address of the packet --to-destination ipaddress
MASQUERADE
Used to do Source Network Address Translation. By default the source IP address is the same as that used by the firewall's interface [--to-ports <port>[-<port>]]
We try to define a rule that will accept all packages on interface eth0 that uses TCP and has destination address 192.168.1.1. We first define the MATCH criterias:
Use default filter table (absense of t ) Append a rule to end of INPUT chain (-A INPUT ) Match on source address can be any 0/0 address (-s 0/0 ) Input interface used is eth0 (-i eth0 ) Match on destination address 192.168.1.1 (-d 192.168.1.1) Match Protocol TCP (-p TCP ) If all matches is fulfilled, then jump to ACCEPT chain. (-j ACCEPT )
The return traffic from webbserver is allowed, but only of sessions are opened:
iptables -A FORWARD -d 0/0 -o eth0 -s 192.168.1.58 -i eth1 -p TCP \ -m state --state ESTABLISHED -j ACCEPT
If sessions are used, you can reduce an attack called half open
Half open is known to consume server all free sockets (tcp stack memory) and is senced as a denial of service attack, but it is not. Sessions are usally waiting 3 minutes.
Here we allow ipsec, ah and ssh from outside and everything from inside and out
In Windows 2003 server you find the same entries in the registry. You will need to reboot your server after doing the hardening above
iptables -t nat --policy POSTROUTING ACCEPT iptables -t nat --policy PREROUTING ACCEPT
Verify valid source and destination addresses for all packets : iptables -A INPUT -i eth0 -j valid-src
iptables -A FORWARD -i eth0 -j valid-src iptables -A OUTPUT -o eth0 -j valid-dst iptables -A FORWARD -o eth0 -j valid-dst
Drop packets from networks covered in RFC 1918 (private nets) Drop packets from external interface IP address
Allow Your protected Network To Access The Firewall Allow all bidirectional traffic from your firewall to the protected network :
iptables -A INPUT iptables -A OUTPUT -j ACCEPT -p all -s 192.168.1.0/24 -i eth1 -j ACCEPT -p all -d 192.168.1.0/24 -o eth1
Prior to masquerading, the packets are routed via the filter table's FORWARD chain :
iptables -A FORWARD -t filter -o eth0 -m state \ --state NEW,ESTABLISHED,RELATED -j ACCEPT iptables -A FORWARD -t filter -i eth0 -m state \ --state ESTABLISHED,RELATED -j ACCEPT
Port Forwarding Type NAT port 80 forwarded to port 8080 on server 192.168.1.200 :
iptables -t nat -A PREROUTING -p tcp -i eth0 -d $external_ip \ --dport 80 --sport 1024:65535 -j DNAT --to 192.168.1.200:8080
After DNAT, the packets are routed via the filter table's FORWARD chain :
iptables -A FORWARD -p tcp -i eth0 -o eth1 -d 192.168.1.200 \ --dport 8080 --sport 1024:65535 -m state --state NEW -j ACCEPT iptables -A FORWARD -t filter -o eth0 -m state \ --state NEW,ESTABLISHED,RELATED -j ACCEPT iptables -A FORWARD -t filter -i eth0 -m state \ --state ESTABLISHED,RELATED -j ACCE
Connections on port 80 to the target machine on the private network must be allowed.
For connections originating from the Internet. Notice how you use the real IP addresses here :
iptables -A FORWARD -p tcp -i eth0 -o eth1 -d 192.168.1.100 \ -m multiport --dport 80,443,22 \ -m state --state NEW -j ACCEPT
Static NAT / Source NAT Allow forwarding for all New and Established SNAT connections originating on the home network AND already established DNAT connections :
iptables -A FORWARD -t filter -o eth0 -m state \ --state NEW,ESTABLISHED,RELATED -j ACCEPT
Allow forwarding for all NAT connections originating on the Internet that have already passed through the NEW forwarding statements above :
iptables -A FORWARD -t filter -i eth0 -m state \ --state ESTABLISHED,RELATED -j ACCEPT
You will have to create alias IP addresses for each of these public Internet IPs for one to one NAT to work. This is the basic technology of the logical DMZ
Troubleshooting iptables LOG (/var/log/messages) Log and drop all other packets to file /var/log/messages :
iptables -A OUTPUT -j LOG iptables -A INPUT -j LOG iptables -A FORWARD -j LOG iptables -A OUTPUT -j DROP iptables -A INPUT -j DROP iptables -A FORWARD -j DROP
Firewall denies replies to DNS queries (UDP port 53) destined to server 192.168.1.102 on the home network.
Feb 23 20:33:50 bigboy kernel: IN=wlan0 OUT= MAC=00:06:25:09:69:80:00:a0:c5:e1:3e:88:08:00 SRC=192.42.93.30 DST=192.168.1.102 LEN=220 TOS=0x00 PREC=0x00 TTL=54 ID=30485 PROTO=UDP SPT=53 DPT=32820 LEN=200