6.1 扩展匹配模块概述

模块加载机制

iptables 通过可加载模块来扩展匹配功能。这些模块提供了比基本匹配更强大和灵活的条件判断能力。

模块使用语法

# 基本语法
iptables -A chain -m module_name --module-option value -j target

# 示例
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m limit --limit 10/min -j ACCEPT

查看可用模块

# 查看已加载的模块
lsmod | grep xt_
lsmod | grep ipt_
lsmod | grep ip6t_

# 查看模块帮助
iptables -m module_name --help

# 示例
iptables -m state --help
iptables -m limit --help
iptables -m recent --help

6.2 连接状态匹配

state 模块(传统)

连接状态类型

# NEW:新连接
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT

# ESTABLISHED:已建立的连接
iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT

# RELATED:相关连接(如 FTP 数据连接)
iptables -A INPUT -m state --state RELATED -j ACCEPT

# INVALID:无效连接
iptables -A INPUT -m state --state INVALID -j DROP

# 组合状态
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state NEW,ESTABLISHED -j ACCEPT

实际应用示例

#!/bin/bash
# 基于连接状态的防火墙配置

# 清空规则
iptables -F

# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 允许本地回环
iptables -A INPUT -i lo -j ACCEPT

# 允许已建立和相关的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 丢弃无效连接
iptables -A INPUT -m state --state INVALID -j DROP

# 只允许新的 SSH 连接从管理网段
iptables -A INPUT -m state --state NEW -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT

# 只允许新的 HTTP/HTTPS 连接
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT

# 默认拒绝
iptables -A INPUT -j DROP

conntrack 模块(推荐)

基本用法

# 连接状态匹配(推荐使用 conntrack 替代 state)
iptables -A INPUT -m conntrack --ctstate NEW -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m conntrack --ctstate INVALID -j DROP

高级连接跟踪

# 连接方向
iptables -A INPUT -m conntrack --ctdir ORIGINAL -j ACCEPT     # 原始方向
iptables -A INPUT -m conntrack --ctdir REPLY -j ACCEPT        # 回复方向

# 连接状态和方向组合
iptables -A INPUT -m conntrack --ctstate ESTABLISHED --ctdir REPLY -j ACCEPT

# 连接原始源地址
iptables -A INPUT -m conntrack --ctorigdst 192.168.1.10 -j ACCEPT
iptables -A INPUT -m conntrack --ctorigsrc 192.168.1.0/24 -j ACCEPT

# 连接回复源地址
iptables -A INPUT -m conntrack --ctreplsrc 8.8.8.8 -j ACCEPT
iptables -A INPUT -m conntrack --ctrepldst 192.168.1.10 -j ACCEPT

连接状态详细说明

# DNAT 后的连接跟踪示例
#!/bin/bash

# 假设有 DNAT 规则:外网 80 端口映射到内网 192.168.1.10:8080
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.10:8080

# 使用 conntrack 精确控制
iptables -A FORWARD -m conntrack --ctstate NEW --ctorigdst 192.168.1.10 --ctorigdstport 8080 -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

连接跟踪优化

跳过连接跟踪

# 对于高流量的静态内容,跳过连接跟踪以提高性能
iptables -t raw -A PREROUTING -p tcp --dport 80 -m string --string "GET /static/" --algo bm -j NOTRACK
iptables -t raw -A OUTPUT -p tcp --sport 80 -m string --string "HTTP/1.1 200 OK" --algo bm -j NOTRACK

# 跳过本地回环的连接跟踪
iptables -t raw -A PREROUTING -i lo -j NOTRACK
iptables -t raw -A OUTPUT -o lo -j NOTRACK

连接跟踪表调优

# 增加连接跟踪表大小
echo 'net.netfilter.nf_conntrack_max = 1048576' >> /etc/sysctl.conf

# 调整超时时间
echo 'net.netfilter.nf_conntrack_tcp_timeout_established = 3600' >> /etc/sysctl.conf
echo 'net.netfilter.nf_conntrack_tcp_timeout_time_wait = 30' >> /etc/sysctl.conf
echo 'net.netfilter.nf_conntrack_tcp_timeout_close_wait = 30' >> /etc/sysctl.conf

