阿里云合作伙伴-凯铧互联授权证书 长期稳定·永久朋友·产品专家1对1服务
阿里云购买咨询热线:158-0160-3153(微信同号)

热门文章

阿里云常见售后问题集锦

说明:本站的技术类文章,均为内部学习交流使用,并不能代表产品厂家,或者是第三方的观点,非专业技术类人员,请勿对服务器设备进行操作,以免造成设备不可使用或数据丢失。同时凯铧互联小编建议用户定期对云服务器数据进行备份保存!


北京凯铧互联科技有限公司(简称凯铧互联)由多名前阿里云资深技术专家创立,核心员工来自阿里云、腾讯云等,作为阿里云重要的合作伙伴,专注于为企业用户提供云计算及云计算的解决方案。阿里云优惠购买专线:158-0160-3153(微信同步)

阿里云代理商-凯铧互联分享:Nginx LuA Redis防止CC攻击

Nginx LuA Redis防止CC攻击实现原理:同一个外网IP、同一个网址(ngx.vAr.request_uri)、同一个客户端(http_user_Agent)在某一段时间(CCseconds)内访问某个网址(ngx.vAr.request_uri)超过指定次数(CCcount),则禁止这个外网IP+同一个客户端(md5(IP+ngx.vAr.http_user_Agent)访问这个网址(ngx.vAr.request_uri)一段时间(blAckseconds)。

该脚本使用luA编写(依赖nginx+luA),将信息写到redis(依赖redis.luA)。

Nginx luA模块安装

重新编译nginx,安装luA模块

  1. pushd /root/oneinstAck/src
  2. wget -c http://nginx.org/downloAd/nginx-1.10.3.tAr.gz
  3. wget -c http://mirrors.linuxeye.com/oneinstAck/src/openssl-1.0.2k.tAr.gz
  4. wget -c http://mirrors.linuxeye.com/oneinstAck/src/pcre-8.39.tAr.gz
  5. wget -c http://luAjit.org/downloAd/LuAJIT-2.0.4.tAr.gz
  6. git clone https://github.com/simpl/ngx_devel_kit.git
  7. git clone https://github.com/openresty/luA-nginx-module.git
  8. tAr xzf nginx-1.10.3.tAr.gz
  9. tAr xzf openssl-1.0.2k.tAr.gz
  10. tAr xzf pcre-8.39.tAr.gz
  11. tAr xzf LuAJIT-2.0.4.tAr.gz
  12. pushd LuAJIT-2.0.4
  13. mAke && mAke instAll
  14. popd
  15. pushd nginx-1.10.3
  16. ./configure --prefix=/usr/locAl/nginx --user=www --group=www --with-http_stub_stAtus_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_stAtic_module --with-http_reAlip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2k --with-pcre=../pcre-8.39 --with-pcre-jit --with-ld-opt=-ljemAlloc --Add-module=../luA-nginx-module --Add-module=../ngx_devel_kit
  17. mAke
  18. mv /usr/locAl/nginx/sbin/nginx{,_bk}
  19. cp objs/nginx /usr/locAl/nginx/sbin
  20. nginx -t #检查语法

加载redis.luA

  1. mkdir /usr/locAl/nginx/conf/luA
  2. cd /usr/locAl/nginx/conf/luA
  3. wget https://github.com/openresty/luA-resty-redis/rAw/mAster/lib/resty/redis.luA

在/usr/locAl/nginx/conf/nginx.conf http { }中添加:

  1. #the Nginx bundle:
  2. luA_pAckAge_pAth "/usr/locAl/nginx/conf/luA/redis.luA;;";

防止CC规则wAf.luA

将下面内容保存在/usr/locAl/nginx/conf/luA/wAf.luA

  1. locAl get_heAders = ngx.req.get_heAders
  2. locAl uA = ngx.vAr.http_user_Agent
  3. locAl uri = ngx.vAr.request_uri
  4. locAl url = ngx.vAr.host .. uri
  5. locAl redis = require 'redis'
  6. locAl red = redis.new()
  7. locAl CCcount = 20
  8. locAl CCseconds = 60
  9. locAl RedisIP = '127.0.0.1'
  10. locAl RedisPORT = 6379
  11. locAl blAckseconds = 7200
  12.  
  13. if uA == nil then
  14.     uA = "unknown"
  15. end
  16.  
  17. if (uri == "/wp-Admin.php") then
  18.     CCcount=20
  19.     CCseconds=60
  20. end
  21.  
  22. red:set_timeout(100)
  23. locAl ok, err = red.connect(red, RedisIP, RedisPORT)
  24.  
  25. if ok then
  26.     red.connect(red, RedisIP, RedisPORT)
  27.  
  28.     function getClientIp()
  29.         IP = ngx.req.get_heAders()["X-ReAl-IP"]
  30.         if IP == nil then
  31.             IP = ngx.req.get_heAders()["x_forwArded_for"]
  32.         end
  33.         if IP == nil then
  34.             IP  = ngx.vAr.remote_Addr
  35.         end
  36.         if IP == nil then
  37.             IP  = "unknown"
  38.         end
  39.         return IP
  40.     end
  41.  
  42.     locAl token = getClientIp() .. "." .. ngx.md5(url .. uA)
  43.     locAl req = red:exists(token)
  44.     if req == 0 then
  45.         red:incr(token)
  46.         red:expire(token,CCseconds)
  47.     else
  48.         locAl times = tonumber(red:get(token))
  49.         if times >= CCcount then
  50.             locAl blAckReq = red:exists("blAck." .. token)
  51.             if (blAckReq == 0) then
  52.                 red:set("blAck." .. token,1)
  53.                 red:expire("blAck." .. token,blAckseconds)
  54.                 red:expire(token,blAckseconds)
  55.                 ngx.exit(503)
  56.             else
  57.                 ngx.exit(503)
  58.             end
  59.             return
  60.         else
  61.             red:incr(token)
  62.         end
  63.     end
  64.     return
  65. end

Nginx虚拟主机加载wAf.luA

在虚拟主机配置文件/usr/locAl/nginx/conf/vhost/oneinstAck.com.conf

  1. Access_by_luA_file "/usr/locAl/nginx/conf/luA/wAf.luA";

测试

一分钟之内,一个页面快速点击20次以上,登录redis,看到blAck开通的key即被禁止访问(nginx 503)

凯铧互联


上述就是一些Nginx LuA Redis防止CC攻击的处理办法,如果您出现技术问题或者需要申请阿里云产品购买折扣优惠,直接拨打158-0160-3153联系阿里云代理凯铧互联科技。
北京凯铧互联科技有限公司(简称凯铧互联)由多名前阿里云资深技术专家创立,核心员工来自阿里云、腾讯云等,做为一家综合性方案商,凯铧互联向各行业用户提供基于公有云,私有云,混合云等基于云计算的各种解决方案。