OpenResty(nginx扩展)实现防cc攻击实例教程



OpenResty(nginx扩展)实现防cc攻击实例教程。防cc攻击,推荐使用HttpGuard本文介绍使用openresty来实现防cc攻击的功能。openresty官网http://openresty.org/cn/index.html。下面是防cc攻击的流程图。

根据流程图,我们知道防cc攻击主要包括两部分,一是限制请求速度,二是给用户发送js跳转代码进行验证请求是否合法。
cc

一、安装依赖

centos:

  1. yum install readline-devel pcre-devel openssl-devel

ubuntu:

  1. apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl

二、luajit安装

  1. cd /tmp/
  2. git clone http://luajit.org/git/luajit-2.0.git
  3. cd luajit-2.0/
  4. make && make install
  5. ln -sf luajit-2.0.0-beta10 /usr/local/bin/luajit
  6. ln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/lib/

三、openresty安装

  1. cd /tmp
  2. wget http://agentzh.org/misc/nginx/ngx_openresty-1.2.4.13.tar.gz
  3. tar xzf ngx_openresty-1.2.4.13.tar.gz
  4. cd ngx_openresty-1.2.4.13/
  5. ./configure –prefix=/usr/local/openresty –with-luajit
  6. make && make install

四、nginx配置

nginx.conf:

  1. http{
  2. [......]
  3. lua_shared_dict limit 10m;
  4. lua_shared_dict jsjump 10m;
  5.     server {
  6. #lua_code_cache off;
  7.         listen       80;
  8.         server_name  www.centos.bz;
  9.         location / {
  10. default_type  text/html;
  11. content_by_lua_file “/usr/local/openresty/nginx/conf/lua”;
  12.         }
  13.         location @cc {
  14.             internal;
  15.             root   html;
  16.             index  index.html index.htm;
  17.         }
  18.     }
  19. }

/usr/local/openresty/nginx/conf/lua文件:

  1. local ip = ngx.var.binary_remote_addr
  2. local limit = ngx.shared.limit
  3. local req,_=limit:get(ip)
  4. if req then
  5.         if req > 20 then
  6.                 ngx.exit(503)
  7.         else
  8.                 limit:incr(ip,1)
  9.         end
  10. else
  11.         limit:set(ip,1,10)
  12. end
  13. local jsjump = ngx.shared.jsjump
  14. local uri = ngx.var.request_uri
  15. local jspara,flags=jsjump:get(ip)
  16. local args = ngx.req.get_uri_args()
  17. if jspara then
  18.     if flags then
  19.         ngx.exec(“@cc”)
  20.     else
  21.                 local p_jskey=”
  22.                 if args["jskey"] and type(args["jskey"])==’table’ then
  23.                          p_jskey=args["jskey"][table.getn(args["jskey"])]
  24.                 else
  25.                          p_jskey=args["jskey"]
  26.                 end
  27.         if p_jskey and p_jskey==tostring(jspara) then
  28.                         jsjump:set(ip,jspara,3600,1)
  29.                         ngx.exec(“@cc”)
  30.         else
  31.                         local url=”
  32.                         if ngx.var.args then
  33.                                 url=ngx.var.scheme..”://”..ngx.var.host..uri..”&jskey=”..jspara
  34.                         else
  35.                                 url=ngx.var.scheme..”://”..ngx.var.host..uri..”?jskey=”..jspara
  36.                         end
  37.                         local jscode=”<script>window.location.href=’”..url..”‘;</script>”
  38.                         ngx.say(jscode)
  39.         end
  40.     end
  41. else
  42. math.randomseed( os.time() );
  43.     local random=math.random(100000,999999)
  44.     jsjump:set(ip,random,60)
  45.     local url=”
  46.     if ngx.var.args then
  47.         url=ngx.var.scheme..”://”..ngx.var.host..uri..”&jskey=”..random
  48.     else
  49.         url=ngx.var.scheme..”://”..ngx.var.host..uri..”?jskey=”..random
  50.     end
  51.     local jscode=”<script>window.location.href=’”..url..”‘;</script>”
  52.     ngx.say(jscode)
  53. end

lua代码部分解释:
1、1-12行是限速功能实现,第5和第10行表示10秒钟内容最多只能请求20次。
2、14-48行是验证部分,24行中的3600表示验证通过后,白名单时间为3600秒,即1小时。

update: 2013.5.26
1、修复JS无限跳转bug
2、增加随机种子