Skip to content

防火墙配置

正确配置防火墙是保障 DNS-Go 安全运行的重要步骤。

端口说明

DNS-Go 使用的端口及安全建议:

端口协议用途访问范围安全级别
8085TCPWeb 管理界面内网/指定 IP
53UDPDNS 查询服务全网
53TCPDNS 查询服务全网
5432TCPPostgreSQL仅本地/内网
3306TCPMySQL仅本地/内网
6379TCPValkey/Redis仅本地/内网

Linux 防火墙配置

iptables

基本配置:

bash
#!/bin/bash

# 清空现有规则
iptables -F
iptables -X

# 默认策略
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

# 允许 SSH(必须保留,否则可能无法远程连接)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允许 DNS 服务(53 端口)- 全网可访问
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT

# 允许 Web 管理界面(8085 端口)- 仅内网
iptables -A INPUT -p tcp -s 192.168.0.0/16 --dport 8085 -j ACCEPT
iptables -A INPUT -p tcp -s 10.0.0.0/8 --dport 8085 -j ACCEPT
iptables -A INPUT -p tcp -s 172.16.0.0/12 --dport 8085 -j ACCEPT

# 允许数据库连接(仅本地)
iptables -A INPUT -p tcp -s 127.0.0.1 --dport 5432 -j ACCEPT
iptables -A INPUT -p tcp -s 127.0.0.1 --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp -s 127.0.0.1 --dport 6379 -j ACCEPT

# 允许 ICMP(ping)
iptables -A INPUT -p icmp -j ACCEPT

# 保存规则
iptables-save > /etc/iptables/rules.v4

应用规则:

bash
chmod +x firewall.sh
sudo ./firewall.sh

保存规则(Debian/Ubuntu):

bash
sudo apt-get install -y iptables-persistent
sudo netfilter-persistent save

保存规则(CentOS/RHEL):

bash
sudo service iptables save
# 或
sudo iptables-save > /etc/sysconfig/iptables

firewalld

CentOS/RHEL 系统:

bash
# 启动 firewalld
sudo systemctl start firewalld
sudo systemctl enable firewalld

# 允许 DNS 服务
sudo firewall-cmd --permanent --add-service=dns

# 允许 Web 管理界面(限制 IP)
sudo firewall-cmd --permanent --add-rich-rule='
  rule family="ipv4" source address="192.168.0.0/16" port protocol="tcp" port="8085" accept'

# 允许 SSH
sudo firewall-cmd --permanent --add-service=ssh

# 重新加载
sudo firewall-cmd --reload

# 查看规则
sudo firewall-cmd --list-all

ufw(Ubuntu 推荐)

bash
# 启用 ufw
sudo ufw enable

# 默认策略
sudo ufw default deny incoming
sudo ufw default allow outgoing

# 允许 SSH(必须)
sudo ufw allow ssh
# 或指定端口
sudo ufw allow 22/tcp

# 允许 DNS 服务
sudo ufw allow 53/tcp
sudo ufw allow 53/udp

# 允许 Web 管理界面(限制 IP)
sudo ufw allow from 192.168.0.0/16 to any port 8085
sudo ufw allow from 10.0.0.0/8 to any port 8085

# 查看状态
sudo ufw status verbose

云服务安全组

阿里云安全组

入方向规则:

协议类型端口范围授权对象说明
自定义 TCP808510.0.0.0/8Web 管理
自定义 UDP530.0.0.0/0DNS 服务
自定义 TCP530.0.0.0/0DNS 服务
SSH22指定 IP远程管理

腾讯云安全组

入站规则:

类型来源协议端口策略
自定义10.0.0.0/8TCP:8085允许
DNS0.0.0.0/0UDP:53允许
DNS0.0.0.0/0TCP:53允许
SSH指定 IPTCP:22允许

AWS Security Groups

Inbound Rules:

