防火墙配置实践(Firewall Configuration Practice)

通过实际案例学习如何配置和管理各种防火墙,包括企业级防火墙和个人防火墙


防火墙配置概述

防火墙是网络安全的第一道防线,正确配置防火墙对保护网络安全至关重要。本章节通过实际案例演示防火墙的配置和管理。

防火墙类型

  • 网络层防火墙:基于IP地址、端口过滤
  • 应用层防火墙:基于应用内容过滤
  • 状态检测防火墙:跟踪连接状态
  • 下一代防火墙:集成多种安全功能

配置原则

  1. 最小权限原则:只允许必要流量通过
  2. 默认拒绝:默认拒绝所有流量,明确允许需要的流量
  3. 分层防御:部署多道防线
  4. 定期审计:定期审计规则有效性
  5. 日志记录:记录所有防火墙活动

个人防火墙配置

1. Windows防火墙配置

基本设置

# 查看防火墙状态
Get-NetFirewallProfile
 
# 启用防火墙
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
 
# 允许应用通过防火墙
New-NetFirewallRule -DisplayName "MyApp" -Direction Inbound -Program "C:\MyApp\MyApp.exe" -Action Allow -Protocol TCP -LocalPort 8080
 
# 允许端口
New-NetFirewallRule -DisplayName "Allow HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow

高级配置

# 创建入站规则
New-NetFirewallRule -DisplayName "Block Malicious IP" -Direction Inbound -RemoteAddress 192.168.100.0/24 -Action Block
 
# 创建出站规则
New-NetFirewallRule -DisplayName "Restrict Outbound" -Direction Outbound -RemoteAddress 10.0.0.0/8 -Action Block
 
# 设置日志
Set-NetFirewallProfile -Profile Domain -LogFileName "%systemroot%\system32\LogFiles\Firewall\pfirewall.log" -LogMaxSize 4096 -LogAllowed True -LogBlocked True

2. Linux防火墙配置

iptables配置

# 查看当前规则
iptables -L -n -v
 
# 清空所有规则
iptables -F
iptables -X
iptables -t nat -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
 
# 允许SSH
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
 
# 记录并拒绝其他
iptables -A INPUT -j LOG --log-prefix "iptables-drop: "
iptables -A INPUT -j DROP
 
# 保存规则
iptables-save > /etc/iptables/rules.v4

UFW配置

# 启用UFW
ufw enable
 
# 设置默认策略
ufw default deny incoming
ufw default allow outgoing
 
# 允许SSH
ufw allow 22/tcp
 
# 允许HTTP/HTTPS
ufw allow 80/tcp
ufw allow 443/tcp
 
# 限制SSH尝试
ufw limit 22/tcp
 
# 查看状态
ufw status verbose
 
# 删除规则
ufw delete allow 80/tcp

企业级防火墙配置

1. Cisco ASA防火墙配置

基本配置

# 配置主机名和域名
hostname ASA-5505
domain-name example.com
 
# 配置接口
interface Ethernet0/0
 nameif outside
 security-level 0
 ip address 203.0.113.1 255.255.255.0
!
interface Ethernet0/1
 nameif inside
 security-level 100
 ip address 192.168.1.1 255.255.255.0
 
# 配置NAT
nat (inside,outside) source dynamic any interface
 
# 配置访问规则
access-list OUTSIDE_IN extended permit tcp any host 203.0.113.10 eq 80
access-list OUTSIDE_IN extended permit tcp any host 203.0.113.10 eq 443
access-group OUTSIDE_IN in interface outside
 
# 配置默认路由
route outside 0.0.0.0 0.0.0.0 203.0.113.254
 
# 保存配置
write memory

高级配置

# 配置对象
object network OBJ_WEB_SERVER
 host 192.168.1.10
 nat (inside,outside) static interface service tcp www www
 
# 配置服务组
object-group service HTTPS-SERVICES
 service-object tcp
  port-object eq https
  port-object eq 8080
 
# 配置时间范围
time-range BUSINESS_HOURS
 periodic weekdays 08:00 to 18:00
 
