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. php 第1讲 html介绍 html运行原理①

    1. html (hypertext mark-up language )是 超文本编辑语言,主要的用处是做网页,可以在网页上显示文字.图形.动画.视频... “标记“有时候也称之为“元素” 动态网页 ...

  2. 第五章 Unity中的基础光照(2)

    目录 1. Unity中的环境光和自发光 2. 在UnityShader中实现漫反射光照模型 2.1 实践:逐顶点光照 2.2 实践:逐像素光照 2.3 半兰伯特模型 1. Unity中的环境光和自发 ...

  3. 痞子衡嵌入式:恩智浦i.MX RTxxx系列MCU启动那些事(8)- 从Serial(1-bit SPI) NOR恢复启动

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是恩智浦i.MX RTxxx系列MCU的1-bit SPI NOR恢复启动. 在前几篇里痞子衡介绍的Boot Device都属于主动启动的 ...

  4. 从入门到入土:Lambda完整学习指南,包教包会!

    什么是Lambda表达式 Lambda表达式是Java SE 8中一个重要的新特性.lambda表达式允许你通过表达式来代替功能接口.lambda表达式就和方法一样,它提供了一个正常的参数列表和一个使 ...

  5. 基于Docker搭建分布式消息队列Kafka

    本文基于Docker搭建一套单节点的Kafka消息队列,Kafka依赖Zookeeper为其管理集群信息,虽然本例不涉及集群,但是该有的组件都还是会有,典型的kafka分布式架构如下图所示.本例搭建的 ...

  6. Python文本转化语音模块大比拼,看看青铜与王者的差别!

    文本转语音 如果把Python比喻成游戏中的一个英雄,你觉得它是谁?对于Dota老玩家来说,我会想到钢琴手卡尔!感觉Python和卡尔一样,除了生孩子什么都可以做的角色.日常生活中,我们会涉及到很多语 ...

  7. 在Tinymce编辑器里,集成数学公式

    在以前,需要在Web页面显示数学公式,常用的都是先制作成图片,然后插入到页面里.这使得后期对数学公式的修改变的麻烦,同时也不利于搜索引擎搜索. 本文将介绍如何在TinyMce编辑器里集成数学公式.先看 ...

  8. 每个pool pg数计算

    ceph PGs per Pool Calculator 原文档:http://xiaqunfeng.cc/2017/09/18/ceph-PGs-per-Pool-Calculator/ 2017- ...

  9. Django 06

    目录 注意 聚合查询 分组查询 F与Q查询 orm字段和参数 自定义字段类型 事务 ACID 三大范式 开启事务 注意 只要和数据库相关的功能, 基本在 django.db.models 里面 聚合查 ...

  10. 量化投资学习笔记01——初识Pyalgotrade量化交易回测框架

    年初学习量化投资,一开始想自己从头写,还是受了C/C++的影响.结果困在了计算回测数据那里,结果老也不对,就暂时放下了.最近试了一下python的各个量化投资框架,发现一个能用的——pyalgotra ...