一、回顾
1、iptables/netfilter
2、framework:netfilter
a、filter:包过滤
b、nat:地址转换
c、mangle:报文的修改
d、raw:关闭nat表上所启用的连接追踪机制
e、security
3、五链
a、PREOUTING
b、INPUT
c、FORWARD
d、OUTPUT
e、POSTROUTING
4、四表五链对应关系
a、filter:INPUT,FORWARD,OUTPUT
b、nat:PREROUTING ,INPUT,OUTPUT,POSTROUTING
c、mangle:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
d、raw:OUTPUT,PREROUTING
5、iptables命令
a、iptables [-t table] COMMAND chain [rulenum] [-m machename] [per-match-options] [-j targetname [per-target-options]] [options]
(1)、匹配条件
1)、基本匹配条件
-s,-d,-p,-i,-o
2)、扩展匹配条件
-m atchname per-match-options
(2)、处理动作:
1)、基本处理动作:ACCEPT,DROP
2)、扩展处理动作:REJECT,RETURN,LOG,REDIRECT,...
3)、自定义用户链
(3)、COMMAND:
1)、链管理:-N,-X,-E,-P,-F,-Z
2)、规则管理:-A,-I,-R,-D
3)、查看:-L,-n,-v,-x,--line-numbers
二、iptables(2)
1、iptables [-t table] COMMAND [chain] [PARAMETERS] [-m machename] [per-match-options] [-j targetname [per-target-options]] [options]
2、匹配条件
a、基本匹配条件:PARAMETERS
b、扩展匹配条件:
(1)、隐式扩展:在使用-p选项指明了特定的协议时,无需再同时使用-m选项指明扩展模块的扩展机制。我们可以使用rpm -ql iptables命令查看,可以看到凡是大写的我们用来做处理条件的结果扩展,小写的是用来做匹配条件扩展的。其中我们可以看到有一个tcp.so的扩展模块,这个相当于是对tcp协议做条件扩展的。如果我们加了-p参数那么我们就不用再使用-m参数了。而我们可以看到还有cpu,ecn,ipvs等许许多多的扩展模块,这种因为我们没办法使用-p来指明那么我们就必须要使用-m来指定说我要调用哪个扩展。即对于我们的检查条件来讲我们可以基于我们的扩展模块来调用更强大的报文匹配扩展检查条件,在这种扩展检查时,如果你前面使用了基本参数匹配中的-p选项指定了要匹配某个协议,每一个协议基本上都有他自己的扩展模块,tcp,像icmp,udp等都有,这些模块都是需要装入后才能使用的,装入模块要使用-m选项来定义。不过我们一旦在前面使用了-p这个选项制定了协议以后对于那些本来是对协议进行扩展的模块就无需再同时使用-m。
[root@node3 ~]# rpm -ql iptables |grep -Ei "(tcp|udp|icmp|sctp)" /usr/lib64/xtables/libip6t_icmp6.so /usr/lib64/xtables/libipt_icmp.so /usr/lib64/xtables/libxt_TCPMSS.so /usr/lib64/xtables/libxt_TCPOPTSTRIP.so /usr/lib64/xtables/libxt_sctp.so /usr/lib64/xtables/libxt_tcp.so /usr/lib64/xtables/libxt_tcpmss.so /usr/lib64/xtables/libxt_udp.so
(2)、显示扩展:必须使用-m选项指明要调用的扩展模块的扩展机制。
c、扩展匹配条件:需要加载扩展模块,方可生效
(1)、隐式扩展:不需要手动加载扩展模块;因为他们是对协议的扩展,所以,但凡使用-p指明了协议,就表示已经指明了要扩展的模块
tcp:
1)、[!] --source-port,--sport port[:port]:匹配报文的源端口;可以是端口范围
2)、[!] --destination-port,--dport port[:port]:匹配报文的目标端口,可以是端口范围
3)、[!] --tcp-flages mask comp
mask is the flags which we should examine,written as a comma-separated list,例如SYN,ACK,FIN,RST
comm is a comma-separated list of flags which must be set,例如SYN,例如:"--tcp-flags SYN,ACK,FIN,RST SYN"表示,要检查的标志位为SYN,ACK,FIN,RST四个,其中SYN必须为1,余下的必须为0;
4)、[!] --syn:用于匹配第一次握手,相当于"--tcp-flags SYN,ACK,FIN,RST SYN";
5)、TCP三次握手和四次挥手
udp:
1)、[!] --source-port,--sport port[:port]:匹配报文的源端口,可以是端口范围
2)、[!] --destination-port,--dport port[:port]:匹配报文的目标端口;可以是端口范围
icmp:
1)、[!] --icmp-type {type[/code] | typename}
2)、echo-request:8
3)、echo-reply:0
(2)、显示扩展:必须使用-m选项指明要调用的扩展模块的扩展机制
1)、multiport
以离散或连续的方式定义多端口匹配条件,最多15个
[!] --source-ports,--sports port[,port|,port:port]...:指定多个源端口
[!] --destination-ports,--dports port[,port|,port:port]...:指定多个目标端口
iptables -I INPUT -d 192.168.10.15 -p tcp -m multiport --dports 22,80,139,445,3306 -j ACCEPT
2)、iprange
以连续地址块的方式来指明多IP地址匹配条件
[!] --src-range from [-to]
[!] --dst-range from [-to]
3、相应操作
a、我们来写一条规则,能匹配本机的的ssh服务能被来自于192.168.10.0网络内的主机访问,而且我把其添加为input和output第一条规则
(1)、首先开放input
[root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy DROP 3852 packets, 297K bytes) num pkts bytes target prot opt in out source destination 1 782 58265 ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 2 144 12096 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy DROP 4 packets, 338 bytes) num pkts bytes target prot opt in out source destination 1 466 40829 ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 2 60 5040 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0 [root@node3 ~]# iptables -I INPUT -s 192.168.10.0/24 -d 192.168.10.15 -p tcp --dport 22 -j ACCEPT [root@node3 ~]# iptables --line-numbers -vnL INPUT Chain INPUT (policy DROP 3 packets, 234 bytes) num pkts bytes target prot opt in out source destination 1 52 3744 ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 2 912 67645 ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 3 144 12096 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 [root@node3 ~]#
(2)、我们配置OUTPUT
[root@node3 ~]# iptables -I OUTPUT -s 192.168.10.15 -d 192.168.10.0/24 -p tcp --sport 22 -j ACCEPT [root@node3 ~]# iptables --line-numbers -vnL OUTPUT Chain OUTPUT (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 15 1572 tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 2 680 61549 ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 3 60 5040 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0
b、如果我们使用iptables -F清空了我们的防火墙,因为此时我们的默认策略设置的为DROP,因此我们的ssh就连接不上了。此时我们可以如下策略。
(1)、INPUT中即如果不能被前面的所匹配,任何人访问我们本机的任何服务我们都是拒绝的。OUTPUT中表示,本机访问外面时前两条匹配的话就放行,除此之外访问外面任何人都被拒绝。
[root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy DROP 549 packets, 46754 bytes) num pkts bytes target prot opt in out source destination 1 578 45578 ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 2 144 12096 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 212 23547 ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 2 60 5040 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0 [root@node3 ~]# iptables -A INPUT -d 192.168.10.15 -j REJECT [root@node3 ~]# iptables -A OUTPUT -s 192.168.10.15 -j REJECT [root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy DROP 1 packets, 78 bytes) num pkts bytes target prot opt in out source destination 1 730 56530 ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 2 144 12096 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 3 0 0 REJECT all -- * * 0.0.0.0/0 192.168.10.15 reject-with icmp-port-unreachable Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 310 33675 ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 2 60 5040 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0 3 0 0 REJECT all -- * * 192.168.10.15 0.0.0.0/0 reject-with icmp-port-unreachable [root@node3 ~]#
(2)、然后我们将INPUT和OUTPUT的策略修改为ACCEPT。此时我们是否是所有请求都放行了呢?显然不是的,我们第三条规则已经拒绝了所有,所以他就相当于策略是DROP了。所以有这条规则我们就不用改默认策略了,就相当于DROP了。这样的话如果你一不小心iptables -F了还能给自己留条后路了。
[root@node3 ~]# iptables -P INPUT ACCEPT [root@node3 ~]# iptables -P OUTPUT ACCEPT [root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 863 66126 ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 2 144 12096 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 3 0 0 REJECT all -- * * 0.0.0.0/0 192.168.10.15 reject-with icmp-port-unreachable Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 400 42635 ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 2 60 5040 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0 3 0 0 REJECT all -- * * 192.168.10.15 0.0.0.0/0 reject-with icmp-port-unreachable [root@node3 ~]#
(3)、并且这样写还有一个好处。
1)、我们先来将INPUT和OUTPUT的默认策略改为DROP并且删除我们刚刚添加的拒绝所有
[root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 1048 79770 ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 2 144 12096 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 539 56323 ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 2 60 5040 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0
2)、现在我们来ping 127.0.0.1,如果我们目标地址是127.0.0.1那么他要去连他的源地址也必须得是127.0.0.1,因为回环地址就是自己访问自己,此时我们发现是ping不通的, 因为我们默认策略是拒绝的,所以我们设置默认策略会有这么一个坏处,因此我们(2)中这种设置方式就可以ping通,因为他并没有设置我们本机访问本机的策略,所以就会使用默认策略,这样就能ping通了。我们可以来尝试。
3)、现在我们将相应的策略加回来
[root@node3 ~]# iptables -A INPUT -d 192.168.10.15 -j REJECT [root@node3 ~]# iptables -A OUTPUT -s 192.168.10.15 -j REJECT [root@node3 ~]# iptables -P INPUT ACCEPT [root@node3 ~]# iptables -P OUTPUT ACCEPT [root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy ACCEPT 8 packets, 624 bytes) num pkts bytes target prot opt in out source destination 1 1342 102K ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 2 144 12096 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 3 0 0 REJECT all -- * * 0.0.0.0/0 192.168.10.15 reject-with icmp-port-unreachable Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 751 77239 ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 2 60 5040 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0 3 0 0 REJECT all -- * * 192.168.10.15 0.0.0.0/0 reject-with icmp-port-unreachable [root@node3 ~]# ping 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.019 ms ^C --- 127.0.0.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.019/0.019/0.019/0.000 ms
4)、我们还可以设置从某个网卡进来和出去的默认都拒绝
[root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 1553 117K ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 2 144 12096 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 3 0 0 REJECT all -- * * 0.0.0.0/0 192.168.10.15 reject-with icmp-port-unreachable 4 3 234 REJECT all -- ens33 * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 892 92339 ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 2 60 5040 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0 3 0 0 REJECT all -- * * 192.168.10.15 0.0.0.0/0 reject-with icmp-port-unreachable 4 0 0 REJECT all -- * ens33 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
b、我们现在来配置我们能ping通别人而别人ping不通自己
(1)、首先我们删掉我们相应的icmp规则,然后我们发现我们已经ping不通我们自己的主机了
[root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy ACCEPT 16 packets, 1344 bytes) num pkts bytes target prot opt in out source destination 1 1941 145K ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 2 187 14745 REJECT all -- ens33 * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 16 packets, 1344 bytes) num pkts bytes target prot opt in out source destination 1 1177 126K ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 2 0 0 REJECT all -- * ens33 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable [root@node3 ~]# ping -I ens33 192.168.10.15 PING 192.168.10.15 (192.168.10.15) from 192.168.10.15 ens33: 56(84) bytes of data. ^C --- 192.168.10.15 ping statistics --- 2 packets transmitted, 0 received, 100% packet loss, time 1000ms
(2)、我们现在设置我们icmp能出去
1)、然后我们再ping 192.168.10.14
[root@node3 ~]# iptables -I OUTPUT 2 -s 192.168.10.15 -p icmp --icmp-type 8 -j ACCEPT
[root@node3 ~]# ping -I ens33 192.168.10.14 PING 192.168.10.14 (192.168.10.14) from 192.168.10.15 ens33: 56(84) bytes of data. ^C
2)、我们尝试在我们192.168.10.14上抓包,可以看到我们相应的icmp报文
[root@node2 ~]# tcpdump -i ens33 -nn icmp tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 14:32:25.673439 IP 192.168.10.15 > 192.168.10.14: ICMP echo request, id 2799, seq 1, length 64 14:32:25.673505 IP 192.168.10.14 > 192.168.10.15: ICMP echo reply, id 2799, seq 1, length 64 14:32:26.673340 IP 192.168.10.15 > 192.168.10.14: ICMP echo request, id 2799, seq 2, length 64
3)、然后我们再设置INPUT,设置我们icmp的响应报文为0位,然后我们再ping 192.168.10.14可以发现能ping通。
[root@node3 ~]# iptables -I INPUT 2 -d 192.168.10.15 -p icmp --icmp-type 0/0 -j ACCEPT
[root@node3 ~]# iptables -I INPUT 2 -d 192.168.10.15 -p icmp --icmp-type 0/0 -j ACCEPT [root@node3 ~]# ping 192.168.10.14 PING 192.168.10.14 (192.168.10.14) 56(84) bytes of data. 64 bytes from 192.168.10.14: icmp_seq=1 ttl=64 time=0.509 ms
4)、然后我们在192.168.10.14上ping 192.168.10.15发现ping不通
[root@node2 ~]# ping 192.168.10.15 PING 192.168.10.15 (192.168.10.15) 56(84) bytes of data. ^C --- 192.168.10.15 ping statistics --- 2 packets transmitted, 0 received, 100% packet loss, time 1000ms
c、我们隐式扩展的每一个写法也可以写成显示扩展的格式
(1)、比如我们要开放本机的UDP 137-138端口允许别人访问。
1)、先写INPUT
[root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 2383 177K ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 2 3 252 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 icmptype 0 code 0 3 339 27009 REJECT all -- ens33 * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 1481 156K ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 2 21 1764 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0 icmptype 8 3 15 1680 REJECT all -- * ens33 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable [root@node3 ~]# iptables -I INPUT -d 192.168.10.15 -p udp --dport 137:138 -j ACCEPT [root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT udp -- * * 0.0.0.0/0 192.168.10.15 udp dpts:137:138 2 2527 187K ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 3 3 252 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 icmptype 0 code 0 4 339 27009 REJECT all -- ens33 * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 1571 166K ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 2 21 1764 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0 icmptype 8 3 15 1680 REJECT all -- * ens33 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
2)、写OUTPUT
[root@node3 ~]# iptables -I OUTPUT -s 192.168.10.15 -p udp --sport 137:138 -j ACCEPT [root@node3 ~]# iptables --line-numbers -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT udp -- * * 0.0.0.0/0 192.168.10.15 udp dpts:137:138 2 2645 196K ACCEPT tcp -- * * 192.168.10.0/24 192.168.10.15 tcp dpt:22 3 3 252 ACCEPT icmp -- * * 0.0.0.0/0 192.168.10.15 icmptype 0 code 0 4 354 28179 REJECT all -- ens33 * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT udp -- * * 192.168.10.15 0.0.0.0/0 udp spts:137:138 2 1647 174K ACCEPT tcp -- * * 192.168.10.15 192.168.10.0/24 tcp spt:22 3 21 1764 ACCEPT icmp -- * * 192.168.10.15 0.0.0.0/0 icmptype 8 4 15 1680 REJECT all -- * ens33 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable