$ cd /usr/local/src $ wget https://www.modsecurity.org/tarball/2.9.3/modsecurity-2.9.3.tar.gz $ tar zxvf modsecurity-2.9.3.tar.gz $ cd modsecurity-2.9.3 $ sudo ./autogen.sh $ sudo ./configure --enable-standalone-module --disable-mlogc $ sudo make
下载并编译Nginx
1 2 3 4 5 6 7
$ cd /usr/local/src $ wget http://nginx.org/download/nginx-1.19.1.tar.gz $ tar zxvf nginx-1.19.1.tar.gz $ cd nginx-1.19.1 $ ./configure --add-module=/usr/local/src/modsecurity-2.9.3/nginx/modsecurity --with-http_ssl_module $ sudo make $ sudo make install
添加规则
ModSecurity 倾向于过滤和阻止 Web 危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
下载OWASP规则:
1 2 3 4
$ cd /usr/local/nginx/conf $ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs $ cd owasp-modsecurity-crs $ mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
启用OWASP规则
1 2 3 4 5 6
$ cp /usr/local/src/modsecurity-2.9.3/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf $ cp /usr/local/src/modsecurity-2.9.3/unicode.mapping /usr/local/nginx/conf/unicode.mapping # 将 SecRuleEngine 设置为 on $ vim /usr/local/nginx/conf/modsecurity.conf
# 配置规则 $ vim /usr/local/nginx/conf/modsecurity.conf
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf