0 前言

  ModSecurity是一个开源的跨平台Web应用程序防火墙(WAF)引擎,用于Apache,IIS和Nginx,由Trustwave的SpiderLabs开发。作为WAF产品,ModSecurity专门关注HTTP流量,当发出HTTP请求时,ModSecurity检查请求的所有部分,如果请求是恶意的,它会被阻止和记录。  

优势

完美兼容nginx,是nginx官方推荐的WAF
支持OWASP规则
.0版本比老版本更新更快,更加稳定,并且得到了nginx、Inc和Trustwave等团队的积极支持
免费

功能

SQL Injection (SQLi):阻止SQL注入
Cross Site Scripting (XSS):阻止跨站脚本攻击
Local File Inclusion (LFI):阻止利用本地文件包含漏洞进行攻击
Remote File Inclusione(RFI):阻止利用远程文件包含漏洞进行攻击
Remote Code Execution (RCE):阻止利用远程命令执行漏洞进行攻击
PHP Code Injectiod:阻止PHP代码注入
HTTP Protocol Violations:阻止违反HTTP协议的恶意访问
HTTPoxy:阻止利用远程代理感染漏洞进行攻击
Shellshock:阻止利用Shellshock漏洞进行攻击
Session Fixation:阻止利用Session会话ID不变的漏洞进行攻击
Scanner Detection:阻止黑客扫描网站
Metadata/Error Leakages:阻止源代码/错误信息泄露
Project Honey Pot Blacklist:蜜罐项目黑名单
GeoIP Country Blocking:根据判断IP地址归属地来进行IP阻断

劣势

不支持检查响应体的规则,如果配置中包含这些规则,则会被忽略,nginx的的sub_filter指令可以用来检查状语从句:重写响应数据,OWASP中相关规则是95X。
不支持OWASP核心规则集DDoS规则REQUEST--DOS- PROTECTION.conf,nginx本身支持配置DDoS限制
不支持在审计日志中包含请求和响应主体

  以上内容摘自:ModSecurity:一款优秀的开源WAF

00 Preface

  本篇介绍如何在CentOS7.6上安装ModSecurity。上面的给出的链接内容比较杂乱,故重新整理以记录。

安装

安装nginx

  如果有nginx,可忽略;如果没有请参考:RHEL/CentOS 安装最新版Nginx

安装依赖

# yum install epel-release -y
# yum install gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre pcre-devel libxml2 libxml2-devel autoconf automake lmdb-devel ssdeep-devel ssdeep-libs lua-devel libmaxminddb-devel git apt-utils autoconf automake build-essential git libcurl4-openssl-dev libgeoip-dev liblmdb-dev ibpcre++-dev libtool libxml2-dev libyajl-dev pkgconf wget zlib1g-dev -y

编译ModSecurity

  我们用的是v3版本,我们在/opt目录下进行安装。

# cd /opt/    # 切换到/opt
# git clone --depth -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity    # 下载
# cd ModSecurity/
# git submodule init     # 初始化
# git submodule update    # 更新
# ./build.sh
# ./configure
# make
# make install

【注】在执行build.sh会出现如下错误,可忽略。

fatal: No names found, cannot describe anything

ModSecurity-nginx连接器

  我们现在需要将ModSecurity-nginx编入。

# cd /opt/
# git clone --depth https://github.com/SpiderLabs/ModSecurity-nginx.git
# nginx -v            # 查看当前nginx版本
nginx version: nginx/1.17.5
# wget http://nginx.org/download/nginx-1.17.5.tar.gz
# tar -xvf nginx-1.17..tar.gz
# ls
ModSecurity ModSecurity-nginx nginx-1.17. nginx-1.17..tar.gz
# cd nginx-1.17./
# ./configure --with-compat --add-dynamic-module=../ModSecurity-nginx # 如果出现不兼容的问题,请去掉--with-compat参数
# make modules # 会生成如下*.so
# ls ./objs/ngx_http_modsecurity_module.so
./objs/ngx_http_modsecurity_module.so       # 查看
# cp ./objs/ngx_http_modsecurity_module.so /etc/nginx/modules/   
# 移动位置
# vim /etc/nginx/nginx.conf  
load_module /etc/nginx/modules/ngx_http_modsecurity_module.so; # 添加到配置文件首行
# nginx -t                                     # 测试通过
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

