Linux Ip Tables
Linux Ip Tables
Linux Ip Tables
by VIVEK GITE on DECEMBER 13, 2011 last updated JANUARY 21, 2016
in IPTABLES, LINUX, LINUX DISTRIBUTION, LINUX EMBEDDED DEVICES, LINUX LAPTOP
inux comes with a host based firewall called Netfilter. According to the
This Linux based firewall is controlled by the program called iptables to handles
filtering for IPv4, and ip6tables handles filtering for IPv6. I strongly recommend
that you first read ourquick tutorial that explains how to configure a host-based
firewall called Netfilter (iptables) under CentOS / RHEL / Fedora / Redhat
Enterprise Linux. This post lists most simple iptables solutions required by a
new Linux user to secure his or her Linux operating system from intruders.
Most of the actions listed in this post written with the assumption that
they will be executed by the root user running the bash or any other modern
shell. Do not type commands on the remote system as it will disconnect
your access.
For demonstration purpose, Ive used RHEL 6.x, but the following
command should work with any modern Linux distro that use the netfliter.
prot opt in
out
source
destination
prot opt in
out
source
destination
prot opt in
out
source
destination
Above output indicates that the firewall is not active. The following sample
shows an active firewall:
# iptables - L - n - v
Sample outputs:
Chain INPUT (policy DROP . packets,. bytes)
. DROP
prot opt in
out
source
destination
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
all
- -
br.
. ,. ,. ,. -.
. ,. ,. ,. -.
all
- -
lo
state INVALID
state RELATED,ESTABLISHED
91 / 7090 ACCEPT
/ 20 ACCEPT
. ,. ,. ,. -.
. ,. ,. ,. -.
prot opt in
out
source
destination
. ACCEPT
all
- -
br.
br.
. ,. ,. ,. -.
. ,. ,. ,. -.
. DROP
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
state INVALID
. TCPMSS
tcp
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
. ACCEPT
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
state RELATED,ESTABLISHED
. wanin
all
- -
vlan0 (
. ,. ,. ,. -.
. ,. ,. ,. -.
. wanout
all
- -
vlan0
. ,. ,. ,. -.
. ,. ,. ,. -.
. ACCEPT
all
- -
br.
. ,. ,. ,. -.
. ,. ,. ,. -.
prot opt in
out
source
destination
out
source
destination
out
source
destination
prot opt in
prot opt in
Where,
-L : List rules.
num
target
DROP
destination
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
state
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
state
INVALID
ACCEPT
RELATED,ESTABLISHED
ACCEPT
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
ACCEPT
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
num
target
destination
ACCEPT
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
DROP
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
state
INVALID
TCPMSS
tcp
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
tcp
ACCEPT
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
RELATED,ESTABLISHED
wanin
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
wanout
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
ACCEPT
all
- -
. ,. ,. ,. -.
. ,. ,. ,. -.
num
target
destination
state
num
target
destination
num
target
destination
You can use line numbers to delete or insert new rules into the firewall.
#1.2: To display INPUT or OUTPUT chain rules, enter:
# iptables - L INPUT - n - v
# iptables - L OUTPUT - n - v - - line- numbers
-X : Delete chain.
num
target
DROP
all
- -
0. 0,32,/ ,/
ACCEPT
all
- -
. ,. ,. ,. -.
destination
. ,. ,. ,. -.
. ,. ,. ,. -.
state
NEW,ESTABLISHED
num
target
destination
DROP
all
- -
0. 0,32,/ ,/
. ,. ,. ,. -.
DROP
all
- -
0. 0,32,/ ,0
. ,. ,. ,. -.
ACCEPT
all
- -
. ,. ,. ,. -.
NEW,ESTABLISHED
. ,. ,. ,. -.
state
#8.1: IPv4 Address Ranges For Private Networks (make sure you block them on public
interface)
10.0.0.0/: -j (A)
192.16.0.0/12 (B)
192.16:.0.0/16 (C)
224.0.0.0/4 (MULTICAST D)
240.0.0.0/7 (E)
129.0.0.0/: (LOOPBACK)
Note down its ip address and type the following to block all outgoing traffic to
97.126.173.206:
# iptables - A OUTPUT - d 73,/ 04,/ 31,0. 4 - j DROP
You can use a subnet as follows:
49,/ 7/ ,002,. -/ 9
Type the following to log and block IP spoofing on public interface called eth1
# iptables - A INPUT - i eth/ - s / . ,. ,. ,. -6 - j LOG - - log- prefix "IP_SPOOF A:"
# iptables - A INPUT - i eth/ - s / . ,. ,. ,. -6 - j DROP
By default everything is logged to /var/log/messages file.
# tail - f -var-log-messages
# grep - - color '
IP SPOOF'-var-log-messages
#13: Log and Drop Packets with Limited Number of Log Entries
The -m limit module can limit the number of log entries created per time. This is
used to prevent flooding your log file. To log and drop spoofing per 7 minutes,
in bursts of at most 9 entries .
# iptables - A INPUT - i eth/ - s / . ,. ,. ,. -6 - m limit - - limit 3-m - - limit- burst 7
- j LOG - - log- prefix "IP_SPOOF A:"
# iptables - A INPUT - i eth/ - s / . ,. ,. ,. -6 - j DROP
## nat example ##
iptables - t nat - A POSTROUTING - j SNAT - - to- source
/ 90,/ 46,/ ,0. - / 90,/ 46,/ ,03
## open cups (printing service) udp/tcp port 631 for LAN users ##
iptables - A INPUT - s / 90,/ 46,/ ,. /02 - p udp - m udp - - dport 41/ - j ACCEPT
iptables - A INPUT - s / 90,/ 46,/ ,. /02 - p tcp - m tcp - - dport 41/ - j ACCEPT
## allow time sync via NTP for lan users (open udp port 123) ##
iptables - A INPUT - s / 90,/ 46,/ ,. /02 - m state - - state NEW - p udp - - dport / 01 - j ACCEPT
0,
--connlimit-mask 24 : Group hosts using the prefix length. For IPv4, this
must be a number between (including) 0 and 32.
Connected to www,cyberciti,biz,
^]
telnet> quit
Connection closed,
You can use nmap to probe your own server using the following syntax:
$ nmap - sS - p 6. www,cyberciti,biz
Sample outputs:
Starting Nmap 3,. . (http:--nmap,org ) at 0. / / - / 0- / 1 / 1:/ 9 IST
PORT
STATE SERVICE
6. -tcp open
http
I also recommend you install and use sniffer such as tcpdupm and ngrep to test
your firewall settings.
Conclusion:
This post only list basic rules for new Linux users. You can create and build
more complex rules. This requires good understanding of TCP/IP, Linux kernel
tuning via sysctl.conf, and good knowledge of your own setup. Stay tuned for
next topics:
Layer 2 filtering.