您的位置:首页 > 其它

iptebles(二)

2015-08-28 16:55 363 查看
一、iptables编写规则
1、命令格式




2、常用选项说明
[-t 表名]:该规则所操作的哪个表,可以使用filter、nat等,如果没有指定则默认为filter

-A:新增一条规则,到该规则链列表的最后一行

-I:插入一条规则,原本该位置上的规则会往后顺序移动,没有指定编号则为1

-D:从规则链中删除一条规则,要么输入完整的规则,或者指定规则编号加以删除

-R:替换某条规则,规则替换不会改变顺序,而且必须指定编号。

-P:设置某条规则链的默认动作

-nL:-L、-n,查看当前运行的防火墙规则列表

chain名:指定规则表的哪个链,如INPUT、OUPUT、FORWARD、PREROUTING等

[规则编号]:插入、删除、替换规则时用,--line-numbers显示号码

[-i|o 网卡名称]:i是指定数据包从哪块网卡进入,o是指定数据包从哪块网卡输出

[-p 协议类型]:可以指定规则应用的协议,包含tcp、udp和icmp等

[-s 源IP地址]:源主机的IP地址或子网地址

[--sport 源端口号]:数据包的IP的源端口号

[-d目标IP地址]:目标主机的IP地址或子网地址

[--dport目标端口号]:数据包的IP的目标端口号

<-j 动作>:处理数据包的动作,包括ACCEPT、DROP、REJECT等

3、应用语法及举例
创建一条自定义的空规则链

语法:iptables [-t table] -N chain
举例:# iptables -t filter -N testchain
删除一条自定义的空规则链

语法:iptables [-t table] -X chain
举例:# iptables -t filter -X testchain
为自定义的链修改名子

语法:iptables [-t table] -E old-chin-name new-chain-name
举例:# iptables -t filter -E testchain chain
为链指定默认策略,修改默认的属性

语法:iptables [-t table] -P chain—name target
举例:# iptables -t filter -P FORWARD DROP
清空链中的规则,操作前将规则做好备份,注意,规则是有编号,在链中自上而下,从1开始

语法: iptables [-t table] -F [chain [rulenum]] [options...]
举例:# iptables -t filter -F INPUT
列出表中的所有规则

语法: iptables [-t table] -L [chain [rulenum]] [options...]
-n:数字格式显示ip和port
-v:显示信息
-x:exactly:精确值,不执行单位换
--line-nambers:显示各规则的行号
[root@example.com ~]# iptables -t filter -nvL
Chain INPUT (policy ACCEPT 292 packets, 22404 bytes)
pkts bytes target     prot opt in     out     source               destination
6   396 ACCEPT     all  --  eth0   *       192.168.0.0/16       192.168.1.8

Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 36 packets, 2688 bytes)
pkts bytes target     prot opt in     out     source               destination
141 14166 ACCEPT     all  --  *      *       192.168.1.8          0.0.0.0/0


pkts:packets,被本规则所有匹配到的报文的个数

baytes:被本规则所匹配到的所有报文的大小子和,单位是字节,会执行单位换算

target:目标,即处理机制

port:协议一般为(TCP|UDP|ICMP)

opt:可选项

in:数据包的流入端口

out:数据包的流出接口

scource:源地址

destination:目标地址

把规则的计数器清零

语法: iptables [-t table] -Z [chain [rulenum]] [options...]
举例:# iptables -t filter -Z
附加/删除一条规则

语法:iptables [-t table] {-A|-D} chain rule-specification
说明:rule-specification :匹配条件 -j 处理机制包括ACCEPT、DROP、REJECT等
通用匹配条件:
-s 匹配原地址,可以IP,也可以网络地址:可以使用!操作符取反,!172.168.0.0/16 相当于 --src,或 --source

-d 匹配目标地址,可以IP,也可以网络地址:可以使用!操作符取反,!172.168.0.0/16

-p 匹配协议,通常只能使用{TCP|UDP|ICMP}三者之一

-i 数据报文流入的接口,通常只用于INPUT,FORWARD和PREROUTING

-o 数据报文流出的接口,通常只用OUTPUT,FORWARD和POSTROUTING

举例:# iptables -t filter -A INPUT -s 192.168.0.0/16 -d 192.168.1.8 -j ACCEPT
# iptables -t filter -A OUTPUT -s 192.168.1.8 -j ACCEPT
# iptables -A INPUT -i eth0 -s 192.168.0.0/16 -d 192.168.1.8 -j ACCEPT
# iptables -A INPUT -i eth0 -d 192.168.1.8 -p tcp -j ACCEPT
# iptables -A INPUT -i eth0 -d 192.168.1.8 -p icmp -j REJECT
删除一条规则