# 应用配置
sysctl -p

6.3 速率限制和流量控制

limit 模块

基本速率限制

# 基本限速语法
iptables -A INPUT -m limit --limit rate[/second|/minute|/hour|/day] -j ACCEPT

# 每秒限制
iptables -A INPUT -p icmp -m limit --limit 1/s -j ACCEPT
iptables -A INPUT -p icmp -j DROP

# 每分钟限制
iptables -A INPUT -p tcp --dport 22 -m limit --limit 5/m -j ACCEPT

# 每小时限制
iptables -A INPUT -p tcp --dport 80 -m limit --limit 1000/h -j ACCEPT

突发流量控制

# 突发限制(burst)
iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 5 -j ACCEPT
iptables -A INPUT -p icmp -j DROP

# 解释:允许突发 5 个包,然后限制为每秒 1 个

# SSH 连接限制(防暴力破解)
iptables -A INPUT -p tcp --dport 22 -m limit --limit 3/m --limit-burst 3 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP

日志限制

# 限制日志频率,避免日志洪水
iptables -A INPUT -m limit --limit 5/m --limit-burst 10 -j LOG --log-prefix "[FIREWALL-DENIED]: "
iptables -A INPUT -j DROP

# 针对特定服务的日志限制
iptables -A INPUT -p tcp --dport 80 -m limit --limit 10/m -j LOG --log-prefix "[HTTP-ACCESS]: "
iptables -A INPUT -p tcp --dport 443 -m limit --limit 10/m -j LOG --log-prefix "[HTTPS-ACCESS]: "

hashlimit 模块

基于源 IP 的限制

# 每个源 IP 独立限制
iptables -A INPUT -p tcp --dport 80 -m hashlimit --hashlimit-above 10/min --hashlimit-mode srcip --hashlimit-name http -j DROP

# 每个源 IP 的 SSH 连接限制
iptables -A INPUT -p tcp --dport 22 -m hashlimit --hashlimit-above 3/min --hashlimit-mode srcip --hashlimit-name ssh -j DROP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

基于目标端口的限制

# 每个目标端口的限制
iptables -A INPUT -p tcp -m hashlimit --hashlimit-above 100/sec --hashlimit-mode dstport --hashlimit-name port_limit -j DROP

组合条件限制

# 基于源 IP 和目标端口的组合限制
iptables -A INPUT -p tcp --dport 80 -m hashlimit --hashlimit-above 20/min --hashlimit-mode srcip,dstport --hashlimit-name web_limit -j DROP

# 基于源网段的限制
iptables -A INPUT -p tcp --dport 443 -m hashlimit --hashlimit-above 50/min --hashlimit-mode srcip --hashlimit-srcmask 24 --hashlimit-name https_subnet -j DROP

connlimit 模块

连接数限制

# 限制每个 IP 的并发连接数
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 10 -j DROP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 限制每个网段的连接数
iptables -A INPUT -p tcp --dport 443 -m connlimit --connlimit-above 50 --connlimit-mask 24 -j DROP

# 严格的数据库连接限制
iptables -A INPUT -p tcp --dport 3306 -m connlimit --connlimit-above 5 --connlimit-mask 32 -j DROP
iptables -A INPUT -p tcp --dport 3306 -s 192.168.1.0/24 -j ACCEPT

基于源地址的连接限制

# 不同网段不同的连接限制
iptables -A INPUT -p tcp --dport 80 -s 192.168.1.0/24 -m connlimit --connlimit-above 20 -j DROP
iptables -A INPUT -p tcp --dport 80 -s 10.0.0.0/8 -m connlimit --connlimit-above 5 -j DROP
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 2 -j DROP

6.4 时间和日期匹配

time 模块

时间段控制

# 工作时间访问控制
iptables -A INPUT -p tcp --dport 22 -m time --timestart 09:00 --timestop 18:00 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP

