Working With Iptables
Working With Iptables
hostpresto.com/community/tutorials/working-with-iptables
Hitesh Jethva
Setting up a good firewall is necessary for every system administrator to secure their
operating system. Iptables is a command line tool that allows a linux system
administrator to configure the tables provided by the linux kernel implemented within
the Netfilter project.
Iptables is used to protect your server from unwanted traffic from the internet. There are
many different firewall tools available that you can use to configure your firewall.
Iptables is one of them included in most linux distributions by default.
Iptables uses a set of tables with different chains, chains contains set of built in rules or
user defined rules.
FILTER Table : This is the default table that contains following chains"
NAT Table : This table is used when packet tries to create a new connection. It has the
following built in chains.
1. PREROUTING : This chain alters packets before routing. This is used to translate the
destination ip address of the packets that matches the routing on the local
machine. It is also used for destination NAT.
2. OUTPUT : This chain is used for altering packets that is generated from local
machine.
3. POSTROUTING : This chain is used for altering packets after routing. This is used
translate the source ip address of the packets that match the routing on the local
machine.
MANGLE Table : This table is used for packet altering. Currently there are five chains
available.
In this tutorial, we will see different iptables tips and tricks to protect your server on
Ubuntu 14.04.
1/10
Requirements
A CentOS-7 server
A Non-root user account with sudo privileges
Installing Iptables
By default firewalld is available on CentOS-7 to manage iptables, so you will need to
disable firewalld and enable first. You can disable firewalld by running the following
commands:
Enable the service to start at boot time by running the following commands:
sudo systemctl start iptables sudo systemctl stop iptables sudo systemctl restart iptables
When you reboot the system and restart the iptable service, the existing rules will be
flushed out, so you will need to run the following command whenever you add any rule.
sudo iptables -L
You should see the following output:
If you want to list ipatbles rules for specific tables like the nat table or mangle table, then
run the following commands:
2/10
sudo iptables -L -t nat sudo iptables -L -t mangle
You should see the following output:
If you want to list all the rules with more data, then run the following command:
sudo iptables -L -n -v
Output:
You can also list all rules with line number by running the following command:
3/10
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 296 22494 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state
RELATED,ESTABLISHED
2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
3 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 3 568 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-
prohibitedChain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-
prohibited
Chain OUTPUT (policy ACCEPT 159 packets, 19056 bytes)
num pkts bytes target prot opt in out source destination
If you want to list rules for specific chain like INPUT and OUTPUT, then run the following
command:
sudo iptables -P INPUT DROP sudo iptables -P OUTPUT DROP sudo iptables -P FORWARD
DROP
After running above commands, you will not be able to connect anywhere.
To block all incoming / forwarded packets, but allow outgoing traffic, run the following
commands:
sudo iptables -P INPUT DROP sudo iptables -P FORWARD DROP sudo iptables -P OUTPUT
ACCEPT sudo iptables -A INPUT -m state --state NEW,ESTABLISHED -j ACCEPT
Now, check the firewall status by running the following command:
sudo iptables -L -v -n
Output:
4/10
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
33 2444 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state
RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-
prohibited
0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state
NEW,ESTABLISHEDChain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-
prohibited
Chain OUTPUT (policy ACCEPT 3 packets, 328 bytes)
pkts bytes target prot opt in out source destination
Block by IP Address
If you want to block specific IP address (192.168.1.34), then run the following command:
The DROP option drops the packet without responding any acknowledgement.
If you want to block only TCP traffic from that specific IP address then run the following
command:
5/10
To allow incoming connections on port 22, run the following command:
sudo iptables -A INPUT -i eth1 -j LOG --log-prefix "IP_SPOOF A: " sudo iptables -A INPUT -i
eth1 -j DROP
6/10
The messages are logged in to file /var/log/messages, you can see by running the
following command:
sudo iptables -A INPUT -s 192.168.20.0/24 -m state --state NEW -p udp --dport 123 -j ACCEPT
To open SMTP port for mail, run:
sudo iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT sudo iptables -A
INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
To allow printing service for specific network, run:
sudo iptables -A INPUT -s 192.168.20.0/24 -p udp -m udp --dport 631 -j ACCEPT sudo
iptables -A INPUT -s 192.168.20.0/24 -p tcp -m tcp --dport 631 -j ACCEPT
To open POP3 and IMAP port, run:
sudo iptables -A INPUT -m state --state NEW -p tcp --dport 110 -j ACCEPT sudo iptables -A
INPUT -m state --state NEW -p tcp --dport 143 -j ACCEPT
To allow Samba file server to specific network, run:
sudo iptables -A INPUT -s 192.168.20.0/24 -m state --state NEW -p tcp --dport 137 -j
ACCEPT sudo iptables -A INPUT -s 192.168.20.0/24 -m state --state NEW -p tcp --dport 138 -j
ACCEPT sudo iptables -A INPUT -s 192.168.20.0/24 -m state --state NEW -p tcp --dport 139 -j
ACCEPT sudo iptables -A INPUT -s 192.168.20.0/24 -m state --state NEW -p tcp --dport 445 -j
ACCEPT
To allow access for mysql, run:
If you want to allow only established and related incoming traffic for incomming
connections, run the following command:
sudo iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-port 8080
The above command forwards all incoming traffic on interface eth1 from port 80 to port
8080. You may change the ports with your need.
If you want to share internet, you will need to set FORWARD chain to ACCEPT target by
running the following command:
sudo iptables -F
To delete chain, run the following command:
sudo iptables -X
You can delete chains from specific table like nat and mangle table by running the
following command:
First, display all rules for INPUT chain with line number run the following command:
8/10
In the above output, you can see the list of all rules with line number.
To check whether iptables allowing access to the port 22 from outside or not by running
the following command:
You can also use the telnet command to see if firewall allows to connect to port 22. On
remote machine, run the following command:
telnet server-ip-address 22
You should see the following output:
Trying 192.168.43.7...
Connected to 192.168.43.7.
Escape character is '^]'.
SSH-2.0-OpenSSH_6.6.1
You can also use nmap command to check whether port 22 allow or not:
9/10
Output: ``` language-bash Starting Nmap 6.40 ( http://nmap.org ) at 2016-08-17 14:25
IST Nmap scan report for centOS-7 (192.168.43.7) Host is up (0.00082s latency). PORT
STATE SERVICE 22/tcp open ssh MAC Address: 08:00:27:8C:3F:C6 (Cadmus Computer
Systems)
10/10