一、Kali Linux 中的防火墙工具
ufw (Uncomplicated Firewall):简化的防火墙配置工具
iptables:底层防火墙工具,功能强大但配置复杂
nftables:新一代防火墙工具,iptables的替代品
二、ufw 配置
2.1 安装 ufw
Kali Linux 默认已安装 ufw:
# 检查是否安装
ufw --version
# 如果未安装,执行
apt update && apt install ufw -y
2.2 基本配置
2.2.1 启用/禁用 ufw
# 启用防火墙
sudo ufw enable
# 禁用防火墙
sudo ufw disable
# 查看状态
sudo ufw status
sudo ufw status verbose
2.2.2 默认策略
# 默认拒绝所有入站流量,允许所有出站流量
sudo ufw default deny incoming
sudo ufw default allow outgoing
2.2.3 允许特定端口
# 允许 SSH (22)
sudo ufw allow 22/tcp
# 允许 HTTP (80)
sudo ufw allow 80/tcp
# 允许 HTTPS (443)
sudo ufw allow 443/tcp
# 允许多个端口
sudo ufw allow 8000:9000/tcp
2.2.4 允许特定服务
# 允许 SSH 服务
sudo ufw allow ssh
# 允许 HTTP 服务
sudo ufw allow http
# 允许 HTTPS 服务
sudo ufw allow https
2.2.5 允许特定 IP 地址
# 允许特定 IP 地址的所有流量
sudo ufw allow from 192.168.1.100
# 允许特定 IP 地址访问特定端口
sudo ufw allow from 192.168.1.100 to any port 22
2.2.6 拒绝规则
# 拒绝特定 IP 地址
sudo ufw deny from 192.168.1.200
# 拒绝特定端口
sudo ufw deny 3389/tcp
2.2.7 删除规则
# 查看所有规则及其编号
sudo ufw status numbered
# 删除特定编号的规则
sudo ufw delete 3
2.2.8 高级规则
# 允许特定网络访问特定端口
sudo ufw allow from 192.168.1.0/24 to any port 22
# 允许特定接口的流量
sudo ufw allow in on eth0 to any port 80
# 限制连接速率(防止暴力破解)
sudo ufw limit ssh/tcp
三、iptables 配置
3.1 基本概念
3.2 基本命令
3.2.1 查看规则
# 查看所有规则
sudo iptables -L
sudo iptables -L-n-v# 详细信息
# 查看特定表
sudo iptables -t nat -L
3.2.2 清除规则
# 清除所有规则
sudo iptables -F
# 清除特定链
sudo iptables -F INPUT
3.2.3 默认策略
# 设置默认策略
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
3.2.4 添加规则
# 允许已建立的连接
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许本地回环
sudo iptables -A INPUT -i lo -j ACCEPT
# 允许 SSH
sudo iptables -A INPUT -p tcp --dport22-j ACCEPT
# 允许 HTTP 和 HTTPS
sudo iptables -A INPUT -p tcp --dport80-j ACCEPT
sudo iptables -A INPUT -p tcp --dport443-j ACCEPT
# 允许特定 IP
sudo iptables -A INPUT -s192.168.1.100 -j ACCEPT
3.2.5 保存规则
# 保存规则
sudo iptables-save > /etc/iptables/rules.v4
# 恢复规则
sudo iptables-restore < /etc/iptables/rules.v4
3.3 高级配置
3.3.1 端口转发
# 启用 IP 转发
echo 1 > /proc/sys/net/ipv4/ip_forward
# 将 80 端口转发到 8080
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to -port 8080
3.3.2 限制连接速率
# 限制 SSH 连接速率(防止暴力破解)
sudo iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m limit --limit5/minute --limit-burst10-j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j DROP
3.3.3 日志记录
# 记录被拒绝的流量
sudo iptables -A INPUT -j LOG --log -prefix "IPTABLES DROP: "--log -level 4
四、nftables 配置
4.1 安装 nftables
# 安装 nftables
apt update && apt install nftables -y
# 启用服务
systemctl enable nftables
systemctl start nftables
4.2 基本配置
# 查看当前配置
sudo nft list ruleset
# 清除所有规则
sudo nft flush ruleset
# 创建基本规则
sudo nft add table inet filter
sudo nft add chain inet filter input { type filter hook input priority 0 \; policy drop \; }
sudo nft add chain inet filter output { type filter hook output priority 0 \; policy accept \; }
sudo nft add chain inet filter forward { type filter hook forward priority 0 \; policy drop \; }
# 允许已建立的连接
sudo nft add rule inet filter input ct state established,related accept
# 允许本地回环
sudo nft add rule inet filter input iif lo accept
# 允许 SSH
sudo nft add rule inet filter input tcp dport 22 accept
# 允许 HTTP 和 HTTPS
sudo nft add rule inet filter input tcp dport { 80, 443 } accept
4.3 保存配置
# 保存配置
sudo nft list ruleset > /etc/nftables.conf
# 加载配置
sudo nft -f /etc/nftables.conf
五、常见防火墙规则配置
5.1 基本安全配置
# ufw 配置
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw enable
# iptables 配置
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -p tcp --dport22-j ACCEPT
sudo iptables -A INPUT -p tcp --dport80-j ACCEPT
sudo iptables -A INPUT -p tcp --dport443-j ACCEPT
sudo iptables-save > /etc/iptables/rules.v4
5.2 渗透测试环境配置
# 允许所有出站流量,拒绝入站流量(除了必要的服务)
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 4444/tcp # Metasploit 反向 Shell
sudo ufw allow 8080/tcp # 代理服务
sudo ufw enable
5.3 服务器环境配置
# 只允许必要的服务
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw allow 3306/tcp # MySQL
sudo ufw allow 5432/tcp # PostgreSQL
sudo ufw enable
六、防火墙状态检查和管理
6.1 状态检查
# ufw 状态
sudo ufw status
sudo ufw status verbose
sudo ufw status numbered
# iptables 状态
sudo iptables -L-n-v
sudo iptables-save
# nftables 状态
sudo nft list ruleset
6.2 服务管理
# ufw 服务
sudo systemctl status ufw
sudo systemctl enable ufw
sudo systemctl start ufw
# iptables 服务
sudo systemctl status netfilter-persistent
sudo systemctl enable netfilter-persistent
sudo systemctl start netfilter-persistent
# nftables 服务
sudo systemctl status nftables
sudo systemctl enable nftables
sudo systemctl start nftables
6.3 日志查看
# 查看 ufw 日志
sudo tail -f /var/log/ufw.log
# 查看 iptables 日志
sudo tail -f /var/log/kern.log | grep IPTABLES
# 查看 nftables 日志
sudo tail -f /var/log/kern.log | grep nftables
七、最佳实践
7.1 安全建议
默认拒绝:默认拒绝所有入站流量
最小权限:只开放必要的端口和服务
定期更新:保持系统和防火墙规则更新
日志监控:定期检查防火墙日志
备份配置:定期备份防火墙配置
7.2 常见端口参考
| 服务 | 端口 | 协议 | 建议 |
|---|
| SSH | 22 | TCP | 建议修改为非默认端口 |
| HTTP | 80 | TCP | 仅在需要时开放 |
| HTTPS | 443 | TCP | 仅在需要时开放 |
| FTP | 21 | TCP | 尽量避免使用 |
| SMTP | 25 | TCP | 仅在需要时开放 |
| MySQL | 3306 | TCP | 限制访问IP |
| PostgreSQL | 5432 | TCP | 限制访问IP |
| RDP | 3389 | TCP | 尽量避免使用 |
| Metasploit | 4444 | TCP | 仅在测试时开放 |
7.3 故障排除