iptables防火墙
- netfilter/iptables:工作在主机或网络的边缘,对于进出本主机或网络的报文根据事先定义好的检查规则作匹配检测,对于能够被规则所匹配到的报文做出相应的处理
iptables框架
-
iptables的4表
- filter:数据过滤表
- 包含三个链:INPUT,UOTPUT,FORWARD
- nat:地址转换表,不能过滤数据包,仅仅修改数据包中的IP和端口
- 包含三个链:PREROUTING,POSTROUTING,OUTPUT
- raw:状态跟踪表,决定是否跟踪数据包
- 包含两个链:OUTPUT,PREROUTING
- mangle:包标记表,不能过滤也不能修改数据包
- 包含五个链:PREROUTING,FORWARD,POSTROUTING,INPUT,OUTPUT
- filter:数据过滤表
-
iptables的5链
- PREROUTING链:路由前规则,防火墙在刚刚接收到数据包,对数据包进行路径选择之前所需要的链
- FORWARD链:转发规则,将数据包从一个网络转发到另外一个网络所需要的链
- POSTROUTING链:路由后规则,对数据包进行路径选择后,并在防火墙即将把数据包转发出去之前,所要需要的链
- INPUT链:入站规则,限制客户端数据包目地地址是防火墙主机的上层应用所需要的链
- OUTPUT链:出站规则,限制防火墙主机上层应用产生的数据包是否可以出站需要的链
-
目标操作
- ACCEPT:允许通过/放行
- DROP:直接丢弃,不给出任何回应
- REJECT:拒绝通过,有明确回应
- LOG:记录日志,然后传给下一条规则
iptables命令格式
-
命令格式:Iptables [-t 表名] 选项 [链名] [条件] [-j 目标操作]
-
添加规则:
- -A #追加一条防火墙规则至链的末尾
- -I #插入一条防火墙规则至链的开头
-
查看规则:
- -L #查看iptables所有规则,与-n连用
- -n #以数字形式显示地址、端口等信息,与-L连用
- –line-numbers #查看规则时,显示规则的行号
-
删除规则:
- -D #删除链内指定的序号(或内容)的一条规则
- -F #清空所有规则
-
默认规则:
- -P #为指定的链设置默认规则
iptables防火墙规则的条件
-
通用匹配:
-
协议匹配:-p #协议名称
-
地址匹配:-s 源地址、-d 目标地址
-
接口匹配:-i 接受数据的网卡、-o 发送数据的网卡
-
端口匹配:–sport 源端口号、–dport 目标端口号
-
ICMP类别匹配:–icmp-type ICMP 类型
-
-
创建规则注意事项
- 可以不指定表,默认为filter表
- 如果没有找到匹配条件,执行防火墙默认规则
主机型防火墙规则配置
#安装iptables服务
[root@master ~]# yum -y install iptables-services
[root@master ~]# systemctl start iptables
#拒绝icmp访问
[root@master ~]# iptables -t filter -I INPUT -p icmp -j REJECT
#查看规则
[root@master ~]# iptables -L -n --line-numbers
#测试icmp访问
[root@slave ~]# ping 192.168.0.10
PING 192.168.0.10 (192.168.0.10) 56(84) bytes of data.
From 192.168.0.10 icmp_seq=1 Destination Port Unreachable
[root@master ~]# iptables -t filter -I INPUT -p icmp -j DROP
[root@master ~]# iptables -t filter -I INPUT -p icmp -j ACCEPT
#清空某一条规则(默认为filter表)
[root@iptables ~]# iptables -t filter -D INPUT 8
#清空所有规则(默认为filter表所有规则)
[root@master ~]# iptables -F
#清空其他表所有规则
[root@master ~]# iptables -t nat -F
[root@master ~]# iptables -t raw -F
[root@master ~]# iptables -t mangle -F
#为filter表INPUT链添加规则,允许任何人使用TCP协议访问本机
[root@master ~]# iptables -t filter -I INPUT -p tcp -j ACCEPT
#为filter表INPUT链添加规则至第一行,允许任何人使用UDP协议访问本机
[root@master ~]# iptables -t filter -I INPUT -p udp -j ACCEPT
#为filter表INPUT链添加规则至第二行,允许任何人使用ICMP协议访问本机
[root@master ~]# iptables -t filter -I INPUT 2 -p icmp -j ACCEPT
[root@master ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0
#查看INPUT链规则
[root@master ~]# iptables -nL INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0
#清空filter表所有规则
[root@master ~]# iptables -F
**
设置防火墙默认规则
**
#查看filter表
[root@master ~]# iptables -t filter -nL
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
#允许22号端口被访问
[root@master ~]# iptables -t filter -I INPUT -p tcp --dport 22 -j ACCEPT
[root@master ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
#将filter表INPUT链默认规则修改为DROP
[root@master ~]# iptables -t filter -P INPUT DROP
[root@master ~]# iptables -nL
Chain INPUT (policy DROP) #默认为拒绝
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 #只允许22端口访问,其他全部拒绝
#将默认规则修改为ACCEPT
[root@master ~]# iptables -t filter -P INPUT ACCEPT
#清空防火墙规则
[root@master ~]# iptables -F
#设置防火墙拒绝所有80端口的访问
[root@master ~]# iptables -t filter -I INPUT -p tcp --dport 80 -j REJECT
#查看规则
[root@master ~]# iptables -nL
#安装httpd服务
[root@master ~]# yum -y install httpd
#启动服务
[root@master ~]# systemctl start httpd
#修改默认首页
[root@master ~]# echo xxoo > /var/www/index.html
#访问测试
[root@slave ~]# curl http://192.168.0.10
[root@master ~]# iptables -F
#单独拒绝某个IP
[root@master ~]# iptables -t filter -I INPUT -s 192.168.0.20 -j REJECT
#单独拒绝某个IP不允许访问本机的某个服务(端口)
[root@iptables-server ~]# iptables -I INPUT -s 192.168.0.27 -p tcp --dport 80 -j REJECT
#查看规则
[root@master ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT all -- 192.168.0.20 0.0.0.0/0 reject-with icmp-port-unreachable
192.168.0.20访问测试
[root@slave ~]# curl http://192.168.0.10
curl: (7) Failed connect to 192.168.0.10:80; 拒绝连接
#拒绝客户端访问本机网卡的80端口
[root@master ~]# iptables -t filter -I INPUT -i ens32 -p tcp --dport 80 -j REJECT
#客户端测试
[root@agent ~]# curl http://192.168.0.10
curl: (7) Failed connect to 192.168.0.10:80; 拒绝连接
[root@master ~]# iptables -F
#设置防火墙拒绝某个网段
[root@master ~]# iptables -t filter -I INPUT -s 192.168.1.0/24 -j REJECT
#查看规则
[root@master ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT all -- 192.168.1.0/24 0.0.0.0/0
网络型防火墙规则配置
- 通过路由转发配置网络型防火墙
主机名 | 网卡、IP地址、网关设置 |
---|---|
client28 | ens32:192.168.1.28,网关指向防火墙外网IP:192.168.1.100 |
proxy | ens32内网IP:192.168.0.26,ens34外网IP:192.168.1.100 |
web27 | ens32:192.168.0.27,网关指向防火墙内网IP:192.168.0.26 |
- 防火墙主机添加网卡并设置IP,IP设置为:192.168.0.100/24
#开启路由转发功能
[root@proxy ~]# echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
[root@proxy ~]# sysctl -p //加载配置立即生效
net.ipv4.ip_forward = 1
#查看路由转发
[root@proxy ~]# cat /proc/sys/net/ipv4/ip_forward
1
- 第二台服务器提供网站服务:web27将网关指向1192.168.0.26
#安装httpd服务
[root@web1 ~]# yum -y install httpd
#修改默认首页
[root@web1 ~]# echo web27 > /var/www/html/index.html
[root@web1 ~]# systemctl start httpd
[root@web1 ~]# systemctl status httpd
-
第三台为客户端:client将IP修改为192.168.1.30网段,将网关指向防火墙主机的第二块网卡:192.168.0.100
-
访问测试:curl http://192.168.0.20
-
防火墙主机配置规则:拒绝192.168.1.30访问80端口
[root@proxy ~]# iptables -I FORWARD -s 192.168.1.28 -p tcp --dport 80 -j DROP
#客户端192.168.1.30访问测试:
curl http://192.168.0.20
#拒绝所有客户端地址访问内网80端口
[root@iptables ~]# iptables -t filter -I FORWARD -p tcp --dport 80 -j REJECT
[root@proxy ~]# iptables -F
防火墙扩展
- 命令格式:iptables 选项 链名 -m 扩展模块 --具体扩展条件 -j 动作
#根据MAC地址封锁主机,安装nmap扫描获取地方IP的MAC地址
[root@proxy ~]# yum -y install nmap
#扫描对方主机IP
[root@proxy ~]# nmap 192.168.28
Starting Nmap 6.40 ( http://nmap.org ) at 2020-09-25 17:12 CST
Nmap scan report for 192.168.0.111
Host is up (0.00044s latency). //当前主机状态
MAC Address: 00:0C:29:CA:87:81 (VMware) //MAC地址
Nmap done: 1 IP address (1 host up) scanned in 4.05 seconds
#通过MAC地址限制对方访问
[root@proxy ~]# iptables -t filter -I FORWARD -p tcp --dport 80 -m mac --mac-source 00:0C:29:D5:29:0F -j REJECT
- 基于多端口设置过滤规则
[root@proxy ~]# iptables -t filter -I FORWARD -p tcp -m multiport --dports 20,21,80,443 -j ACCEPT
#multiport :多端口模块
#根据IP范围设置封锁规则
[root@proxy ~]# iptables -t filter -I FORWARD -p tcp --dport 80 -m iprange --src-range 192.168.1.20-192.168.1.30 -j REJECT
#iprange模块:ip范围模块
#--src-range:源IP
#--dst-range:目标IP
配置SNAT实现共享上网
- 通过防火墙规则,允许局域网中的主机访问外网
主机名 | 网卡、IP地址、网关 |
---|---|
内部主机:host28 | ens32:192.168.1.28,网关:192.168.1.100 |
内部防火墙:proxy | ens32外网IP:192.168.0.26,ens34内网IP:192.168.1.100 |
外部主机:web27 | ens32:192.168.0.27,网关:192.168.0.26 |
#实现192.168.1.28转换为192.168.0.26
[root@proxy ~]# iptables -t nat -I POSTROUTING -s 192.168.1.0/24 -p tcp -j SNAT --to-source 192.168.0.26
#POSTROUTING:路由后链
#-s:指定源地址为192.168.1.0网段的地址
#-p:想要通过tcp协议
#--dport:访问目标的80端口时
#-j:SNAT转换
#--to-source:转换源地址为192.168.0.26
#web1动态查看访问日志
[root@web1 ~]# tail -f /var/log/httpd/access_log
client 192.168.1.30访问网站
curl http://192.168.0.27
#所有iptables规则都是临时规则,如果需要永久保留规则需要执行如下命令
[root@proxy ~]# service iptables save