软件防火墙iptables/netfilter实操篇(二)

简介

上文将iptables的基础概念给大家进行了讲解,请先补充基础知识再来阅读本篇文章。本篇文章主要是防火墙的日常操作。

表规则查看

表规则输出内容解析

在进行表操作之前,请大家再复习下上节整理的这张表结构。

raw prerouting,output
mangle prerouting,input,forward,output,postrouting
nat prerouting,output,postrouting
filter input,forward,output

下面的操作最好是在测试机上面进行,以免影响你主机的正常访问。

如上图,我们使用-t选项,指定需要操作的表,使用-L选项,查看-t对应的表的规则,-L列出规则。该命令现实了三个链,input,forword,output,每个链都有自己的规则。这些字段的属性介绍如下:

target: 规则对应的target,往往表示规则对应的动作,即规则匹配成功之后采取的措施;
prot: 表示规则对应的协议,是否针对某些协议应用此规则;
opt: 表示规则对应的选项;
source: 表示规则对应的源头地址,可以是一个ip,也可以是一个网段;
destination: 表示规则对应的目标地址。可以是一个ip,也可以是一个网段;

相对的我们也可以直接使用如下命令去查看其它表中的规则:

iptables -t raw -L
iptables -t mangle -L
iptables -t nat -L

iptables 默认不加-t查看的filter表

查看表规则常用命令

iptables -t 表名 -L  #查看对应表的所有规则

iptables -t 表名 -L 链名 #查看指定表指定链的规则

iptables -t 表名 -L -v #表示指定表的所有规则,并且更详细输出

iptables -t 表名 -n -L #表示查看表的所有规则,并且在显示规则时,不对规则中的IP或者端口进行名称反解,-n选项表示不解析IP地址。

iptables --line-numbers -t 表名 -L #表示查看表的所有规则,并且显示规则的序号,--line-numbers选项表示显示规则的序号,注意,此选项为长选项,不能与其他短选项合并,不过此选项可以简写为--line,注意,简写后仍然是两条横杠,仍然是长选项。

最佳实践:实际中常长会将短选项进行合并处理:

iptables --line -t filter -nvxL
iptables --line -t filter -nvxL INPUT

表规则常用操作

在参照本文进行iptables操作时,请在测试机器进行

操作指令

添加规则

iptables -F INPUT  #删除filter表中INPUT链的所有规则
iptables -t filter -I INPUT -s 192.168.56.12 -j DROP #表示丢弃从192.168.56.12来的主机的所有数据; 
iptables -A INPUT -s 192.168.56.12 -j ACCEPT #这个命令并没有指定操作那个表,我们之前强调默认的表是filter表,所以此处操作的也是filter表;-A表示在对应的链INPUT中追加规则,而上一步使用的-I则是插入操作;—I是在首部添加。此时虽然添加了规则允许访问,但是注意顺序,匹配到上个丢弃规则之后就不会往下执行了,所以依然无法访问
iptables -t filter -I INPUT 2 -s 192.168.56.12 -j accept #表示插入到第几行
iptables -t 表名 -P 链名 动作 #设置指定链的默认策略
示例:iptables -t filter -P FORWARD ACCEPT

删除规则

如果没有保存规则删除规则的时候请慎重操作

iptables -t filter -D INPUT 3 #删除filter表中INPUT链中序号为3的规则
iptables -t filter -D INPUT -s 192.168.1.146 -j DROP # 删除指定表的指定链的指定规则
iptables -t filter -F INPUT  # 删除指定表的指定链的所有规则
iptables -t filter -F  #删除指定表filter中的所有规则

修改规则

命令语法:iptables -t 表名 -R 链名 规则序号 规则原本的匹配条件 -j 动作
示例:iptables -t filter -R INPUT 3 -s 192.168.1.146 -j ACCEPT

上述示例表示修改filter表中INPUT链的第3条规则,将这条规则的动作修改为ACCEPT, -s 192.168.1.146为这条规则中原本的匹配条件,如果省略此匹配条件,修改后的规则中的源地址可能会变为0.0.0.0/0。

其它修改规则的方法:先通过编号删除规则,再在原编号位置添加一条规则。

保存规则

iptables-save > /etc/sysconfig/iptables
iptables-restore < /etc/sysconfig/iptables

表规则匹配操作

常用匹配语句

说明:-s用于匹配报文的源地址,可以同时指定多个源地址,每个ip之间使用逗号隔开,
也可以指定一个网段

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

-d用于匹配报文的目标地址,可以同时指定多个目标地址,每个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

-p用于匹配报文的协议类型,可以匹配的协议类型有tcp、udp、icmp、esp等

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

-i用于匹配报文是从那个网卡流入本机的,由于匹配条件只是用于匹配报文流入本机的,所以在output链与postrouting链中不能使用此选项;

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

-o 用于匹配报文将要从那个网卡流出本机,所以在input链和postrouting链中不能使用此选项;

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

扩展匹配语句

tcp扩展模块

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

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 ACCEPT

multiport扩展模块

multiport
常用的语法如下:
-p tcp -m multiport --sports 用于匹配报文的源端口,可以指定不连续的端口号,使用逗号分隔
-p udp -m multiport --dports 用于匹配报文的目标端口,可以指定不连续的目标端口号,用逗号隔开

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

常用扩展模块

iprange

iprange多用于指定一段连续的地址范围;
包含的扩展匹配条件如下
--src-range: 指定连续的源地址范围
--dst-range: 指定连续的目标地址范围

#示例
iptables -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
iptables -t filter -I INPUT -m iprange ! --src-range 192.168.1.127-192.168.1.146 -j DROP

string模块

使用string扩展模块,可以指定要匹配的字符串,如果报文包含对应的字符串,则符合匹配的条件;
--algo: 指定对应的匹配算法,可用算法为bm\kmp,这是必填项
--string: 指定需要匹配的字符串

#示例
iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo bm --string "OOXX" -j REJECT
iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo bm --string "OOXX" -j REJECT

time模块

根据实践段来匹配报文,如果报文到达的时间在指定的时间范围以内,则符合匹配条件。
--timestart: 用于指定时间范围的开始时间,不可取反
--timestop:用于指定时间范围的结束时间,不可取反
--weekdays: 用于指定“星期几",可取反
--monthdays: 用于指定“几号”,可取反
--datestart: 用于指定日期范围的开始日期,不可取反
--datestop: 用于指定日期范围的结束时间,不可取反

#示例
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 2017-12-24 --datestop 2017-12-27 -j REJECT

connlimit模块

该模块主要限制每个ip地址同时链接到server端的链接数量。注意不需要使用ip,其默认就是针对每个客户端做ip限制;即对单ip的并发链接数限制;

#示例
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

limits模块

limits模块主要对报文的到达速率进行限制
--limit-burst:

#示例 #注意,如下两条规则需配合使用,
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

tcp flage

#示例
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

猜你喜欢

转载自www.cnblogs.com/chenxiba/p/11334697.html