测试

ECHO测试

  新增配置文件:/etc/nginx/conf.d/echo.conf :

# service nginx start   # 启动nginx
Redirecting to /bin/systemctl start nginx.service
# vim /etc/nginx/conf.d/echo.conf
server {
listen localhost:;
location / {
default_type text/plain;
return "Thank you for requesting ${request_uri}\n";
}
}
# 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
[root@localhost ~]# curl -D - http://localhost:8085
HTTP/1.1 OK
Server: nginx/1.17.
Date: Mon, Nov :: GMT
Content-Type: text/plain
Content-Length:
Connection: keep-alive Thank you for requesting /
[root@localhost ~]# curl -D - http://localhost:8085/notexist
HTTP/1.1 OK
Server: nginx/1.17.
Date: Mon, Nov :: GMT
Content-Type: text/plain
Content-Length:
Connection: keep-alive Thank you for requesting /notexist

  可以看到正常echo。

配置反向代理

  新增配置文件:/etc/nginx/conf.d/proxy.conf ,内容如下:

[root@localhost ~]# cat /etc/nginx/conf.d/proxy.conf
server {
listen ;
location / {
proxy_pass http://localhost:8085;
proxy_set_header Host $host;
}
}

  因为正常安装后,nginx是有默认配置的:/etc/nginx/conf.d/default.conf,这个会影响到上面的正常生效。

