OpenResty(nginx扩展)实现防cc攻击 - Linux就该这么学 - 博客园


本站和网页 https://www.cnblogs.com/linuxprobe/p/5615581.html 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

OpenResty(nginx扩展)实现防cc攻击 - Linux就该这么学 - 博客园
首页
新闻
博问
专区
闪存
班级
我的博客
我的园子
账号设置
简洁模式 ...
退出登录
注册
登录
Linux就该这么学
《Linux就该这么学》是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程。
博客园
首页
新随笔
联系
订阅
管理
OpenResty(nginx扩展)实现防cc攻击
OpenResty(nginx扩展)实现防cc攻击
导读
OpenResty 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统
流程图
本文介绍使用openresty来实现防cc攻击的功能。openresty官网http://openresty.org/cn/index.html。下面是防cc攻击的流程图。
根据流程图,我们知道防cc攻击主要包括两部分,一是限制请求速度,二是给用户发送js跳转代码进行验证请求是否合法。
安装依赖
RHEL/Centos:
yum install readline-devel pcre-devel openssl-devel
ubuntu:
apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl
luajit安装
cd /tmp/
git clone http://luajit.org/git/luajit-2.0.git
cd luajit-2.0/
make && make install
ln -sf luajit-2.0.0-beta10 /usr/local/bin/luajit
ln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/lib/
openresty安装
cd /tmp
wget http://agentzh.org/misc/nginx/ngx_openresty-1.2.4.13.tar.gz
tar xzf ngx_openresty-1.2.4.13.tar.gz
cd ngx_openresty-1.2.4.13/
./configure --prefix=/usr/local/openresty --with-luajit
make && make install
nginx配置
nginx.conf:
http{
[......]
lua_shared_dict limit 10m;
lua_shared_dict jsjump 10m;
server {
#lua_code_cache off;
listen 80;
server_name www.centos.bz;
location / {
default_type text/html;
content_by_lua_file "/usr/local/openresty/nginx/conf/lua";
location @cc {
internal;
root html;
index index.html index.htm;
/usr/local/openresty/nginx/conf/lua文件:
local ip = ngx.var.binary_remote_addr
local limit = ngx.shared.limit
local req,_=limit:get(ip)
if req then
if req > 20 then
ngx.exit(503)
else
limit:incr(ip,1)
end
else
limit:set(ip,1,10)
end
local jsjump = ngx.shared.jsjump
local uri = ngx.var.request_uri
local jspara,flags=jsjump:get(ip)
local args = ngx.req.get_uri_args()
if jspara then
if flags then
ngx.exec("@cc")
else
local p_jskey=''
if args["jskey"] and type(args["jskey"])=='table' then
p_jskey=args["jskey"][table.getn(args["jskey"])]
else
p_jskey=args["jskey"]
end
if p_jskey and p_jskey==tostring(jspara) then
jsjump:set(ip,jspara,3600,1)
ngx.exec("@cc")
else
local url=''
if ngx.var.args then
url=ngx.var.scheme.."://"..ngx.var.host..uri.."&jskey="..jspara
else
url=ngx.var.scheme.."://"..ngx.var.host..uri.."?jskey="..jspara
end
local jscode="window.location.href='"..url.."';"
ngx.say(jscode)
end
end
else
math.randomseed( os.time() );
local random=math.random(100000,999999)
jsjump:set(ip,random,60)
local url=''
if ngx.var.args then
url=ngx.var.scheme.."://"..ngx.var.host..uri.."&jskey="..random
else
url=ngx.var.scheme.."://"..ngx.var.host..uri.."?jskey="..random
end
local jscode="window.location.href='"..url.."';"
ngx.say(jscode)
end
lua代码部分解释:
1、1-12行是限速功能实现,第5和第10行表示10秒钟内容最多只能请求20次。
2、14-48行是验证部分,24行中的3600表示验证通过后,白名单时间为3600秒,即1小时。
update: 2013.5.26
1、修复JS无限跳转bug
2、增加随机种子
免费提供最新Linux技术教程书籍,为开源技术爱好者努力做得更多更好:https://www.linuxprobe.com/
posted @
2016-06-25 22:20
Linux就该这么学
阅读(2902)
评论(0)
编辑
收藏
举报
刷新评论刷新页面返回顶部
Copyright 2022 Linux就该这么学
Powered by .NET 7.0 on Kubernetes

Copyright ©uecom 京ICP备18064371号-3 IPV6
2024-03-29 09:35:21
tech.zxsbr.com
10.0.12.16