爲演示已安裝Nginx而未添加ModSecurity的狀況,如下操做爲先安裝Nginx,後添加ModSecurity模塊。nginx
ModSecurity是一個開源的跨平臺Web應用程序防火牆(WAF)引擎,,完美兼容nginx,是nginx官方推薦的WAF,而且支持支持OWASP規則。c++
中文網站:http://www.modsecurity.cn
git
應用實戰可參考:http://www.modsecurity.cn/practice/
github
wget http://nginx.org/download/nginx-1.14.2.tar.gz
wget http://www.modsecurity.cn/download/modsecurity/modsecurity-v3.0.4.tar.gz
yum install -y pcre* openssl* gcc c++ make
tar -xvf nginx-1.14.2.tar.gz cd nginx-1.14.2/
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
make make install
useradd nginx -s /sbin/nologin mkdir /var/tmp/nginx/
yum install -y gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre-devel lmdb-devel libxml2-devel ssdeep-devel lua-devel libtool autoconf automake
tar -xvf modsecurity-v3.0.4.tar.gz cd modsecurity-v3.0.4/
./configure make make install cp modsecurity.conf-recommended /usr/local/modsecurity/modsecurity.conf cp unicode.mapping /usr/local/modsecurity/
/usr/local/nginx/sbin/nginx -V
git clone https://github.com/SpiderLabs/ModSecurity-nginx.git 或至下載 https://github.com/SpiderLabs/ModSecurity-nginx
unzip ModSecurity-nginx-master.zip #僅在下載安裝包狀況下,若是是git clone 則不須要
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --add-module=../ModSecurity-nginx #此處須要注意/ModSecurity-nginx路徑 make make install /usr/local/nginx/sbin/nginx -V #查看已經有了ModSecurity模塊
mkdir /usr/local/nginx/conf/modsecurity cp /usr/local/modsecurity/modsecurity.conf /usr/local/nginx/conf/modsecurity/ cp /usr/local/modsecurity/unicode.mapping /usr/local/nginx/conf/modsecurity/
vim /usr/local/nginx/conf/nginx.conf modsecurity on; modsecurity_rules_file /usr/local/nginx/conf/modsecurity/modsecurity.conf; #在http節點添加表示全局配置,在server節點添加表示爲指定網站配置
vim /usr/local/nginx/conf/modsecurity/modsecurity.conf #更改一下部分 SecRuleEngine On SecAuditLogParts ABCDEFHZ #追加到配置文件 Include /usr/local/nginx/conf/modsecurity/crs-setup.conf Include /usr/local/nginx/conf/modsecurity/rules/*.conf
wget http://www.modsecurity.cn/download/corerule/owasp-modsecurity-crs-3.3-dev.zip #Modsecurity中文網下載 wget https://github.com/coreruleset/coreruleset #github下載
unzip owasp-modsecurity-crs-3.3-dev.zip cd owasp-modsecurity-crs-3.3-dev/ cp crs-setup.conf.example /usr/local/nginx/conf/modsecurity/crs-setup.conf cp -r rules /usr/local/nginx/conf/modsecurity/ cd /usr/local/nginx/conf/modsecurity/rules mv REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf mv RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
#瀏覽器 或 curl訪問測試,返回403 curl -I 'http://localhost/?id=1 AND 1=1'