简介:本文详细介绍如何在Azure Linux虚拟机上通过Apache mod_evasive模块构建DDoS防护体系,涵盖配置原理、参数调优、性能监控及应急响应方案,帮助运维人员有效抵御CC攻击及低频DDoS威胁。
Azure全球基础设施虽具备基础DDoS防护(Azure DDoS Protection Standard),但针对Web应用的L7层攻击(如HTTP Flood、Slowloris)仍需应用层防护。据2023年云安全报告,42%的Web攻击针对Apache服务,其中68%采用低速高频请求绕过基础防护。
作为Apache模块,mod_evasive通过行为分析实现:
与Azure WAF形成互补:WAF侧重规则匹配,mod_evasive专注行为建模,二者协同可降低92%的CC攻击成功率。
# Ubuntu 22.04示例sudo apt updatesudo apt install -y apache2 libapache2-mod-evasivesudo a2enmod evasive
编辑/etc/apache2/mods-available/evasive.conf,关键参数说明:
<IfModule mod_evasive24.c>DOSPageCount 20 # 单IP单页面1秒内最大请求DOSSiteCount 100 # 单IP全站1秒内最大请求DOSPageInterval 1 # 检测间隔(秒)DOSSiteInterval 1 # 全站检测间隔DOSBlockingPeriod 10 # 封禁时长(秒)DOSEmailNotify admin@example.comDOSSystemCommand "iptables -A INPUT -s %s -j DROP"DOSLogDir "/var/log/apache2/mod_evasive"</IfModule>
# 创建日志目录并设置权限sudo mkdir -p /var/log/apache2/mod_evasivesudo chown www-data:www-data /var/log/apache2/mod_evasive# 配置logrotatecat > /etc/logrotate.d/mod_evasive <<EOF/var/log/apache2/mod_evasive/*.log {dailymissingokrotate 7compressdelaycompressnotifemptycreate 640 www-data admsharedscriptspostrotateif /etc/init.d/apache2 status > /dev/null ; then \/etc/init.d/apache2 reload > /dev/null; \fi;endscript}EOF
基于Azure Monitor指标实现自适应防护:
# Python示例:根据CPU使用率动态调整阈值import requestsfrom azure.monitor.query import MetricsQueryClientfrom azure.identity import DefaultAzureCredentialdef adjust_thresholds(resource_id):credential = DefaultAzureCredential()client = MetricsQueryClient(credential)response = client.query_resource(resource_id,["Percentage CPU"],timespan="PT5M",interval="PT1M")cpu_avg = sum(m.average for m in response.metrics[0].timeseries[0].data) / len(response.metrics[0].timeseries[0].data)base_threshold = 20scale_factor = 1 + (cpu_avg - 50) / 100 # CPU>50%时线性增加阈值return int(base_threshold * scale_factor)
// 检测异常请求模式ModEvasiveLogs| where TimeGenerated > ago(1h)| summarize RequestCount=count() by ClientIP| where RequestCount > 100| join kind=inner (SecurityAlert| where Severity == "High"| project AlertTime=TimeGenerated, AlertIP=ExtendedProperties.["Source IP"]) on $left.ClientIP == $right.AlertIP
使用ab(Apache Benchmark)进行压力测试:
# 正常流量测试ab -n 1000 -c 50 http://your-vm-ip/# 攻击模拟测试ab -n 5000 -c 200 http://your-vm-ip/ # 预期触发mod_evasive防护
| 指标 | 正常范围 | 防护触发阈值 |
|---|---|---|
| 请求延迟(ms) | <200 | >500持续3秒 |
| 5xx错误率 | <1% | >10% |
| 连接数(per IP) | <15 | >20 |
for ip in $BLOCKEDIPS; do
az network nsg rule create \
—nsg-name your-nsg \
—name Block${ip//./_} \
—priority 3000 \
—access Deny \
—protocol ““ \
—direction Inbound \
—source-address-prefixes $ip \
—destination-port-ranges ““
done
# 五、最佳实践与注意事项## 5.1 配置黄金原则- **渐进式调优**:从宽松参数(PageCount=50)开始,逐步收紧- **白名单机制**:对CDN节点(如Azure Front Door IP)设置例外```apacheDOSWhiteList 168.63.129.16 # Azure服务标签示例
DOSPageInterval是否过短(建议≥0.5秒)DOSLogDir权限及mod_evasive日志级别设置mod_reqtimeout防止慢速攻击消耗资源mod_evasive日志,优化DOSPageCount阈值
{"name": "ModEvasiveTriggerAlert","severity": "Sev3","condition": {"allOf": [{"field": "customMetrics/modEvasiveBlocks","operator": "GreaterThan","threshold": 50}]},"actions": {"actionGroups": ["your-action-group-id"]}}
对于多区域部署,建议:
在Azure Linux VM上部署Apache mod_evasive可有效构建应用层DDoS防护体系。通过合理配置参数、建立监控告警机制、与Azure原生安全服务集成,能够显著提升Web应用抵御CC攻击及低频DDoS的能力。实际部署中需结合压力测试持续优化参数,并建立完善的应急响应流程,最终形成覆盖网络层、传输层、应用层的多维防护体系。