iptables 的使用 与 模块
今日内容
- Iptables 的使用
- 模块·
内容详细
一、Iptables 的使用
1、使用前奏
1、安装Iptables
[root@m01 ~]# yum install iptables*
2、启动Iptables
[root@m01 ~]# systemctl start iptables
3、关闭firewalld
[root@m01 ~]# systemctl disable --now firewalld
2、命令格式
iptables -t 表名 [选项] 链接名 条件 动作
3、Iptables 动作
ACCEPT
ACCEPT : 将数据包放行
REJECT
REJECT : 阻拦数据包,并传送数据包通知对方
DROP
DROP : 丢弃数据包不予处理,
REDIRECT
REDIRECT : 将数据包重新导向另一个端口
4、Iptables 基本的条件匹配(协议)
TCP
UDP
ICMP(ping)
ALL
5、总的参数预览
-t: 指定操作的表
-L, --list 列出当前的规则
-v 显示数据包和数据包大小
-n 不反解地址
-A, --append 追加一条规则到链中
-I, --insert 插入一条规则,插入到顶部
-F, --flush 清空
-Z, --zero 清空计数器( 包数量 、包大小)
-D, --delete 删除链中的规则
-R, --replace 修改
-S, --list-rules 列出所有的规则
-N, --new-chain 创建一个自定义 链
-X, --delete-chain 删除一个自定义链
-P, --policy 指定链的默认策略
6、iptables 参数选项的具体使用方法
-s -d 源地址、目标地址
源地址 : 发送访问请求的地址
目标地址 : 要访问的地址
--sport 源端口 --dport 目标端口
源端口 : 发送请求的端口
目标端口 : 访问的端口
-p -i -o -m -j 动作
-p : 指定的协议
-i : 进来的网卡
-o : 出去的网卡
-m : 指定模块
-j : 转化动作
查看 iptables 表 的已有规则 & 基本使用
-L 查看表的规则,默认是 filter 表
iptables -L
[root@m01 ~]# iptables -L
Chain INPUT (policy ACCEPT) <-- INPUT 链
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT) <-- FORWARD 链
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT) <-- OUTPUT 链
target prot opt source destination
-v -n 显示数据包数量和大小;不反解地址
iptables -vnL -t filter
# 注意:L 参数一定要在最后面,不然报错
[root@m01 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
295 17380 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 46 packets, 3904 bytes)
pkts bytes target prot opt in out source destinationz
-F 清空规则
iptables -F
[root@m01 ~]# iptables -F
[root@m01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-A 在 filter 表的 INPUT 链上追加一条规则
三个固定参数:
-t [指定表] -A [指定的链] -p [指定协议]
iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
[root@m01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-I 在顶部插入一条规则
iptables 执行规则过滤请求都是从表的第一行开始自上而下匹配规则的
在上方的规则首先被执行匹配
iptables -t filter -I INPUT -p TCP --dport 80 -j ACCEPT
[root@m01 ~]# iptables -t filter -I INPUT -p TCP --dport 443 -j ACCEPT
[root@m01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:https
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
-Z 清空计数器
iptables -t filter -F
[root@m01 ~]# iptables -t filter -F
[root@m01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
查看本机端口被占用的命令
netstat -nutlp
[root@m01 ~]# netstat -nutlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1307/sshd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1669/nginx: master
tcp6 0 0 :::22 :::* LISTEN 1307/sshd
tcp6 0 0 :::80 :::* LISTEN 1669/nginx: master
7、案例实操
1、只允许22端口可以被访问,其他端口全部无法访问。
iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
158 9236 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
0 0 DROP tcp -- * * 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 33 packets, 2612 bytes)
pkts bytes target prot opt in out source destination
2、只允许22,80,443端口可以被访问,其他端口全部无法访问。
[root@m01 ~]# iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP --dport 80 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP --dport 443 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -j DROP
3、只允许22,80,443端口可以被访问,其他端口全部无法访问,但是本机可以访问百度。
[root@m01 ~]# iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP --dport 80 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP --dport 443 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -j DROP
# 不需要动 OUTPUT 端口即可
4、要求只能链接 192.168.15.81 的22端口,其他的端口不行
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -d 192.168.15.81 --dport 22 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
151 8848 ACCEPT tcp -- * * 0.0.0.0/0 192.168.15.81 tcp dpt:22
0 0 DROP tcp -- * * 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 21 packets, 1708 bytes)
pkts bytes target prot opt in out source destination
5、只允许192.168.15.71能够通过192.168.15.81的22端口链接,其他的不行
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -s 192.168.15.71 -d 192.168.15.81 --dport 22 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -j DROP
6、要求192.168.15.81对外部不可见
外部不能访问本机,那本机的 IP 地址就是目标地址(连自己都访问不到,只能通过内网 172.16.1.81 访问)
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -d 192.168.81 -j DROP
7、要求使用 eth0 网卡的所有请求全部拒绝
要求请求被全部拒绝,那就是在数据流出过滤时被拦截(OUTPUT)
-o 出去的网卡
iptables -t filter -A OUTPUT -p TCP -o eth0 -j DROP
[root@m01 ~]# iptables -t filter -A OUTPUT -p TCP -o eth0 -j DROP
自己的请求也全被拦截,从此 Xsehll 链接不上,只能在本机操作
使用172.16.1.71登录进来的窗口,不允许访问百度。
要先用 172.16.1.71 登录进去,然后再添加 iptables 规则
-o 出去的网卡
[root@m01 ~]# iptables -t filter -I OUTPUT -p TCP -o eth1 -j DROP
8、要求访问服务器的8080端口转发至80端口
也就是别人请求访问本机服务器时(流入模式),把它请求的端口是 8080 的转为 80 端口访问( --to-port )
网络转址 nat 表
端口重定向动作 REDIRECT
流入模式第一条链是 PREROUTING
iptables -t nat -A PREROUTING -p TCP --dport 8080 -j REDIRECT --to-port 80
[root@m01 ~]# iptables -t nat -A PREROUTING -p TCP --dport 8080 -j REDIRECT --to-port 80
[root@m01 ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REDIRECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 redir ports 80
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
9、要求只允许windows通过ssh连接192.168.15.81,其他的拒绝
# 先用 ipconfig 查看 windows 本机的 Ip 地址是多少,作为源地址
windows IP : 192.168.15.1
iptables -t filter -A INPUT -p TCP -s 192.168.15.1 -d 192.168.15.81 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -s 192.168.15.1 -d 192.168.15.81 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
93 6068 ACCEPT tcp -- * * 192.168.15.1 192.168.15.81
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0
二、模块
指定模块命令:
-m : 指定模块
1、连续匹配多个端口(multiport)
--dports(是两个 - )
制定多个端口(不同端口之间用逗号分隔,连续端口使用冒号分隔)
例子
1、要求将22,80,443以及30000-50000之间所有的端口向外暴露,其他端口拒绝
iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,3000:5000 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,3000:5000 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
113 7300 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 22,80,443,3000:5000
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0
2、指定一段连续的 ip 地址范围(iprange)
源地址范围
--src-range from[-to] : 源地址范围
目标地址范围
--dst-range from[-to] : 目标地址范围
例子
2、要求192.168.15.1 - 192.168.15.10之间的所有IP能够连接192.168.15.81,其他拒绝。
iptables -t filter -A INPUT -p TCP -m iprange --src-range 192.168.15.1-192.168.15.10 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -m iprange --src-range 192.168.15.1-192.168.15.10 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
42 2512 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.15.1-192.168.15.10
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0
3、匹配指定字符串( string )
指定要匹配的字符串
--string pattern
匹配查询算法(必选)
--algo {bm | kmp}
例子
3、要求访问数据包中包含HelloWorld的数据不允许通过。
# 1、首先去到 /usr/share/nginx/html 目录下
# 2、把原有的 html 文件全部删掉
# 3、创建自己的 index.html 文件,里面写入文字
# 4、在创建一个测试文件 demo.html ,里面写入与 index.html 不一样的文字
# 5、测试在浏览器访问本机的时候会不会过滤掉相应的字所属文件
iptables -t filter -A INPUT -p TCP -m string --string "HelloWorld" --algo kmp -j DROP
# 修改 nginx 的 html 文件
[root@m01 ~]# cd /usr/share/nginx/html
[root@m01 html]# cat index.html
HelloWorld
[root@m01 html]# cat demo.html
hello
[root@m01 html]# iptables -t filter -A INPUT -p TCP -m string --string "HelloWorld" --algo kmp -j DROP
# 测试:
[root@m01 ~]# curl http://192.168.15.81/demo.html
hello
[root@m01 ~]# curl http://192.168.15.81/index.html
^C
4、根据时间段匹配报文( time )
开始时间
--timestart hh:mm[ : ss]
结束时间
--timestop hh:mm[ : ss]
指定一个月的某一天
--monthdays day[ , day...]
指定周 还是 周天
--weekdays day[ , day...]
例子
4、要求每天的12到13之间,不允许访问
# 注意 : 我们处在东八区,而该模块默认使用了东 0 区,我们早了 8 小时,减去 8
iptables -t filter -A INPUT -p TCP -m time --timestart 14:00 --timestop 15:00 -j DROP
必须使用UTC时间
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -m time --timestart 14:00 --timestop 15:00 -j DROP
5、禁 ping ,默认本机无法 ping 别人、别人无法 ping 自己( icmp )
--icmp-type
echo-request (8) 请求
echo-reply (0) 回应
例子
5、要求别人不能ping本机,但是本机可以ping别人 # 注意协议要改为 ICMP
# 丢弃别人的请求..
iptables -t filter -A INPUT -p ICMP -m icmp --icmp-type "echo-request" -j DROP
6、限制链接数,并发链接数(connlimit)(链接虚拟机的窗口的数量)
如果现有连接数小于或等于 n 则 匹配
--connlimit-upto n
如果现有连接数大于n 则匹配
--connlimit-above n
例子
6、要求主机连接最多有2个
iptables -t filter -A INPUT -p TCP --dport 22 -m connlimit --connlimit-above 2 -j DROP
7、针对 报文速率 进行限制。秒、分钟、小时、天。( limit )
报文数量
--limit rate[/second | /minute | /hour | /day]
报文数量(默认:5)
--limit-burst number
例子
7、要求限制速率在500k/s左右
iptables -t filter -A OUTPUT -p TCP -m limit --limit 333/s -j ACCEPT
iptables -t filter -A OUTPUT -p TCP -j DROP
# 生成一个 1G 的文件做传输测试
[root@m01 ~]# dd if=/dev/zero of=1.txt bs=100M count=10
发送文件命令:
[root@m01 ~]# scp 1.txt root@192.168.15.71:/root
root@192.168.15.71's password:
1.txt 8% 83MB 41.8MB/s 00:21 ETA
# 添加限制传输速率规则
[root@m01 ~]# iptables -t filter -A OUTPUT -p TCP -m limit --limit 333/s -j ACCEPT
[root@m01 ~]# iptables -t filter -A OUTPUT -p TCP -j DROP
# 限制后速率在一直下降,而且速率并不准确
[root@m01 ~]# scp 1.txt root@192.168.15.71:/root
root@192.168.15.71's password:
1.txt 15% 155MB 7.5MB/s 01:52 ETA