[root@localhost ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
[root@localhost ~]# nginx -s reload
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# curl -D - http://localhost
HTTP/1.1 OK
Server: nginx/1.17.
Date: Mon, Nov :: GMT
Content-Type: text/plain
Content-Length:
Connection: keep-alive Thank you for requesting /
[root@localhost ~]# curl -D - http://localhost/noexist
HTTP/1.1 OK
Server: nginx/1.17.
Date: Mon, Nov :: GMT
Content-Type: text/plain
Content-Length:
Connection: keep-alive Thank you for requesting /noexist
[root@localhost ~]#

  可以看到访问默认的80端口,会反向代理到8085端口。

启用WAF

  配置NGINX WAF以通过阻止某些请求来保护演示web应用程序。

[root@localhost ~]# mkdir /etc/nginx/modsec
[root@localhost ~]# cd /etc/nginx/modsec
[root@localhost modsec]# sudo wget https://raw.githubusercontent.com/SpiderLabs/ModSecurity/v3/master/modsecurity.conf-recommended
[root@localhost modsec]# sudo mv modsecurity.conf-recommended modsecurity.conf

  修改modsecurity.conf配置文件

[root@localhost modsec]# vim  modsecurity.conf
# -- Rule engine initialization ----------------------------------------------
...
SecRuleEngine On <== 设置为On

  修改nginx waf配置文件:/etc/nginx/modsec/main.conf ,添加响应规则。

# cat /etc/nginx/modsec/main.conf
# Include the recommended configuration
Include /etc/nginx/modsec/modsecurity.conf
# A test rule
SecRule ARGS:testparam "@contains test" "id:1234,deny,log,status:403"
  • Include:包括modsecurity.conf文件中建议的配置。
  • SecRule:创建一个规则,当查询字符串中的testparam参数包含字符串test时,通过阻止请求并返回状态代码403来保护应用程序。

  修改nginx配置文件,来启用WAF防护。

# cat /etc/nginx/conf.d/proxy.conf
server {
listen ;
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
location / {
proxy_pass http://localhost:8085;
proxy_set_header Host $host;
}
}
  • modsecurity on:启用Nginx WAF;
  • modsecurity_rules_file:指定规则文件路径。
[root@localhost modsec]# cp /opt/ModSecurity/unicode.mapping /etc/nginx/modsec/    # 需要先拷贝下unicode.mapping文件
[root@localhost modsec]# nginx -s reload        # 重载配置
[root@localhost modsec]# nginx -t            # 测试
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

  测试参数中带有test,会被禁止。

[root@localhost modsec]# curl -D - http://localhost/foo?testparam=thisisatestofmodsecurity
HTTP/1.1 Forbidden
Server: nginx/1.17.
Date: Mon, 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>

日志记录功能

  修改nginx配置文件:/etc/nginx/nginx.conf,

# vim /etc/nginx/nginx.conf
load_module /opt/nginx-1.17./objs/ngx_http_modsecurity_module.so;    # 加载模块
user nginx;
worker_processes ; error_log /var/log/nginx/error.log info;      # 将错误日志设置为info级别
[root@localhost modsec]# nginx -s reload       # 重载配置
[root@localhost modsec]# nginx -t           # 测试
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost modsec]# curl -D - http://localhost/foo?testparam=thisisatestofmodsecurity      # 再次访问
HTTP/1.1 Forbidden
Server: nginx/1.17.
Date: Mon, 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>
[root@localhost modsec]# tail - /var/log/nginx/error.log       # 查看错误日志文件
// :: [notice] #: worker process exited with code
// :: [notice] #: signal (SIGIO) received
// :: [notice] #: ModSecurity-nginx v1.0.0 (rules loaded inline/local/remote: //)
// :: [error] #: * [client 127.0.0.1] ModSecurity: Access denied with code (phase ). Matched "Operator `Contains' with parameter `test' against variable `ARGS:testparam' (Value: `thisisatestofmodsecurity' ) [file "/etc/nginx/modsec/main.conf"] [line ""] [id ""] [rev ""] [msg ""] [data ""] [severity ""] [ver ""] [maturity ""] [accuracy ""] [hostname "127.0.0.1"] [uri "/foo"] [unique_id "157405692985.199277"] [ref "o7,4v19,"], client: 127.0.0.1, server: , request: "GET /foo?testparam=thisisatestofmodsecurity HTTP/1.1", host: "localhost"
// :: [info] #: * client 127.0.0.1 closed keepalive connection

参考

ModSecurity:一款优秀的开源WAF

    https://www.freebuf.com/sectool/211354.html

  Installing NGINX WAF

    https://docs.nginx.com/nginx-waf/admin-guide/nginx-plus-modsecurity-waf-installation-logging/#

