1.什么是iptables(官方文档:https://netfilter.org)
iptables其实不是真正的防火墙,我们可以把它理解成一个客户端代理,用户通过iptables这个代理,将用户的安全设定执行到对应的”安全框架”中,这个”安全框架”才是真正的防火墙,这个框架的名字叫netfilter
2.四表五链
2.1四表
filer:对数据进行过滤
nat:进行网络地址转换
mangle:流量整形
raw:流量跟踪
2.2五链
INPUT:表示进站的流量到达本身
OUTPUT:本地出站的流量
FORWARD:经过本地转发的流量
PREROUTING:DNAT
POSTROUTING:SNAT
2.3数据包的匹配优先顺序
raw>mangle>nat>filter
2.4链间的匹配顺序
入站数据:PREROUTING INPUT
出战数据:OUTPUT POSTROUTING
转发数据:PREROUTING FORWARD POSTROUTING
2.5链内的匹配数据
自上向下按顺序依次进行检查,找到相匹配的规则即停止(LOG选项表示记录相关日志)
若在该链内找不到相匹配的规则,则按该链的默认测略处理(未修改的状况下,默认策略为允许)
3.iptables的使用
3.1安装和启动iptables-services服务
[root@localhost ~]# yum -y install iptables-services
[root@localhost ~]# systemctl start iptables
[root@localhost ~]# systemctl status iptables
3.2命令语法
iptables [-t 表名] 选项 [链名] [条件匹配] [-j 控制类型]
注意
1.不指定表名时,默认是指filter表
2.不指定链名时,默认是指的表内的所有链
3.除非设置链的默认策略,否则必须指定匹配条件
4.选项,链名,控制类型必须使用大写,其他均为小写的。
常见选项
-A 追加一条规则(放在最后)
-I 插入一条规则,默认是在开头,可以指定位置
-R 替换或者修改规则
-D 删除规则
-F 清空规则(清空之前一定要查看默认规则)
-P 设置默认规则
-Z 清空数据包
-L 列出规则
控制类型
ACCEPT:允许通过
DROP:直接丢失,不给出任何回应
REJECT:拒绝通过,必要时给出回应
LOG:记录日志信息,然后传给下一条记录继续匹配
SNAT:修改数据包源地址
DNAT:修改数据包的目的地址
REDIRECT:重定向
匹配类型
通用匹配
端口匹配
显示匹配
常见的通用匹配条件
1)协议匹配:-p 协议名
2)地址匹配:-s 源地址、-d 目标地址
3)接口匹配:-i 入站网卡、-o 出站网卡
常见的隐式匹配
端口匹配:
--sport 源端口
--dport 目标端口
ICMP类型匹配:
--icmp-type ICMP类型
eg:iptables -A INPUT -p icmp --icmp-type 8 -j DROP #禁止其它主机ping 本机
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT #允许本机 ping 其它主机
iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT #当本机 ping 不通其它主机时提示目标不可达
显示匹配
多端口匹配:-m multiport --sport 源端口列表
多端口匹配:-m multiport --dport 目标端口列表
IP地址范围: -m iprange --src-range ip地址范围
MAC地址匹配:-m mac -mac-source MAC地址
状态匹配:-m state --state 连接状态
匹配字符串(string)
指定要匹配的字符串
--string pattern
匹配查询算法(必选)
--algo {bm | kmp}
根据时间段匹配报文(time)
开始时间
--timestart hh:mm[ : ss]
结束时间
--timestop hh:mm[ : ss]
指定一个月的某一天
--monthdays day[ , day...]
指定周 还是 周天
--weekdays day[ , day...]
案例
案例1:只允许22端口(目标端口)可以访问,其他端口全部无法访问
[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 -L -v
# 查看(结果太长不写了)
[root@m01 ~]# iptables -L -v
案例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
# 这时获取百度就响应超时了,不知道curl的端口
[root@m01 ~]# curl www.baidu.com
curl: (7) Failed connect to www.baidu.com:80; Connection timed out
案例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
案例4:要求能够通过22端口(目标端口)去链接地址192.168.15.81(目标地址),但是其他的不行
# 通俗理解:通过22端口可以连接到192.168.15.81地址,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
# 可以通过新开窗口测试,或者新开虚拟机测试
案例5:要求地址192.168.15.71(源地址)允许通过22端口(目标端口)连接地址192.168.15.81(目标地址),其他不行
[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
# 可以从71去连接81,81已经断开
案例6:要求192.168.15.71对外部不可见
[root@prometheus ~]# iptables -t filter -A INPUT -p TCP -d 192.168.15.71 -j DROP
# 相当于虚拟机里的这个地址对外不可见,会连接不上虚拟机
案例7:要求使用eth0网卡的所有请求全部拒绝(进来的网卡,连接虚拟机)
[root@prometheus ~]# iptables -t filter -A INPUT -p TCP -i etho -j DROP
案例8:使用eth1网卡登录进来的窗口,不允许访问百度。(出去的网卡,连接虚拟机)
[root@prometheus ~]# iptables -t filter -I OUTPUT -p TCP -o eth1 -j DROP
案例9:要求访问服务器的8080端口转发至80端口
[root@m01 ~]# iptables -t nat -A PREROUTING -p TCP --dport 8080 -j REDIRECT --to-port 80
案例10:要求只允许windows通过ssh连接192.168.15.81,其他的拒绝
# 先查看windows下的地址:ipconfig > WMnet8 >IPV4地址
[root@m01 ~]# iptables -t filter -I INPUT -p TCP -s 192.168.15.1 -d 192.168.15.81 --dport 22 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP --dport 22 -j DROP
# IP过滤
# 禁止192.168.1.3 IP地址的所有类型数据访问
iptables -A INPUT ! -s 192.168.1.3 -j DROP
# 开放80端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #开放80端口
# 开放端口范围
iptables -I INPUT -p tcp --dport 22:80 -j ACCEPT #开发22-80范围的端口
# 不允许80端口流出
iptables -I OUTPUT -p tcp --dport 80 -j DROP
# filter是默认的表,可以不指定,也可以指定
# 案例:要求访问数据包中包含HelloWorld的数据不允许通过
[root@m01 ~]# cd /usr/share/nginx/html/
[root@m01 html]# rm -rf ./*
[root@m01 html]# echo "Helloworld" > index.html
[root@m01 html]# echo "Hello" > demo.html
# 规则
[root@m01 html]# iptables -t filter -A INPUT -p TCP -m string --string "Helloworld" --algo kmp -j DROP
[root@m01 html]# curl http://192.168.15.81/demo.html
Hello
[root@m01 html]# curl http://192.168.15.81/index.html
# Helloworld被过滤掉了
# 案例: 要求每天的22到23之间,不允许访问
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -m time --timestart 14:00 --timestop 15:00 -j DROP
# 注意这里使用的时间是UTC,记得-8
1、要求将22,80,443以及30000-50000之间所有的端口向外暴露,其他端口拒绝
iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,30000:50000 -j ACCEPT
iptables -f filter -A INPUT -p TCP -j DROP
2、要求访问数据包中包含HelloWorld的数据不允许通过。
iptables -t filter -A INPUT -p TCP -m string --string "HelloWorld" --algo kmp -j DROP
3、要求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 -f filter -A INPUT -p TCP -j DROP
4、要求每天的12到13之间,不允许访问
iptables -t filter -A INPUT -p TCP -m time --timestart 4:00 --timestop 5:00 -j DROP
必须使用UTC时间
5、要求别人不能ping本机,但是本机可以ping别人
iptables -t filter -A INPUT -p TCP -m icmp --icmp-type "echo-request" -j DROP
6、要求主机连接最多有2个
iptables -t filter -A INPUT -p TCP --dport 22 -m connlimit --connlimit-above 2 -j DROP
7、要求限制速率在500k/s左右
iptables -t filter -A INPUT -p TCP -m limit 333/s -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
8、只允许windows连接本机的iptables规则
iptables -t filter -A INPUT -p tcp -s 192.168.15.1 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
9、只允许192.168.15.0网段的IP连接本机,用两种方式实现。
iptables -t filter -A INPUT -p tcp -m iprange --src-range 192.168.15.1-192.168.15.254 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
iptables -t filter -A INPUT -p tcp -i eth0 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
10、要求本机流出的数据中包含“元旦快乐”
iptables -t filter -A OUTPUT -p tcp --dport 80 -m string --string "元旦快乐" --algo kmp -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 80 -j DROP
11、要求每天的九点到17点之间可以正常访问
iptables -t filetr -A INPUT -p tcp -m time --timestart 1:00 --timestop 9:00 -j ACCEPT
iptables -t filter -A INPUT -p tcp -j DROP