Preface 

  前述文章开源WAF工具ModSecurity,介绍了ModSecurity作为Nginx的动态加载模块的基本安装和使用。

  本篇简单介绍ModSecurity CRS规则集的使用。

# nginx -v                # nginx版本
nginx version: nginx/1.17.
# which nginx              # nginx可执行文件路径
/usr/sbin/nginx
# find / -name nginx.conf       # nginx配置文件路径
/etc/nginx/nginx.conf
# ls /etc/nginx/modules/       # modsecurity模块位置
ngx_http_modsecurity_module.so
# ls /etc/nginx/modsec/*.conf     # modsecurity配置文件路径
/etc/nginx/modsec/main.conf /etc/nginx/modsec/modsecurity.conf

  如果参照前面给出的链接,除了版本上可能会有差异外(由于更新),其它的是一样的。

OWASP CRS

下载OWASP规则并生成配置文件

# cd /etc/nginx/modsec/      # 切换到我们设置的modsecurity配置文件路径
# git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git    # 下载CRS规则文件
# cd owasp-modsecurity-crs    
# cp crs-setup.conf.example crs-setup.conf    # 使用默认配置
# git branch
* v3.3/dev            # 目前在最新的3.3分支

配置OWASP规则

  编辑crs-setup.conf文件(使用命令)。

# sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
# sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
# sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
# sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf

生成例外排除请求的配置文件