# 非工作时间限制
iptables -A INPUT -p tcp --dport 80 -m time --timestart 18:00 --timestop 09:00 -j DROP

# 24小时制时间
iptables -A INPUT -p tcp --dport 443 -m time --timestart 08:30 --timestop 17:30 -j ACCEPT

星期控制

# 工作日访问
iptables -A INPUT -p tcp --dport 22 -m time --weekdays Mon,Tue,Wed,Thu,Fri -j ACCEPT

# 周末限制
iptables -A INPUT -p tcp --dport 80 -m time --weekdays Sat,Sun -j DROP

# 特定日期访问
iptables -A INPUT -p tcp --dport 443 -m time --weekdays Mon,Wed,Fri -j ACCEPT

日期范围控制

# 年度访问控制
iptables -A INPUT -p tcp --dport 80 -m time --datestart 2024-01-01 --datestop 2024-12-31 -j ACCEPT

# 月度访问控制
iptables -A INPUT -p tcp --dport 443 -m time --datestart 2024-06-01 --datestop 2024-06-30 -j DROP

# 临时访问控制
iptables -A INPUT -p tcp --dport 8080 -m time --datestart 2024-03-15 --datestop 2024-03-20 -j ACCEPT

组合时间条件

# 工作时间和工作日组合
iptables -A INPUT -p tcp --dport 22 -m time --timestart 09:00 --timestop 18:00 --weekdays Mon,Tue,Wed,Thu,Fri -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP

# 复杂时间控制
iptables -A INPUT -p tcp --dport 80 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri,Sat -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -m time --timestart 10:00 --timestop 18:00 --weekdays Sun -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j DROP

时区考虑

# 使用 UTC 时间
iptables -A INPUT -p tcp --dport 22 -m time --timestart 01:00 --timestop 10:00 --utc -j ACCEPT

# 本地时间(默认)
iptables -A INPUT -p tcp --dport 22 -m time --timestart 09:00 --timestop 18:00 --localtz -j ACCEPT

实际应用场景

办公环境访问控制

#!/bin/bash
# 办公环境时间访问控制

# 工作时间允许所有访问
iptables -A INPUT -m time --timestart 08:00 --timestop 19:00 --weekdays Mon,Tue,Wed,Thu,Fri -j ACCEPT

# 非工作时间只允许紧急访问
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j ACCEPT  # 管理员 SSH
iptables -A INPUT -p tcp --dport 443 -j ACCEPT                  # HTTPS 访问

# 周末限制访问
iptables -A INPUT -m time --weekdays Sat,Sun -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT
iptables -A INPUT -m time --weekdays Sat,Sun -j DROP

学校网络管理

#!/bin/bash
# 学校网络时间管理

# 上课时间限制娱乐网站
iptables -A FORWARD -m time --timestart 08:00 --timestop 12:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 80 -m string --string "game" --algo bm -j DROP
iptables -A FORWARD -m time --timestart 14:00 --timestop 18:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 80 -m string --string "video" --algo bm -j DROP

# 晚上限制网络访问
iptables -A FORWARD -m time --timestart 22:00 --timestop 06:00 -s 192.168.100.0/24 -j DROP

# 周末相对宽松
iptables -A FORWARD -m time --weekdays Sat,Sun -j ACCEPT

6.5 字符串和内容匹配

string 模块

基本字符串匹配

# HTTP 请求内容过滤
iptables -A INPUT -p tcp --dport 80 -m string --string "GET /admin" --algo bm -j DROP
iptables -A INPUT -p tcp --dport 80 -m string --string "POST /login" --algo bm -j LOG --log-prefix "[LOGIN-ATTEMPT]: "

# User-Agent 过滤
iptables -A INPUT -p tcp --dport 80 -m string --string "User-Agent: BadBot" --algo bm -j DROP
iptables -A INPUT -p tcp --dport 80 -m string --string "User-Agent: Scanner" --algo bm -j DROP

算法选择

