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. IDEA如何重置窗口布局

    如何重置窗口布局 我不知道怎么搞的,左边的,上边的,下边的,视图都没有了 , 重启了一下,然后重置为默认视图,就好了

  2. webpack4.0(二)--热更新

    webpack4.0---热更新 使用webpack可帮助我们的开发以及打包,在开发过程中,有时候我们只修改了部分代码并想不必刷新整个页面即可看到更改后的效果,这时候 webpack-dev-serv ...

  3. 浅议Grpc传输机制和WCF中的回调机制的代码迁移

    浅议Grpc传输机制和WCF中的回调机制的代码迁移 一.引子 如您所知,gRPC是目前比较常见的rpc框架,可以方便的作为服务与服务之间的通信基础设施,为构建微服务体系提供非常强有力的支持. 而基于. ...

  4. 洛谷 题解 SP3267 【DQUERY - D-query】

    今天机房讲了莫队. 但是蒟蒻我并没有听懂,所以晚上回家恶补,才弄明白莫队. 莫队是莫涛大神发明的,它的作用就是用优秀的复杂度求解于一些区间之间的操作,莫队其实就是一个优雅的暴力,它的复杂度是O(n s ...

  5. 一台电脑如何管理多个ssh key

    目录 一.生成ssh key 1.1 生成密钥(必须) 1.2 设置路径 (可选) 1.3 指定密语字符串(可选) 二.设置ssh key的代理 2.1. 首先查看代理 2.2. 添加私钥 三.添加公 ...

  6. 数据库Oracle日期函数

    SYSDATE 函数:是一个日期函数,它返回当前数据库服务器的日期和时间. 用日期计算: • 从日期加或者减一个数,结果是一个日期值 • 两个日期相减,得到两个日期之间的天数 ,可以加小时到日期上 S ...

  7. [TimLinux] MySQL InnoDB的外键约束不支持set default引用选项

    1. 外键 MySQL的MyISAM是不支持外键的,InnoDB支持外键,外键是MySQL中的三大约束中的一类:主键约束(PRIMARY KEY),唯一性约束(UNIQUE),外键约束(FOREIGN ...

  8. I_want_all 2019训练记录

    Team members StarHai binarycopycode Fly_White Caution 读题 数组第一维的访问速度比其他维速度快. 清空数组 乘法运算取模里面涉及到减法注意变为负数 ...

  9. HDU1907 Jhon

    Little John is playing very funny game with his younger brother. There is one big box filled with M& ...

  10. vs2017/vs2019 去掉 单击aspx文件预览页面

    初次安装vs2017或者vs2019,新建的项目中,每次单击项目文件时,文件自动打开了 然后 打开 工具->选项->环境->选项卡和窗口->预览选项卡->勾掉" ...