diff --git a/iptables.md b/iptables.md index 7c44f83..5a86200 100644 --- a/iptables.md +++ b/iptables.md @@ -1,463 +1,463 @@ -

Iptables

- ------- - -## 一:防火墙介绍 - -### 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 -``` - +

Iptables

+ +------ + +## 一:防火墙介绍 + +### 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`链输出 + +注意: + +​ 规则顺序:匹配即刻停止 + +![](http://182.92.143.66:40072/directlink/img/firewall/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 +``` +