Iptables Tutorial For Beginners
Iptables Tutorial For Beginners
Introduction
---------------
Iptables is a Linux based packet filtering firewall. Iptables interfaces to the Linux netfilter
module to perform filtering of network packets. This can be to deny/allow traffic filter or
perform Network Address Translation (NAT). With careful configuration iptables can be a
very cost effective, powerful and flexible firewall or gateway solution. Iptables is available
from http://www.netfilter.org/ or via your Linux distribution.
In short, iptables is a packet filtering tool which allows system administrator to define
incoming and outgoing packets to and from the system using certain rules. Iptables can be
confusing it's pretty straightforward once you get the hang of it.
Iptables rules are grouped into chains. A chain is a set of rules used to determine what to do
with a packet. These chains are grouped into tables. Iptables has three built in tables filter,
NAT, mangle. More tables can be added through iptables extensions.
Filter Table
The filter table is used to allow and block traffic, and contains three chains INPUT,
OUTPUT, FORWARD. The input chain is used to filter packets destined for the local
system. The output chain is used to filter packets created by the local system. The forward
chain is used for packets passing through the system, mainly used for gateways/routers.
* INPUT
Which is used to grant or deny incoming connections to your machine.
* OUTPUT
Which is used to grant or deny outgoing connections from your machine.
* FORWARD
Which is used for forwarding packages across interfaces, only really needed (in general)
when you're setting up a gateway machine.
NAT Table
The NAT table is used to setup the rules to rewrite packets allowing NAT to happen. This
table also has 3 chains, PREROUTING, POSTROUTING, and OUTPUT. The prerouting
chain is where packets come to prior to being parsed by the local routing table. The
postrouting chain is where packets are sent after going through the local routing table.
The CHAIN we've briefly covered before, "INPUT", "OUTPUT", "FORWARD", etc. Here "-
A INPUT" means "append this rule to the input chain".
The "-p tcp" means this rule applies only to TCP connections, not UDP. (To specify UDP
connections you'd use "-p udp" instead.)
Finally "-j ACTION" is used to specify what to do to packets which match your rule. Usually
an action will be one of "-j DROP" to drop the package, "-j ACCEPT", to accept the packet
or "-j LOG" to log it.
Commands
Main commands
Code:
iptables -A INPUT ...
Code:
iptables -D INPUT 1
iptables -D INPUT --dport 80 -j DROP
Code:
iptables -R INPUT 1 -s 192.168.0.1 -j DROP
Code:
iptables -I INPUT 1 --dport 80 -j ACCEPT
Code:
iptables -L # Display all the rules of the FILTER chains
iptables -L INPUT # Display all the INPUT rules (FILTER)
Code:
iptables -F INPUT # Delete all the rules of the INPUT chain
iptables -F # Delete all the rules
* -N --new-chain : Allow to create a new chain
Code:
iptables -N LOG_DROP
Code:
iptables -X LOG_DROP # Delete the LOG_DROP chain
iptables -X # Delete the chains
* -P --policy : Allow to specify to the kernel the default policy of a chain ACCEPT,
REJECT, DROP ...
Code:
iptables -P INPUT DROP
Basic Uses
The most common use of iptables is to simply block and allow traffic.
Allow Traffic
Iptables allows you to allow traffic based on a number of different conditions such as
Ethernet adapter, IP Address, port, and protocol.
Allow incoming TCP traffic on port 80 (HTTP) for the IP range 192.168.0.1 to
192.168.0.254.
iptables -A INPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 80 -j ACCEPT
Block Traffic
Iptables can block traffic on the same conditions that traffic can be allowed.
Limit Traffic
Along with allowing and denying traffic IP tables can be used to limit the number of
connections allowed over time thresholds.
iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m sshbrute --set
iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m sshbrute --update --
seconds 60 --hitcount 4 -j DRROP
[:p:] this is a common set of rules used to block brute force ssh attacks. The first rule makes
sure the IP connecting is added to the sshbrute list. The second rule tells iptables to check the
sshbrute list and if the packet threshold is exceeded to drrop the traffic.
Examples :
We used the "-m state --state NEW --dport 21" to match against new connections to port 21.
Other options allow you to match against different things.
ref:
http://ubuntuforums.org/showthread.php?t=159661
http://www.higherpass.com/linux/Tutorials/Iptables-Primer/