# pwd               # 当前已在这个路径         
/etc/nginx/modsec/owasp-modsecurity-crs
# cp rules/REQUEST--EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST--EXCLUSION-RULES-BEFORE-CRS.conf    # 例外排除请求
# cp rules/RESPONSE--EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE--EXCLUSION-RULES-AFTER-CRS.conf    # 例外排除请求
$ cp rules/*.data ..      # data文件拷贝至modsecurity配置文件路径,即/etc/nginx/modsec
# cd /etc/nginx/modsec/
# ls    # /etc/nginx/modsec路径下的内容是这样
crawlers-user-agents.data java-errors.data owasp-modsecurity-crs php-function-names-.data scanners-headers.data sql-errors.data
iis-errors.data lfi-os-files.data php-config-directives.data php-variables.data scanners-urls.data unicode.mapping
java-classes.data main.conf php-errors.data restricted-files.data scanners-user-agents.data unix-shell.data
java-code-leakages.data modsecurity.conf php-function-names-.data restricted-upload.data scripting-user-agents.data windows-powershell-commands.data

  其中:

  • modsecurity.conf为modsecurity的配置文件;
  • main.conf为我们自己添加的主配置文件,规则都可添加到这里,方便进行管理;

添加规则

  在main.conf配置文件中添加我们想要的规则:

# vim main.conf    # 以下是main.conf中的内容
# Include the recommended configuration
include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf
include owasp-modsecurity-crs/rules/REQUEST--EXCLUSION-RULES-BEFORE-CRS.conf
include owasp-modsecurity-crs/rules/REQUEST--INITIALIZATION.conf
Include owasp-modsecurity-crs/rules/REQUEST-903.9002-WORDPRESS-EXCLUSION-RULES.conf
include owasp-modsecurity-crs/rules/REQUEST--COMMON-EXCEPTIONS.conf
include owasp-modsecurity-crs/rules/REQUEST--IP-REPUTATION.conf
include owasp-modsecurity-crs/rules/REQUEST--METHOD-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST--DOS-PROTECTION.conf
include owasp-modsecurity-crs/rules/REQUEST--SCANNER-DETECTION.conf
include owasp-modsecurity-crs/rules/REQUEST--PROTOCOL-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST--PROTOCOL-ATTACK.conf
include owasp-modsecurity-crs/rules/REQUEST--APPLICATION-ATTACK-LFI.conf
include owasp-modsecurity-crs/rules/REQUEST--APPLICATION-ATTACK-RFI.conf
include owasp-modsecurity-crs/rules/REQUEST--APPLICATION-ATTACK-RCE.conf
include owasp-modsecurity-crs/rules/REQUEST--APPLICATION-ATTACK-PHP.conf
include owasp-modsecurity-crs/rules/REQUEST--APPLICATION-ATTACK-XSS.conf
include owasp-modsecurity-crs/rules/REQUEST--APPLICATION-ATTACK-SQLI.conf
include owasp-modsecurity-crs/rules/REQUEST--APPLICATION-ATTACK-SESSION-FIXATION.conf
include owasp-modsecurity-crs/rules/REQUEST--BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE--DATA-LEAKAGES.conf
include owasp-modsecurity-crs/rules/RESPONSE--DATA-LEAKAGES-SQL.conf
include owasp-modsecurity-crs/rules/RESPONSE--DATA-LEAKAGES-JAVA.conf
include owasp-modsecurity-crs/rules/RESPONSE--DATA-LEAKAGES-PHP.conf
include owasp-modsecurity-crs/rules/RESPONSE--DATA-LEAKAGES-IIS.conf
include owasp-modsecurity-crs/rules/RESPONSE--BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE--CORRELATION.conf
include owasp-modsecurity-crs/rules/RESPONSE--EXCLUSION-RULES-AFTER-CRS.conf
# A test rule
SecRule ARGS:testparam "@contains test" "id:1234,deny,log,status:403"

  【注】考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。

测试

# nginx -s reload      # 重载配置
# nginx -t          # 测试
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful # curl -D - http://localhost/?id=1      # 正常访问
HTTP/1.1 OK
Server: nginx/1.17.
Date: Tue, Nov :: GMT
Content-Type: text/plain
Content-Length:
Connection: keep-alive Thank you for requesting /?id=1
# curl -D - http://localhost/?id='1 and 1=1'  # 测试是否存在SQL注入
HTTP/1.1 Forbidden               <<== 会被拦截
Server: nginx/1.17.
Date: Tue, Nov :: GMT
Content-Type: text/html
Content-Length:
Connection: keep-alive <html>
<head><title> Forbidden</title></head>
<body>
<center><h1> Forbidden</h1></center>
<hr><center>nginx/1.17.</center>
</body>
</html> # curl -D - http://localhost/?input='<script>alert(/xss/)</script>'    # 测试XSS
HTTP/1.1 Forbidden              <<== 同样会被拦截
Server: nginx/1.17.
Date: Tue, Nov :: GMT
Content-Type: text/html
Content-Length:
Connection: keep-alive <html>
<head><title> Forbidden</title></head>
<body>
<center><h1> Forbidden</h1></center>
<hr><center>nginx/1.17.</center>
</body>
</html>

查看日志文件

  modsecurity日志文件:/var/log/modsec_audit.log

  可以清楚的看到日志中记录了XSS的攻击记录。

  ModSecurity CRS规则集的基本使用就是这样,最主要的是要知道CRS规则集中的内容,规则的编写,才能编写出符合自己要求的规则。

参考

  利用 ModSecurity 在 Nginx 上构建 WAF

    https://www.hi-linux.com/posts/45920.html

OWASP ModSecurity Core Rule Set (CRS)的基本使用的更多相关文章

  1. OpenResty + ModSecurity + OWASP CRS

    本篇将介绍如何使用OpenResty和ModSecurity 来构建自己的WAF,安装过程整体与Nginx是类似的,但也有些区别,在文中会特别指出,本篇算是用openresty对前面两篇nginx和c ...

  2. ModSecurity web application firewall (WAF) Research

    catalog . 引言 . OWASP ModSecurity Core Rule Set (CRS) Project . Installation mod_security for Apache ...

  3. nginx配合modsecurity实现WAF功能

    一.准备工作 系统:centos 7.2 64位.nginx1.10.2, modsecurity2.9.1 owasp3.0 1.nginx:http://nginx.org/download/ng ...

  4. ModSecurity:一款优秀的开源WAF

    一.ModSecurity3.0介绍 ModSecurity是一个开源的跨平台Web应用程序防火墙(WAF)引擎,用于Apache,IIS和Nginx,由Trustwave的SpiderLabs开发. ...

  5. ModSecurity for Nginx

    Announcing the availability of ModSecurity extension for Nginx ModSecurity for Nginx ModSecurity for ...

  6. Web漏洞总结: OWASP Top 10

    本文原创,更多内容可以参考: Java 全栈知识体系.如需转载请说明原处. 开发安全 - OWASP Top 10 在学习安全需要总体了解安全趋势和常见的Web漏洞,首推了解OWASP,因为它代表着业 ...

  7. CRSF Defense Using Content Injection Support By ModSecurity

    The most advanced and imaginative use of the content injection feature is that devised byRyan C. Bar ...

  8. 开源WAF工具ModSecurity

    0 前言 ModSecurity是一个开源的跨平台Web应用程序防火墙(WAF)引擎,用于Apache,IIS和Nginx,由Trustwave的SpiderLabs开发.作为WAF产品,ModSec ...

  9. ModSecurity的规则

    一.ModSecurity的规则 基本格式 SecRule VARIABLES OPERATOR ACTIONS SecRule:ModSecurity主要的指令,用于创建安全规则. VARIABLE ...

随机推荐

  1. warning: rpmts_HdrFromFdno: Header V4 DSA/SHA1 Signature, key ID XXXXXX: NOKEY

    我在使用Centos时,会出现这种错误: 本人实践有效的办法是: 加上"--nogpgcheck"参数 就是在你要执行的语句后面加上该参数就行了! 我当时是为了安装jenkins时 ...

  2. 如何提高 PHP 代码的质量?第二部分 单元测试

    在“如何提高 PHP 代码的质量?”的前一部分中:我们设置了一些自动化工具来自动检查我们的代码.这很有帮助,但关于我们的代码如何满足业务需求并没有给我们留下任何印象.我们现在需要创建特定代码域的测试. ...

  3. C++ 入门第一篇 Hello Word

    C++基础教程                                                                                              ...

  4. 使用cookies弹出层每24小时弹出一次

    第一步:下载cookies的库 https://github.com/js-cookie/js-cookie 第二步:设置Cookies的失效时间,这里有两种方法,按天计算和按小时计算 functio ...

  5. Ubuntu 18.04 LTS上安装NFS服务器和客户端

    NFS是基于UDP/IP协议的应用,其实现主要是采用远程过程调用RPC机制,RPC提供了一组与机器.操作系统以及低层传送协议无关的存取远程文件的操作.RPC采用了XDR的支持.XDR是一种与机器无关的 ...

  6. 为什么QQ能帮你找到失散多年的兄弟?----图论

    编程三分钟的第 44 篇原创文章 为什么qq里"可能认识的人"功能推荐的如此精准? 为什么两个没有什么联系的朋友会相互认识? 一切的背后到底是道德的沦丧,还是人性的扭曲 ? 让我们 ...

  7. SmartSVN提示 svn: File has inconsistent newlines 解决

    用SmartSVN提交代码的时候提示:svn: File has inconsistent newlines 本文转自:http://www.youduoshao.com/2014-10-05/201 ...

  8. Prometheus笔记(二)监控go项目实时给grafana展示

    欢迎加入go语言学习交流群 636728449 Prometheus笔记(二)监控go项目实时给grafana展示 Prometheus笔记(一)metric type 文章目录 一.promethe ...

  9. HDU3032 Nim or not Nim?(Lasker’s Nim游戏)

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  10. AtCoder-3867

    Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N. C ...