溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Nginx+Lua實現(xiàn)的WAF網(wǎng)站防護功能

發(fā)布時間:2020-07-21 23:09:13 來源:網(wǎng)絡(luò) 閱讀:2002 作者:Mr_sheng 欄目:建站服務(wù)器


一.OpenResty安裝和測試

官方網(wǎng)站:https://openresty.org/cn/ 

OpenResty?

OpenResty? 是一個基于 Nginx 與 Lua 的高性能 Web 平臺,其內(nèi)部集成了大量精良的 Lua 庫、第三方模塊以及大多數(shù)的依賴項。用于方便地搭建能夠處理超高并發(fā)、擴展性極高的動態(tài) Web 應(yīng)用、Web 服務(wù)和動態(tài)網(wǎng)關(guān)。

OpenResty? 通過匯聚各種設(shè)計精良的 Nginx 模塊(主要由 OpenResty 團隊自主開發(fā)),從而將 Nginx 有效地變成一個強大的通用 Web 應(yīng)用平臺。這樣,Web 開發(fā)人員和系統(tǒng)工程師可以使用 Lua 腳本語言調(diào)動 Nginx 支持的各種 C 以及 Lua 模塊,快速構(gòu)造出足以勝任 10K 乃至 1000K 以上單機并發(fā)連接的高性能 Web 應(yīng)用系統(tǒng)。

OpenResty? 的目標是讓你的Web服務(wù)直接跑在 Nginx 服務(wù)內(nèi)部,充分利用 Nginx 的非阻塞 I/O 模型,不僅僅對 HTTP 客戶端請求,甚至于對遠程后端諸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都進行一致的高性能響應(yīng)。


LUA學(xué)習(xí):http://blog.jobbole.com/70480/


1.安裝OpenResty:

# yum install -y readline-devel pcre-devel openssl-devel

# cd /usr/local/src

下載并編譯安裝openresty

# wget https://openresty.org/download/ngx_openresty-1.9.3.2.tar.gz

# tar zxf ngx_openresty-1.9.3.2.tar.gz

# cd ngx_openresty-1.9.3.2

# ./configure --prefix=/usr/local/ngx_openresty-1.9.3.2 \

--with-luajit --with-http_stub_status_module \

--with-pcre --with-pcre-jit

# gmake && gmake install

# ln -s /usr/local/openresty-1.9.3.2/ /usr/local/openresty


#報錯提醒:

在./configure時的報錯:

/usr/bin/env: perl: No such file or directory

出現(xiàn)這種錯誤可能是沒有安裝perl,解決yum install perl -y

[root@localhost ngx_openresty-1.9.3.2]# ./configure --prefix=/usr/local/openresty-1.9.3.2 \--with-luajit --with-http_stub_status_module \--with-pcre --with-pcre-jit
platform: linux (linux)
cp -rp bundle/ build
cd build
cd LuaJIT-2.1-20151028
Can't exec "cc": No such file or directory at ./configure line 588.
gmake TARGET_STRIP=@: CCDEBUG=-g CC=cc PREFIX=/usr/local/openresty-1.9.3.2/luajit
==== Building LuaJIT 2.1.0-beta1 ====
gmake -C src
gmake[1]: cc: Command not found
gmake[1]: Entering directory `/usr/local/ngx_openresty-1.9.3.2/build/LuaJIT-2.1-20151028/src'
gmake[1]: cc: Command not found
gmake[1]: cc: Command not found
gmake[1]: cc: Command not found
gmake[1]: cc: Command not found
gmake[1]: cc: Command not found
Makefile:237: *** Unsupported target architecture.  Stop.
gmake[1]: Leaving directory `/usr/local/ngx_openresty-1.9.3.2/build/LuaJIT-2.1-20151028/src'
gmake: *** [default] Error 2
ERROR: failed to run command: gmake TARGET_STRIP=@: CCDEBUG=-g CC=cc PREFIX=/usr/local/openresty-1.9.3.2/luajit

解決:

yum install *gcc* -y


2.測試openresty安裝:

vim /usr/local/openresty/nginx/conf/nginx.conf

   server {
            listen       80;
            server_name  localhost;    
        location / {
            root   html;
            index  index.html index.htm;
        }
        ......此處省略
       }

在此處省略處,添加一行

         location /hello {
             default_type text/html;
             content_by_lua_block {
             ngx.say("HelloWorld")
            }


3.啟動openresty并測試:

/usr/local/openresty/nginx/sbin/nginx -t 

/usr/local/openresty/nginx/sbin/nginx


[root@localhost ~]# curl http://10.0.0.50/hello
HelloWorld


4. 性能測試

-- 1. 安裝壓力測試工具

[root@localhost ~]# yum install httpd-tools -y


-- 2. 測試

[root@localhost ~]# ab -c10 -n5000 http://10.0.0.50/hello
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 10.0.0.50 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software:        openresty/1.9.3.2
Server Hostname:        10.0.0.50
Server Port:            80
Document Path:          /hello
Document Length:        11 bytes
Concurrency Level:      10
Time taken for tests:   0.505 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      790000 bytes
HTML transferred:       55000 bytes
Requests per second:    9901.83 [#/sec] (mean)
Time per request:       1.010 [ms] (mean)
Time per request:       0.101 [ms] (mean, across all concurrent requests)
Transfer rate:          1527.82 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:     0    1   0.3      1       2
Waiting:        0    0   0.2      0       2
Total:          1    1   0.3      1       3
Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      2
  98%      2
  99%      2
 100%      3 (longest request)



...

需求產(chǎn)生 由于原生態(tài)的Nginx的一些安全防護功能有限,就研究能不能自己編寫一個WAF,參考(照抄)Kindle大神的ngx_lua_waf,自己嘗試寫一個了,使用兩天時間,邊學(xué)Lua,邊寫。不過不是安全專業(yè),只實現(xiàn)了一些比較簡單的功能:

####功能列表:

  1. 支持IP白名單和黑名單功能,直接將黑名單的IP訪問拒絕。

  2. 支持URL白名單,將不需要過濾的URL進行定義。

  3. 支持User-Agent的過濾,匹配自定義規(guī)則中的條目,然后進行處理(返回403)。

  4. 支持CC***防護,單個URL指定時間的訪問次數(shù),超過設(shè)定值,直接返回403。

  5. 支持Cookie過濾,匹配自定義規(guī)則中的條目,然后進行處理(返回403)。

  6. 支持URL過濾,匹配自定義規(guī)則中的條目,如果用戶請求的URL包含這些,返回403。

  7. 支持URL參數(shù)過濾,原理同上。

  8. 支持日志記錄,將所有拒絕的操作,記錄到日志中去。

  9. 日志記錄為JSON格式,便于日志分析,例如使用ELKStack進行***日志收集、存儲、搜索和展示。

####WAF實現(xiàn) WAF一句話描述,就是解析HTTP請求(協(xié)議解析模塊),規(guī)則檢測(規(guī)則模塊),做不同的防御動作(動作模塊),并將防御過程(日志模塊)記錄下來。所以本文中的WAF的實現(xiàn)由五個模塊(配置模塊、協(xié)議解析模塊、規(guī)則模塊、動作模塊、錯誤處理模塊)組成。


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI