iptables 教程,iptables 详解,iptables 常见使用实例

时间:2020-04-07
本文章向大家介绍iptables 教程,iptables 详解,iptables 常见使用实例,主要包括iptables 教程,iptables 详解,iptables 常见使用实例使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

iptables的详细教程,可以参考朱双印的个人博客,http://www.zsythink.net/archives/1199

iptables由4张表 5条链组成

表像一个容器,来存储不同类型的规则,
链是类似一个关卡,大门上写好了不同的表(表里有规则)

表: filter表,nat表,mangle表,raw表
链: input链,output链,prerouting链,postrouting链,forward链

centos 7 默认使用firewalld 没有使用iptables
可以通过如下命令禁用firewalld 并启用iptables

yum install iptables-services -y
systemctl stop firewalld
systemctl disable firewalld
systemctl enable iptables
service iptables start
iptables -F
service iptables save

以上操作后,启用iptables 并且清空了iptables的规则

(一)查询规则

iptables -nL

n参数是禁止把ip转换成域名的形式,因为很多ip会反解析成域名,不方便查看
L是列表的意思
查看现有的filter表的规则,默认省却了 -t filter, 其实全写是iptables -t filter -nL
如果要查询nat表的规则,那就iptables -t nat -nL

iptables -nvL

比上面的事例多了一个v, v参数可以显示不同规则,产生的流量包数量和流量大小

iptables -nvxL

比上面的事例多了一个x, 这个在shell编程中可能会用到,不加x ,类似 ls -h, 流量是为了人类可读的方式,根据流量
大小,转换成Kb或者Mb
加了x,那么流量的单位就变成了小b, byte,方便shell脚本计算

iptables -t filter -nvxL INPUT
iptables -t filter -nvxL OUTPUT
iptables -t filter -nvxL self_define

可以指定查看不同链,在行尾放不同的链名称,或者自定义的链名称

iptables -t filter -nvxL INPUT --line

比上一个多了一个--line,两个短杠加line ,双短杠不可以省略,原本全写是--line-numbers,已经是省略过的了
可以查看不同规则的行号

(二)修改规则

iptables -t filter -A INPUT -s 192.168.1.146 -j DROP
iptables -t filter -A INPUT -s 192.168.1.146 -j ACCEPT

命令语法: iptabels -t 表名 -A 链名 匹配条件 -j 动作
-A 是append的意思,这里如果写 -I 就是insert的意思
append把新增规则放到最后一行
insert把新增规则放到第一行
第一个命令是把 s (source的意思) 来自IP 192.168.1.146 的数据包 drop 掉这个命令加到行尾
第二个命令是把 s (source的意思) 来自IP 192.168.1.146 的数据包 accept 这个命令加到行尾

iptables -t filter -D INPUT -s 192.168.1.146 -j DROP

命令语法: iptabels -t 表名 -D 链名 匹配条件 -j 动作
-D 是delete 删除的意思
是把 s (source的意思) 来自IP 192.168.1.146 的数据包 drop掉这个命令删除

iptables -t filter -A INPUT 2 -s 192.168.1.146 -j DROP

比上面的事例多了一个2(在INPUT后面),这个2的意思是把规则添加到第2行

iptables -t filter -D INPUT 2

表示删除filter表中关于INPUT链的2号规则(--line-numbers 中的序列号为2)

iptabels -t filter -P INPUT ACCEPT
iptabels -t filter -P FORWARD ACCEPT

命令的意思是给某个链设定默认的规则,
第一个命令把INPUT链设置为默认ACCEPT
第二个命令把FORWARD链设置为默认ACCEPT

iptables -t filter -F INPUT
iptables -t filter -F OUTPUT

第一个命令表示清空filter表中的关于INPUT链的所有规则
第二个命令表示清空filter表中关于OUTPUT链的所有规则

iptables -t filter -F
iptables -t nat -F

第一个命令表示清空filter表中的所有规则,-f filter 可以省却,写作iptables -F
第二个命令表示清空nat表中的所有规则

