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. 【SSL1455&1456】 电子老鼠闯迷宫 & 骑士游行

    考点概况: 广搜板子 题面: \[\Large\text{电子老鼠闯迷宫}\] \[Time~Limit:1000MS~~Memory~Limit:65536K\] Description 如下图12 ...

  2. python原类、类的创建过程与方法

    今天为大家介绍一下python中与class 相关的知识-- 获取对象的类名 python是一门面向对象的语言,对于一切接对象的python来说,咱们有必要深入的学习与了解一些知识 首先大家都知道,要 ...

  3. 【跟唐老师学习云网络】 - 第6篇 ARP你在哪

    [摘要] 这一章节你的角色就是ping报文这个"使者",你要去往目的地,然后回到本机.然而目的地路漫漫,不知道要经过多少个小地盘(局域网),没两把刷子也不好行走江湖.所以你作为使者 ...

  4. 小程序使用wxs解决wxml保留2位小数问题

    1.出现溢出表现 从图中可以看到数字超出了很长长度.代码里面是如下这样的.为什么在0.35出现?或者一些相成的计算出现? 而 0.34却不会.(wap.0834jl.com) 0.41 也会出现,好像 ...

  5. HTML语言和CSS开发商业站点 错题

    1.关于css为什么会出现Bug说法不正确的是(). (选项两项) A.编写CSS样式是需要在不同浏览器中实现表现一致 B.各大主流浏览器由于不同厂家开发,浏览器使用的内核不同,支持CSS的程度不同 ...

  6. 纯手工搭建K8s(单节点)

    准备说明: 因为为纯手动搭建,所以针对安装时需要的一些安装包需提前下载好 cfssl_linux-amd64. cfssljson_linux-amd64. cfssl-certinfo_linux- ...

  7. wxxcx_learn独立验证与REST

    模块,控制器,方法 validate  接口参数校验 独立验证(验证器对独立验证做了更好的封装) $date = [ 'name' => '111', 'email' => '123@qq ...

  8. Jenkins编译过程中出现ERROR_ Failed to parse POMs错误

    一.在使用jenkins编写过程中突然出现以下问题 Parsing POMs Established TCP socket on 59407 [java] $ java -cp /var/lib/je ...

  9. Manjaro-kde-18.1.3安装体验

    身为Linux排名第一的版本,怎么能不安装一下亲自体验 制作启动U盘 Manjaro身为arc系列的一员,所以我选择使用raw的方式写入(其实我是在Ubuntu中用dd方式写入的,Windows用户也 ...

  10. 基于 HTML5 WebGL 构建智能数字化城市 3D 全景

    前言 自 2011 年我国城镇化率首次突破 50% 以来,<新型城镇化发展规划>将智慧城市列为我国城市发展的三大目标之一,并提出到 2020 年,建成一批特色鲜明的智慧城市.截至现今,全国 ...