iptables 详解


一.iptables内置链

PREROUTING:数据包进入路由表之前

INPUT:通过路由表后目的地为本机

FORWARDING:通过路由表后,目的地不为本机

OUTPUT:由本机产生,向外转发

POSTROUTIONG:发送到网卡接口之前

 

二:策略

Framework:

默认规则:

    开放:堵

    关闭:通

三:规则匹配

规则 :匹配标准

IP SIPDIP

TCP SPORT DPORT           

syn=1,ack=0,fin=0,rst=0;  SYN=1,ACK=1,FIN=0.RST=0;

 ACK=1,SYN=0.RST=0,FIN=0(ESTABLISH)

 

UDP SPORTDPORT

ICMPicmp-type echo-request 8 echo-request 0

 

四:命令格式

iptables  [-t table]  COMMAND  chain CRETIRIA -j ACTION

 

 Command

1链管理  -P   eg:iptables  –P INPUT (DROP|ACEPT)

                       -F    清空链

                       -N   NEWCHAIN 新建一个链

                       -X   用于删除用户自定义的空链

                       -E  old  new  给用户自定义的链重命名

                       -Z  清空链,以及链中的规则的计数器

 2规则管理 –A 追加 –I num 插入 –R num 替换 –D num 删除

            

3、查看命令 –L 

(-n 不解析 –v –vv –vvv  -line-numbers  –t

  -x: 在计数器上显示精确值,不做单位换算)

 CRETIRIA:   1.通用匹配 –s(必须是ip) | –d| –p (tcp|udp|icmp)

                      -i eth0  一般用在INPUT和PREROUTING

                      -o eth0  一般在OUTPUT和POSTROUTING

             2.拓展匹配

                  2.1隐藏拓展

                           -p tcp    --dport  xx-xx |  xx

                                    --sport

                                    --tcp-flags(两个参数1.标志位,2.必须为1的)

-p udp    --dport  | --sport

-p icmp   --icmp-type 

Echo-request 8 

Echo-reply  0

2.2  -m  (显示拓展)

  Staus  --status  

      NEW

     ESTABLISHED

      RELATED

     INVALID

 

五.iptables查看设置

命令中设置的规则是临时的,保存到/etc/sysconfig/iptables中重启服务永久生效

# /etc/rc.d/init.d/iptables save

# /etc/init.d/iptables restart

查看

# iptables -L -n

设置

【1】filter链

# iptables -p INPUT DROP

# iptables -p OUTPUT ACCEPT

# iptables -p FORWARD DROP

INPUT,FORWARD两个链采用的是允许什么包通过,而OUTPUT链采用的是不允许什么包通过.

注:如果你是远程SSH登陆的话,当你输入第一个命令回车的时候就应该掉了.因为你没有设置任何规则.

怎么办,去本机操作呗!(可先设置规则再DROP)

1.首先添加INPUT链,INPUT链的默认规则是DROP,所以我们就写需要ACCETP(通过)

为了能采用远程SSH登陆,我们要开启22端口

# iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

(注:这个规则,如果你把OUTPUT 设置成DROP的就要写上这一步)

如果开启了web服务器,OUTPUT设置成DROP的话,同样也要添加一条链:

# iptables -A INTPUT -p tcp --dport 80 -j ACCEPT

# iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT

如果做了邮件服务器,开启25,110端口.

# iptables -A INPUT -p tcp --dport 110 -j ACCEPT

# iptables -A INPUT -p tcp --dport 25 -j ACCEPT

如果做了FTP服务器,开启21端口

# iptables -A INPUT -p tcp --dport 21 -j ACCEPT

# iptables -A INPUT -p tcp --dport 20 -j ACCEPT

如果做了DNS服务器,开启53端口

# iptables -A INPUT -p tcp --dport 53 -j ACCEPT

允许loopback!(不然会导致DNS无法正常关闭等问题)

IPTABLES -A INPUT -i lo -p all -j ACCEPT (如果是INPUT DROP)

IPTABLES -A OUTPUT -o lo -p all -j ACCEPT(如果是OUTPUT DROP)

下面写OUTPUT链,OUTPUT链默认规则是ACCEPT,所以我们就写需要DROP(放弃)的链.

减少不安全的端口连接

[root@tp ~]# iptables -A OUTPUT -p tcp --sport 31337 -j DROP

[root@tp ~]# iptables -A OUTPUT -p tcp --dport 31337 -j DROP

有些些特洛伊***会扫描端口31337到31340(即***语言中的 elite 端口)上的服务。既然合法服务都不使用这些非标准端口来通信,阻塞这些端口能够有效地减少你的网络上可能被感染的机器和它们的远程主服务器进行独立通信的机会

2.iptables中的icmp

在iptables看来,只有四种ICMP分组,以下分组类型可以被归为NEW、ESTABLISHED

ECHO请求(ping,8)和ECHO应答(ping,0)

时间戳请求(13)和应答(14)

信息请求(15)和应答(16)

地址掩码请求(17)和应答(18)

这些ICMP分组类型中,请求分组属于NEW,应答分组属于ESTABLISHED。而其它类型的ICMP分组不基于请求/应答方式,一律被归入RELATED。