说明:在使用前最好用--line-number 选项列出行号,然后根据行号进行删除,当一条被删除,另外的下一条会被补上。不要执行多次删除行号的操作,这是由于在删除一行以后该行的下一行的行号会自行减一,若在执行一遍删除就会把刚上来的一行给删除。
语法: iptables [-t table] -D chain rulenum
举例:# iptables -D INPUT 2
指定位置插入规则

语法:iptables [-t table] -I chain [rulenum] rule-specification
举例:# iptables -I INPUT -i lo -j ACCEPT //不加行号就是插入到最前端
# iptables -I INPUT 2 -i lo -j ACCEPT //规则插入到指定行号

替换规则(覆盖指定规则)

语法:iptables [-t table] -R chain rulenum rule-specification
举例:# iptables -R INPUT 1 -i lo -j DROP //替换规则一定要是正确
显示指定链上的规则添加命令

语法:iptables [-t table] -S [chain [rulenum]]
举例:# iptables -S //显示所有链上的规则
# iptables -S INPUT //只显示INPUT链上的规则
# iptables -S INPUT 3 //显示INPUT链上第三条规则

4、扩展匹配条件之隐含扩展及举例
使用-p {tcp|udp|icmp}指定某特定协议后,自动能够对协议进行的扩展
-p tcp

--dport m[-n],匹配的目标端口,可以是连续的多个端口,可以取反
--sport:m[-n],匹配的源端口,可以是连续的多个端口,可以取反
--tcp-flags (常用参数:urg psh rst syn ack fin ALL NONE)
-p udp

--dport m[-n],匹配的目标端口,可以是连续的多个端口,可以取反
--sport:m[-n],匹配的源端口,可以是连续的多个端口,可以取反
-p icmp

--icmp-type (0:ping响应、8:ping请求)

放行来自于192.168.0.0/16网络的主机对本机192.168.1.8的ssh服务的请求:
# iptables -t filter -A INPUT -s 192.168.0.0/16 -d 192.168.1.8 -p tcp --dport 22 -j REJECT
# iptables -t filter -A OUTPUT -s 192.168.1.8 -d 192.168.0.0/16 -p tcp --sport 22 -j REJECT

清除tcp报头所有标志位全为1或0的报文,这种为恶意访问:
# iptables -A -p tcp --tcp-flags ALL ALL -j DORP
# iptables -A -p tcp --tcp-flags ALL NONE -j DORP

放行本机的tftp服务:
# iptables -A INPUT -s 192.168.0.0/16 -d 192.168.1.8 -p udp --dport 69 -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -d 192.168.0.0/16 -p udp --sport 69 -j ACCEPT

放行本机的DNS服务:
# iptables -A INPUT -s 192.168.0.0/16 -d 192.168.1.8 -p udp --dport 53 -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -d 192.168.0.0/16 -p udp --sport 53 -j ACCEPT
# iptables -A INPUT -d 192.168.1.8 -p udp --dport 53 -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -p udp --sport 53 -j ACCEPT
# iptables -A INPUT -s 192.168.0.0/16 -d 192.168.1.8 -p tcp --dport 53 -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -d 192.168.0.0/16 -p tcp --sport 53 -j ACCEPT
# iptables -A INPUT -d 192.168.1.8 -p tcp --dport 53 -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -p tcp --sport 53 -j ACCEPT

放行ping其他主机:
# iptables -A OUTPUT -s 192.168.1.8 -p icmp --icmp-type 8 -j ACCEPT
# iptables -A INPUT -d 192.168.1.8 -p icmp --icmp-type 0 -j ACCEPT

# iptables -A OUTPUT -s 192.168.1.8 -p icmp --icmp-type 0 -j ACCEPT
# iptables -A INPUT -d 192.168.1.8 -p icmp --icmp-type 8 -j ACCEPT

5、扩展匹配条件之显式扩展
说明:必须要明确指定的扩展模块

语法规则:-m 扩展模块名称 --专用选项1 --专用选项2
multiport:多端口匹配,一次指定多个离散端口,可以取反

--source-ports --sport ports{port1 port2}
--destination-ports, --dports
--ports
将本机的ssh和web服务开启:
# iptables -I INPUT -d 192.168.1.8 -p tcp -m multiport --dports 22,80 -j ACCEPT
# iptables -I OUTPUT -s 192.168.1.8 -p tcp -m multiport --sports 22,80 -j ACCEPT

iprange:ip地址服务

[!] --src-range from [-to]
[!] --dst-range from [-to]
将本机的telnet服务仅允许192.168.1.1-192.168.1.88网段的主机能访问:
# iptables -A INPUT -d 192.168.1.8 -p tcp --dport 23 -m iprange --src-range 192.168.1.1-192.168.1.88 -j ACCEPT
# iptables -A INPUT -s 192.168.1.8 -p tcp --sport 23 -m iprange --dst-range 192.168.1.1-192.168.1.88 -j ACCEPT

