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. centos7更新php5.4到php5.6/php7

    centos7系统yum安装的php版本为5.4. 因业务需求,开发可能需要php5.6环境. 本文应需而生,介绍从php5.4升级到php5.6. 如需更新到php7环境,步骤一样. 如果是线上应用 ...

  2. C语言l博客作业01

    2.1 你对软件工程专业或者计算机科学与技术专业了解是怎样?(2分) 计算机科学与技术是国家一级学科,下设信息安全,软件工程,计算机软件与理论等专业,主要培养符合教育部门实际需要的计算机教学及应用管理 ...

  3. 拿 C# 搞函数式编程 - 2

    前一阵子在写 CPU,导致一直没有什么时间去做其他的事情,现在好不容易做完闲下来了,我又可以水文章了哈哈哈哈哈.顺便成立了自己的专栏:hez2010 的编程日常,欢迎大家关注(逃 有关 FP 的类型部 ...

  4. ios instancetype与id区别

    我们都知道未知类型的的对象可以用id关键字表示,那为什么还会再有一个instancetype呢? instancetype能返回相关联的类型(使那些非关联返回类型的方法返回所在类的类型):而id 返回 ...

  5. “Unknown class XXViewController in Interface Builder file.”问题处理

    在静态库中写了一个XXViewController类,然后在主工程的xib中,将xib的类指定为XXViewController,程序运行时,报了如下错误: “Unknown class XXView ...

  6. Spring事物实例

    Spring事务实例: entity实体类: public class Accounts { private int accountid; private String accountname; pr ...

  7. UVA-136Ugly numbers

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...

  8. 初次在Vue项目使用TypeScript,需要做什么

    前言 总所周知,Vue新版本3.0 使用 TypeScript 开发,让本来就很火的 TypeScript 受到更多人的关注.虽然 TypeScript 在近几年才火,但其实它诞生于2012年10月, ...

  9. Socket无法通过防火墙的问题

    无论是配好端口还是例外的应用程序都不行 更改本地终结点为 socket.Bind()); IPAddress.Any 不要使用127.0.0.1 不要使用127.0.0.1 不要使用127.0.0.1

  10. 【ZooKeeper系列】1.ZooKeeper单机版、伪集群和集群环境搭建

    ZooKeeper安装模式主要有3种: 单机版(Standalone模式)模式:仅有一个ZooKeeper服务 伪集群模式:单机多个ZooKeeper服务 集群模式:多机多ZooKeeper服务 1 ...