我来帮您编写一个自动配置防火墙的脚本,这里提供基于 Linux iptables 和 firewalld 的示例:

基于 iptables 的防火墙配置脚本
#!/bin/bash
# 防火墙自动配置脚本 (iptables)
# 适用于 CentOS 6/7, Ubuntu, Debian 等系统
# 错误处理函数
error_exit() {
echo "错误: $1" >&2
exit 1
}
# 检查root权限
if [[ $EUID -ne 0 ]]; then
error_exit "此脚本需要root权限运行"
fi
# 检查 iptables 是否安装
if ! command -v iptables &> /dev/null; then
error_exit "iptables 未安装"
fi
echo "开始配置防火墙..."
# 清除现有规则
iptables -F
iptables -X
iptables -Z
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 OUTPUT -o lo -j ACCEPT
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 基本服务配置
echo "配置基本服务..."
# SSH (端口22)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# HTTP (端口80)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# HTTPS (端口443)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# DNS (端口53)
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
# 允许ICMP (ping)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 日志记录被拒绝的包
iptables -A INPUT -j LOG --log-prefix "iptables-denied: " --log-level 7
# 保存规则 (不同系统的保存方式不同)
if [[ -f /etc/centos-release ]] || [[ -f /etc/redhat-release ]]; then
# CentOS/RHEL
service iptables save
elif [[ -f /etc/debian_version ]]; then
# Debian/Ubuntu
iptables-save > /etc/iptables/rules.v4
fi
echo "防火墙配置完成!"
echo "已开放的端口: 22(SSH), 80(HTTP), 443(HTTPS), 53(DNS)"
基于 firewalld 的防火墙配置脚本
#!/bin/bash
# 防火墙自动配置脚本 (firewalld)
# 适用于 CentOS 7+, RHEL 7+, Fedora
# 错误处理函数
error_exit() {
echo "错误: $1" >&2
exit 1
}
# 检查root权限
if [[ $EUID -ne 0 ]]; then
error_exit "此脚本需要root权限运行"
fi
# 检查 firewalld 是否安装
if ! command -v firewall-cmd &> /dev/null; then
error_exit "firewalld 未安装,请先安装: yum install firewalld"
fi
# 检查 firewalld 是否运行
if ! systemctl is-active firewalld &> /dev/null; then
echo "firewalld 未运行,正在启动..."
systemctl start firewalld
fi
echo "开始配置防火墙..."
# 重置配置
firewall-cmd --zone=public --remove-service=ssh
echo "已重置默认配置"
# 配置基本服务
configure_services() {
local services=("ssh" "http" "https" "dns")
for service in "${services[@]}"; do
echo "配置 $service 服务..."
firewall-cmd --permanent --add-service="$service"
done
}
# 添加自定义端口
add_custom_ports() {
local ports=("8080/tcp" "8443/tcp" "3000/tcp")
for port in "${ports[@]}"; do
echo "添加自定义端口: $port"
firewall-cmd --permanent --add-port="$port"
done
}
# 配置IP白名单
configure_whitelist() {
local whitelist_ips=("192.168.1.0/24" "10.0.0.0/8")
for ip in "${whitelist_ips[@]}"; do
echo "添加IP白名单: $ip"
firewall-cmd --permanent --add-source="$ip"
done
}
# 配置端口转发(可选)
configure_port_forwarding() {
# 示例:转发80端口到8080
echo "配置端口转发: 80 -> 8080"
firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080
}
# 执行配置
configure_services
add_custom_ports
configure_whitelist
# configure_port_forwarding # 如需端口转发,取消注释
# 重载防火墙配置
echo "重载防火墙配置..."
firewall-cmd --reload
# 显示最终配置
echo -e "\n当前防火墙配置:"
firewall-cmd --list-all
# 设置开机自启
systemctl enable firewalld
echo -e "\n防火墙配置完成!"
echo "已配置的服务和端口:"
firewall-cmd --list-services
firewall-cmd --list-ports
高级配置脚本(包含日志和监控)
#!/bin/bash
# 高级防火墙配置脚本
# 配置变量
FIREWALL_LOG="/var/log/firewall.log"
CONFIG_FILE="/etc/firewall-rules.conf"
# 日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$FIREWALL_LOG"
}
# 备份当前规则
backup_rules() {
local backup_dir="/backup/firewall"
mkdir -p "$backup_dir"
iptables-save > "$backup_dir/rules-$(date +%Y%m%d-%H%M%S).backup"
log "防火墙规则已备份"
}
# 配置DDoS防护
configure_ddos_protection() {
log "配置DDoS防护..."
# 限制连接数
iptables -A INPUT -p tcp --syn -m connlimit --connlimit-above 100 -j DROP
# 限制每秒连接数
iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT
# 防止端口扫描
iptables -A INPUT -m recent --name portscan --rcheck --seconds 60 -j DROP
iptables -A FORWARD -m recent --name portscan --rcheck --seconds 60 -j DROP
}
# 配置IP黑名单
configure_blacklist() {
local blacklist_file="/etc/firewall-blacklist.txt"
if [[ -f "$blacklist_file" ]]; then
log "加载IP黑名单..."
while IFS= read -r ip; do
[[ -z "$ip" || "$ip" =~ ^# ]] && continue
iptables -A INPUT -s "$ip" -j DROP
log "已阻止IP: $ip"
done < "$blacklist_file"
fi
}
# 主配置函数
main() {
log "开始配置防火墙规则..."
# 备份现有规则
backup_rules
# 应用基本规则(使用前面的基本配置)
# ... [这里插入基本配置代码]
# 应用高级配置
configure_ddos_protection
configure_blacklist
# 保存配置
iptables-save > /etc/iptables/rules.v4
log "防火墙规则已保存"
}
main
使用说明
-
赋予执行权限:
chmod +x firewall-config.sh
-
运行脚本:
sudo ./firewall-config.sh
-
查看防火墙状态:
# iptables iptables -L -n -v
firewalld
firewall-cmd --list-all
## 安全建议
1. **先测试再应用**:在生产环境前先在测试环境验证
2. **保留SSH访问**:确保SSH端口始终开放
3. **定期备份**:保存防火墙规则的备份
4. **监控日志**:定期检查防火墙日志
5. **最小权限原则**:只开放必要的端口和服务
根据您的具体需求,可以修改服务的端口号、添加更多的安全规则或配置特定的访问控制策略。