ZBLOG

openresty实现CC攻击防护

OpenResty实现CC攻击防护方案

CC攻击(Challenge Collapsar)是一种针对应用层的DDoS攻击,OpenResty基于Nginx和Lua的高性能特性非常适合构建防护方案。以下是完整的实现方案:

1. 基础频率限制

IP访问频率控制

  1. -- nginx.conf http部分添加共享内存区
  2. lua_shared_dict ip_limit 10m;
  3. -- Lua脚本实现
  4. local limit_count = require "resty.limit.count"
  5. local function anti_cc()
  6. local lim, err = limit_count.new("ip_limit", 100, 60) -- 60秒内最多100次请求
  7. if not lim then
  8. ngx.log(ngx.ERR, "failed to instantiate a resty.limit.count object: ", err)
  9. return ngx.exit(500)
  10. end
  11. local key = ngx.var.binary_remote_addr -- 客户端IP作为key
  12. local delay, err = lim:incoming(key, true)
  13. if not delay then
  14. if err == "rejected" then
  15. return ngx.exit(503)
  16. end
  17. ngx.log(ngx.ERR, "failed to limit count: ", err)
  18. return ngx.exit(500)
  19. end
  20. -- 请求通过
  21. end

2. 增强型防护策略

滑动窗口限流(更精确的控制)

  1. local limit_req = require "resty.limit.req"
  2. local rate = 10 -- 每秒10个请求
  3. local burst = 20 -- 允许突发20个请求
  4. local lim, err = limit_req.new("ip_limit", rate, burst)
  5. if not lim then
  6. ngx.log(ngx.ERR, "failed to instantiate a resty.limit.req object: ", err)
  7. return ngx.exit(500)
  8. end
  9. local key = ngx.var.binary_remote_addr..":"..ngx.var.host -- IP+host组合键
  10. local delay, err = lim:incoming(key, true)
  11. if delay and delay > 0 then -- need to delay request
  12. if delay > 0.5 then -- requests too frequent
  13. return ngx.exit(503)
  14. end
  15. ngx.sleep(delay)
  16. end

##3.JS挑战验证(防自动化工具)

  1. -- nginx配置返回JS挑战页面
  2. location /challenge {
  3. content_by_lua_block {
  4. local token=ngx.md5(math.random()..ngx.now())
  5. local expires=ngx.time()+300 --5分钟有效
  6. local js=[[<script>function submitToken(){...}</script>]]
  7. ngx.header["Set-Cookie"]="__challenge_token="..token.."; Path=/; Max-Age=300"
  8. redis:hset("challenge:"..token,"expires",expires)
  9. ngx.say([[<html><body onload="setTimeout(submitToken,2000)">...</body></html>]])
  10. }
  11. }
  12. --验证中间件:
  13. if redis:hget("challenge:"..cookie_token,"passed")~="1"then
  14. return ngx.redirect("/challenge?url="..ngx.encode_args({next=request_uri}))
  15. end

##4.行为分析防护层

  1. --收集用户行为特征:
  2. local user_behavior={
  3. ua=ngx.var.http_user_agent,
  4. accept=ngx.var.http_accept,
  5. lang=ngk.xvar.http_accept_language,
  6. req_rate=(redis:get("rate:"..ip)or0)+1,
  7. mouse_move=false,--通过前端JS埋点设置
  8. }
  9. --机器学习模型判断(PMMLONNX运行时):
  10. if ml_model:predict(user_behavior)>THRESHOLD then
  11. add_to_blocklist(ip)
  12. end
  13. function add_to_blocklist(ip)
  14. redis:sadd("blocked_ips",ip)
  15. redis:expire("blocked_ips",3600)--封禁1小时
  16. end
  17. access_by_lua_block{
  18. if redis:sismember("blocked_ips",ngk.xvar.binary_remote_addr)==1then
  19. return ngn.xexit(403)
  20. end
  21. }

##5.WAF规则集成(可选)

  1. http{
  2. lua_package_path"/path/to/lua-resty-waf/?.lua;;";
  3. init_by_lua_block{
  4. waf=require"resty.waf"
  5. config={rules_file="/path/to/rules.json"},
  6. }
  7. server{
  8. access_by_luablock{
  9. local res={}
  10. waf.exec(config,nilres)
  11. if res.action=="DENY"then
  12. return ngn.xexit(res.status or403)
  13. end
  14. }
  15. }
  16. }

##6.DDoS弹性扩展设计

  • 边缘节点协作:使用lua-resty-consul同步黑名单

  • 云原生集成:K8s HPA根据QPS自动扩容

  • BGP引流:与云厂商API联动触发清洗


    内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=23089

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?