time :指定时间范围

--datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
--datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
--timestart hh:mm[:ss]
--timestop hh:mm[:ss]
[!] --weekdays day[,day....]
将本机的901端口在每周一到周五的早上08:00-晚上18:00开启:
# iptables -A INOUT -d 192.168.1.8 -p tcp --dport 901 -m time --weekdays Mob,Tus,Wed,Thu,FrI --timestart 08:00:00 --timestop 18:00:00 -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -p tcp --sport 901 -j ACCEPT

string :字符串匹配

--alog {bm|kmp} :字符匹配查找时使用的算法
--string “STRING”:要查找的字符串
--hex-string "HEX-STRING" 要查找的字符,先编码成16进制格式
将页面含有字母bols页面DROP:
# iptables -A OUTPUT -s 192.168.1.8 -p tcp --sport 80 -m string --algo kmp --string "bols" -j DROP

connlimit:每IP对指定服务的最大并发连接数

[!] --connlimit-above

限定访问web服务器页面的最带请求不能超过5个:
# iptables -I INPUT -d 192.168.1.8 -p tcp --dport 80 -m connlimit --connlimit-above 5 -j DROP

limit:报文速率控制

--limit #[/second|/minute|/hour|/day]默认每小时三个
--limit-burst #初始峰值
设定别人ping主机每秒钟次数为2次,峰值不能超过5次:
# iptables -I INPUT -d 192.168.1.8 -p icmp --icmp-type 8 -m limit --limit 2/second --limit-burst 5 -j ACCEPT

state:状态追踪

--state {NEW,ESTATBLISHED,INVALID,RELATED} 指定检测那种状态
NEW:该包想要开始一个连接(重新连接或将连接重定向)。

RELATED:该包是属于某个已经建立的连接所建立的新连接。例如:FTP的数据传输连接就是控制连接所 RELATED出来的连接。--icmp-type 0 ( ping 应答) 就是--icmp-type 8 (ping 请求)所RELATED出来的。

ESTABLISHED :只要发送并接到应答,一个数据连接从NEW变为ESTABLISHED,而且该状态会继续匹配这个连接的后续数据包。

INVALID:数据包不能被识别属于哪个连接或没有任何状态比如内存溢出,收到不知属于哪个连接的ICMP错误信息,一般应该DROP这个状态的任何数据。

应用场景:我们知道服务器的80端口是提供web服务的,一般客户端连接服务端时的状态为NEW或者ESTABLISHED,而服务端响应服务时的状态为ESTABLISHED,现在有一种木马叫反弹木马,(其工作原理为:其驻留在用户计算机里的一段服务程序,而攻击者控制的则是相应的客户端程序。服务程序通过特定的端口,打开用户计算机的连接资源。一旦攻击者所掌握的客户端程序发出请求,反弹木马便和他连接起来,将用户的信息窃取出去。)但这种木马的特点就是服务器端响应客户端的状态为NEW,而不是ESTABLISHED,基于这种原理我们就可以将响应状态为ESTABLISHED的端口服务给DROP掉。但是一定要注意当使用此模块时会消耗一定的内存空间,若服务器的响应请求很多就不建议使用。特别是反向代理的服务器。

当请求本机的22、80端口时只开启 NEW,ESTABLISHED两种状态,而本机响应客户端是之开启ESTABLISHED状态,同时开启本机的ping请求,但是其每秒响应2次,峰值为5次:
# iptables -A INPUT -d 192.168.1.8 -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
# iptables -P INPUT DROP
# iptables -P OUTPUT DROP
# iptables -A INPUT -d 192.168.1.8 -p icmp --icmp-type 8 -m limit --limit 2/second --limit-burst 5 -m state --state NEW,ESTABLISHED -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -p icmp --icmp-type 0 -m state --state ESTABLISHED -j ACCEPT
# iptables -A INPUT -d 192.168.1.8 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
优化上例的规则:
# iptables -I INPUT -d 192.168.1.8 ! -p icmp -m state --state ESTABLISHED -j ACCEPT
# iptables -I INPUT 2 -d 192.168.1.8 -p tcp -m multiport --dports 22,80 -m state --state NEW -j ACCEPT
# iptables -D INPUT 3
# iptables -D INPUT 4
# iptables -I OUTPUT -s 192.168.1.8 -m state --state ESTABLISHED -j ACCEPT
# iptables -D OUTPUT 2
[root@example.com ~]# iptables -nvL
Chain INPUT (policy DROP 66 packets, 5148 bytes)
pkts bytes target     prot opt in     out     source               destination
172 10448 ACCEPT    !icmp --  *      *       0.0.0.0/0            192.168.1.8         state ESTABLISHED
2   120 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.8         multiport dports 22,80 state NEW

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
499 61149 ACCEPT     all  --  *      *       192.168.1.8          0.0.0.0/0           state ESTABLISHED