# Boyer-Moore 算法(适合短字符串)
iptables -A INPUT -p tcp --dport 80 -m string --string "malware" --algo bm -j DROP

# Knuth-Morris-Pratt 算法(适合长字符串)
iptables -A INPUT -p tcp --dport 80 -m string --string "very-long-malicious-signature" --algo kmp -j DROP

大小写和编码

# 忽略大小写
iptables -A INPUT -p tcp --dport 80 -m string --string "admin" --algo bm --icase -j DROP

# 十六进制字符串
iptables -A INPUT -p tcp --dport 80 -m string --hex-string "|47 45 54 20 2f 61 64 6d 69 6e|" --algo bm -j DROP
# 对应 "GET /admin"

安全防护应用

SQL 注入防护

#!/bin/bash
# SQL 注入防护规则

# 常见 SQL 注入模式
iptables -A INPUT -p tcp --dport 80 -m string --string "union select" --algo bm --icase -j DROP
iptables -A INPUT -p tcp --dport 80 -m string --string "' or 1=1" --algo bm -j DROP
iptables -A INPUT -p tcp --dport 80 -m string --string "'; drop table" --algo bm --icase -j DROP
iptables -A INPUT -p tcp --dport 80 -m string --string "<script" --algo bm --icase -j DROP
iptables -A INPUT -p tcp --dport 80 -m string --string "javascript:" --algo bm --icase -j DROP

# 记录可疑请求
iptables -A INPUT -p tcp --dport 80 -m string --string "../" --algo bm -j LOG --log-prefix "[PATH-TRAVERSAL]: "
iptables -A INPUT -p tcp --dport 80 -m string --string "../" --algo bm -j DROP

恶意软件通信阻断

#!/bin/bash
# 恶意软件通信特征阻断

# 阻断已知恶意软件通信
iptables -A OUTPUT -p tcp -m string --string "malware-c2-domain.com" --algo bm -j DROP
iptables -A OUTPUT -p tcp -m string --string "botnet-command" --algo bm -j DROP

# 阻断可疑的 HTTP 头
iptables -A INPUT -p tcp --dport 80 -m string --string "X-Forwarded-For: 127.0.0.1" --algo bm -j DROP
iptables -A INPUT -p tcp --dport 80 -m string --string "User-Agent: curl" --algo bm -j LOG --log-prefix "[CURL-ACCESS]: "

内容过滤

#!/bin/bash
# 企业内容过滤

# 阻断社交媒体
iptables -A FORWARD -p tcp --dport 80 -m string --string "facebook.com" --algo bm -j DROP
iptables -A FORWARD -p tcp --dport 80 -m string --string "twitter.com" --algo bm -j DROP
iptables -A FORWARD -p tcp --dport 443 -m string --string "instagram.com" --algo bm -j DROP

# 阻断在线游戏
iptables -A FORWARD -p tcp -m string --string "steam" --algo bm --icase -j DROP
iptables -A FORWARD -p tcp -m string --string "game" --algo bm --icase -j DROP

# 工作时间限制
iptables -A FORWARD -m time --timestart 09:00 --timestop 18:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 80 -m string --string "youtube.com" --algo bm -j DROP

6.6 网络地址和端口匹配

iprange 模块

IP 地址范围匹配

# 源地址范围
iptables -A INPUT -m iprange --src-range 192.168.1.10-192.168.1.50 -j ACCEPT

# 目标地址范围
iptables -A OUTPUT -m iprange --dst-range 8.8.8.8-8.8.4.4 -j ACCEPT

# 组合源和目标范围
iptables -A FORWARD -m iprange --src-range 192.168.1.1-192.168.1.100 --dst-range 10.0.0.1-10.0.0.100 -j ACCEPT

排除地址范围

# 排除特定范围
iptables -A INPUT -m iprange ! --src-range 192.168.1.100-192.168.1.200 -j ACCEPT

# 允许除了特定范围外的所有地址
iptables -A INPUT -m iprange ! --src-range 10.0.0.1-10.0.0.255 -j ACCEPT

multiport 模块