# 基于时间的访问控制
access-list OUTSIDE_IN extended permit tcp any object OBJ_WEB_SERVER object-group HTTPS-SERVICES time-range BUSINESS_HOURS
 
# 配置VPN
access-list VPN_SPLIT_TUNNEL standard permit ip 192.168.1.0 255.255.255.0
group-policy DfltGrpPolicy attributes
 split-tunnel-policy tunnelspecified
 split-tunnel-network-list value VPN_SPLIT_TUNNEL

2. Palo Alto防火墙配置

基本配置

# 配置接口
<interface>
  <ethernet>
    <layer3>
      <units>
        <unit>
          <name>ethernet1/1</name>
          <ip>
            <entry>
              <ip>192.168.1.1/24</ip>
            </entry>
          </ip>
          <layer3>
            <units>
              <unit>
                <name>ethernet1/1</name>
                <ip>
                  <entry>
                    <ip>203.0.113.1/24</ip>
                  </entry>
                </ip>
              </unit>
            </units>
          </layer3>
        </unit>
      </units>
    </layer3>
  </ethernet>
</interface>
 
# 配置安全策略
<rulebase>
  <rules>
    <entry name="Allow-Web">
      <from>
        <member>untrust</member>
      </from>
      <to>
        <member>trust</member>
      </to>
      <source>
        <member>any</member>
      </source>
      <destination>
        <member>web-servers</member>
      </destination>
      <application>
        <member>web-browsing</member>
      </application>
      <action>allow</action>
    </entry>
  </rules>
</rulebase>

高级配置

# 配置安全配置文件
<security>
  <profiles>
    <entry name="strict-profile">
      <virus>
        <threat>yes</threat>
        <action>default</action>
      </virus>
      <spyware>
        <threat>yes</threat>
        <action>default</action>
      </spyware>
      <vulnerability>
        <threat>yes</threat>
        <action>default</action>
      </vulnerability>
    </entry>
  </profiles>
</security>
 
# 配置QoS
<qos>
  <profiles>
    <entry name="voip-profile">
      <class>
        <name>voice</name>
        <priority>high</priority>
        <rate-limit>
          <bandwidth>1000</bandwidth>
          <burst>125</burst>
        </rate-limit>
      </class>
    </entry>
  </profiles>
</qos>

Web应用防火墙配置

1. ModSecurity配置

安装配置

# 安装ModSecurity
apt-get install libapache2-mod-security2
 
# 启用模块
a2enmod security2
systemctl restart apache2
 
# 配置文件位置
/etc/modsecurity/modsecurity.conf-recommended

基本配置

# 启用ModSecurity引擎
SecRuleEngine On
 
# 设置请求体限制
SecRequestBodyAccess On
SecRequestBodyLimit 13107200
SecRequestBodyNoFilesLimit 131072
 
# 基本规则
SecRule ARGS "@detectSQLi" \
    "id:1001,phase:2,block,msg:'SQL Injection Attack Detected',\
    tag:'application-multi',tag:'language-multi',tag:'platform-multi',\
    tag:'attack-sqli'"
 
SecRule ARGS "@detectXSS" \
    "id:1002,phase:2,block,msg:'XSS Attack Detected',\
    tag:'application-multi',tag:'language-multi',tag:'platform-multi',\
    tag:'attack-xss'"
 
# 自定义规则
SecRule REQUEST_HEADERS:User-Agent "@pmFromFile user_agents.data" \
    "id:1003,phase:1,deny,status:403,msg:'Blocked Malicious User-Agent'"
 
SecRule ARGS "@pmFromFile bad_params.data" \
    "id:1004,phase:2,deny,status:403,msg:'Blocked Bad Parameter'"

2. Nginx + ModSecurity配置

安装配置

# 编译Nginx支持ModSecurity
./configure --add-module=/path/to/ModSecurity-nginx
make && make install
 
# 配置Nginx
server {
    listen 80;
    server_name example.com;
    
    # 启用ModSecurity
    ModSecurityEnabled on;
    ModSecurityConfig /etc/nginx/modsecurity.conf;
    
    location / {
        root /var/www/html;
        index index.html;
    }
}

防火墙监控与日志

1. 日志分析

iptables日志分析

