上传文件至 /

This commit is contained in:
wxin 2025-04-13 20:43:38 +08:00
commit 02166837d3
2 changed files with 1027 additions and 0 deletions

564
firewalld.md Normal file
View File

@ -0,0 +1,564 @@
<h2><center>Firewalld</center></h2>
------
## 一Firewalld 简介
`centos 7`中默认将原来的防火墙`iptables`升级为`firewalld``firewalld``iptables`比起来至少有两大好处:
- `firewalld`可以动态修改单条规则,而不需要像`iptables`那样,在修改了规则后必须得全部刷新才可以生效。
- `firewalld`在使用上要比`iptables`人性化很多,即使不明白“四张表五条链”而且对`TCP/IP`协议也不理解也可以实现大部分功能。
### 1. 目录结构
`firewalld`的配置文件以`xml`格式为主(主配置文件`firewalld.conf`例外),有两个存储位置:
- `/etc/firewalld/`
- `/usr/lib/firewalld/`
使用规则:
当需要一个文件时`firewalld`会首先到第一个目录中去查找,如果可以找到,那么就直接使用,否则会继续到第二个目录中查找。
注意:
- 两个目录下的同名配置文件第一个目录内的生效
- 二个目录中存放的是`firewalld`给提供的通用配置文件,如果我们想修改配置, 那么可以`copy`一份到第一个目录中
- 当然也可以直接修改第二个目录下的文件
### 2. 相关命令
查看服务状态
```bash
[root@wxin ~]# systemctl status firewalld
[root@wxin ~]# systemctl status -l firewalld
```
启动关闭服务
```bash
[root@wxin ~]# systemctl start firewalld
[root@wxin ~]# systemctl stop firewalld
[root@wxin ~]# systemctl reload firewalld
[root@wxin ~]# systemctl disable firewalld
```
## 二Firewalld 之 Zone
`firewalld`默认提供了九个`zone`配置文件:`block.xml``dmz.xml``drop.xml``external.xml``home.xml``internal.xml``public.xml``trusted.xml``work.xml`,他们都保存在`/usr/lib /firewalld/zones/`目录下;九个`zone`其实就是九种方案,而且起决定作用的其实是每个`xml`文件所包含的内容,而不是文件名,所以不需要对每种`zone`(每个文件名)的含义花费过多的精力,比如`trusted`这个`zone`会信任所有的数据包,也就是说所有数据包都会放行,但是`public`这个`zone`只会放行其中所配置的服务,其他的一律不予放行,其实我们如果将这两个文件中的内容互换一下他们的规则就换过来了,也就是`public`这个`zone`会放行所有的数据包。
注意:
- 生效的只有默认`zone`里面的规则。
- `trusted.xml``zone``target`,就是因为他设置为了`ACCEPT`,所以才会放行所有的数据包。
- 而`public.xml`中的`zone`没有`target`属性,这样就会默认拒绝通过,所以`public`这个`zone`只有其中配置过的服务才可以通过。
### 1. drop丢弃
任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。
### 2. block限制
任何接收的网络连接都被`IPv4``icmp-host-prohibited`信息和`IPv6``icmp6-adm-prohibited`信息所拒绝。
### 3. public公共
在公共区域内使用,不能相信网络内的其他计算机不会对您的计算机造成危害,只能接收经过选取的连接。
### 4. external外部
是为路由器启用了伪装功能的外部网。不信任来自网络的其他计算,不相信它们不会对您的计算机造成危害,只接收经过选择的连接。
### 5. dmz非军事区
用于您的非军事区内的电脑,此区域内可公开访问,可以有限地进入您的内部网络,仅仅接收经过选择的连接。
### 6. work工作
用于工作区。您可以基本相信网络内的其他电脑不会危害您的电脑。仅仅接收经过选择的连接。
### 7. home家庭
用于家庭网络。您可以基本信任网络内的其他计算机不会危害您的计算机。仅仅接收经过选择的连接。
### 8. internal内部
用于内部网络。您可以基本上信任网络内的其他计算机不会威胁您的计算机。仅仅接受经过选择的连接。
### 9. trusted信任
可接受所有的网络连接。
## 三:规则配置
### 1. 方式
- `firewall-config`(图形方式)
- `firewall-cmd`(命令行方式)
- 直接编辑`xml`文件
### 2. 命令行方式
#### 1. 查看
**查看当前默认区间**
```bash
[root@wxin ~]# firewall-cmd --get-default-zone
public
```
**显示当前正在使用的区域及其对应的网卡接口**
```bash
[root@wxin ~]# firewall-cmd --get-active-zones
public
interfaces: ens33
```
**显示所有可用的区域**
```bash
[root@wxin ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
```
**显示所有区域及其规则**
```bash
[root@wxin ~]# firewall-cmd --list-all-zones
block
target: %%REJECT%%
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
dmz
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
drop
target: DROP
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
external
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh
ports:
protocols:
masquerade: yes
forward-ports:
source-ports:
icmp-blocks:
rich rules:
home
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client mdns samba-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
internal
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client mdns samba-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
trusted
target: ACCEPT
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
work
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
```
**查看默认区域内允许访问的所有服务**
```bash
[root@wxin ~]# firewall-cmd --list-service
dhcpv6-client ssh
```
**查看指定区域允许访问的服务列表**
```bash
firewall-cmd --list-service --zone=区域名
[root@wxin ~]# firewall-cmd --list-service --zone=work
dhcpv6-client ssh
```
**查看指定区域允许访问的端口列表**
```bash
firewall-cmd --list-ports --zone=区域名
[root@wxin ~]# firewall-cmd --list-ports --zone=work
```
**查看与网卡绑定的区域**
```bash
firewall-cmd --get-zone-of-interface=网卡名
[root@wxin ~]# firewall-cmd --get-zone-of-interface=ens33
public
```
**查看所有icmp类型**
```bash
[root@wxin ~]# firewall-cmd --get-icmptypes
address-unreachable bad-header communication-prohibited destination-unreachable echo-reply echo-request fragmentation-needed host-precedence-violation host-prohibited host-redirect host-unknown host-unreachable ip-header-bad neighbour-advertisement neighbour-solicitation network-prohibited network-redirect network-unknown network-unreachable no-route packet-too-big parameter-problem port-unreachable precedence-cutoff protocol-unreachable redirect required-option-missing router-advertisement router-solicitation source-quench source-route-failed time-exceeded timestamp-reply timestamp-request tos-host-redirect tos-host-unreachable tos-network-redirect tos-network-unreachable ttl-zero-during-reassembly ttl-zero-during-transit unknown-header-type unknown-option
```
#### 2. 添加
**给指定区域添加绑定的网卡**
```bash
firewall-cmd --add-interface=网卡名 --zone=区域名
```
**给指定区域添加源地址**
```bash
firewall-cmd --add-source=源地址 --zone=区域名
```
**给指定区域添加允许访问的服务**
```bash
firewall-cmd --add-service=服务名 --zone=区域名
```
**给指定区域添加允许访问的服务列表**
```bash
firewall-cmd --add-service={服务名1,服务名2,...} --zone=区域名
```
**给指定区域添加允许访问的端口**
```bash
firewall-cmd --add-port=端口/协议 --zone=区域名
```
**给指定区域添加允许访问的连续的端口列表**
```bash
firewall-cmd --add-port=端口1-端口2/协议 --zone=区域名
```
**给指定区域添加允许访问的不连续的端口**
```bash
firewall-cmd --add-port={端口1,端口2,...}/协议 --zone=区域名
```
**给指定区域添加拒绝访问的icmp类型**
```bash
firewall-cmd --add-icmp-block=icmp类型 --zone=区域名
```
#### 3. 删除
**根据服务名删除**
```bash
firewall-cmd --remove-service=服务名 --zone=区域名
```
**根据端口/协议删除**
```bash
firewall-cmd --remove-port=端口/协议 --zone=区域名
```
**根据icmp类型删除**
```bash
firewall-cmd --remove-icmp-block=icmp类型 --zone=区域名
```
**从指定区域里删除绑定的网卡**
```bash
firewall-cmd --remove-interface=网卡名 --zone=区域名
```
**从指定区域里删除绑定的源地址**
```bash
firewall-cmd --remove-source=源地址 --zone=区域名
```
#### 4. 修改
**修改当前默认区域**
```bash
firewall-cmd --set-default-zone=区域名
[root@wxin ~]# firewall-cmd --set-default-zone=work
success
[root@wxin ~]# firewall-cmd --get-default-zone
work
```
**修改/添加网卡 绑定给指定区域**
```bash
firewall-cmd --change-interface=网卡名 --zone=区域名
```
**修改/添加源地址 绑定给指定区域**
```bash
firewall-cmd --change-source=源地址 --zone=区域名
```
#### 5.案例
比如我当前的默认zone是public需要开放80端口对外访问则执行如下命令
```bash
[root@wxin ~]# firewall-cmd --zone=public --permanent --add-port=80/tcp
success
[root@wxin ~]# firewall-cmd --reload
success
```
### 3. 设置配置方法
#### 1. 运行时配置
- 实时生效并持续至Firewalld重新启动或重新加载配置
- 不中断现有连接
- 不能修改服务配置
```bash
[root@wxin ~]# firewall-cmd ...
将之前的运行时配置都转换成永久配置
[root@wxin ~]# firewall-cmd --runtime-to-permanent
```
#### 2. 永久配置
- 不立即生效除非Firewalld重新启动或重新加载配置
- 中断现有连接
- 可以修改服务配置
```bash
[root@wxin ~]# firewall-cmd .... --permanent
[root@wxin ~]# firewall-cmd --reload 或 systemctl restart firewalld
```
### 4. 修改配置文件
```bash
以public zone为例对应的配置文件是/etc/firewalld/zones/public.xml像我们刚刚添加80端口后体现在public.xml 中的内容为:
[root@wxin ~]# vim /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="ssh"/>
<service name="dhcpv6-client"/>
<port protocol="tcp" port="80"/>
</zone>
注意在修改配置文件后 --reload 或重启 firewall 服务。
```
## 四:高级规则
### 1. Direct Rules 概述
通过`firewall-cmd`工具,可以使用`--direct`选项在运行时间里增加和删除链。如果不熟悉`iptables`,使用直接接口非常危险,因为可能无意间导致防火墙被入侵。直接端口模式适用于服务或者程序,以便于在运行时间内增加特定的防火墙规则。直接端口模式添加的规则优先应用。
```bash
Direct Options
The direct options give a more direct access to the firewall. These options
require user to know basic iptables concepts, i.e. table
(filter/mangle/nat/...), chain (INPUT/OUTPUT/FORWARD/...), commands
(-A/-D/-I/...), parameters (-p/-s/-d/-j/...) and targets
(ACCEPT/DROP/REJECT/...).
Direct options should be used only as a last resort when it's not possible to
use for example --add-service=service or --add-rich-rule='rule'.
The first argument of each option has to be ipv4 or ipv6 or eb. With ipv4 it
will be for IPv4 (iptables(8)), with ipv6 for IPv6 (ip6tables(8)) and with eb
for ethernet bridges (ebtables(8)).
[--permanent] --direct --get-all-chains
Get all chains added to all tables. This option concerns only chains
previously added with --direct --add-chain.
[--permanent] --direct --get-chains { ipv4 | ipv6 | eb } table
```
查看防火墙上设置的规则
```bash
[root@wxin ~]# firewall-cmd --direct --get-all-rules
```
添加高级规则
```bash
[root@wxin ~]# firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.77 -j ACCEPT
```
注意:
只允许172.25.254.77通过80端口访问主机位为110的http服务。因为访问的是主机位为110的http服务需要主机位为110的内核同意开启http服务需要在表filter中设置INPUT-p 数据包类型;--dport 服务端口。
删除防火墙上的规则
```bash
[root@wxin ~]# firewall-cmd --permanent --direct --remove-rule ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.217 -j REJECT
```
注意:
移除不允许172.25.254.217通过22端口的访问ssh连接172.25.254.110这条规则
### 2. 端口转发
端口转发可以将指定地址访问指定的端口时,将流量转发至指定地址的指定端口。转发的目的如果不指定 ip 的话就默认为本机,如果指定了 ip 却没指定端口,则默认使用来源端口。 如果配置好端口转发之后不能用,可以检查下面两个问题。
比如我将 80 端口转发至 8080 端口,首先检查本地的 80 端口和目标的 8080 端口是否开放监听了。
其次检查是否允许伪装 IP没允许的话要开启伪装 IP
案例
```bash
# 将80端口的流量转发至8080
[root@wxin ~]# firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080
success
# 将80端口的流量转发至192.168.0.1
[root@wxin ~]# firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1
success
# 将80端口的流量转发至192.168.0.1的8080端口
[root@wxin ~]# firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1:toport=8080
success
```
开启`IP`伪装
```bash
查看:
[root@wxin ~]# firewall-cmd --query-masquerade
开启:
[root@wxin ~]# firewall-cmd --add-masquerade
success
关闭:
[root@wxin ~]# firewall-cmd --remove-masquerade
success
```
作用:
- 当我们想把某个端口隐藏起来的时候,就可以在防火墙上阻止那个端口访问,然后再开一个不规则的端口,之后配置防火墙的端口转发,将流量转发过去。
- 端口转发还可以做流量分发,一个防火墙拖着好多台运行着不同服务的机器,然后用防火墙将不同端口的流量转发至不同机器。

463
iptables.md Normal file
View File

@ -0,0 +1,463 @@
<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
```