多端口匹配

# 多个源端口
iptables -A INPUT -p tcp -m multiport --sports 80,443,8080,8443 -j ACCEPT

# 多个目标端口
iptables -A INPUT -p tcp -m multiport --dports 22,80,443 -j ACCEPT

# 源端口和目标端口(任一匹配)
iptables -A INPUT -p tcp -m multiport --ports 22,80,443,3306 -j ACCEPT

端口范围组合

# 端口和端口范围组合
iptables -A INPUT -p tcp -m multiport --dports 22,80,443,8000:8999 -j ACCEPT

# UDP 多端口
iptables -A INPUT -p udp -m multiport --dports 53,67,68,123 -j ACCEPT

# 复杂端口组合
iptables -A INPUT -p tcp -m multiport --dports 21,22,25,53,80,110,143,443,993,995 -j ACCEPT

实际应用示例

Web 服务器端口管理

#!/bin/bash
# Web 服务器多端口管理

# 标准 Web 端口
iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT

# 开发和测试端口
iptables -A INPUT -p tcp -m multiport --dports 8000,8080,8443,9000 -s 192.168.1.0/24 -j ACCEPT

# 管理端口(限制源地址)
iptables -A INPUT -p tcp -m multiport --dports 22,3000,9090 -s 192.168.1.100 -j ACCEPT

# 数据库端口(严格限制)
iptables -A INPUT -p tcp -m multiport --dports 3306,5432,6379 -m iprange --src-range 192.168.1.10-192.168.1.20 -j ACCEPT

企业网络分段管理

#!/bin/bash
# 企业网络分段访问控制

# 管理网段(192.168.1.0/24)可以访问所有服务
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT

# 开发网段(192.168.10.0/24)只能访问开发相关端口
iptables -A INPUT -s 192.168.10.0/24 -p tcp -m multiport --dports 22,80,443,3000,8080 -j ACCEPT

# 生产网段(192.168.20.0/24)只能访问生产端口
iptables -A INPUT -s 192.168.20.0/24 -p tcp -m multiport --dports 80,443 -j ACCEPT

# 数据库网段(192.168.30.0/24)
iptables -A INPUT -m iprange --src-range 192.168.30.10-192.168.30.50 -p tcp -m multiport --dports 3306,5432 -j ACCEPT

# 办公网段(192.168.100.0/24)基本访问
iptables -A INPUT -s 192.168.100.0/24 -p tcp -m multiport --dports 22,80,443 -j ACCEPT

6.7 用户和进程匹配

owner 模块

用户 ID 匹配

# 特定用户的出站流量控制
iptables -A OUTPUT -m owner --uid-owner 1000 -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -m owner --uid-owner 1000 -p tcp --dport 443 -j ACCEPT
iptables -A OUTPUT -m owner --uid-owner 1000 -j DROP

# 禁止 root 用户发送邮件
iptables -A OUTPUT -m owner --uid-owner 0 -p tcp --dport 25 -j DROP

# 允许特定用户访问数据库
iptables -A OUTPUT -m owner --uid-owner 1001 -p tcp --dport 3306 -j ACCEPT

组 ID 匹配

# 特定组的网络访问
iptables -A OUTPUT -m owner --gid-owner 100 -p tcp --dport 80 -j ACCEPT

# 数据库管理组
iptables -A OUTPUT -m owner --gid-owner 200 -p tcp --dport 3306 -j ACCEPT
iptables -A OUTPUT -m owner --gid-owner 200 -p tcp --dport 5432 -j ACCEPT

进程匹配

# 进程 ID 匹配
iptables -A OUTPUT -m owner --pid-owner 1234 -j ACCEPT

# 进程名匹配
iptables -A OUTPUT -m owner --cmd-owner firefox -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -m owner --cmd-owner chrome -p tcp --dport 443 -j ACCEPT

# 禁止特定程序访问网络
iptables -A OUTPUT -m owner --cmd-owner torrent -j DROP
iptables -A OUTPUT -m owner --cmd-owner p2p -j DROP

