本篇將介紹如何使用OpenResty和ModSecurity 來構建自己的WAF,安裝過程整體與Nginx是類似的,但也有些區別,在文中會特別指出,本篇算是用openresty對前面兩篇nginx和crs的集中介紹。
Preface
版本信息
- CentOS Linux release 7.6.1810 (Core)
- nginx version: openresty/1.13.6.1
- ModSecurity 3.0
安裝依賴
# yum install epel-release -y # 安裝modsecurity依賴 # yum install gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre pcre-devel libxml2 libxml2-devel autoconf automake lmdb-devel ssdeep-devel ssdeep-libs lua-devel libmaxminddb-devel git apt-utils autoconf automake build-essential git libcurl4-openssl-dev libgeoip-dev liblmdb-dev ibpcre++-dev libtool libxml2-dev libyajl-dev pkgconf wget zlib1g-dev -y # 安裝nginx/openresty依賴
# yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
下載
# mkdir /opt/waf #創建一個專屬目錄 # cd /opt/waf # git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity # 下載ModSecurity # wget https://openresty.org/download/openresty-1.13.6.1.tar.gz # 下載openresty # git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git # 下載ModSecurity-nginx連接器
# pwd /opt/waf # ls ModSecurity ModSecurity-nginx openresty-1.13.6.1.tar.gz
編譯ModSecurity
# cd /opt/waf/ # cd ModSecurity/ # 切換到ModSecurity目錄 # git submodule init # 初始化 # git submodule update # 更新 # ./build.sh # ./configure # make # make install
【注】在執行build.sh會出現如下錯誤,可忽略。
fatal: No names found, cannot describe anything
編譯modsecurity_module
# cd /opt/waf/ # tar xvf openresty-1.13.6.1.tar.gz # ls ModSecurity ModSecurity-nginx openresty-1.13.6.1 openresty-1.13.6.1.tar.gz
# cd openresty-1.13.6.1/ # ./configure --with-compat --add-dynamic-module=/opt/waf/ModSecurity-nginx # 需用絕對路徑,相對路徑會出問題 # gmake # 不能使用make modules命令 # gmake install
... 上一步的輸出 cp objs/ngx_http_modsecurity_module.so '/usr/local/openresty/nginx/modules/ngx_http_modsecurity_module.so' gmake[2]: Leaving directory `/opt/waf/openresty-1.13.6.1/build/nginx-1.13.6' gmake[1]: Leaving directory `/opt/waf/openresty-1.13.6.1/build/nginx-1.13.6' mkdir -p /usr/local/openresty/site/lualib /usr/local/openresty/site/pod /usr/local/openresty/site/manifest ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty
openresty默認安裝到/usr/local/openresty,最終的是ngx_http_modsecurity_module.so庫文件ok。
配置
基本測試
為了便於后續操作,創建一個openresty的符號鏈接。
# ln -s /usr/local/openresty/bin/openresty /usr/bin/openresty # openresty -v nginx version: openresty/1.13.6.1
# cd /usr/local/openresty/nginx/conf # head -1 nginx.conf load_module /usr/local/openresty/nginx/modules/ngx_http_modsecurity_module.so; # 首行加入
# openresty # 啟動 # openresty -s reload # 重載配置(如果已啟動則重載配置即可) # openresty -t # 測試ok nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
啟用WAF
# cd /usr/local/openresty/nginx # ls client_body_temp conf fastcgi_temp html logs modules proxy_temp sbin scgi_temp uwsgi_temp # mkdir modsec # 創建一個專屬modsecurity的規則文件夾 # cd modsec/ # sudo wget https://raw.githubusercontent.com/SpiderLabs/ModSecurity/v3/master/modsecurity.conf-recommended # mv modsecurity.conf-recommended modsecurity.conf
修改modsecurity.conf配置文件
# vim modsecurity.conf # -- Rule engine initialization ----------------------------------------------
... SecRuleEngine On <== 設置為On
拷貝mapping文件。
# cp /opt/waf/ModSecurity/unicode.mapping .
# ls
main.conf modsecurity.conf unicode.mapping
創建一個主規則文件main.conf,內容如下。
# cat /etc/nginx/modsec/main.conf # Include the recommended configuration Include /usr/local/openresty/nginx/modsec/modsecurity.conf # A test rule SecRule ARGS:testparam "@contains test" "id:1234,deny,log,status:403"
修改nginx配置文件,簡要圖示如下:
... server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; modsecurity on; modsecurity_rules_file /usr/local/openresty/nginx/modsec/main.conf; location / { default_type text/plain; return 200 "Thank you for requesting ${request_uri}\n"; } ...
- modsecurity on:啟用Nginx WAF;
- modsecurity_rules_file:指定規則文件路徑。
測試WAF
# curl localhost Thank you for requesting / [root@localhost conf]# curl localhost/foo?testparam=thisisatestofmodsecurity # 攜帶惡意參數test <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <<= 禁止訪問 <hr><center>openresty/1.13.6.1</center> </body> </html>
可以看到參數testparam中帶有test關鍵字會被攔截。
CRS
OpenResrty 配置文件nginx.conf請參考:https://www.cnblogs.com/Hi-blog/p/ModSecurity.html#autoid-3-4-0