同时还可以自定义个链,把这个链应用到上例中:
# iptables -t filter -N clean_in
# iptables -A clean_in -d 192.168.1.8 -p tcp --tcp-flags ALL ALL -j DROP
# iptables -A clean_in -d 192.168.1.8 -p tcp --tcp-flags ALL NONE -j DROP
# iptables -A clean_in -d 192.168.255.255 -p icmp -j DROP
# iptables -A clean_in -d 255.255.255.255 -p icmp -j DROP
# iptables -A clean_in -d 192.168.1.8 -m state --state INVALID -j DROP
# iptables -A clean_in -d 192.168.1.8 -j RETURN
# iptables -I INPUT -d 192.168.1.8 -j clean_in
注意:前面我们已经看了state模块的安全机制很不错,但是要注意若使用state模块一般尽量不要用在一个繁忙的服务器上,特别是反向代理服务器。这是因为使用state模块会占用一定的系统资源,下面在给出使用state模块是一些建议:
对于进入状态为ESTABLISHED都应该放行

对于返回状态为ESTABLISHED都应该放行

严格检查进入的状态为NEW的链接

所有状态为INVALID都应该拒绝

举例:如何放行工作于被动模式下的FTP服务
# iptables -A INPUT -d 192.168.1.8 -p tcp --dport 22 -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -p tcp --sport 22 -j ACCEPT
# iptables -P INPUT DROP
# iptables -P OUTPUT DROP
# iptables -A INPUT -d 192.168.1.8 -p tcp --dport 21 -j ACCEPT

# iptables -A OUTPUT -s 192.168.1.8 -p tcp --sport 21 -j ACCEPT

# iptables -A INPUT -d 192.168.1.8 -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT
# iptables -A OUTPUT -s 192.168.1.8 -p tcp -m state --state ESTABLISHED -j ACCEPT
在开启ftp服务时还要将其相关模块加载至内核中,下面是手动加载,重启后会丢失:
[root@example.com ~]# modprobe ip_nat_ftp
[root@example.com ~]# modprobe ip_conntrack_ftp
也可以写入配置文件中,并把配置文件定义在/etc/rc.local中,来开机自动加载:
[root@example.com ~]# vim /etc/sysconfig/iptables-config
IPTABLES_MODULES="ip_nat_ftp ip_conntraack_ftp"
下面是将上面的规则进行优化的一种方法:
# iptables -I INPUT -d 192.168.1.8 -m state --state RELATED,ESTABLISHED -j ACCEPT
# iptables -D INPUT 4
# iptables -I INPUT 2 -d 192.168.1.8 -p tcp -m multiport --dports 21,22,80 -m state --state NEW -j ACCEPT
# iptables -D INPUT 3
# iptables -D INPUT 3
# iptables -I OUTPUT -s 192.168.1.8 -m state --state ESTABLISHED -j ACCEPT
# iptables -D OUTPUT 2
# iptables -D OUTPUT 2
# iptables -D OUTPUT 2

扩展学习
在上面的state模块中还有下面一些其性能相关的配置文件:
/proc/sys/net/nf_conntrack_max:调整连接追踪功能所能容纳的最大连接数,因此,建议按需调大此值

/proc/net/nf_conntrack:记录了当前追踪的所有连接

/proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established:定义了连接的超时时间

6、总结
由于在写iptables规则是保存在内存中,若机器重启或者iptables服务重启均会导致规则的丢失,下面就是讲如何保存及生效规则:
第一种:service iptables save ,规则会被保存至/etc/sysconfig/iptables文件中, 默认若在此启动iptables时也会读取此文件中的内容生成此文件指定的规则。
第二种: iptables-save > /path/to/some_rulefile 保存iptables至别的位置 ,若想在次载入规则就使用iptables-restore < /path/to/some_rulefile 从自定义的位置读取并使之生效。
我们在写iptables规则时一般有两种总体方针,第一、通即定义白名单,默认为堵,只对能识别的进行放行,第二、堵即定义黑名单,默认为通,只对能识别的进行阻截。上面两种在日常使用时最好使用默认为堵,设置白名单,这样做的好处是能根据自己的需求灵活定义规则,但是最大的劣势就是一旦自己不小心清空规则或者规则定义错误,那么就不能通过ssh连接服务器,遇到这种情况只有两种方法,要么在写规则时执行一个定时任务,用来清理刚才定义的内容。还有就是让人到机房把机器重启。若两者都不行就要看自己的人品了你放服务器的机房突然断电。最后在写规则时最好将控制强的放在前面,应用访问频繁的也要放在前面,这要做能节约系统资源。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iptables 规则