使用multiport模块可以添加对个不连续的端口,最多可以添加15组。语法:-m multiport <--sport | --dport | --ports> 端口1 [,端口2,端口3....,端口n]
示例:192.168.122.1 访问本机的20,21,22,443端口允许通过。
root@uos-PC:~# iptables -t filter -A INPUT -s 192.168.122.1 -p tcp -m multiport --dport 20,21,22,443 -j ACCEPT
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~# iptables -t filter -A INPUT -s 192.168.122.1 -p tcp -j DROP
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~# iptables -L -n -v -t filter --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 2400 153K ACCEPT tcp -- * * 192.168.122.1 0.0.0.0/0 multiport dports 20,21,22,443
2 39 2340 DROP tcp -- * * 192.168.122.1 0.0.0.0/0
Chain FORWARD (policy ACCEPT 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
root@uos-PC:~#
· 使用iprange模块可以指定“一段“连续的IP地址范围”,用于匹配报文的源地址或者目标地址,iptables扩展模块中有两个扩展匹配条件可以使用。
· --src-range ip-from -- [ip-to] 源地址范围;
· --dst-range ip-from -- [ip-to] 目标地址范围;
示例: 192.168.122.1--192.168.122.10范围内的地址ping本机,则拒绝。
root@uos-PC:~# iptables -t filter -A INPUT -p icmp -m iprange --src-range 192.168.122.1-192.168.122.10 -j REJECT
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~# iptables -L -n -v -t filter --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT icmp -- * * 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.122.1-192.168.122.10 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 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
· 使用string扩展模块,可以指定要匹配的字符串,如果报文中包含对应的字符串,则匹配调节。
--algo {bm | kmp}: 字符串匹配查询的算法
--string pattern 匹配的字符串。
root@uos-PC:~# iptables -t filter -I OUTPUT -d 192.168.122.1 -m string --string 'hello' --algo kmp -j DROP
root@uos-PC:~#
root@uos-PC:~# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 0.0.0.0/0 192.168.122.1 STRING match "hello" ALGO name kmp TO 65535
root@uos-PC:~#
测试:
(base) uos@uos-PC:~$ curl 192.168.122.27
^C
(base) uos@uos-PC:~$ curl 192.168.122.27/1.html
test iptables
(base) uos@uos-PC:~$
· 使用time扩展模块,根据时间段去匹配报文,如果报文到达的时间在指定的时间范围以内,则符合匹配条件。
· --timestart hh:mm:ss 开始时间
· --timestop hh:mm:ss 结束时间
· --monthdays day[,day]... 指定一个月的某一天
· --weekdays day[, day...] 指定周一到周天
· --kerneltz 使用内核时区而不是UTC时间
示例:拒绝每天在9.46--9.48时间段,192.168.122.1的主机向本机发送icmp协议
root@uos-PC:~# iptables -t filter -R INPUT 1 -p icmp -s 192.168.122.1 -m time --timestar 01:46 --timestop 01:48 -j REJECT
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
7 588 REJECT icmp -- * * 192.168.122.1 0.0.0.0/0 TIME from 01:46:00 to 01:48:00 UTC reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
· 默认情况下当我们禁ping后,其他主机无法ping通本机,本主机也无法ping通其他主机。假设现在需求是本机可以ping通其他主机,而其他主机依然无法ping通本机。
--icmp-type {echo-request | echo-reply } 指定icmp类型,echo-request(请求),echo-request(回应)
示例:允许192.168.122.1ping通本机,不允许本机ping通192.168.122.1
root@uos-PC:~# iptables -t filter -I INPUT -s 192.168.122.1 -p icmp -m icmp --icmp-type "echo-request" -j REJECT
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT icmp -- * * 192.168.122.1 0.0.0.0/0 icmptype 8 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
root@uos-PC:~#
测试:
root@uos-PC:~# ping 192.168.122.1
PING 192.168.122.1 (192.168.122.1) 56(84) bytes of data.
64 bytes from 192.168.122.1: icmp_seq=1 ttl=64 time=0.309 ms
64 bytes from 192.168.122.1: icmp_seq=2 ttl=64 time=0.218 ms
64 bytes from 192.168.122.1: icmp_seq=3 ttl=64 time=0.236 ms
^C
--- 192.168.122.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 38ms
rtt min/avg/max/mdev = 0.218/0.254/0.309/0.041 ms
root@uos-PC:~#
(base) uos@uos-PC:~/Desktop$ ping 192.168.122.27
PING 192.168.122.27 (192.168.122.27) 56(84) bytes of data.
^C
--- 192.168.122.27 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 60ms
(base) uos@uos-PC:~/Desktop$ ping 192.168.122.27
PING 192.168.122.27 (192.168.122.27) 56(84) bytes of data.
From 192.168.122.27 icmp_seq=1 Destination Port Unreachable
From 192.168.122.27 icmp_seq=2 Destination Port Unreachable
From 192.168.122.27 icmp_seq=3 Destination Port Unreachable
From 192.168.122.27 icmp_seq=4 Destination Port Unreachable
From 192.168.122.27 icmp_seq=5 Destination Port Unreachable
^C
--- 192.168.122.27 ping statistics ---
5 packets transmitted, 0 received, +5 errors, 100% packet loss, time 104ms
(base) uos@uos-PC:~/Desktop$
· limit扩展模块:针对报文速率进行限制。
· 限制单位时间内流入包的数量,我们可以以秒为单位进行限制,也可以以分钟,小时,天作为单位进行限制。
· --limit rate/[second | minute | hour | day]
· --limit-burst number 超过限制速率的包,允许超过burst所设定值,默认可超过5个
示例1:限制主机每分钟接收10个ICMP数据包(差不多6s接收一个数据包)
root@uos-PC:~# iptables -t filter -I INPUT -p icmp -m limit --limit 10/minute -j ACCEPT
root@uos-PC:~#
root@uos-PC:~# iptables -t filter -A INPUT -p icmp -j REJECT
root@uos-PC:~# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 10/min burst 5
0 0 REJECT icmp -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
root@uos-PC:~#
示例2:允许ICMP瞬间通过10个数据包通过,超过的数据包每分钟仅能通过一个
root@uos-PC:~# iptables -t filter -I INPUT -p icmp -m limit --limit 1/m --limit-burst 10 -j ACCEPT
root@uos-PC:~# iptables -t filter -A INPUT -p icmp -j REJECT
root@uos-PC:~# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 1/min burst 10
0 0 REJECT icmp -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
root@uos-PC:~#
· 根据每个客户端ip做并发连接数数量匹配;可以放置DOS攻击。
· --connlimit-upto N #连接的数量小于等于N时匹配
· --connlimit-above N #连接的数量大于N时匹配
示例:当连接数超过2个之后就拒绝。
root@uos-PC:~# iptables -t filter -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 2 -j REJECT
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 #conn src/32 > 2 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
root@uos-PC:~#
· state 扩展模块,可以根据”连接追踪机制“去检查连接的状态,较耗资源
· conntrack机制:追踪本机上的请求和响应之间的关系
转态类型:
NEW: 新发出请求;连接追踪信息库中不存在此连接的相关信息条目,因此,将其识别为第一次发
出的请求
ESTABLISHED: NEW状态之后,连接追踪信息库中为其建立的条目失效之前期间内所进行的通信
状态
RELATED: 新发起的但与已有连接相关联的连接,如:ftp协议中的数据连接与命令连接之间的关
系
INVALID: 无效的连接,如flag标记不正确
UNTRACKED: 未进行追踪的连接,如:raw表中关闭追踪
· 记录旧信息路径
/prox/sys/net/netfilter/nf_conntrack_max
示例1:老用户允许连接,新用户拒绝连接
root@uos-PC:~# iptables -t filter -I INPUT -m state --state ESTABLISHED -j ACCEPT
root@uos-PC:~#
root@uos-PC:~# iptables -t filter -I INPUT -m state --state NEW -j REJECT
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 state NEW reject-with icmp-port-unreachable
244 17008 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
root@uos-PC:~#
示例2: 允许本机可以访问192.168.122.1,但是192.168.122.1不能访问本机
root@uos-PC:~# iptables -A INPUT -s 192.168.122.1 -m state --state NEW -j REJECT
root@uos-PC:~#
root@uos-PC:~#
root@uos-PC:~# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 192.168.122.1 0.0.0.0/0 state NEW reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
root@uos-PC:~#
· 网络地址转换(NAT)就是对数据包的网络地址(ip + port)进行转换。
· 例如:机器自己的ip是10.10.10.10是能正常与外部正常通信的,但是192.168.1的私有ip段,无法与外界通信,因此当源地址为192.168.1.x段的包要出去时,机器就会将源ip换成自己的10.10.10.10再发送出去;收到应答包时,在进行相反的转换,这就是NAT的基本过程。
· SNAT:源地址转换
· DNAT:目标地址装换
· PNAT:端口转换
## 开启转发功能
root@Route:~# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
root@Route:~# sysctl -p
方式一:
##配置iptables的SNAT转发
root@Route:~# iptables -t nat -I POSTROUTING -s 192.168.1.0/24 -j SNAT --to 10.10.10.129
root@Route:~#
root@Route:~#
root@Route:~# iptables -L -n -v -t nat
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
2 113 SNAT all -- * * 192.168.1.0/24 0.0.0.0/0 to:10.10.10.129
root@Route:~#
方式二:
root@Route:~# iptables -t nat -I POSTROUTING -s 192.168.1.0/24 -j MASQUERADE
root@Route:~# iptables -t nat -I PREROUTING -d 10.10.10.129 -p tcp --dport 2222 -j DNAT --to 192.168.1.11:22
root@Route:~#
root@Route:~#
root@Route:~# iptables -t nat -A PREROUTING -d 10.10.10.129 -p tcp --dport 2223 -j DNAT --to 192.168.1.12:22
root@Route:~#
root@Route:~#
root@Route:~# iptables -t nat -L -n -v
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DNAT tcp -- * * 0.0.0.0/0 10.10.10.129 tcp dpt:2222 to:192.168.1.11:22
0 0 DNAT tcp -- * * 0.0.0.0/0 10.10.10.129 tcp dpt:2223 to:192.168.1.12:22
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
11 738 SNAT all -- * * 192.168.1.0/24 0.0.0.0/0 to:10.10.10.129
root@Route:~#
· 此前我们一直在使用iptables的默认链中定义规则,那么此处,我们就来了解一下定义链,这里可能有疑问,iptables的默认链就已经能够满足我们了,为什么需要自定义链呢?
· 当默认链中的规则非常多时,不便于管理。假设INPUT链中存放了200条规则,这200条规则有针对80端口的,有针对22端口的,有针对私网ip的,等等。
· 假设想要修改针对80端口的规则,可能需要从头到尾看一遍200条规则,找出哪些规则是针对80端口的,这显然不合理,所以我们需要使用自定义链,通过自定义链即可解决上述问题。
· 假设我们定义了一条链,链名叫IN_HTTP,我们可以将所有针对80端口入站规则都写入到这条自定义链中,当以后修改针对80端口入站规则时,就直接修改IN_HTTP链中的规则就可以了,即使默认链中有在多的规则,也没有关系,因为所有针对80端口的入站规则都存放在IN_HTTP链中,同理,我们可以将针对22端口的出站规则放入到OUT_SSH自定义链中。
root@Route:~# iptables -t filter -N IN_HTTP
root@Route:~#
root@Route:~#
root@Route:~# iptables -t filter -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain IN_HTTP (0 references)
pkts bytes target prot opt in out source destination
root@Route:~#
root@Route:~# iptables -t filter -I IN_HTTP -p icmp -j REJECT
root@Route:~#
root@Route:~#
root@Route:~# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain IN_HTTP (0 references)
pkts bytes target prot opt in out source destination
0 0 REJECT icmp -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
root@Route:~#
root@Route:~# iptables -I INPUT -p icmp -j IN_HTTP
root@Route:~#
root@Route:~#
root@Route:~# iptables -L -n -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 IN_HTTP icmp -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain IN_HTTP (1 references)
pkts bytes target prot opt in out source destination
0 0 REJECT icmp -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable