一、七层协议攻击类型与特征
攻击类型 | 协议 | 特征 |
---|
HTTP慢速攻击 | HTTP | 低速率发送不完整请求 |
DNS隧道 | DNS | 异常长域名、高频率TXT查询 |
API滥用攻击 | HTTP | 高频调用关键接口(如短信发送) |
WebSocket洪水 | WebSocket | 海量小消息耗尽服务器资源 |
二、HTTP协议深度防护
1. 慢速攻击防御(Nginx配置)
http { # 限制请求头和体的读取时间 client_header_timeout 10s; client_body_timeout 10s; # 限制请求速率 limit_req_zone $binary_remote_addr zone=api_limit:10m rate=100r/s; server { location / { limit_req zone=api_limit burst=200; # 限制最小传输速率(100字节/秒) client_body_in_file_only on; client_body_temp_path /dev/shm/; client_max_body_size 1m; limit_rate_after 1k; limit_rate 100; } }
}
2. 请求特征过滤(OpenResty Lua脚本)
location /api { access_by_lua_block { local headers = ngx.req.get_headers() if not headers["User-Agent"] then ngx.exit(ngx.HTTP_FORBIDDEN) end local method = ngx.req.get_method() if not ({GET=true, POST=true})[method] then ngx.exit(ngx.HTTP_NOT_ALLOWED) end }
}
三、DNS协议定制防护
1. DNS隧道检测(Python深度学习)
import tensorflow as tf
import numpy as np
model = tf.keras.Sequential([ tf.keras.layers.Dense(64, activation='relu', input_shape=(3,)), tf.keras.layers.Dense(1, activation='sigmoid')
]) def predict_dns_tunnel(domain): features = [ len(domain), entropy(domain), domain.count('.') ] return model.predict(np.array([features]))[0] > 0.9 def entropy(s): p, lns = tf.unique(tf.strings.bytes_split(s)) return -tf.reduce_sum(p * tf.math.log(p))
2. DNS查询限频(Bind配置)
options { rate-limit { responses-per-second 50; window 5; qps-scale 100; };
}; zone "example.com" { type master; file "db.example.com"; # 限制单个客户端查询频率 rate-limit { responses-per-second 10; window 3; };
};
四、WebSocket协议防护
1. 消息频率限制(Node.js示例)
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', (ws) => { let messageCount = 0; setInterval(() => messageCount = 0, 1000); ws.on('message', (data) => { if (++messageCount > 100) { ws.close(1008, '消息频率超限'); return; } });
});
2. 协议头校验(Go语言实现)
func handleWebSocket(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Origin") != "https://example.com" { http.Error(w, "Invalid Origin", http.StatusForbidden) return } if !strings.Contains(r.Header.Get("Sec-WebSocket-Version"), "13") { http.Error(w, "Unsupported Version", http.StatusUpgradeRequired) return }
}
五、防御工具链与监控体系
- 应用层防火墙:
- ModSecurity(自定义规则示例):
SecRule REQUEST_URI "@contains /api/send_sms" \ "id:1001,phase:2,deny,log,msg:'SMS API滥用'"
- 日志分析平台:
- ELK Stack(检测异常请求模式)
- Grafana(可视化实时QPS)
- 云WAF集成:
- AWS WAF(速率限制规则)
- Cloudflare Workers(边缘JS挑战)
六、防御效果验证方法
- 压力测试工具:
slowhttptest -c 1000 -u https://example.com -r 10
wsdump.py --fragment 1000 ws://example.com
- 防御验证指标:
- 攻击期间CPU占用率 < 70%
- 正常请求成功率 > 99.9%
- 攻击IP自动封禁时间 < 5秒