组合条件

# 用户和进程组合
iptables -A OUTPUT -m owner --uid-owner 1000 --cmd-owner firefox -p tcp --dport 80 -j ACCEPT

# 用户和组组合
iptables -A OUTPUT -m owner --uid-owner 1000 --gid-owner 100 -j ACCEPT

实际应用场景

桌面环境用户控制

#!/bin/bash
# 桌面环境用户网络控制

# 普通用户只能访问 HTTP/HTTPS
iptables -A OUTPUT -m owner --uid-owner 1000 -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -m owner --uid-owner 1000 -p tcp --dport 443 -j ACCEPT
iptables -A OUTPUT -m owner --uid-owner 1000 -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -m owner --uid-owner 1000 -j DROP

# 开发用户可以访问更多端口
iptables -A OUTPUT -m owner --uid-owner 1001 -p tcp -m multiport --dports 22,80,443,3000,8080 -j ACCEPT
iptables -A OUTPUT -m owner --uid-owner 1001 -p udp --dport 53 -j ACCEPT

# 管理员用户不受限制
iptables -A OUTPUT -m owner --uid-owner 0 -j ACCEPT

服务器进程控制

#!/bin/bash
# 服务器进程网络控制

# Web 服务器进程
iptables -A OUTPUT -m owner --cmd-owner apache2 -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -m owner --cmd-owner apache2 -p tcp --dport 443 -j ACCEPT
iptables -A OUTPUT -m owner --cmd-owner apache2 -p udp --dport 53 -j ACCEPT

# 数据库进程
iptables -A OUTPUT -m owner --cmd-owner mysqld -p tcp --dport 3306 -j ACCEPT
iptables -A OUTPUT -m owner --cmd-owner mysqld -p udp --dport 53 -j ACCEPT

# 邮件服务器
iptables -A OUTPUT -m owner --cmd-owner postfix -p tcp --dport 25 -j ACCEPT
iptables -A OUTPUT -m owner --cmd-owner postfix -p tcp --dport 587 -j ACCEPT

# 禁止其他进程的出站连接
iptables -A OUTPUT -j DROP

6.8 最近访问记录

recent 模块

基本用法

# 记录访问的 IP 地址
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --set

# 检查最近的访问记录
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --rcheck --seconds 60 --hitcount 3 -j DROP

# 更新访问记录
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --update --seconds 60 --hitcount 3 -j DROP

# 移除访问记录
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --remove

防暴力破解

#!/bin/bash
# SSH 防暴力破解完整配置

# 记录 SSH 连接尝试
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --set

# 5分钟内超过5次尝试则封禁
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --rcheck --seconds 300 --hitcount 5 -j DROP

# 正常 SSH 连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

多级防护

#!/bin/bash
# 多级 SSH 防护

# 第一级:1分钟内3次尝试,封禁5分钟
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH_LEVEL1 --set
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH_LEVEL1 --rcheck --seconds 60 --hitcount 3 -m recent --name SSH_BAN1 --set -j DROP
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH_BAN1 --rcheck --seconds 300 -j DROP

# 第二级:10分钟内10次尝试,封禁1小时
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH_LEVEL2 --set
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH_LEVEL2 --rcheck --seconds 600 --hitcount 10 -m recent --name SSH_BAN2 --set -j DROP
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH_BAN2 --rcheck --seconds 3600 -j DROP

# 第三级:1小时内50次尝试,永久封禁
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH_LEVEL3 --set
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH_LEVEL3 --rcheck --seconds 3600 --hitcount 50 -m recent --name SSH_PERMANENT --set -j DROP
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH_PERMANENT --rcheck -j DROP

# 正常连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Web 服务防护

#!/bin/bash
# Web 服务防护

# HTTP 请求频率限制
iptables -A INPUT -p tcp --dport 80 -m recent --name HTTP --set
iptables -A INPUT -p tcp --dport 80 -m recent --name HTTP --rcheck --seconds 60 --hitcount 100 -j DROP