service iptables save
/usr/sbin/iptables-save > /etc/sysconfig/iptables
/usr/sbin/iptables-restore < /etc/sysconfig/iptables-new

第一行的命令和第二行的命令,功能相同,都是把现有规则写入到系统中 /etc/sysconfig/iptables中
并且即使重启服务器,iptables规则也不会改变
第三行命令是说把文件/etc/sysconfig/iptables-new中的规则应用到系统中,需要service iptabels save
才可以真正写入到系统文件/etc/sysconfig/iptables 中,保证开启重启后有效

(三)基本匹配条件

iptables -t filter -I INPUT -s 192.168.1.111,192.168.1.118 -j DROP
iptables -t filter -I INPUT -s 192.168.1.0/24 -j ACCEPT
iptables -t filter -I INPUT ! -s 192.168.1.0/24 -j ACCEPT

参数-s 用于匹配源地址,如有多个源地址,每个IP之间用逗号隔开,
支持网段

iptables -t filter -I OUTPUT -d 192.168.1.111,192.168.1.118 -j DROP
iptables -t filter -I INPUT -d 192.168.1.0/24 -j ACCEPT
iptables -t filter -I INPUT ! -d 192.168.1.0/24 -j ACCEPT

参数-d 用于匹配目标地址,如有多个源地址,每个IP之间用逗号隔开,
支持网段

iptables -t filter -I INPUT -p tcp -s 192.168.1.146 -j ACCEPT
iptables -t filter -I INPUT ! -p udp -s 192.168.1.146 -j ACCEPT

参数-p 用于匹配tcp协议,类似的匹配协议类型有 udp,icmp,udplite,esp,ah,sctp等
centos7 还支持,icmpv6,mh

iptables -t filter -I INPUT -p icmp -i eth4 -j DROP
iptables -t filter -I INPUT -p icmp ! -i eth4 -j DROP

参数-i ,通常和INPUT 配合,针对某个网卡,使用规则

iptables -t filter -I OUTPUT -p icmp -o eth4 -j DROP
iptables -t filter -I OUTPUT -p icmp ! -o eth4 -j DROP

参数-o, 通常和OUTPUT配合,针对某个网卡,使用规则

(四)扩展匹配条件

参数-m 后面接模块名称,如tcp模块,udp模块,multiport模块

iptables -t filter -I OUTPUT -d 192.168.1.146 -p tcp -m tcp --sport 22 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport 22:25 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport :22 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport 80: -j REJECT
iptables -t filter -I OUTPUT -d 192.168.1.146 -p tcp -m tcp ! --sport 22 -j REJECT

参数-p tcp -m tcp 用于匹配tcp协议,
--sport 源端口
--dport 目标端口
可以使用冒号,指定一个连续的端口范围

iptables -t filter -I OUTPUT -d 192.168.1.146 -p udp -m multiport --sports 137,138 -j DROP
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80 -j DROP
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport ! --dports 22,80 -j DROP
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 80:88 -j DROP
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80:88 -j DROP
iptables -t filter -A INPUT -p tcp -m multiport --dports 8088,8000:10000 -j DROP

模块multiport 代替tcp 模块,可以指定多个离散的端口,或者连续的端口

iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN -j REJECT
iptables -t filter -I OUTPUT -p tcp -m tcp --sport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN,ACK -j REJECT
iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags ALL SYN -j REJECT
iptables -t filter -I OUTPUT -p tcp -m tcp --sport 22 --tcp-flags ALL SYN,ACK -j REJECT

参数 --tcp-flags,判断tcp 头中的标志位

iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --syn -j REJECT

--syn参数,等价于"--tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN"
专门匹配第一次握手

(五)更多扩展匹配条件

iprange 模块 (-m iprange --src-range 192.168.1.127-192.168.1.146)
string 模块 (-m string --algo bm --string "XXOOstring")
time 模块
connlimit 模块
limit 模块

iptabels -t filter -I INPUT -m iprange --src-range 192.168.1.127-192.168.1.146 -j DROP
iptables -t filter -I OUTPUT -m iprange --dst-range 192.168.1.127-192.168.1.146 -j DROP
iptabels -t filter -I INPUT -m iprange ! --src-range 192.168.1.127-192.168.1.146 -j DROP

