架设在内网和公网之间,起到保护和隔离的作用
RHEL7默认使用的是firewalld ,不过其底层还是调用iptables(包过滤防火墙)
[root@firewall51 ~]# systemctl stop firewalld
[root@firewall51 ~]# systemctl disable firewalld.service
[root@firewall51 ~]# yum -y install iptables-services.x86_64
[root@firewall51 ~]# systemctl start iptables.service
iptables的表,链结构
4张表:iptables服务功能分类 优先级顺序(高->低)raw -> mangle -> nat -> filter
5条链:ip包传输的方向
INPUT 匹配进入防火墙本机的ip包
OUTPUT 匹配从防火墙本机出去的ip包
FORWARD 匹配经过防火墙主机的ip包(源地址和目标地址均不是防火墙本机ip)
POSTROUTING 路由后处理
PREROUTING 路由前处理
raw表(状态跟踪表) | mangle表(包标记表) | nat表(地址转换表) | filter表(过滤表)默认 |
---|---|---|---|
PREROUTING OUTPUT |
PREROUTING POSTROUTING INPUT OUTPUT FORWARD |
PREROUTING POSTROUTING INPUT(rhel7/CentOS7) OUTPUT
|
INPUT OUTPUT FORWARD |
包过滤匹配流程:顺序比对,匹配即停止(LOG除外),若无匹配,则按照该链的默认策略处理
规则链之间的顺序:入站 PREROUTING->INPUT 出站 OUTPUT->POSTROUTING
转发 PREROUTING->FORWARD->POSTROUTING
iptables用法
管理程序位置:/sbin/iptables
规则永久保存: iptables-save > /etc/sysconfig/iptables
指令组成: iptables [-t 表名] 选项 [链名] [条件] [-j 目标操作]
类别 | 选项 | 用途 |
---|---|---|
查看规则 | -L | 列出所有的规则条目(如果和n连用,放在后面) |
-n | 以数字形式显示地址,端口等信息 | |
--line-numbers | 查看规则时,显示规则的序号 | |
添加 | -A | 在链的默认追加一条规则 |
-I | 在链的开头(或指定序列号)插入一条规则 | |
删除规则 | -D | 删除链内指定序号(或内容)的一条规则 |
-F | 清空所有规则 | |
默认策略 | -P | 为指定的链设置默认规则 |
类别 | 选项 | 用法 |
---|---|---|
通用匹配 | 协议匹配 | -p 协议名(icmp tcp udp...) |
地址匹配 | -s 源地址 -d 目标地址 | |
接口匹配 | -i 收数据的网卡 -o 发数据的网卡 | |
隐含匹配 | 端口匹配 | --sport 源端口 --dport 目标端口 |
ICMP类型匹配 | --icmp-type ICMP类型 |
类别 | 选项 | 用法 |
---|---|---|
扩 展 匹 配 -m |
MAC地址匹配 | -m mac --mac-source MAC地址 |
多端口匹配 | -m multiport --sports 源端口列表 | |
-m multiport --dports 目标端口列表 | ||
IP范围匹配 | -m iprange --src-range IP1-IP2 |
|
-m iprange --dst-range IP1-IP2 |
ACCEPT | 允许通过/放行 |
DROP | 直接丢弃,不给出任何回应 |
REJECT | 拒绝通过,必要时给出提示 |
LOG | 记录日志,传给下一条规则( “匹配即停止”规律的例外) |
注意事项/规律
可以不指定表,默认为filter,若不指定链,则默认对应表的所有链,没有规则,使用默认规则
选项(个别除外)/链名/目标操作用大写字母,其余都小写
命令行修改规则之后,需要保存到相关配置文件,否则重启又恢复最初状态
[root@firewall51 ~]# iptables -t filter -nL INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
[root@firewall51 ~]# iptables -t filter -F
[root@firewall51 ~]# iptables -t filter -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 destinatio
[root@firewall51 ~]# iptables-save //保存输出到屏幕,但不能永久保存
# Generated by iptables-save v1.4.21 on Fri Jan 4 10:44:40 2019
*raw
:PREROUTING ACCEPT [188:13984]
:OUTPUT ACCEPT [131:12720]
...
*filter
:INPUT ACCEPT [233:17208]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [155:15000]
COMMIT
# Completed on Fri Jan 4 10:44:40 2019
主机型防火墙 通过入站进行控制filter表INPUT
[root@firewall51 ~]# iptables -t filter -P INPUT DROP //修改默认规则
[root@firewall51 ~]# iptables -t filter -nL INPUT //其他主机无法远程本机
Chain INPUT (policy DROP)
target prot opt source destination
[root@firewall51 ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT 修改权限之后可以远程链接
[root@guo ~]# ssh -X 192.168.4.51
[email protected]'s password:
Last login: Fri Jan 4 10:16:36 2019 from 192.168.4.254
[root@firewall51 ~]# iptables -t filter -nL INPUT
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
[root@firewall51 ~]# iptables -t filter -nL INPUT
...
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
[root@firewall51 ~]# iptables -t filter -I INPUT -p tcp -s 192.168.4.254 -j ACCEPT
//修改防火墙规则,可以接收来自192.168.4.254的包
[root@firewall51 ~]# iptables -t filter -nL INPUT
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 192.168.4.254 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
[root@guo ~]# ping 192.168.4.51 //通过物理机ping 192.168.4.51 不能到达
PING 192.168.4.51 (192.168.4.51) 56(84) bytes of data.
[root@firewall51 ~]# iptables -t filter -I INPUT -p icmp -j ACCEPT //修改规则
[root@guo ~]# ping 192.168.4.51
PING 192.168.4.51 (192.168.4.51) 56(84) bytes of data.
64 bytes from 192.168.4.51: icmp_seq=149 ttl=64 time=0.333 ms
64 bytes from 192.168.4.51: icmp_seq=150 ttl=64 time=0.184 ms
修改规则使自己可以ping别的主机,别的主机不可以ping自己
--icmp-type ICMP类型 echo-reply (pong) echo-request (ping)
[root@firewall51 ~]# iptables -t filter -D INPUT 1 /删除上面添加的允许icmp规则
[root@firewall51 ~]# iptables -t filter -nL INPUT
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 192.168.4.254 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
[root@firewall51 ~]# iptables -t filter -I INPUT -p icmp --help //查看帮助
[root@firewall51 ~]# iptables -t filter -I INPUT -p icmp --icmp-type echo-reply -j ACCEPT //修改规则使ping包的回应接收
[root@firewall51 ~]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere icmp echo-reply
[root@firewall51 ~]# iptables -nL
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 0 //使用数字显示规则显示为0
[root@firewall51 ~]# ping 192.168.4.254
PING 192.168.4.254 (192.168.4.254) 56(84) bytes of data.
64 bytes from 192.168.4.254: icmp_seq=1 ttl=64 time=0.129 ms
64 bytes from 192.168.4.254: icmp_seq=2 ttl=64 time=0.173 ms
[root@guo ~]# ping 192.168.4.51
PING 192.168.4.51 (192.168.4.51) 56(84) bytes of data.
^C
--- 192.168.4.51 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 1999ms
通过控制MAC地址禁止其他主机ping自己(恢复默认INPUT为ACCEPT,删除其他规则进行验证)
[root@firewall51 ~]# arp -n //可以看到ping自己主机的MAC地址
Address HWtype HWaddress Flags Mask Iface
192.168.4.52 ether 52:54:00:c0:85:a7 C eth0
192.168.4.254 ether 52:54:00:37:78:11 C eth
[root@firewall51 ~]# iptables -t filter -A INPUT -p icmp -m mac --mac-source 52:54:00:c0:85:a7 -j DROP //禁用192.168.4.52主机
[root@firewall51 ~]# iptables -t filter -nL INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP icmp -- 0.0.0.0/0 0.0.0.0/0 MAC 52:54:00:C0:85:A7
通过多端口禁止访问本机的80(httpd/nginx)和8080(tomcat)端口
[root@firewall51 ~]# iptables -t filter -A INPUT -p tcp -m multiport --dports 80,8080 -j DROP
[root@firewall51 ~]# iptables -nL INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports 80,8080
通过IP范围控制其他主机
[root@firewall51 ~]# iptables -t filter -A INPUT -p icmp -m iprange --src-range 192.168.4.100-192.168.4.110 -j DROP
[root@firewall51 ~]# iptables -t filter -nL INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports 80,8080
DROP icmp -- 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.4.100-192.168.4.110
网络型防火墙 通过网关控制
拓扑结构:
192.168.4.51 eth0 | 192.168.4.52 eth0 192.168.2.52 eth1 |
192.168.2.53 eth1 |
实现4.51和2.53互通(分别在51上和53上操作)
[root@firewall51 ~]# systemctl stop NetworkManager
[root@firewall51 ~]# route add default gw 192.168.4.52 //添加网关 删除用del
[root@firewall51 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.4.52 0.0.0.0 UG 0 0 0 eth0
192.168.4.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@firewall53 ~]# systemctl stop NetworkManager
[root@firewall53 ~]# route add default gw 192.168.2.52
[root@firewall53 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.2.52 0.0.0.0 UG 0 0 0 eth1
192.168.2.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
配置52内核路由转发
[root@firewall52 ~]# sysctl -a | grep forward //列出所有内核参数,搜索关于forward的
...
net.ipv4.ip_forward = 1
...
[root@firewall52 ~]# vim /etc/sysctl.conf
[root@firewall52 ~]# tail -1 /etc/sysctl.conf
net.ipv4.ip_forward = 1
//默认这个是开启的
在主机52上写防火墙规则
[root@firewall52 ~]# iptables -t filter -A FORWARD -p tcp --dport 80 -j DROP
[root@firewall52 ~]# iptables -t filter -nL FORWARD
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
[root@firewall53 ~]# curl http://192.168.4.51 //测试不能链接
[root@firewall52 ~]# iptables -t filter -F FORWARD
[root@firewall52 ~]# iptables -t filter -A FORWARD -p tcp -m multiport --dports 80,8080,22 -j DROP //集中禁用服务
[root@firewall52 ~]# iptables -t filter -nL FORWARD
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports 80,8080,22
当默认策略为DROP时 修改规则允许访问
[root@firewall52 ~]# iptables -t filter -F
[root@firewall52 ~]# iptables -t filter -P FORWARD DROP
[root@firewall52 ~]# iptables -t filter -nL FORWARD
Chain FORWARD (policy DROP)
[root@firewall52 ~]# iptables -t filter -A FORWARD -p tcp --dport 80 -j ACCEPT
[root@firewall51 ~]# tcpdump -i eth0 -A tcp port 80 //在51上抓取包来进行验证,看是否到达
[root@firewall52 ~]# iptables -t filter -A FORWARD -p tcp --sport 80 -j ACCEPT
[root@firewall52 ~]# iptables -t filter -nL FORWARD
Chain FORWARD (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:80
[root@firewall53 ~]# curl 192.168.4.51 //验证可以访问,可以用firefox测试
默认策略为DROP 修改规则使主机可以 互相ping通
可以直接修改为 iptables -t filter -A FORWARD -p icmp -j ACCEPT 也可以用下面的方法
[root@firewall52 ~]# iptables -t filter -A FORWARD -p icmp --icmp-type echo-reply -j ACCEPT
[root@firewall52 ~]# iptables -t filter -A FORWARD -p icmp --icmp-type echo-request -j ACCEPT
[root@firewall52 ~]# iptables -L FORWARD
Chain FORWARD (policy DROP)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT tcp -- anywhere anywhere tcp spt:http
ACCEPT icmp -- anywhere anywhere icmp echo-reply
ACCEPT icmp -- anywhere anywhere icmp echo-request
模拟内网 | Linux网关(防火墙) | 模拟公网 |
---|---|---|
192.168.2.53 eth1 | 192.168.2.52 eth1 192.168.4.52 eth0 |
192.168.4.51 eth0 |
源地址转换(共享一个IP地址)
将2.53默认网关指向2.52
[root@firewall53 ~]# route -n //53为内网,网关地址改为192.168.2.52
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.2.52 0.0.0.0 UG 0 0 0 eth1
192.168.2.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
配置主机52
[root@firewall52 ~]# iptables -F
[root@firewall52 ~]# iptables -t filter -P FORWARD ACCEPT
[root@firewall52 ~]# iptables -t filter -nL
Chain INPUT (policy ACCEPT)
Chain FORWARD (policy ACCEPT)
Chain OUTPUT (policy ACCEPT)
配置主机51并打开httpd或者nginx服务
[root@firewall51 ~]# route -n //51为公网,不用指定默认网关
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.4.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@firewall51 ~]# netstat -pntul | grep :80
tcp6 0 0 :::80 :::* LISTEN 6370/httpd
[root@firewall51 ~]# echo "123" > /var/www/html/test.html //写个测试页
在52上进行地址转换
[root@firewall52 ~]# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -p tcp --dport 80 -j SNAT --to-source 192.168.4.52
//-s 192.168.2.0/24局域网网段地址 -p tcp --dport 80 -j SNAT --to-source 192.168.4.52 外网接口 IP地址
在53上链接51的http服务,在51上查看日志文件
[root@firewall53 ~]# curl 192.168.4.51/test.html
123
[root@firewall51 ~]# tail -1 /var/log/httpd/access_log //查看日志
192.168.4.52 - - [04/Jan/2019:17:53:50 +0800] "GET / HTTP/1.1" 403 3985 "-" "curl/7.29.0"
目标地址转换(发布私有网络服务)