TypeProtocolPort RangeSourceDescription
Custom TCPTCP808510.0.0.0/8Web Management
DNS (UDP)UDP530.0.0.0/0DNS Service
DNS (TCP)TCP530.0.0.0/0DNS Service
SSHTCP22My IPSSH Access

高级安全配置

限制管理界面访问

Nginx 反向代理 + IP 白名单:

nginx
server {
    listen 80;
    server_name dns-admin.example.com;
    
    # 只允许指定 IP 访问
    allow 192.168.1.0/24;
    allow 10.0.0.0/8;
    deny all;
    
    location / {
        proxy_pass http://127.0.0.1:8085;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

使用 VPN 访问管理界面

推荐通过 VPN 访问管理界面,不对外开放 8085 端口:

bash
# 防火墙仅允许 VPN 网段访问 8085
iptables -A INPUT -p tcp -s 10.8.0.0/24 --dport 8085 -j ACCEPT

DDoS 防护

限制 DNS 查询速率:

bash
# 使用 iptables 限制每秒连接数
iptables -A INPUT -p udp --dport 53 -m limit --limit 100/second --limit-burst 200 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j DROP

iptables -A INPUT -p tcp --dport 53 -m limit --limit 100/second --limit-burst 200 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j DROP

使用 fail2ban:

bash
sudo apt-get install -y fail2ban

# 配置 DNS 防护
sudo tee /etc/fail2ban/jail.local <<EOF
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 5

[dns-attack]
enabled = true
port = 53
filter = dns-attack
logpath = /var/log/dns-go/app.log
maxretry = 100
findtime = 60
bantime = 3600
EOF

安全审计

查看防火墙日志

bash
# 启用 iptables 日志
iptables -A INPUT -j LOG --log-prefix "IPTABLES_DROP: " --log-level 4

# 查看日志
sudo tail -f /var/log/kern.log | grep IPTABLES_DROP

连接监控

bash
# 查看当前连接
sudo netstat -tuln

# 查看连接状态
sudo ss -tuln

# 查看 DNS 连接统计
sudo netstat -tuln | grep :53

故障排查

无法访问 Web 界面

bash
# 检查防火墙规则
sudo iptables -L -n | grep 8085

# 检查端口监听
sudo netstat -tlnp | grep 8085

# 临时关闭防火墙测试
sudo iptables -F

DNS 查询无响应

bash
# 检查 53 端口是否开放
sudo iptables -L -n | grep 53

# 测试端口连通性
nc -zv localhost 53

# 检查 DNS 服务状态
curl http://localhost:8085/api/dns/status

规则不生效

bash
# 检查规则顺序(iptables 按顺序匹配)
sudo iptables -L -n --line-numbers

# 删除错误规则
sudo iptables -D INPUT <line_number>

# 重新加载规则
sudo iptables-restore < /etc/iptables/rules.v4

最佳实践

安全原则

  1. 最小权限原则

    • 只开放必要的端口
    • 限制管理界面访问范围
    • 使用内网 IP 白名单
  2. 分层防御

    • 云安全组 + 主机防火墙
    • 网络层 + 应用层防护
    • 定期安全审计
  3. 监控告警

    • 启用连接日志
    • 配置异常流量告警
    • 定期审查访问记录

生产环境配置建议

bash
# 1. 管理界面只允许 VPN 或堡垒机访问
iptables -A INPUT -p tcp -s 10.8.0.0/24 --dport 8085 -j ACCEPT
iptables -A INPUT -p tcp --dport 8085 -j DROP

# 2. DNS 服务限制查询速率
iptables -A INPUT -p udp --dport 53 -m limit --limit 1000/second -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j DROP

# 3. 数据库只允许本地访问
iptables -A INPUT -p tcp --dport 5432 -j DROP
iptables -A INPUT -p tcp --dport 3306 -j DROP
iptables -A INPUT -p tcp --dport 6379 -j DROP

相关文档

基于 MIT 许可发布