CentOS 7.7 OpenResty實現WAF應用防火牆

1、WAF相關概念介紹:

(1)WAF簡介:

WAFWeb Appalication FirewallWeb應用防火牆,是一種工作在應用層的、通過一系列針對HTTP/HTTPS的安全策略爲Web應用提供安全防護的產品。

(2)WAF可以實現如下功能:

a、防止SQL注入、本地包含、部分溢出、Fuzzing測試、XSSWeb Attack

b、防止SVN/備份之類的文件泄漏;

c、防止Apache Bench之類的壓測工具Attack

d、屏蔽常見的Hacker掃描工具;

e、屏蔽異常的網絡請求;

f、屏蔽圖片附件類目錄的PHP執行權限;

g、防止Webshell上傳等。

2、安裝依賴軟件包:

# yum -y install gcc gcc-c++ make zlib zlib-devel openssl openssl-devel pcre pcre-devel perl-devel perl-ExtUtils-Embed gd-devel libxml2 libxml2-devel libxslt libxslt-devel GeoIP GeoIP-devel GeoIP-data git libuuid-devel libblkid-devel libudev-devel fuse-devel libedit-devel libatomic_ops-devel httpd-tools

3、編譯安裝OpenResty

# useradd -s /sbin/nologin -M nginx

# mkdir -pv /usr/local/openresty/nginx/logs/

# tar -xf openresty-1.15.8.2.tar.gz -C /usr/src

# cd /usr/src/openresty-1.15.8.2/

# ./configure --prefix=/usr/local/openresty --user=nginx --group=nginx --with-threads --with-file-aio --with-http_iconv_module --with-luajit --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --with-stream_ssl_preread_module --with-pcre --with-pcre-jit --with-libatomic --http-log-path=/usr/local/openresty/nginx/logs/access.log

# gmake && gmake install

4、配置環境變量,並啓動OpenResty

# vim /etc/profile.d/openresty.sh

export PATH=/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin:$PATH

# . /etc/profile.d/openresty.sh

# nginx -v

image.png

# nginx

# ss -tunlp | grep -w :80

image.png

5、測試Lua環境:

# vim /tmp/hello.lua --> print("Hello Lua")

# lua /tmp/hello.lua

image.png

# lua

image.png

6、測試OpenResty Lua模塊:

# cd /usr/local/openresty/nginx/conf

# cp nginx.conf{,.bak}

# vim nginx.conf,在server配置段中新增如下location

location /lua {

default_type  text/html;

content_by_lua_block {

ngx.say("Hello Lua")

}

}

# nginx -t

# nginx -s reload

image.png

7、創建保存***日誌的目錄:

# mkdir -pv /usr/local/openresty/nginx/logs/hack

8、下載解壓ngx_lua_waf模塊:

ngx_lua_waf:基於lua-nginx-moduleWeb應用防火牆,https://github.com/loveshell/ngx_lua_waf

# tar -xf ngx_lua_waf-0.7.2.tar.gz -C /usr/local/openresty/nginx/conf

# cd /usr/local/openresty/nginx/conf

# mv ngx_lua_waf-0.7.2 waf

# chown -R nginx.nginx /usr/local/openresty

備註:waf目錄主要結構

(1)config.lua:配置文件;

(2)init.lua:規則函數;

(3)waf.lua:定義WAF檢測順序;

image.png

(4)wafconf:保存過濾規則的目錄,每條規則需換行或用|分割;

(5)wafconf/args:按照GET參數過濾(默認已開啓);

(6)wafconf/cookie:按照Cookie過濾;

(7)wafconf/post:按照POST請求過濾(默認已開啓);

(8)wafconf/url:按照GET請求URL過濾;

(9)wafconf/user-agent:按照User Agent過濾;

(10)wafconf/whiteurl:按照白名單中的URL做匹配,匹配到則不做過濾。

9、修config.lua配置文件中waf規則目錄的路徑:

# vim /usr/local/openresty/nginx/conf/waf/config.lua --> RulePath="/usr/local/openresty/nginx/conf/waf/wafconf/"

備註:config.lua配置文件

指令

含義

RulePath="/usr/local/openresty/nginx/conf/waf/wafconf/"

規則存放目錄

attacklog="on"

開啓日誌

logdir="/usr/local/openresty/nginx/logs/hack/"

Log日誌目錄

UrlDeny="on"

攔截URL訪問

Redirect="on"

攔截後重定向

CookieMatch="on"

攔截Cookie Attack

postMatch="on"

攔截Post Attack

whiteModule="on"

開啓URL白名單

black_fileExt={"php","jsp"}

不允許上傳的文件後綴類型

ipWhitelist={"127.0.0.1"}

IP白名單,多個IP之間使用逗號分隔

ipBlocklist={"1.0.0.1"}

IP黑名單,多個IP之間使用逗號分隔

CCDeny="on"

開啓攔截CC Attack(需要在nginx.confhttp配置段中新增代碼lua_shared_dict limit 10m;

CCrate="100/60"

設置CC Attack頻率,單位爲秒

默認1分鐘同一個IP只能請求同一個地址100

10、修改nginx.conf配置文件:

# vim /usr/local/openresty/nginx/conf/nginx.conf,在http配置段中新增如下代碼:

lua_package_path  "/usr/local/openresty/nginx/conf/waf/?.lua";

lua_shared_dict  limit  10m;

init_by_lua_file  "/usr/local/openresty/nginx/conf/waf/init.lua";

access_by_lua_file  "/usr/local/openresty/nginx/conf/waf/waf.lua";

# nginx -t

# nginx -s reload

11、測試WAF應用防火牆:

(1)模擬URL參數檢測:http://192.168.0.121/lua?id=../etc/shadow

image.png

(2)使用ab命令模擬CC Attack# ab -n 10000 -c 100 http://192.168.0.121/lua

image.png

備註:ab命令選項

a、-n requests:執行的請求總數,默認爲1

b、-c concurrency:一次併發執行的請求數,默認爲1

(3)查看日誌:# tail -3 /usr/local/openresty/nginx/logs/hack/localhost_2020-02-18_sec.log

image.png

192.168.0.121 [2020-02-18 00:47:49] "UA localhost/lua" "-"  "ApacheBench/2.3" "(HTTrack|harvest|audit|dirbuster|pangolin|nmap|sqln|-scan|hydra|Parser|libwww|BBBike|sqlmap|w3af|owasp|Nikto|fimap|havij|PycURL|zmeu|BabyKrokodil|netsparker|httperf|bench| SF/)"


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章