Nginx+Lua腳本+Redis 實(shí)現(xiàn)自動(dòng)封禁訪問頻率過高IP
自己搭建的網(wǎng)站剛上線,短信接口就被一直攻擊,并且攻擊者不停變換IP,導(dǎo)致阿里云短信平臺(tái)上的短信被惡意刷取了幾千條,加上最近工作比較忙,就直接在OpenResty上對(duì)短信接口做了一些限制,采用OpenResty+Lua的方案成功動(dòng)態(tài)封禁了頻繁刷短信接口的IP。
一、臨時(shí)解決方案
由于事情比較緊急,所以,當(dāng)發(fā)現(xiàn)這個(gè)問題時(shí),就先采用快速的臨時(shí)方案解決。
(1)查看Nginx日志發(fā)現(xiàn)被攻擊的IP 和接口
[root@binghe ~]# tail -f /var/log/nginx/access.log
發(fā)現(xiàn)攻擊者一直在用POST請(qǐng)求 /fhtowers/user/getVerificationCode這個(gè)接口
(2)用awk和grep腳本過濾nginx日志,提取攻擊短信接口的ip(一般這個(gè)接口是用來發(fā)注冊(cè)驗(yàn)證碼的,一分鐘如果大于10次請(qǐng)求的話就不是正常的訪問請(qǐng)求了,大家根據(jù)自己的實(shí)際情況更改腳本)并放到一個(gè)txt文件中去,然后重啟nginx
[root@binghe ~]# cat denyip.sh #!/bin/bash nginx_home=/usr/local/openresty/nginx log_path=/var/log/nginx/access.log tail -n5000 $log_path | grep getVerification | awk '{print $1}' |sort | uniq -c | sort -nr -k1 | head -n 100 |awk '{if($1>10)print ""$2""}' >$nginx_home/denyip/blocksip.txt /usr/bin/nginx -s reload
(3)設(shè)置Nginx去讀取用腳本過濾出來的blocksip.txt(注意一下,我這里的Nginx是用的openresty,自帶識(shí)別lua語法的,下面會(huì)有講openresty的用法)
location = /fhtowers/user/getVerificationCode { #短信接口 access_by_lua ' local f = io.open("/usr/local/openresty/nginx/denyip/blocksip.txt") #黑名單列表 for line in f:lines() do if ngx.var.http_x_forwarded_for == line then #如果ip在黑名單列表里直接返回403 ngx.exit(ngx.HTTP_FORBIDDEN) end end '; proxy_pass http://appservers; #不在名單里就轉(zhuǎn)發(fā)給后臺(tái)的tomcat服務(wù)器 }
(4)把過濾腳本放進(jìn)crontab任務(wù)里,一分鐘執(zhí)行一次
[root@binghe ~]# crontab -e */1 * * * * sh /root/denyip.sh
(5)查看一下效果,發(fā)現(xiàn)攻擊者的請(qǐng)求都被返回403并拒絕了
二、OpenResty+Lua方案
臨時(shí)方案有效果后,再將其調(diào)整成使用OpenResty+Lua腳本的方案,來一張草圖。
接下來,就是基于OpenResty和Redis實(shí)現(xiàn)自動(dòng)封禁訪問頻率過高的IP。
2.1 安裝OpenResty
安裝使用 OpenResty,這是一個(gè)集成了各種 Lua 模塊的 Nginx 服務(wù)器,是一個(gè)以Nginx為核心同時(shí)包含很多第三方模塊的Web應(yīng)用服務(wù)器,使用Nginx的同時(shí)又能使用lua等模塊實(shí)現(xiàn)復(fù)雜的控制。
(1)安裝編譯工具、依賴庫
[root@test1 ~]# yum -y install readline-devel pcre-devel openssl-devel gcc
(2)下載openresty-1.13.6.1.tar.gz 源碼包,并解壓;下載ngx_cache_purge模塊,該模塊用于清理nginx緩存;下載nginx_upstream_check_module模塊,該模塊用于ustream健康檢查。
[root@test1 ~]# cd /usr/local/ [root@test1 local]# wget https://openresty.org/download/openresty-1.13.6.1.tar.gz [root@test1 local]# tar -zxvf openresty-1.13.6.1.tar.gz [root@test1 local]# cd openresty-1.13.6.1/bundle [root@test1 local]# wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz [root@test1 local]# tar -zxvf ngx_cache_purge-2.3.tar.gz [root@test1 local]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz [root@test1 local]# tar -zxvf v0.3.0.tar.gz
(3)配置需安裝的模塊
# ./configure --help可查詢需要安裝的模塊并編譯安裝 [root@test1 openresty-1.13.6.1]# ./configure --prefix=/usr/local/openresty --with-luajit --with-http_ssl_module --user=root --group=root --with-http_realip_module --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ --with-http_stub_status_module [root@test1 openresty-1.13.6.1]# make && make install
(4)創(chuàng)建一個(gè)軟鏈接方便啟動(dòng)停止
[root@test1 ~]# ln -s /usr/local/openresty/nginx/sbin/nginx /bin/nginx
(5)啟動(dòng)nginx
[root@test1 ~]# nginx #啟動(dòng) [root@test1 ~]# nginx -s reload #reload配置
如果啟動(dòng)時(shí)候報(bào)錯(cuò)找不到PID的話就用以下命令解決(如果沒有更改過目錄的話,讓它去讀nginx的配置文件就好了)
[root@test1 ~]# /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
隨后,打開瀏覽器訪問頁面。
(6)在Nginx上測(cè)試一下能否使用Lua腳本
[root@test1 ~]# vim /usr/local/openresty/nginx/conf/nginx.conf
在server里面加一個(gè)
location /lua { default_type text/plain; content_by_lua ‘ngx.say(“hello,lua!”)'; }
加完后重新reload配置。
[root@test1 ~]# nginx -s reload
在瀏覽器里輸入 ip地址/lua,出現(xiàn)下面的字就表示Nginx能夠成功使用lua了
2.2 安裝Redis
(1)下載、解壓、編譯安裝
[root@test1 ~]# cd /usr/local/ [root@test1 local]# wget http://download.redis.io/releases/redis-6.0.1.tar.gz [root@test1 local]# tar -zxvf redis-6.0.1.tar.gz [root@test1 local]# cd redis-6.0.1 [root@test1 redis-6.0.1]# make [root@test1 redis-6.0.1]# make install
(2)查看是否安裝成功
[root@test1 redis-6.0.1]# ls -lh /usr/local/bin/ [root@test1 redis-6.0.1]# redis-server -v Redis server v=3.2.5 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=dae2abf3793b309d
(3)配置redis 創(chuàng)建dump file、進(jìn)程pid、log目錄
[root@test1 redis-6.0.1]# cd /etc/ [root@test1 etc]# mkdir redis [root@test1 etc]# cd /var/ [root@test1 var]# mkdir redis [root@test1 var]# cd redis/ [root@test1 redis]# mkdir data log run
(4)修改配置文件
[root@test1 redis]# cd /usr/local/redis-6.0.1/ [root@test1 redis-6.0.1]# cp redis.conf /etc/redis/6379.conf [root@test1 redis-6.0.1]# vim /etc/redis/6379.conf #綁定的主機(jī)地址 bind 192.168.1.222 #端口 port 6379 #認(rèn)證密碼(方便測(cè)試不設(shè)密碼,注釋掉) #requirepass #pid目錄 pidfile /var/redis/run/redis_6379.pid #log存儲(chǔ)目錄 logfile /var/redis/log/redis.log #dump目錄 dir /var/redis/data #Redis默認(rèn)不是以守護(hù)進(jìn)程的方式運(yùn)行,可以通過該配置項(xiàng)修改,使用yes啟用守護(hù)進(jìn)程 daemonize yes
(5)設(shè)置啟動(dòng)方式
[root@test1 redis-6.0.1]# cd /usr/local/redis-6.0.1/utils/ [root@test1 utils]# cp redis_init_script /etc/init.d/redis [root@test1 utils]# vim /etc/init.d/redis #根據(jù)自己實(shí)際情況修改
/etc/init.d/redis文件的內(nèi)容如下。
#!/bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. REDISPORT=6379 EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/etc/redis/${REDISPORT}.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac
增加執(zhí)行權(quán)限,并啟動(dòng)Redis。
[root@test1 utils]# chmod a+x /etc/init.d/redis #增加執(zhí)行權(quán)限 [root@test1 utils]# service redis start #啟動(dòng)redis
(6)查看redis是否啟動(dòng)
2.3 Lua訪問Redis
(1)連接redis,然后添加一些測(cè)試參數(shù)
[root@test1 utils]# redis-cli -h 192.168.1.222 -p 6379 192.168.1.222:6379> set "123" "456" OK
(2)編寫連接Redis的Lua腳本
[root@test1 utils]# vim /usr/local/openresty/nginx/conf/lua/redis.lua local redis = require "resty.redis" local conn = redis.new() conn.connect(conn, '192.168.1.222', '6379') #根據(jù)自己情況寫ip和端口號(hào) local res = conn:get("123") if res==ngx.null then ngx.say("redis集群中不存在KEY——'123'") return end ngx.say(res)
(3)在nginx.conf配置文件中的server下添加以下location
[root@test1 utils]# vim /usr/local/openresty/nginx/conf/nginx.conf location /lua_redis { default_type text/plain; content_by_lua_file /usr/local/openresty/nginx/conf/lua/redis.lua; }
隨后重新reload配置。
[root@test1 utils]# nginx -s reload #重啟一下Nginx
(4)驗(yàn)證Lua訪問Redis的正確性
在瀏覽器輸入ip/lua_redis, 如果能看到下圖的內(nèi)容表示Lua可以訪問Redis。
準(zhǔn)備工作已經(jīng)完成,現(xiàn)在要實(shí)現(xiàn)OpenResty+Lua+Redis自動(dòng)封禁并解封IP了。3.4
2.4 OpenResty+Lua實(shí)現(xiàn)
(1)添加訪問控制的Lua腳本(只需要修改Lua腳本中連接Redis的IP和端口即可)
ok, err = conn:connect(“192.168.1.222”, 6379)
注意:如果在Nginx或者OpenResty的上層有用到阿里云的SLB負(fù)載均衡的話,需要修改一下腳本里的所有…ngx.var.remote_addr,把remote_addr替換成從SLB獲取真實(shí)IP的字段即可,不然獲取到的IP全都是阿里云SLB發(fā)過來的并且是處理過的IP,同時(shí),這些IP全都是一個(gè)網(wǎng)段的,根本沒有辦法起到封禁的效果)。
完整的Lua腳本如下所示。
[root@test1 lua]# vim /usr/local/openresty/nginx/conf/lua/access.lua local ip_block_time=300 --封禁IP時(shí)間(秒) local ip_time_out=30 --指定ip訪問頻率時(shí)間段(秒) local ip_max_count=20 --指定ip訪問頻率計(jì)數(shù)最大值(秒) local BUSINESS = ngx.var.business --nginx的location中定義的業(yè)務(wù)標(biāo)識(shí)符,也可以不加,不過加了后方便區(qū)分 --連接redis local redis = require "resty.redis" local conn = redis:new() ok, err = conn:connect("192.168.1.222", 6379) conn:set_timeout(2000) --超時(shí)時(shí)間2秒 --如果連接失敗,跳轉(zhuǎn)到腳本結(jié)尾 if not ok then goto FLAG end --查詢ip是否被禁止訪問,如果存在則返回403錯(cuò)誤代碼 is_block, err = conn:get(BUSINESS.."-BLOCK-"..ngx.var.remote_addr) if is_block == '1' then ngx.exit(403) goto FLAG end --查詢r(jià)edis中保存的ip的計(jì)數(shù)器 ip_count, err = conn:get(BUSINESS.."-COUNT-"..ngx.var.remote_addr) if ip_count == ngx.null then --如果不存在,則將該IP存入redis,并將計(jì)數(shù)器設(shè)置為1、該KEY的超時(shí)時(shí)間為ip_time_out res, err = conn:set(BUSINESS.."-COUNT-"..ngx.var.remote_addr, 1) res, err = conn:expire(BUSINESS.."-COUNT-"..ngx.var.remote_addr, ip_time_out) else ip_count = ip_count + 1 --存在則將單位時(shí)間內(nèi)的訪問次數(shù)加1 if ip_count >= ip_max_count then --如果超過單位時(shí)間限制的訪問次數(shù),則添加限制訪問標(biāo)識(shí),限制時(shí)間為ip_block_time res, err = conn:set(BUSINESS.."-BLOCK-"..ngx.var.remote_addr, 1) res, err = conn:expire(BUSINESS.."-BLOCK-"..ngx.var.remote_addr, ip_block_time) else res, err = conn:set(BUSINESS.."-COUNT-"..ngx.var.remote_addr,ip_count) res, err = conn:expire(BUSINESS.."-COUNT-"..ngx.var.remote_addr, ip_time_out) end end -- 結(jié)束標(biāo)記 ::FLAG:: local ok, err = conn:close()
(2)在需要做訪問限制的location里加兩段代碼即可,這里用剛才的/lua做演示
[root@test1 lua]# vim /usr/local/openresty/nginx/conf/nginx.conf
主要是添加如下配置。
access_by_lua_file /usr/local/openresty/nginx/conf/lua/access.lua;
其中,set $business “lua”
是為了把IP放進(jìn)Redis的時(shí)候標(biāo)明是哪個(gè)location的,可以不加這個(gè)配置。
隨后,重新reload配置。
[root@test1 lua]# nginx -s reload #修改完后重啟nginx
(3)打開瀏覽器訪問192.168.1.222/lua 并一直按F5刷新。
隨后,連接Redis,查看IP的訪問計(jì)數(shù)。
[root@test1 ~]# redis-cli -h 192.168.1.222 -p 6379
發(fā)現(xiàn)redis已經(jīng)在統(tǒng)計(jì)訪問lua這個(gè)網(wǎng)頁ip的訪問次數(shù)了
這個(gè)key的過期時(shí)間是30秒,如果30秒沒有重復(fù)訪問20次這個(gè)key就會(huì)消失,所以說正常用戶一般不會(huì)觸發(fā)這個(gè)封禁的腳本。
當(dāng)30秒內(nèi)訪問超過了20次,發(fā)現(xiàn)觸發(fā)腳本了,變成了403
再次查看Redis的key,發(fā)現(xiàn)多了一個(gè)lua-block-192.168.1.158,過期時(shí)間是300秒,就是說在300秒內(nèi)這個(gè)ip無法繼續(xù)訪問192.168.1.222/lua這個(gè)頁面了。
過五分鐘后再去訪問這個(gè)頁面,又可以訪問了。
這個(gè)腳本的目的很簡(jiǎn)單:一個(gè)IP如果在30秒內(nèi)其訪問次數(shù)達(dá)到20次則表明該IP訪問頻率太快了,因此將該IP封禁5分鐘。同時(shí)由于計(jì)數(shù)的KEY在Redis中的超時(shí)時(shí)間設(shè)置成了30秒,所以如果兩次訪問間隔時(shí)間大于30秒將會(huì)重新開始計(jì)數(shù)。
大家也可以將這個(gè)腳本優(yōu)化成,第一次封禁5分鐘,第二次封禁半小時(shí),第三次封禁半天,第四次封禁三天,第五次永久封禁等等。
到此這篇關(guān)于Nginx+Lua腳本+Redis 實(shí)現(xiàn)自動(dòng)封禁訪問頻率過高IP的文章就介紹到這了,更多相關(guān)Nginx+Lua+Redis 自動(dòng)封禁IP內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx代理去掉URL前綴的實(shí)現(xiàn)方法
nginx作為一款廣泛使用的反向代理服務(wù)器,在實(shí)際應(yīng)用中,經(jīng)常需要去掉代理請(qǐng)求中的前綴,下面這篇文章主要給大家介紹了關(guān)于nginx代理去掉URL前綴的實(shí)現(xiàn)方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05Nginx+PHP8.0支持視頻上傳的項(xiàng)目實(shí)踐
在Ubuntu 20.04上配置Nginx和PHP 8.0以支持視頻上傳,介紹了調(diào)整Nginx和PHP的配置文件,增加上傳文件大小限制,調(diào)整超時(shí)時(shí)間和緩沖區(qū)大小等相關(guān)配置,感興趣的可以了解一下2025-02-02nginx、Apache、IIS服務(wù)器解決 413 Request Entity Too Large問題方法匯總
這篇文章主要介紹了nginx、Apache、IIS三種服務(wù)器解決413 Request Entity Too Large問題的方法集合,需要的朋友可以參考下2014-05-05nginx: [warn] "log_format" directive used only on "http" lev
這篇文章主要介紹了nginx: [warn] "log_format" directive used only on "http" level 解決方法,需要的朋友可以參考下2014-08-08Nginx反向代理出現(xiàn)502?Bad?Gateway問題解決
在配置Nginx反向代理時(shí)遇到502 Bad Gateway錯(cuò)誤,經(jīng)過排查發(fā)現(xiàn)是SSL握手問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10詳解ngx_cache_purge _proxy_cache指令使用
本文主要介紹了詳解ngx_cache_purge _proxy_cache指令使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07