用脚本配置防火墙规则怎么做

wen 实用脚本 2

本文目录导读:

用脚本配置防火墙规则怎么做

  1. Linux iptables 脚本
  2. Linux firewalld 脚本
  3. Windows netsh 脚本
  4. 配置管理脚本示例
  5. 使用 Ansible 配置(自动化)
  6. 注意事项

Linux iptables 脚本

基础防火墙脚本

#!/bin/bash
# 防火墙规则配置脚本 - iptables
# 清理现有规则
iptables -F
iptables -X
iptables -Z
# 设置默认策略
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(端口22)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP和HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许Ping
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 防止SYN洪水攻击
iptables -A INPUT -p tcp --syn -m limit --limit 1/s -j ACCEPT
# 允许特定IP访问
iptables -A INPUT -s 192.168.1.100 -j ACCEPT
# 拒绝其他所有
iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited
# 保存规则
service iptables save
# 或者 (CentOS 7+)
# iptables-save > /etc/sysconfig/iptables

Linux firewalld 脚本

#!/bin/bash
# 防火墙规则配置脚本 - firewalld
# 创建服务定义文件
cat > /etc/firewalld/services/myapp.xml <<EOF
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>MyApp</short>
  <description>My Custom Application</description>
  <port protocol="tcp" port="8080"/>
  <port protocol="tcp" port="8081"/>
</service>
EOF
# 重新加载配置
firewall-cmd --reload
# 添加规则
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --permanent --add-port=9000-9100/tcp
# 允许特定IP访问
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port port="3306" protocol="tcp" accept'
# 拒绝特定IP
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.5" reject'
# 限制连接速率
firewall-cmd --permanent --add-rich-rule='rule service name="http" limit value="10/minute" accept'
# 重新加载使其生效
firewall-cmd --reload
# 查看当前规则
firewall-cmd --list-all

Windows netsh 脚本

# 防火墙规则配置脚本 - Windows PowerShell
# 以管理员权限运行
# 启用防火墙
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
# 允许端口
New-NetFirewallRule -DisplayName "Allow HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow
New-NetFirewallRule -DisplayName "Allow HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow
New-NetFirewallRule -DisplayName "Allow SSH" -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow
# 允许程序
New-NetFirewallRule -DisplayName "Allow MyApp" -Direction Inbound -Program "C:\Program Files\MyApp\app.exe" -Action Allow
# 允许特定IP
New-NetFirewallRule -DisplayName "Allow IP Range" -Direction Inbound -RemoteAddress 192.168.1.0/24 -Protocol TCP -LocalPort 3389 -Action Allow
# 阻止特定IP
New-NetFirewallRule -DisplayName "Block Bad IP" -Direction Inbound -RemoteAddress 10.10.10.10 -Action Block
# 删除规则
Remove-NetFirewallRule -DisplayName "Allow HTTP"

配置管理脚本示例

#!/bin/bash
# 功能完善的防火墙配置脚本
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
# 日志函数
log() {
    echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $1"
}
error() {
    echo -e "${RED}[ERROR]${NC} $1"
}
# 备份函数
backup_rules() {
    local backup_file="/tmp/firewall_backup_$(date +%Y%m%d_%H%M%S)"
    if command -v iptables >/dev/null 2>&1; then
        iptables-save > "$backup_file"
        log "备份iptables规则到 $backup_file"
    fi
}
# 检查root权限
if [[ $EUID -ne 0 ]]; then
    error "此脚本必须以root权限运行"
    exit 1
fi
# 主配置函数
configure_firewall() {
    log "开始配置防火墙..."
    # 备份现有规则
    backup_rules
    # 清理规则
    iptables -F
    iptables -X
    # 设置默认策略
    iptables -P INPUT DROP
    iptables -P FORWARD DROP
    # 允许本地
    iptables -A INPUT -i lo -j ACCEPT
    # 定义变量
    SSH_PORT=22
    HTTP_PORT=80
    HTTPS_PORT=443
    ALLOWED_IPS=("192.168.1.100" "192.168.1.101")
    # 允许已建立的连接
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    # 添加基本服务
    for port in $SSH_PORT $HTTP_PORT $HTTPS_PORT; do
        iptables -A INPUT -p tcp --dport $port -j ACCEPT
        log "允许端口 $port"
    done
    # 允许特定IP
    for ip in "${ALLOWED_IPS[@]}"; do
        iptables -A INPUT -s "$ip" -j ACCEPT
        log "允许IP $ip"
    done
    # 保存规则
    iptables-save > /etc/sysconfig/iptables
    log "防火墙规则配置完成"
}
# 执行
configure_firewall

使用 Ansible 配置(自动化)

---
- name: 配置防火墙
  hosts: servers
  become: yes
  tasks:
    - name: 安装 firewalld
      package:
        name: firewalld
        state: present
    - name: 启动并启用 firewalld
      service:
        name: firewalld
        state: started
        enabled: yes
    - name: 开放HTTP端口
      firewalld:
        port: 80/tcp
        permanent: yes
        state: enabled
        immediate: yes
    - name: 开放HTTPS端口
      firewalld:
        port: 443/tcp
        permanent: yes
        state: enabled
        immediate: yes
    - name: 允许特定IP
      firewalld:
        rich_rule: 'rule family="ipv4" source address="192.168.1.100" accept'
        permanent: yes
        state: enabled
        immediate: yes

注意事项

  1. 测试环境先验证:在生产环境部署前先测试
  2. 备份规则:配置前备份现有规则
  3. 日志记录:添加日志记录便于排错
  4. 权限要求:需要root或管理员权限
  5. 持久化:确保规则在重启后仍然生效

根据你的实际需求选择合适的方案,需要更多具体的配置示例吗?

抱歉,评论功能暂时关闭!