Firewall/iptables.md

464 lines
17 KiB
Markdown
Raw Normal View History

2025-04-13 20:43:38 +08:00
<h2><center>Iptables</center></h2>
------
## 一:防火墙介绍
### 1. 简介
`iptables`其实并不是真正的防火墙,我们可以把他理解为一个客户端的代理,用户是通过`iptables`这个代理,将用户的安全设定执行到对应的“安全框架”中,这个“安全框架”才是真正的防火墙。这个框架叫做`netfilter`
- `netfilter` 内核态 位于内核空间
- `iptables` 用户态 位于用户空间
注意:
企业环境内部服务器需关闭`Linux`自身防火墙;(解决安全问题尽量不给服务器配置外网`IP`。需要访问的话,就使用代理转发)因为高并发,`iptables`会加大延迟。除非并发小,服务器必须处于公网,考虑开启防火墙;大并发的情况,不能开`iptables`,影响性能因为`iptables`是要消耗`CPU`的,利用硬件防火墙提升架构安全。
### 2. 分类
**逻辑分类**
- 主机防火墙:针对单个主机进行防护
- 网络防火墙:它往往处于网络入口或者边缘,针对于网络入口进行防护,服务于防火墙背后的局域网
**物理分类**
- 硬件防火墙:在硬件级别实现部分防火墙功能,另一部分基于软件实现,性能高,成本高
- 软件防火墙:应用软件处理逻辑运行于通用硬件平台之上,性能低,成本低
## 二:相关术语
### 1. 表tables
tables是链的容器即所有的链chains都属于其对应的表tables如上如果把Netfilter看成是某个小区的一栋楼那么表tables就是楼里的其中的一套房子。
### 2. 链chains
chains是规则Policys的容器。如果把表tables当作有一套房子那么链chains就可以说是房子里的家具柜子等
### 3. 规则Policy
规则Policy就比较容易理解了就是iptables系列过滤信息的规范和具体方法条款了.可以理解为柜子如何增加并摆放柜子东西等。
| **Netfilter/iptables** | 表tables | 链chains | 规则Policy |
| ---------------------- | ------------ | ------------ | -------------------- |
| **一栋楼** | 楼里的房子 | 房子里的柜子 | 柜子里衣服,摆放规则 |
## 三Iptables 表和链
默认情况下,`iptables`根据功能和表的定义划分包含四个表,`filter``nat``mangle``raw`其每个表又包含不同的操作链chains )。
### 1. 表
- `raw`:追踪数据包
- `mangle`:对数据包打标记
- `nat`:地址转换
- `filter`:数据包过滤
### 2. 链
- `PREROUTING`:在路由之前
- `INPUT`:数据包进入时
- `FORWARD`:数据包经过时
- `OUTPUT`:数据包出去时
- `POSTROUTING`:在路由之后
### 3. 表详情
- `filter`表——三个链:`INPUT``FORWARD``OUTPUT`
作用:过滤数据包 内核模块:`iptables_filter`
- `Nat`表——三个链:`PREROUTING``POSTROUTING``OUTPUT`
作用:用于网络地址转换(`IP`、端口) 内核模块:`iptable_nat`
- `Mangle`表——五个链:`PREROUTING``POSTROUTING``INPUT``OUTPUT``FORWARD`
作用:修改数据包的服务类型、`TTL`、并且可以配置路由实现`QOS`内核模块:`iptable_mangle`(别看这个表这么麻烦,咱们设置策略时几乎都不会用到它)。
- `Raw`表——两个链:`OUTPUT``PREROUTING`
作用:决定数据包是否被状态跟踪机制处理 内核模块:`iptable_raw`
### 4. 访问顺序
- 当一个数据包进入网卡,先进入`PREROUTING`链,内核根据数据包的`IP`判断是否需要转发
- 如果是到本机的,就会到`INPUT`链,然后本机的所有进程可收到这个包
- 如果不是到本机的,且内核允许转发,就会到达`FORWARD`链,然后到`POSRTROUTING`链输出
- 本机发出一个数据,会通过`OUTPUT`链,再到`POSRTROUTING`链输出
注意:
规则顺序:匹配即刻停止
![](accents\image-202504130001本.png)
## 四iptables 操作
### 1. 安装
```bash
centos 5/6
启动防火墙:
#/etc/init.d/iptables start
centos 7
安装iptables
[root@wxin ~]# yum -y install iptables iptables-services
关闭firewalld
[root@wxin ~]# systemctl stop firewalld
[root@wxin ~]# systemctl disable firewalld
启动iptables
[root@wxin ~]# systemctl start iptables
查看版本:
[root@wxin ~]# iptables -V
iptables v1.4.21
配置文件:
/etc/sysconfig/iptables-config
/etc/sysconfig/iptables #记录规则文件
```
### 2. 参数解释
```bash
-L:列出一个链或所有链中的规则信息
-n以数字形式显示地址、端口等信息
-v以更详细的方式显示规则信息
--line-numbers查看规则时显示规则的序号方便之处通过需要删除规则-D INPUT 1
-F清空所有的规则-X是清理自定义的链用的少-Z清零规则序号
-D删除链内指定序号或内容的一条规则
-P为指定的链设置默认规则
-A在链的末尾追加一条规则
-I在链的开头或指定序号插入一条规则
-t: 指定表名
.... 更多参数可通过--help查看
```
### 3. 常规操作
```bash
如果不写-t 默认使用filter表
指定表名查看规则
[root@wxin ~]# iptables -t nat -L
默认查看规则:
[root@wxin ~]# iptables -L
以数字的形式显示ip和端口与协议:
[root@wxin ~]# iptables -nL
显示规则行号:
[root@wxin ~]# iptables -nL --line
清空规则:
[root@wxin ~]# iptables -F
清空单独的某一个链里面的规则:
[root@wxin ~]# iptables -F 链名
保存规则:
[root@wxin ~]# service iptables save
[root@wxin ~]# iptables-save > /etc/sysconfig/iptables
[root@wxin ~]# iptables-restore < /etc/sysconfig/iptables
```
### 4. 常见协议
```bash
协议:-p 小p
tcp ---用的最多
udp
icmp ---ping的时候用的协议
#使用协议的时候可以不指定端口,使用端口的时候必须指定协议。
案例:
禁止自己被ping在filter表的INPUT链插入一个丢弃icmp的规则。
[root@wxin ~]# iptables -F
[root@wxin ~]# iptables -A INPUT -p icmp -j REJECT
[root@wxin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
```
### 5. 控制类型
```bash
-j控制类型 通过前面匹配到之后是丢弃还是保留数据包的处理方式:
ACCEPT 允许数据包通过(默认策略)
DROP直接丢弃数据包不给任何回应
REJECT拒绝数据包必要时会给数据发送端一个响应
SNAT源地址转换可以解决內网用户用一个公网IP上网问题 POSTROUTING
DNAT目标地址转换 PREROUTING
REDIRECT做端口映射
LOG写日志
```
### 6. 规则案例
```bash
添加规则:-A
[root@wxin ~]# iptables -t filter -A INPUT -p icmp -j REJECT
[root@wxin ~]# iptables -t filter -A INPUT -p tcp --dport 22 -s 192.168.159.131 -j REJECT
[root@wxin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.131 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
```
插入规则:-I
- 如果不指定插入到第几条,默认插入到第一条
- 插到那默认就是第几条
```bash
[root@wxin ~]# iptables -t filter -I INPUT 2 -p tcp --dport 22 -s 192.168.159.132 -j REJECT
[root@wxin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.132 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.131 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@wxin ~]# iptables -t filter -I INPUT -p tcp --dport 22 -s 192.168.159.133 -j REJECT
[root@wxin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp -- 192.168.159.133 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.132 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.131 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
```
替换(修改)规则:-R
```bash
[root@wxin ~]# iptables -t filter -R INPUT 3 -p tcp --dport 22 -s 192.168.159.134 -j REJECT
[root@wxin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp -- 192.168.159.133 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.134 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.131 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
```
删除规则:-D
```bash
[root@wxin ~]# iptables -t filter -D INPUT -p tcp --dport 22 -s 192.168.159.133 -j REJECT
[root@wxin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.134 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.131 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@wxin ~]# iptables -t filter -D INPUT -p tcp --dport 22 -s 192.168.159.134 -j REJECT
[root@wxin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.131 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
```
修改默认策略:-P 只能用DROP和ACCEPT
```bash
[root@wxin ~]# iptables -t filter -P INPUT DROP
[root@wxin ~]# iptables -t filter -P INPUT ACCEPT
```
添加自定义链:-N 默认不生效 是用来存储规则的
```bash
[root@wxin ~]# iptables -N blackrach 自己创建的链
[root@wxin ~]# iptables -t filter -A blackrach -p tcp --dport 22 -s 192.168.159.133 -j REJECT 往自定义链上添加
[root@wxin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.131 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain blackrach (0 references)
target prot opt source destination
REJECT tcp -- 192.168.159.133 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
[root@wxin ~]# iptables -A INPUT -j blackrach 关联自定义链,使用自定义链
[root@wxin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.131 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
blackrach all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain blackrach (1 references)
target prot opt source destination
REJECT tcp -- 192.168.159.133 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
```
修改自定义链名称:-E
```bash
[root@wxin ~]# iptables -E blackrach blackcloud
[root@wxin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- 192.168.159.131 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
blackcloud all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain blackcloud (1 references)
target prot opt source destination
REJECT tcp -- 192.168.159.133 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
```
删掉自定义规则:不能被关联,必须是空链
```bash
没有关联:
[root@wxin ~]# iptables -X blackcloud
有关联:
# 查找关联规则
[root@wxin ~]# iptables -L INPUT --line-numbers -n
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 REJECT icmp -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
2 REJECT tcp -- 192.168.159.131 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable
3 blackcloud all -- 0.0.0.0/0 0.0.0.0/0
# 删除主链中的跳转规则
[root@wxin ~]# iptables -D INPUT 3
[root@wxin ~]# iptables -L INPUT --line-numbers -n
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 REJECT icmp -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
2 REJECT tcp -- 192.168.159.131 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable
# 清空并删除自定义链
[root@wxin ~]# iptables -X blackcloud
```
### 7. 匹配规则
基本匹配
```bash
协议 -p tcp udp icmp
查端口vim /etc/services 记录的是tcp/udp协议簇
vim /etc/protocols 记录的是icmp
-p tcp udp icmp
端口:使用端口前加协议(-p
--sport源端口
[root@wxin ~]# iptables -A INPUT -p tcp --sport 22 -s 192.168.159.135 -j REJECT
--dport目标端口
[root@wxin ~]# iptables -A INPUT -p tcp --dport 22 -s 192.168.159.136 -j REJECT
IP
-s 源IP source ip地址 网段 逗号可以分开多个地址
-d 目标IP destination
```
扩展规则
```bash
-m 后面+扩展匹配
-m multiport 多端口
[root@wxin ~]# iptables -A INPUT -p tcp -m multiport --source-ports 80,20,22,1000:2000 -j DROP
--dports(--destination-ports)
-m iprange 多ip地址
[root@wxin ~]# iptables -A INPUT -p tcp -m iprange --src-range 192.168.159.138-192.168.159.145 -j REJECT
-m mac
[root@wxin ~]# iptables -A INPUT -m mac --mac-source 00:0c:29:2e:01:0f -j REJECT
获取MAC的方式
[root@wxin ~]# ip link show
```
### 8. 网络地址转换
将公网 80 端口的 HTTP 流量转发到内网服务器 192.168.1.100:80
```bash
# 启用 IP 转发
[root@wxin ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
# NAT 目标地址转换DNAT
[root@wxin ~]# iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:80
# 允许转发流量
[root@wxin ~]# iptables -A FORWARD -p tcp -d 192.168.1.100 --dport 80 -j ACCEPT
# 源地址转换MASQUERADE
[root@wxin ~]# iptables -t nat -A POSTROUTING -p tcp -d 192.168.1.100 --dport 80 -j MASQUERADE
# 开放入站 80 端口(若 INPUT 链默认拒绝)
[root@wxin ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
```
将内网 192.168.159.0/24 的流量通过 eth0 共享公网 IP
```bash
[root@wxin ~]# iptables -t nat -A POSTROUTING -s 192.168.159.0/24 -o eth0 -j SNAT --to-source 192.168.1.5
```