实战DDoS攻击防御:基于Nginx的流量清洗方案

一、DDoS攻击原理分析

SYN洪水攻击是常见的DDoS攻击方式,攻击者通过伪造大量TCP连接请求耗尽服务器资源。根据Cloudflare的统计报告,2023年全球DDoS攻击峰值达到每秒7100万次请求。

二、防御方案实现

# /etc/nginx/nginx.conf 核心配置
http {
    limit_req_zone $binary_remote_addr zone=req_limit:10m rate=100r/s;
    limit_conn_zone $binary_remote_addr zone=conn_limit:10m;

    server {
        listen 80;
        location / {
            limit_req zone=req_limit burst=200 nodelay;
            limit_conn conn_limit 50;
            proxy_pass http://backend;
            
            # 验证User-Agent合法性
            if ($http_user_agent ~* "(wget|curl|python|nikto|scan|burp)") {
                return 403;
            }
        }
    }
}

三、IP黑名单自动更新

#!/usr/bin/python3
# ddos_defender.py
import subprocess
import requests
from datetime import datetime, timedelta

def update_iptables():
    # 获取最新威胁情报
    threat_feeds = [
        "https://lists.blocklist.de/lists/ssh.txt",
        "https://rules.emergingthreats.net/blockrules/compromised-ips.txt"
    ]
    
    # 清空旧规则
    subprocess.run(["iptables -F INPUT"], shell=True)
    
    for feed in threat_feeds:
        response = requests.get(feed)
        for ip in response.text.split('\n'):
            if ip.strip() and len(ip.split('.')) == 4:
                # 添加iptables规则
                cmd = f"iptables -A INPUT -s {ip} -j DROP"
                subprocess.run(cmd, shell=True)
                
if __name__ == "__main__":
    update_iptables()
    print(f"[{datetime.now()}] IP黑名单更新完成")

四、验证与测试

# 压力测试工具验证防御效果
ab -n 10000 -c 500 http://your-server.com/

# 查看Nginx状态
tail -f /var/log/nginx/access.log | grep '503 503'

# 监控连接数
watch -n 1 "netstat -ant | awk '{print \$6}' | sort | uniq -c"


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值