史上最全的iptables应用
第14章 防火墙的使用
14.1 防火墙的概念
将不安全的网络流量信息进行隔离
14.2 防火墙的实现
14.2.1 硬件实现
思科,华为防火墙服务器
14.2.2 软件实现
iptables(centos6)和firewalld(centos7)实现
14.3 防火墙服务(容器)的组成部分
14.3.1 什么是容器
- 防火墙服务(容器) 容器就是表的概念
- 防火墙服务表(容器) 链的概念
- 防火墙链(容器) 规则
14.3.2 配置防火墙的流程
开启防火墙服务------指定表--------指定链-------指定规则
14.4 防火墙表和链
- 红色为表,粉色为链
14.4.1 表对应的链
- filter INPUT、FORWARD、OUTPUT
- nat PREROUTING、POSTROUTING、OUTPUT
- mangle PREROUTING、OUTPUT、FORWARD、POSTROUTING
- raw PREROUTING、OUTPUT
14.5 iptables的工作原理
14.5.1 文字概述
- 防火墙是层层过滤的,实际是按照配置规则从前往后或者从上往下过滤的
- 如果匹配上规则,则明确表示是阻止还是通过,数据包不会在往下进行匹配
- 如果规则中没有明确表明是阻止还是通过,也没有匹配规则,向下匹配,直到匹配到默认规则明确表示是通过还是阻止
- 防火墙的默认规则是所有的规则执行完毕后才执行的
14.6 iptables表和链的关系详细说明
14.6.1 filter表的使用
真正起到防火墙的功能
14.6.1.1 filter表下面的链简要说明
INPUT链 流量在进入时进行访问控制
OUTPUT链 流量在出去的时候进行访问控制
FORWARD链 流量是否可以经过防火墙
14.6.2 iptables防火墙filter表中的3个链的规则配置方法
14.6.2.1 准备工作
14.6.2.1.1 安装iptables服务
[root@firewalld yum.repos.d] # yum install -y iptables
14.6.2.1.2 查看一些默认iptables配置策略
-
-L [chain [rulenum]] List the rules in a chain or all chains
列出链上规则配置信息
-
-n numeric output of addresses and ports
以数字形式输出地址和端口
[root@firewalld ~] # iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:53
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 192.168.122.0/24 ctstate RELATED,ESTABLISHED
ACCEPT all -- 192.168.122.0/24 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:68
You have new mail in /var/spool/mail/root
[root@firewalld ~] #
14.6.2.2 进行初始化的操作
-
-F [chain] Delete all rules in chain or all chains
删除所有的链上的规则,默认是filter表
-
-Z [chain [rulenum]] Zero counters in chain or all chains
清除计数器上面的信息
-
-X [chain] Delete a user-defined chain
删除用户自定义链
14.6.2 nat表的使用
实现访问映射/流量转发
实现内网访问外网
外网访问映射到指定内网主机
14.7 filter表的练习
- 策略处理方法:
- ACCEPT 允许接收
- DROP 丢弃(更安全)
- REJECT 拒绝
-
-t table table to manipulate (default: `filter')
使用-t来定义后面的表信息,默认filter
-
-A chain Append to chain
追加一个链在后面
-
-p proto protocol: by number or name, eg. `tcp'
协议:通过数字或者协议名称
- --dport 规则匹配的目标端口信息
- --sport 规则匹配的源端口信息
- -j target target for rule (may load target extension)
指定处理规则动作
-
-s address[/mask][...] source specification
指定匹配规则的源信息
- -d address[/mask][...] destination specification
指定匹配规则的目标信息
- -P chain target Change policy on chain to target
在链上修改默认策略
- -i input name[+] network interface name ([+] for wildcard)
指定从哪个网卡进入
- -o output name[+] network interface name ([+] for wildcard)
指定从哪个网卡出去
- -m match extended match (may load extension)
扩展匹配的信息
- -I chain [rulenum] Insert in chain as rulenum (default 1=first)
指定行插入规则信息
14.7.1 阻止任意用户访问22端口
14.7.1.1 INPUT练习
[root@firewalld ~] # iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
14.7.1.2 OUTPUT练习
[root@firewalld ~] # iptables -t filter -A OUTPUT -p tcp --sport 22 -j DROP
14.7.2 允许10.0.0.1主机访问22端口,其他网段,其他主机不能访问
14.7.2.1 INPUT练习
[root@firewalld ~] # iptables -A INPUT -s 10.0.0.1 -p tcp -d 10.0.0.81 --dport 22 -j ACCEPT
[root@firewalld ~] # iptables -A INPUT -s 10.0.0.0/24 -p tcp --dport 22 -j DROP
14.7.2.2 OUTPUT练习
[root@firewalld ~] # iptables -A OUTPUT -d 10.0.0.1 -p tcp -s 10.0.0.81 --sport 22 -j ACCEPT
[root@firewalld ~] # iptables -A OUTPUT -d 10.0.0.0/24 -p tcp --sport 22 -j DROP
14.7.3 允许所有通过eth0流量访问22端口, 拒绝通过eth1接口访问22端口
14.7.3.1 INPUT练习
[root@firewalld ~] # iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
[root@firewalld ~] # iptables -A INPUT -i eth1 -p tcp --dport 22 -j DROP
14.7.3.2 OUTPUT练习
[root@firewalld ~] # iptables -A OUTPUT -i eth0 -p tcp --dport 22 -j ACCEPT
[root@firewalld ~] # iptables -A INPUT -i eth1 -p tcp --sport 22 -j DROP
14.7.4 除了10.0.0.1这个地址可以访问22端口,其他所有的地址都不能访问22端口
14.7.4.1 取反操作
[root@firewalld ~] # iptables -A INPUT ! -s 10.0.0.1 -p tcp --dport 22 -j DROP
14.7.4.2 修改默认策略
[root@firewalld ~] # iptables -A INPUT -s 10.0.0.1 -p tcp --dport 22 -j ACCEPT 允许10.0.0.1访问22端口
[root@firewalld ~] # ipatbles -P INPUT DROP 修改默认规则为阻止
14.7.5 如何阻止用户访问防火墙的多个端口
14.7.5.1 连续多个端口信息
[root@firewalld ~] # iptables -A INPUT -s 10.0.0.1 -p tcp --dport 22:80 -j DROP
14.7.5.2 不连续的多个端口信息
[root@firewalld ~] # iptables -A INPUT -s 10.0.0.1 -m multiport -p tcp --dport 22,23,25,80 -j DROP
14.7.6 实现主机禁ping功能
- ping使用的是icmp协议
- type 8:表示ping包请求流量
- type 0:表示ping包响应流量
14.7.6.1 其他服务器ping不通防火墙服务器
14.7.6.1.1 INPUT练习
[root@firewalld ~] # iptables -A INPUT -s 10.0.0.7 -p icmp --icmp-type 8 -j DROP
14.7.6.1.2 OUTPUT练习
[root@firewalld ~] # iptables -A OUTPUT -d 10.0.0.7 -p icmp --icmp-type 0 -j DROP
14.7.6.2 防火墙不能ping其他主机
14.7.6.2.1 OUTPUT练习
[root@firewalld ~] # iptables -A OUTPUT -p icmp --icmp-type 8 -j DROP
14.7.6.2.2 INPUT练习
[root@firewalld ~] # iptables -A INPUT -p icmp --icmp-type 0 -j DROP
14.7.6.3 阻止0和8的流量信息
[root@firewalld ~] # iptables -A INPUT -p icmp -m icmp --icmp-type any -j DROP
- -m icmp 是扩展的意思,可以同时指定多个icmp协议协议
- any 表示任意协议的意思
14.7.7 利用防火墙限制数据包流量
[root@firewalld ~] # iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 6/min --limit-burst 5 -j ACCEPT
[root@firewalld ~] # iptables -P INPUT DROP
- -m limit --limit n/{second/minute/hour} 一定时间内可以产生出n个包
- --limit-burst 5 n个包过后在多长时间内限速不产生包
14.8 企业中防火墙(iptables)实际配置过程
14.8.1 确认filter表中的所有默认策略(一般设置为白名单,切记先操作完第二步骤,在操作第一步骤)
[root@firewalld ~] # iptables -P INPUT DROP 默认INPU
[root@firewalld ~] # iptables -P OUTPUT DROP 默认OUTPUT为阻止
[root@firewalld ~] # iptables -P FORWARD ACCEPT 默认FORWARD为允许
14.8.2 确认自己可以远程连接防火墙服务器
[root@firewalld ~] # iptables -A INPUT -s 10.0.0.0/24 -p tcp --dport 22 -j ACCEPT
[root@firewalld ~] # iptables -P INPUT DROP
[root@firewalld ~] # iptables -P OUTPUT DROP
[root@firewalld ~] # iptables -P FORWARD ACCEPT
14.8.3 设置环回接口(lookback)
14.8.3.1 环回接口的概念和作用
14.8.3.1.1 概念
环回接口是一个逻辑接口
14.8.3.1.2 作用
可以实现服务器自己ping自己
可以设置成一个机器的管理口
14.8.3.2 防火墙环回接口配置
14.8.3.2.1 环回接口INPUT和OUTPUT都设置成允许
[root@firewalld ~] # iptables -A INPUT -i lo -p all -j ACCEPT
You have new mail in /var/spool/mail/root
[root@firewalld ~] # iptables -A OUTPUT -o lo -p all -j ACCEPT
14.8.4 让防火墙的配置永久保存
14.8.4.1 centos6
service iptables save
14.8.4.2 centos7
14.8.4.2.1 使用iptables-save来将显示的信息重定向到/etc/sysconfig/iptables
[root@firewalld ~] # iptables-save > /etc/sysconfig/iptables
14.8.4.2.2 直接编辑/etc/sysconfig/iptables保存(不要把iptables这个添加就可以)
[root@firewalld ~] # cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Thu Dec 5 16:08:22 2019
*filter
:INPUT ACCEPT [826:97750]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [440:68087]
-A INPUT -i lo -j ACCEPT 和这个iptables -A INPUT -i lo -p all -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
COMMIT
# Completed on Thu Dec 5 16:08:22 2019
[root@firewalld ~] #
14.9 nat表的使用
- PREROUTING 可以实现外网主机访问内网
- POSTROUTING 可以实现内网主机访问外网
- OUTPUT 流量在出去时进行访问控制
14.9.1 内网访问外网
14.9.1.1 修改内网网关信息
[root@web01 ~] # cat /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=172.16.1.7
PREFIX=24
DEFROUTE=yes
GATEWAY=172.16.1.5
DNS1=223.5.5.5
DNS2=223.6.6.6
14.9.1.2 实现内网服务器可以ping通网关
[root@lb01 ~] # iptables -A INPUT -s 172.16.1.7 -j ACCEPT
[root@lb01 ~] #
14.9.1.3 开启路由转发的功能
[root@lb01 ~] # cat /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
net.ipv4.ip_forward=1 开启路由转发的功能
[root@lb01 ~] #
[root@lb01 ~] # sysctl -p /etc/sysctl.conf
net.ipv4.ip_forward = 1 重新加载路由转发配置文件
[root@lb01 ~] #
14.9.1.4 配置内网通过外网上网
[root@lb01 ~] # iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT -o eth0 --to-source 10.0.0.5
[root@lb01 ~] # iptables -A FORWARD -i eth1 -s 172.16.1.0/24 -j ACCEPT
[root@lb01 ~] # iptables -A FORWARD -o eth0 -s 172.16.1.0/24 -j ACCEPT
[root@lb01 ~] # iptables -A FORWARD -i eth0 -s 172.16.1.0/24 -j ACCEPT
[root@lb01 ~] # iptables -A FORWARD -o eth1 -s 172.16.1.0/24 -j ACCEPT
[root@lb01 ~] #
14.9.2 实现外网可以访问内网
[root@firewalld ~] # iptables -t nat -A PREROUTING(链) -d 10.0.0.81(访问的目标地址) -p tcp --dport 9000(目标端口) -i eth0(在什么地方做映射) -j DNAT(目的地址做NAT转换) --to-destination 172.16.1.7:22(转换成的目的地址是多少)
[root@firewalld ~] # iptables -t nat -A PREROUTING -d 10.0.0.81 -p tcp --dport 9000 -i eth0 -j DNAT --to-destination 172.16.1.7:22
14.10 自定义链的使用(相当于shell脚本中的变量)
14.10.1 创建自定义链
[root@firewalld ~] # iptables -N oldboy
14.10.2 设置自定义链规则
[root@firewalld ~] # iptables -A oldboy -p tcp --dport 8080 -j ACCEPT
14.10.3 使用自定义链
[root@firewalld ~] # iptables -A INPUT -s 172.16.1.0/24 -j oldboy
14.10.4 调整端口不光能访问8080,是8080:8090
[root@firewalld ~] #iptables -R oldboy 1 -p tcp --dport 8080:8090 -j ACCEPT