例1:

INPUT和OUTPUT均设置为DROP设置如下允许ping

# iptables -A INPUT -p icmp -j ACCEPT

# iptables -A OUTPUT -p icmp -j ACCEPT

例2:

允许3台主机互相ping

192.168.36.220

192.168.36.150

192.168.36.167

主机192.168.36.167

# iptables -A INPUT -s 192.168.36.220 -p icmp -j ACCEPT

# iptables -A OUTPUT -d 192.168.36.220 -p icmp -j ACCEPT

# iptables -A INPUT -s 192.168.36.150 -p icmp -j ACCEPT

# iptables -A OUTPUT -s 192.168.36.150 -p icmp -j ACCEPT

# iptables -P INPUT DROP

# iptables -P OUTPUT DROP

主机192.168.36.220

# iptables -A INPUT -s 192.168.36.167 -p icmp -j ACCEPT

# iptables -A OUTPUT -d 192.168.36.167 -p icmp -j ACCEPT

# iptables -A INPUT -s 192.168.36.150 -p icmp -j ACCEPT

# iptables -A OUTPUT -d 192.168.36.150 -p icmp -j ACCEPT

# iptables -P INPUT DROP

# iptables -P OUTPUT DROP

# iptables -L

主机192.168.36.150

# iptables -A INPUT -s 192.168.36.167 -p icmp -j ACCEPT

# iptables -A OUTPUT -d 192.168.36.167 -p icmp -j ACCEPT

# iptables -A INPUT -s 192.168.36.220 -p icmp -j ACCEPT

# iptables -A OUTPUT -d 192.168.36.220 -p icmp -j ACCEPT

# iptables -P INPUT DROP

# iptables -P OUTPUT DROP

例3:

# iptables -A OUTPUT -p icmp -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

# iptables -A INPUT -p icmp -m state --state ESTABLISHED,RELATED -j ACCEPT

一个ICMP echo请求是一个NEW连接。因此,允许ICMP echo请求通过OUTPUT链。当请求的应答返回,由于iptables两个方向上都发包了,此时连接的状态是ESTABLISED,因此允许通过INPU链。而INPUT链没 有NEW状态,因此不允许echo请求通过INPUT链。也就是说,这两条规则允许内部主机ping外部主机,而不允许外部主机ping内部主机。

一个重定向ICMP(5)分组不是基于请求/应答方式的,因此属于RELATED。INPUT和OUTPUT链都允许RELATED状态的连接,因此重定向(5)分组可以通过INPUT和OUTPUT链。

spacer.gifwKioL1fOM1bRa0vMAABQbGObQJs306.png-wh_50

删除(filter中INPUT的第一条规则,OUTPUT的第一条规则)

# iptables -D INPUT 1

# iptables -D OUTPUT 1

3.只允许192.168.36.220的机器进行SSH连接

# iptables -A INPUT -s 192.168.36.220 --dport 22 -j ACCEPT

注:如果要允许,或限制IP段可用 192.168.36.0/24 表示192.168.36.1-255端的所有IP.

!192.168.0.3 表示除了192.168.0.3的ip地址

/etc/sysconfig/iptables 里的这一行删了:

-A INPUT -p tcp -p tcp --dport 22 -j ACCEPT 因为它表示所有地址都可以登陆.

或命令设置

# iptables -D INPUT -p tcp --dport 22 -j ACCEPT

【2】FORWARD链

# iptables -A FORWARD -j DROP

转发监控

# iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT

# iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

丢弃坏的tcp包

# iptables -A FORWARD -p TCP ! --syn -m state --state NEW -j DROP

处理IP碎片数量,防止***,允许每秒100个

# iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT

设置ICMP包过滤,允许每秒1个包,限制触发条件是10个包

# iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT

【3】nat链

192.168.36.150

192.168.36.220

192.168.36.167

主机192.168.36.167做转发

# iptables -t nat -A PREROUTING -s 192.168.36.150 -p tcp --dport 8081

-j DNAT --to-destination 192.168.36.220:80

# iptables -t nat -A POSTROUTING -d 192.168.36.220 -p tcp --dport 80

-j SNAT --to-source 192.168.36.167:8081

192.168.36.150->192.168.36.167->192.168.36.220

源150、目167 源167、目220

在主机150上执行以下命令,可以获取到远程主机的web页面

curl 192.168.36.167

curl 192.168.36.220

【4】drop非法连接,允许已经建立的连接

# iptables -A INPUT -m state --state INVALID -j DROP

# iptables -A OUTPUT -m state --state INVALID -j DROP

# iptables -A FORWARD -m state --state INVALID -j DROP

# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

例4:

防火墙跳转

客户端通过访问A服务器的999端口转到B服务器的22端口完成ssh登录

# iptables -t nat -A PREROUTING -d 10.1.14.36 -p tcp --dport 999 -j DNAT --to-destination 10.1.16.174:22 #PREROUTING在到达路由之前

# iptables -L -t nat #查看nat表的状态

spacer.gifwKiom1fOM4ajMWuAAABu5WgzFJY837.png-wh_50