- A+
所属分类:Nginx
工欲善其事必先利其器,要想抵御防范ddos和dcc攻击,我们需要先了解什么是ddos和dcc攻击
攻击的特点、攻击方式。。。
**DDoS:Distributed Denial of Service**
分布式拒绝服务:借助于客户/服务器技术,将多个计算机联合起来作为攻击平台,对一个或
多个目标发动DDoS攻击,从而成倍地提高拒绝服务攻击的威力。通俗的来讲就是模拟用户的正常访问,
进而在客户端与服务器之间连理连接(TCP的三次握手),当连接进行到第二步之后不再继续响应服务
器的确认。服务器判断连接超时一般是有一个时间限制上的延迟,通过无线的放大这个延时空间,进而
让正常的用户访问一直在队列中不能正常的建立连接获取web服务器的数据。
CC:ChallengeCollapsar
攻击者借助代理服务器生成指向受害主机的合法请求,实现DDOS和伪装就叫CC攻击:通俗的
来讲cc其实就是模拟的用户浏览器访问页面,造成巨额的用户访问,导致服务器cpu内存带宽持续
飙高,阻断正常的用户访问。这种访问几乎可以完美的模拟用户的正常访问请求,然而要想达到
完美的模拟用户请求是建立在极高的成本上的。
这样就好办了
现在我们了解了两种攻击方式的区别和原理,那么我们就可以根据其特点来进行针对性的抵御
-------------------------------------------------------------------------------
**一:实际环境**
centos7
openresty(nginx二次开发版本)
redis4.0
建议使用nginx二次开发版本的openresty,内置了调用lua模块的功能。
如使用nginx则需要添加
--add-module=src/ngx_devel_kit-0.3.0
--add-module=src/lua-nginx-module-master
-------------------------------------------------------------------------------
**二: 编译安装库LuaJit-2.0.3:**
./configure --prefix=/usr/local/luajit
make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit
在/etc/profile文件中增加环境变量,并执行 source /etc/profile 使之生效(非必须):
export LUAJIT_LIB=/usr/install/luajit/lib
export LUAJIT_INC=/usr/install/luajit/include/luajit-2.0
注:此步骤只是在你的系统没有安装 LuaJIT 的情况下才需要自行编译安装。
-------------------------------------------------------------------------------
**三: 使用lua连接Redis**
(1)(下载连接lua连接操作Redis的必要文件redis.lua)
下载地址:https://pan.baidu.com/s/1n0GHV64xHh2e_vHR8xTI9A
可将其存放在指定文件夹下如:/usr/local/lua-resty/
(2)在nginx.conf中调用该文件
lua_package_path "/usr/local/lua-resty/redis.lua;;";
(3)在需要使用该配置的location段或者serve段调用intercept_ip.lua脚本路径
access_by_lua_file /usr/local/openresty/nginx/conf/intercept_ip.lua;
-------------------------------------------------------------------------------
**四:创建nginx调用Redis的lua脚本,文件名叫intercept_ip.lua**
--封禁IP时间
ip_bind_time = 2
--指定ip访问频率时间段
ip_time_out = 5
--指定客户端ip访问频率计数最大值
connect_count = 30
local host_name = ngx.var.remote_addr
--连接redis
local redis = require "resty.redis"
local cache = redis.new()
local ok , err = cache.connect(cache,"127.0.0.1","6379")
cache:set_timeout(60000)
--如果连接失败,跳转到脚本结尾
if not ok then
goto A
end
查询ip是否在封禁段内,若在则返回403错误代码
--
----因封禁时间会大于ip记录时间,故此处不对ip时间key和计数key做处理
--
is_bind , err = cache:hget(host_name , "bind")
--
----判断是否是被标记的ip,如果已经被标记则检测设置的cookie是否正常返回
--
---cookie正常返回则判断是浏览器行为,删除记录值,否则认为是非正常流量,返回403
--hdel==hash delete
if is_bind == '1' then
is_token , err = cache:hget(host_name , "token")
if (ngx.var.cookie_token == is_token)then
--
res , err = cache:hdel(host_name , "bind")
--
res , err = cache:hdel(host_name , "token")
res , err = cache:hdel(host_name , "count")
res , err = cache:hdel(host_name , "time")
--
goto A
else
--
res , err = cache:hdel(host_name , "token")
--这里可以自定义拒绝访问时返回的状态码,这里我们返回503,对应的我们创建了一个503页面,提醒用户刷新频繁
ngx.exit(503)
end
goto A
--
end
--
start_time , err = cache:hget(host_name , "time")
--
ip_count , err = cache:hget(host_name , "count")
--
----如果ip记录时间大于指定时间间隔或者记录时间或者不存在ip时间key则重置时间key和计数key
--
----如果ip时间key小于时间间隔,则ip计数+1,且如果ip计数大于ip频率计数,则设置ip的封禁key为1
--
----同时设置封禁key的过期时间为封禁ip的时间
--
if start_time == ngx.null or os.time() - start_time > ip_time_out then
--
res , err = cache:hset(host_name , "time" , os.time())
res , err = cache:hset(host_name , "count" , 1)
--
else
--
ip_count = ip_count + 1
--
res , err = cache:hincrby(host_name , "count" , 1)
--
if ip_count >= connect_count then
--
res , err = cache:hset(host_name , "bind" , 1)
--
res , err = cache:expire(host_name , ip_bind_time)
--
local token = ngx.md5(random)
--
res , err = cache:hset(host_name , "token" , token)
--如果Cookie信息能够正常查询到,则不限制。
ngx.header["Set-Cookie"] = {"token="..token}
--
end
--
end
--
----结尾标记
--
::A::
--
local ok, err = cache:close()
测试方式:好了现在所有的服务器都设置好了,接下来我们启动nginx,Apache,Redis
打开个人网站的首页快速的F5舒心页面等待返回提示的503页面友好提示吧,
值得一提的是这里可以自由灵活的设置封禁IP的时长和拒绝策略。不要再让攻击苦恼你了。