ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Nginx Lua Redis防止CC攻击

2021-01-17 23:30:06  阅读:340  来源: 互联网

标签:http lua CC ngx Redis Lua nginx -- local


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模块,或者直接使用《OneinStack》安装OpenResty自带改模块
pushd /root/oneinstack/src
wget -c http://nginx.org/download/nginx-1.10.3.tar.gz
wget -c http://mirrors.linuxeye.com/oneinstack/src/openssl-1.0.2k.tar.gz
wget -c http://mirrors.linuxeye.com/oneinstack/src/pcre-8.39.tar.gz
wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
git clone https://github.com/simpl/ngx_devel_kit.git
git clone https://github.com/openresty/lua-nginx-module.git
tar xzf nginx-1.10.3.tar.gz
tar xzf openssl-1.0.2k.tar.gz
tar xzf pcre-8.39.tar.gz
tar xzf LuaJIT-2.0.4.tar.gz
pushd LuaJIT-2.0.4
make && make install
popd
pushd nginx-1.10.3
./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=…/ngx_cache_purge-2.3 --add-module=…/lua-nginx-module --add-module=…/ngx_devel_kit
make
mv /usr/local/nginx/sbin/nginx{,_bk}
cp objs/nginx /usr/local/nginx/sbin
nginx -t #检查语法
加载redis.lua
mkdir /usr/local/nginx/conf/lua
cd /usr/local/nginx/conf/lua
wget https://github.com/openresty/lua-resty-redis/raw/master/lib/resty/redis.lua
http://groups.tianya.cn/post-208292-ed7f3aecdb414a9f98c77d8a395cd606-1.shtml
#the Nginx bundle:

lua_package_path “/usr/local/nginx/conf/lua/redis.lua;;”;
防止CC规则waf.lua
将下面内容保存在/usr/local/nginx/conf/lua/waf.lua
local get_headers = ngx.req.get_headers
local ua = ngx.var.http_user_agent
local uri = ngx.var.request_uri
local url = ngx.var.host … uri
local redis = require ‘redis’
local red = redis.new()
local CCcount = 20
local CCseconds = 60
local RedisIP = ‘127.0.0.1’
local RedisPORT = 6379
local blackseconds = 7200
http://groups.tianya.cn/post-208292-1b688f36701a4b05953a747199758cc8-1.shtml
if ua == nil then
ua = “unknown”
end

if (uri == “/wp-admin.php”) then
CCcount=20
CCseconds=60
end

red:set_timeout(100)
local ok, err = red.connect(red, RedisIP, RedisPORT)
http://groups.tianya.cn/post-208292-35f39f6813e548fdbe02c9a4f8bccb4a-1.shtml
if ok then
red.connect(red, RedisIP, RedisPORT)

function getClientIp()
    IP = ngx.req.get_headers()["X-Real-IP"]
    if IP == nil then
        IP = ngx.req.get_headers()["x_forwarded_for"]
    end
    if IP == nil then
        IP  = ngx.var.remote_addr
    end
    if IP == nil then
        IP  = "unknown"
    end
    return IP
end

local token = getClientIp() .. "." .. ngx.md5(url .. ua)
local req = red:exists(token)
if req == 0 then
    red:incr(token)
    red:expire(token,CCseconds)
else
    local times = tonumber(red:get(token))
    if times >= CCcount then
        local blackReq = red:exists("black." .. token)
        if (blackReq == 0) then
            red:set("black." .. token,1)
            red:expire("black." .. token,blackseconds)
            red:expire(token,blackseconds)
            ngx.exit(503)
        else
            ngx.exit(503)
        end
        return
    else
        red:incr(token)
    end
end
return

end
Nginx虚拟主机加载waf.lua
在虚拟主机配置文件/usr/local/nginx/conf/vhost/oneinstack.com.conf
access_by_lua_file “/usr/local/nginx/conf/lua/waf.lua”;
测试
一分钟之内,一个页面快速点击20次以上,登录redis,看到black开通的key即被禁止访问(nginx 503)

标签:http,lua,CC,ngx,Redis,Lua,nginx,--,local
来源: https://blog.csdn.net/m0_53869971/article/details/112759804

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有