# HTTPS 请求频率限制
iptables -A INPUT -p tcp --dport 443 -m recent --name HTTPS --set
iptables -A INPUT -p tcp --dport 443 -m recent --name HTTPS --rcheck --seconds 60 --hitcount 50 -j DROP

# 管理页面特殊保护
iptables -A INPUT -p tcp --dport 80 -m string --string "GET /admin" --algo bm -m recent --name ADMIN --set
iptables -A INPUT -p tcp --dport 80 -m string --string "GET /admin" --algo bm -m recent --name ADMIN --rcheck --seconds 300 --hitcount 3 -j DROP

recent 模块管理

查看和管理记录

# 查看 recent 记录
cat /proc/net/xt_recent/SSH
cat /proc/net/xt_recent/HTTP

# 手动清除记录
echo / > /proc/net/xt_recent/SSH
echo / > /proc/net/xt_recent/HTTP

# 手动添加 IP 到黑名单
echo +192.168.1.100 > /proc/net/xt_recent/SSH

# 手动移除 IP
echo -192.168.1.100 > /proc/net/xt_recent/SSH

配置 recent 模块参数

# 修改 recent 模块参数
echo 'options xt_recent ip_list_tot=1000' > /etc/modprobe.d/xt_recent.conf
echo 'options xt_recent ip_pkt_list_tot=20' >> /etc/modprobe.d/xt_recent.conf

# 重新加载模块
modprobe -r xt_recent
modprobe xt_recent

6.9 高级匹配组合应用

综合防护策略

#!/bin/bash
# 综合高级匹配防护策略

# 清空现有规则
iptables -F
iptables -t nat -F
iptables -t mangle -F

# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# === 基础规则 ===
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m conntrack --ctstate INVALID -j DROP

# === 防攻击规则 ===
# 防止端口扫描
iptables -A INPUT -m recent --name portscan --rcheck --seconds 86400 -j DROP
iptables -A INPUT -m recent --name portscan --set -j LOG --log-prefix "[PORT-SCAN]: "
iptables -A INPUT -m recent --name portscan --set -j DROP

# 防止 SYN 洪水
iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT
iptables -A INPUT -p tcp --syn -j DROP

# === SSH 防护 ===
# 工作时间允许内网 SSH
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -j ACCEPT

# 非工作时间只允许管理员
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j ACCEPT

# SSH 防暴力破解
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --set
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --rcheck --seconds 300 --hitcount 5 -j DROP

# === Web 服务防护 ===
# 基本 Web 访问
iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT

# 限制每个 IP 的连接数
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 20 -j DROP
iptables -A INPUT -p tcp --dport 443 -m connlimit --connlimit-above 10 -j DROP

# 防止 HTTP 洪水攻击
iptables -A INPUT -p tcp --dport 80 -m hashlimit --hashlimit-above 50/min --hashlimit-mode srcip --hashlimit-name http_flood -j DROP

# 内容过滤
iptables -A INPUT -p tcp --dport 80 -m string --string "union select" --algo bm --icase -j DROP
iptables -A INPUT -p tcp --dport 80 -m string --string "<script" --algo bm --icase -j DROP

# === 数据库防护 ===
# 只允许应用服务器访问数据库
iptables -A INPUT -p tcp --dport 3306 -m iprange --src-range 192.168.1.10-192.168.1.20 -m connlimit --connlimit-above 10 -j DROP
iptables -A INPUT -p tcp --dport 3306 -m iprange --src-range 192.168.1.10-192.168.1.20 -j ACCEPT

# === ICMP 控制 ===
# 限制 ping 频率
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 3 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP

# 允许其他必要的 ICMP
iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT

# === 日志记录 ===
# 限制日志频率
iptables -A INPUT -m limit --limit 5/m --limit-burst 10 -j LOG --log-prefix "[FIREWALL-DENIED]: "

# === 默认拒绝 ===
iptables -A INPUT -j DROP

企业级防护配置

#!/bin/bash
# 企业级综合防护配置