使用iprange模块,指定不同的src或者dst IP range

iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo bm --string "OOXX" -j DROP
iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo kmp --string "OOXX" -j DROP

使用string模块,--algo 指定算法,算法可以为 bm,kmp,这是必需的选项
--string 指定需要匹配的字符串

iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 19:00:00 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 443 -m time --timestart 09:00:00 --timestop 19:00:00 -J REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --weekdays 6,7 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --monthdays 22,23 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time ! --monthdays 22,23 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 18:00:00 --weekdays 6,7 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --weekdays 5 --monthdays 22,23,24,25,26,27,28 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --datestart 2020-05-01 --datestop 2020-05-07 -j REJECT

使用time模块
--timestart 指定时间范围开始时间,不可取反
--timestop 指定时间范围结束时间,不可取反
--weekdays 用于指定星期几,可取反
--monthdays 用于指定几号,可取反
--datestart 用于指定日期范围的开始日期,不可取反
--datestop 用于指定日期范围的结束日期,不可取反

iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j REJECT 
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 10 --connlimit-mask 27 -j REJECT

使用connlimit模块,对连接数量做限制
--connlimit-above 单独使用此选项时,表示限制每个IP的链接数量
--connlimit-mask 此选项不能单独使用,使用 --connlimit-above选项时,配合此选项,表示针对某IP段内的IP,限制接数量

iptables -t filter -I INPUT -p icmp -m limit --limit-burst 3 --limit 10/minute -j ACCEPT
iptables -t filter -A INPUT -p icmp -j REJECT

以上事例是两条命令的组合,一起组合使用,表示每分钟允许10个包,令牌桶数量为3,多余的包被reject掉
使用limit模块,对报文的到达速率做限制,如果要限制单位时间内流入包的数量,就能用limit模块
--limit-burst 此选项用于指定令牌筒的最大数量,令牌筒要装满,才可以出去
--limit 此选项用于指定令牌桶中生成新令牌的频率,
可用的时间单位有second,minute,hour,day

iptables -t filter -I INPUT -p udp -m udp --dport 137 -j ACCEPT
iptables -t filter -I INPUT -p udp -m udp --dport 137:157 -j ACCEPT
iptables -t filter -I INPUT -p udp -m multiport --dport 53,137:157 -j DROP

udp扩展,可以配合--sport,--dport匹配地址,
连续端口可以使用冒号,离散的端口可以配合multiport模块指定多个端口

iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8/0 -j DROP
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8 -j DROP
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type "echo-request" -j DROP
iptables -t filter -I OUTPUT -p icmp -m icmp --icmp-type 0/0 -j REJECT
iptables -t filter -I OUTPUT -p icmp -m icmp --icmp-type 0 -j REJECT

以上五行效果相同,表示 可以ping 通别人,但是别人不可以ping 通我,
谦三行针对INPUT设置规则,后两行针对OUTPUT设置规则
--icmp-type 匹配icmp报文的具体类型
在有-p 情况下,可以省却-m icmp ,类似的情况有 tcp 和 udp等

iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
iptables -t filter -A INPUT -j REJECT

以上三行,需要一起配合使用,才有效果,表示只有回应我们的抱文或者已经建立好链接的请求才可以通过防火墙
第二行的意思是允许某个端口的第一次tcp连接请求

iptables -t filter -N IN_WEB

表示在filter表中创建名为IN_WEB的自定义链

iptables -t filter -I IN_WEB -p tcp -m multiport --dports 80,8000:10000 -j DROP

给自定义链IN_WEB设置规则

iptables -t filter -I INPUT -p tcp --dport 80 -j IN_WEB

表示在INPUT链中引用刚才创建的自定义链

iptables -E IN_WEB WEB

表示将IN_WEB自定义链重命名为WEB

iptables -X WEB

表示删除自定义链WEB
删除需要满足两个条件
1. 自定义链没有被引用
2. 自定义链中没有任何规则

原文地址:https://www.cnblogs.com/faberbeta/p/iptables001.html