为演示已安装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'