利用Nginx+lua擴展+redis實現網站拒絕高頻次IP訪問

一:基礎環境

CentOS6.2

nginx1.11.10

redis2.8.21

二:擴展安裝

(1)先安裝Nginx需要的一些類庫:

yum install gcc

yum install gcc-c++

注:此步驟只是在你的系統沒有安裝 gcc/gcc-c++?的情況下才需要自行編譯安裝。

(2)編譯安裝庫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 的情況下才需要自行編譯安裝。

(3)下載模塊依賴?pcre-8.34、zlib-1.2.8、ngx_devel_kit 和 lua-nginx-module,最后編譯Nginx:

完整的參數可能這樣:

nginx -V

nginx version: nginx/1.11.10

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_realip_module --add-module=src/http/modules/nginx_upstream_check_module --add-module=src/http/modules/ngx_cache_purge/ --with-http_sub_module --add-module=src/http/modules/nginx-http-concat --with-http_random_index_module --add-module=src/ngx_devel_kit-0.3.0 --add-module=src/lua-nginx-module-master --add-module=src/http/modules/ngx_http_substitutions_filter_module-master/

三:lua使用redis

(1)下載lua連接操作redis必須文件

https://github.com/openresty/lua-resty-redis/blob/master/lib/resty/redis.lua

可將此文件放置于新創建的文件夾下

/usr/local/lua-resty-redis/

(2)nginx中引用

lua_package_path "/usr/local/lua-resty/redis.lua;;";

注:連接redis擴展包的引用要寫在server之外

四:lua連接redis

文件名叫intercept_ip.lua

local redis = require "resty.redis"

local cache = redis.new()

local ok , err = cache.connect(cache,"127.0.0.1","6379")

cache:set_timeout(60000)

如果redis配置了requirepass,需要加一行

ok , err = cache:auth("相應的驗證密碼")

五:實現記錄客戶端IP與訪問時間并對頻次進行限制

(1)訪問頻次設置為30秒10次,進行測試,具體值根據實際場景需求進行設置

ip_time_out = 30

connect_count = 10

local host_name = ngx.var.remote_addr

start_time , err = cache:get(host_name .. "_time")

ip_count , err = cache:get(host_name .."_count")

if is_bind == '1' then

? ? ngx.exit(403)

end

if start_time == ngx.null or os.time() - start_time > ip_time_out then

? ? res , err = cache:set(host_name .. "_time" , os.time())

? ? res , err = cache:set(host_name .. "_count" , 1)

else

? ? ip_count = ip_count + 1

? ? res , err = cache:incr(host_name .. "_count" )

? ? if ip_count >= connect_count then

? ? ? ? res , err = cache:set(host_name .. "_bind" , 1)

? ? end

end

以上針對IP訪問次數進行了屏蔽,但問題依然存在。

第一:采集用的IP通常是不固定的,也就是說每隔一段時間,IP資源會重置,舊的IP資源可能會被分配到正常用戶的客戶端上。所以我們要對IP設置解封時間

第二:如果是公司接入專線,有可能幾十人或者近百人同時訪問。所以我們要做一個可以校驗是否為瀏覽器的測試。

(2)根據以上問題對代碼進行完善

--封禁IP時間

ip_bind_time = 30

--指定ip訪問頻率時間段

ip_time_out = 30

--指定ip訪問頻率計數最大值

connect_count = 10

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")

local random = math.random(99999)

cache:set_timeout(60000)

--如果連接失敗,跳轉到腳本結尾

if not ok then

? ? goto A

end

--查詢ip是否在封禁段內,若在則返回403錯誤代碼

--因封禁時間會大于ip記錄時間,故此處不對ip時間key和計數key做處理

is_bind , err = cache:get("bind_"..host_name)

--判斷是否是被標記的ip,如果已經被標記則檢測設置的cookie是否正常返回

--cookie正常返回則判斷是瀏覽器行為,刪除記錄值,否則認為是非正常流量,返回403

if is_bind == '1' then

? ? is_token , err = cache:get("token_"..host_name)

? ? if (ngx.var.cookie_token == is_token)then

? ? ? ? res , err = cache:del("bind_"..host_name)

? ? ? ? res , err = cache:del("token_"..host_name)

? ? ? ? res , err = cache:del("count_"..host_name)

? ? ? ? res , err = cache:del("time_"..host_name)

? ? ? ? goto A

? ? else

? ? ? ? res , err = cache:del("token_"..host_name)

? ? ? ? ngx.exit(403)

? ? end

? ? goto A

end

start_time , err = cache:get("time_"..host_name

ip_count , err = cache:get("count_"..host_name)

--如果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:set("time_"..host_name , os.time())

? ? res , err = cache:set("count_"..host_name , 1)

else

? ? ip_count = ip_count + 1

? ? res , err = cache:incr("count_"..host_name)

? ? if ip_count >= connect_count then

? ? ? ? res , err = cache:set("bind_"..host_name,1)

? ? ? ? res , err = cache:expire("bind_"..host_name,ip_bind_time)

? ? ? ? local token = ngx.md5(random)

? ? ? ? res , err = cache:set("token_"..host_name,token)

? ? ? ? ngx.header["Set-Cookie"] = {"token="..token}

? ? end

end

--結尾標記

::A::

local ok, err = cache:close()

以上我們完成了對訪問客戶端IP甄別以及頻次限制的nginx_lua腳本,但每個客戶端IP從訪問到結束,如果走完了被屏蔽的流程,會產生4個key。如果沒產生屏蔽,則會留下兩個key。造成了redis中key的臃腫。我們可以使用redis hash進行優化一下

(3)優化腳本

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

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")

? ? ? ? ngx.exit(403)

? ? 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)

? ? ? ? ngx.header["Set-Cookie"] = {"token="..token}

? ? end

end

--結尾標記

::A::

local ok, err = cache:close()

六:nginx中引用此腳本

location / {

? ? access_by_lua_file conf/intercept_ip.lua;

}

七:結束語

在實際生產場景應用中我們會考慮更多的因素,所以腳本只是根據一部分需求進行的實現,還有更多的需求需要去完善。

防采集,防cc攻擊僅僅只是應對策略,我們無法完全禁止采集行為,但可以通過此類方式大大提高攻擊者的成本,進而保證自身的安全與穩定。

例如:

我們設置訪問頻次為每分鐘訪問70次屬于正常行為

采集者通過測試發現了這個頻次,將程序的訪問頻次設置為每秒鐘1次,這樣雖然保證了網站的穩定性,但數據依然是被拿走了。

因此,此腳本仍然有完善的空間,利用更合適的算法去細化訪問的規則。

歡迎拍磚討論,轉載請注明出處,不勝感激。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容