开源WAF工具ModSecurity的更多相关文章

  1. AVL Insight 开源情报工具:一站式情报管理服务

    一.概要 AVL Insight 开源情报工具是安天移动安全推出的一款情报收集工具,它是配合AVL Insight移动威胁情报平台的Chrome浏览器扩展程序,用户可以使用该工具,对网站中的公开信息进 ...

  2. 开源UML工具推荐

    1.StarUML StarUML是一个开源UML项目,可以开发快速,灵活,可扩展,多功能并且免费的UML/MDA平台.此项目运行在Win32平台之上.StarUML项目的目标是成为RationalR ...

  3. Java开源数据库管理工具

    SQuirreL SQL Client   SQuirreL SQL Client 是一个用 Java 编写的程序,它允许您查看数据库的内容.发出 SQL 命令,以及如您将看到的,执行许多其他功能.构 ...

  4. 【转】开源性能测试工具 - Apache ab 介绍

    版权声明:本文可以被转载,但是在未经本人许可前,不得用于任何商业用途或其他以盈利为目的的用途.本人保留对本文的一切权利.如需转载,请在转载是保留此版权声明,并保证本文的完整性.也请转贴者理解创作的辛劳 ...

  5. Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql、oracle...)间进行数据的传递

    http://niuzhenxin.iteye.com/blog/1706203   Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql.postgresql.. ...

  6. 开源JDBC工具类DbUtils

    本篇将会详细地介绍Apache公司的JDBC帮助工具类DbUtils以及如何使用.在上一篇中我们已经通过将以前对dao层使用JDBC操作数据库的冗余代码进行了简易封装形成自己的简单工具类JdbcUti ...

  7. 8个实用的SVG工具,20 个有用的 SVG 工具,五款超实用的开源SVG工具

    8个实用的SVG工具 [导读] 你还在为没有好用的SVG工具而发愁吗?开发人员的福音来啦!小编为大家收集罗列了8款实用的SVG工具,让我们一起来看看吧! SVG可缩放矢量图形(Scalable Vec ...

  8. 性能测试开源小工具——http_load介绍

    淘测试 性能测试开源小工具——http_load介绍 meizhu 发表于:2009-07-02 浏览:3552次 评论:1次 所属分类: 性能测试 性能测试开源小工具——http_load介绍 ht ...

  9. 开源作业调度工具实现开源的Datax、Sqoop、Kettle等ETL工具的作业批量自动化调度

    1.阿里开源软件:DataX DataX 是一个异构数据源离线同步工具,致力于实现包括关系型数据库(MySQL.Oracle等).HDFS.Hive.ODPS.HBase.FTP等各种异构数据源之间稳 ...

随机推荐

  1. ASP.NET Core 中的 ObjectPool 对象重用(一)

    前言 对象池是一种设计模式,一个对象池包含一组已经初始化过且可以使用的对象,而可以在有需求时创建和销毁对象.池的对象可以从池中取得对象,对其进行操作处理,并在不需要时归还给池子而非直接销毁他,他是一种 ...

  2. c# 基于DataTable的Compute方法的扩展

    DataTable.Compute(String, String) 方法 定义 命名空间:System.Data 程序集:System.Data.dll, netstandard.dll, Syste ...

  3. .net core 上传文件Demo

    view: <form method="post" enctype="multipart/form-data" action="@Url.Act ...

  4. 记一次 Java 项目 CPU 占用久高不下故障处理

    事件背景 公司对接了新系统,代码变动很大,项目也很急,于是在上线之后 Zabbix 不时就告警,提示 CPU 使用过载,告警消息类似如下: 一开始以为是系统停机升级,所有人都等着使用系统,导致系统处理 ...

  5. mac 终端高亮显示~

    针对terminal采用bash模式: 编辑 ~/.bash_profile, 加入以下代码: export CLICOLOR=1 export LSCOLORS=gxfxaxdxcxegedabag ...

  6. JS前端将table导出到excel 兼容谷歌 IE 且保留表格样式

    CDSN上博主给我一段代码,可将表格导出为EXCEL文档,原文见: https://blog.csdn.net/zz210891470/article/details/94717644 向博主学习.致 ...

  7. tp5实现支付宝电脑支付(详解)

    首先吐槽一下支付宝的官方文档,它只是简单介绍一下开发的流程和参数,而对于新人来说如果只看它的官方文档很多时候是看不懂的,我也是边看文档边网上查资料才把它弄懂.下面我详细介绍支付宝的电脑支付是如何实现 ...

  8. HTML+Jquery实现多图片上传预览功能

    HTML:使用input的onchange事件,它一改变就触发事件 <p id="p3"> <input name="File" onchan ...

  9. 区块链学习笔记:D03 区块链在各行业领域的应用(一)

    今天主要是学习了区块链在金融和供应链领域的应用,重点体现了区块链多方参与.透明可信.防篡改防抵赖的技术优势 区块链的应用场景最早是在金融行业应用较多,后续逐步扩展到传统行业,如:供应链.政务服务.物联 ...

  10. 转:spring aop 拦截业务方法,实现权限控制

    难点:aop类是普通的java类,session是无法注入的,那么在有状态的系统中如何获取用户相关信息呢,session是必经之路啊,获取session就变的很重要.思索很久没有办法,后来在网上看到了 ...