# === 网络分段访问控制 ===
# 管理网段(192.168.1.0/24)
iptables -A INPUT -s 192.168.1.0/24 -m time --timestart 08:00 --timestop 18:00 --weekdays Mon,Tue,Wed,Thu,Fri -j ACCEPT
iptables -A INPUT -s 192.168.1.100 -j ACCEPT  # 管理员 24/7 访问

# 开发网段(192.168.10.0/24)
iptables -A INPUT -s 192.168.10.0/24 -p tcp -m multiport --dports 22,80,443,3000,8080 -m time --timestart 07:00 --timestop 22:00 -j ACCEPT

# 生产网段(192.168.20.0/24)
iptables -A INPUT -s 192.168.20.0/24 -p tcp -m multiport --dports 80,443 -j ACCEPT

# 数据库网段(192.168.30.0/24)
iptables -A INPUT -s 192.168.30.0/24 -p tcp -m multiport --dports 3306,5432 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j DROP
iptables -A INPUT -s 192.168.30.0/24 -p tcp -m multiport --dports 3306,5432 -j ACCEPT

# === 用户访问控制 ===
# 限制特定用户的出站访问
iptables -A OUTPUT -m owner --uid-owner 1000 -p tcp -m multiport --dports 80,443 -m time --timestart 09:00 --timestop 18:00 --weekdays Mon,Tue,Wed,Thu,Fri -j ACCEPT
iptables -A OUTPUT -m owner --uid-owner 1000 -p tcp -m multiport --dports 22,3306 -j DROP

# === 内容过滤和安全 ===
# 阻止恶意内容
iptables -A INPUT -p tcp --dport 80 -m string --string "eval(" --algo bm --icase -j DROP
iptables -A INPUT -p tcp --dport 80 -m string --string "base64_decode" --algo bm --icase -j DROP
iptables -A INPUT -p tcp --dport 80 -m string --string "system(" --algo bm --icase -j DROP

# === 高级速率限制 ===
# 基于源 IP 的精细化限制
iptables -A INPUT -p tcp --dport 80 -m hashlimit --hashlimit-above 100/min --hashlimit-mode srcip --hashlimit-name web_normal -j DROP
iptables -A INPUT -p tcp --dport 80 -m hashlimit --hashlimit-above 10/min --hashlimit-mode srcip,dstport --hashlimit-name web_strict -j DROP

# === 地理位置限制(需要 geoip 模块)===
# iptables -A INPUT -p tcp --dport 22 -m geoip --src-cc CN,US,JP -j ACCEPT
# iptables -A INPUT -p tcp --dport 22 -j DROP

# === 监控和告警 ===
# 记录重要事件
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH_SUCCESS --set -j LOG --log-prefix "[SSH-SUCCESS]: "
iptables -A INPUT -p tcp --dport 80 -m string --string "admin" --algo bm -j LOG --log-prefix "[ADMIN-ACCESS]: "

本章小结

本章深入介绍了 iptables 的高级匹配条件:

  1. 连接状态匹配:state 和 conntrack 模块的使用
  2. 速率限制:limit、hashlimit、connlimit 模块的应用
  3. 时间控制:time 模块的时间和日期匹配
  4. 内容匹配:string 模块的字符串过滤
  5. 地址端口匹配:iprange 和 multiport 模块
  6. 用户进程匹配:owner 模块的应用
  7. 访问记录:recent 模块的防护应用
  8. 综合应用:多种匹配条件的组合使用

本章练习

  1. 基础练习

    • 配置基于时间的访问控制
    • 实现连接数限制
    • 设置速率限制规则
  2. 进阶练习

    • 实现多级防暴力破解
    • 配置内容过滤规则
    • 设计用户访问控制策略
  3. 实战练习

    • 为企业网络设计分段访问控制
    • 实现 Web 应用安全防护
    • 配置数据库访问安全策略
  4. 综合练习

    • 设计完整的企业级防火墙策略
    • 实现多层安全防护
    • 优化规则性能和安全性

下一章我们将学习 NAT 和端口转发的配置方法。