# 实时查看日志
tail -f /var/log/kern.log | grep "iptables-drop"
 
# 分析被拒绝的连接
grep "iptables-drop" /var/log/kern.log | awk '{print $8}' | sort | uniq -c | sort -nr
 
# 查看特定IP的活动
grep "iptables-drop" /var/log/kern.log | grep "192.168.1.100"

Web应用防火墙日志分析

# ModSecurity审计日志
tail -f /var/log/apache2/modsec_audit.log
 
# 分析攻击模式
grep "id:\"1001\"" /var/log/apache2/modsec_audit.log | wc -l
grep "id:\"1002\"" /var/log/apache2/modsec_audit.log | wc -l

2. 实时监控

开源监控方案

# 使用fail2ban防止暴力破解
apt-get install fail2ban
 
# 配置fail2ban
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
 
# 启用SSH保护
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
 
# 启动服务
systemctl start fail2ban
systemctl enable fail2ban

商业监控方案

  • SIEM系统:Splunk、ELK Stack
  • 防火墙管理平台:Palo Alto Panorama、Cisco FMC
  • 云安全平台:AWS WAF、Azure Firewall
  • 威胁情报平台:AlienVault OTX、MISP

防火墙最佳实践

1. 规则管理

  • 规则分组:按功能分组管理规则
  • 命名规范:使用清晰的规则命名
  • 文档记录:记录每个规则的用途
  • 定期审查:定期审查规则有效性

2. 性能优化

  • 规则优化:优化规则顺序和结构
  • 硬件加速:使用专用硬件加速
  • 负载均衡:部署防火墙集群
  • 缓存优化:优化连接缓存

3. 安全加固

  • 最小暴露:最小化暴露的服务和端口
  • 定期更新:定期更新防火墙软件
  • 多重防护:部署多层防护措施
  • 应急响应:制定应急响应计划

防火墙配置案例

1. 中小企业网络

网络架构

Internet
    │
    ▼
┌─────────────────┐
│   防火墙       │
│   (企业级)      │
└─────┬───────────┘
      │
      ▼
┌─────────────────┐
│    交换机       │
│  ┌─────┐ ┌─────┐ │
│  │财务部│ │技术部│ │
│  └─────┘ └─────┘ │
└─────────────────┘

防火墙规则

# 财务部规则
iptables -A FORWARD -s 192.168.1.0/24 -d 192.168.10.0/24 -p tcp --dport 443 -j ACCEPT
iptables -A FORWARD -s 192.168.10.0/24 -d 192.168.1.0/24 -p tcp --dport 3306 -j ACCEPT
 
# 技术部规则
iptables -A FORWARD -s 192.168.1.0/24 -d 192.168.20.0/24 -p tcp --dport 22 -j ACCEPT
iptables -A FORWARD -s 192.168.20.0/24 -d 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT
 
# 默认拒绝
iptables -A FORWARD -j DROP

2. Web服务器保护

网络架构

Internet
    │
    ▼
┌─────────────────┐
│   WAF/防火墙     │
└─────┬───────────┘
      │
      ▼
┌─────────────────┐
│   负载均衡器     │
└─────┬───────────┘
      │
      ▼
┌─────────────────┐
│   Web服务器     │
└─────────────────┘

WAF规则

# 防止SQL注入
SecRule ARGS "@rx (?i:(union.*select|select.*union))" \
    "phase:2,block,id:'1001',msg:'SQL Injection Attack'"
 
# 防止XSS
SecRule ARGS "@rx (?i:(<script|javascript:|onload=))" \
    "phase:2,block,id:'1002',msg:'XSS Attack'"
 
# 防止目录遍历
SecRule ARGS "@rx (?:\.\./|\.\.\\\)" \
    "phase:2,block,id:'1003',msg:'Directory Traversal Attack'"
 
# 防止命令注入
SecRule ARGS "@rx (;|\||&|`|\$\()" \
    "phase:2,block,id:'1004',msg:'Command Injection Attack'"

🔗 相关链接


最后更新:2025-01-26 维护规范:详见 笔记规范文档

防火墙配置 网络安全 实践案例 iptables WAF