1、WAF相關概念介紹:
(1)WAF簡介:
WAF:Web Appalication Firewall,Web應用防火牆,是一種工作在應用層的、通過一系列針對HTTP/HTTPS的安全策略爲Web應用提供安全防護的產品。
(2)WAF可以實現如下功能:
a、防止SQL注入、本地包含、部分溢出、Fuzzing測試、XSS等Web 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 httpd-tools
3、安裝LuaJIT 2.1:
LuaJIT是採用C語言編寫的Lua代碼解釋器,http://luajit.org/download.html中的穩定版本LuaJIT-2.0.5,版本太低,不建議使用,此次演示使用的是LuaJIT-2.1.0-beta3。
# git clone https://github.com/openresty/luajit2.git
# cd luajit2
# make && make install PREFIX=/usr/local/luajit2
# ln -sv /usr/local/luajit2/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
4、測試Lua環境:
# vim /tmp/hello.lua --> print("Hello Lua")
# lua /tmp/hello.lua
# lua
5、下載解壓ngx_devel_kit模塊:
NDK:Nginx Development Kit,是一個拓展Nginx服務器核心功能的模塊,https://github.com/vision5/ngx_devel_kit。
# tar -xf ngx_devel_kit-0.3.1.tar.gz
6、下載解壓lua-nginx-module模塊:
lua-nginx-module:將Lua的強大功能嵌入到Nginx服務器中,https://github.com/openresty/lua-nginx-module。
# tar -xf lua-nginx-module-0.10.15.tar.gz
與其兼容的Nginx版本(不兼容目前最新穩定1.16.1版本):
7、編譯Nginx穩定版本1.14.2:
# useradd -s /sbin/nologin -M nginx
# tar -xf nginx-1.14.2.tar.gz -C /usr/src
# cd /usr/src/nginx-1.14.2/
export LUAJIT_LIB=/usr/local/luajit2/lib
export LUAJIT_INC=/usr/local/luajit2/include/luajit-2.1
# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-threads --with-file-aio --with-http_ssl_module --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-compat --with-pcre --add-module=/software/ngx_devel_kit-0.3.1 --add-module=/software/lua-nginx-module-0.10.15 --with-ld-opt="-Wl,-rpath,/usr/local/luajit2/lib"
# make -j2 && make install
備註:
(1)--add-module後的模塊目錄中要有config文件
(2)有Makefile文件的需要執行make和make install
8、配置Nginx環境變量,並啓動Nginx:
# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
# . /etc/profile.d/nginx.sh
# nginx -v
# nginx
# ss -tunlp | grep -w :80
9、測試Nginx Lua模塊:
# cd /usr/local/nginx/conf
# cp nginx.conf{,.bak}
# vim nginx.conf
在http配置段中新增代碼:lua_load_resty_core off;
在server配置段中新增如下location:
location /lua {
default_type 'text/plain';
content_by_lua 'ngx.say("Hello Lua")';
}
# nginx -t
# nginx -s reload
備註:在http配置段中新增lua_load_resty_core off;代碼,啓動Nginx時就不會提示上述錯誤信息。
10、創建保存日誌的目錄:
# mkdir -pv /usr/local/nginx/logs/hack
11、下載解壓ngx_lua_waf模塊:
ngx_lua_waf:基於lua-nginx-module的Web應用防火牆,https://github.com/loveshell/ngx_lua_waf。
# tar -xf ngx_lua_waf-0.7.2.tar.gz -C /usr/local/nginx/conf
# cd /usr/local/nginx/conf
# mv ngx_lua_waf-0.7.2 waf
# chown -R nginx.nginx /usr/local/nginx
備註:waf目錄主要結構
(1)config.lua:配置文件;
(2)init.lua:規則函數;
(3)waf.lua:定義WAF檢測順序;
(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做匹配,匹配到則不做過濾。
12、確認config.lua配置文件中waf規則目錄的路徑是否正確:
# vim /usr/local/nginx/conf/waf/config.lua --> RulePath="/usr/local/nginx/conf/waf/wafconf/"
備註:config.lua配置文件
指令 | 含義 |
RulePath="/usr/local/nginx/conf/waf/wafconf/" | 規則存放目錄 |
attacklog="on" | 開啓日誌 |
logdir="/usr/local/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.conf的http配置段中新增代碼lua_shared_dict limit 10m;) |
CCrate="100/60" | 設置CC Attack頻率,單位爲秒 默認1分鐘同一個IP只能請求同一個地址100次 |
13、修改nginx.conf配置文件:
# vim /usr/local/nginx/conf/nginx.conf,在http配置段中新增如下代碼:
lua_package_path "/usr/local/nginx/conf/waf/?.lua";
lua_shared_dict limit 10m;
init_by_lua_file "/usr/local/nginx/conf/waf/init.lua";
access_by_lua_file "/usr/local/nginx/conf/waf/waf.lua";
# nginx -t
# nginx -s reload
14、測試WAF應用防火牆:
(1)模擬URL參數檢測:http://192.168.0.120/lua?id=../etc/passwd
(2)使用ab命令模擬CC Attack:# ab -n 20000 -c 100 http://192.168.0.120/lua
備註:ab命令選項
-n requests:需要執行的請求總數,默認爲1
-c concurrency:同時併發執行的請求數,默認爲1
(3)查看日誌:# tail /usr/local/nginx/logs/hack/localhost_2020-02-17_sec.log
192.168.0.120 [2020-02-17 00:47:17] "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/)"