开源WAF工具ModSecurity
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
参考
https://www.freebuf.com/sectool/211354.html
https://docs.nginx.com/nginx-waf/admin-guide/nginx-plus-modsecurity-waf-installation-logging/#
开源WAF工具ModSecurity的更多相关文章
- AVL Insight 开源情报工具:一站式情报管理服务
		
一.概要 AVL Insight 开源情报工具是安天移动安全推出的一款情报收集工具,它是配合AVL Insight移动威胁情报平台的Chrome浏览器扩展程序,用户可以使用该工具,对网站中的公开信息进 ...
 - 开源UML工具推荐
		
1.StarUML StarUML是一个开源UML项目,可以开发快速,灵活,可扩展,多功能并且免费的UML/MDA平台.此项目运行在Win32平台之上.StarUML项目的目标是成为RationalR ...
 - Java开源数据库管理工具
		
SQuirreL SQL Client SQuirreL SQL Client 是一个用 Java 编写的程序,它允许您查看数据库的内容.发出 SQL 命令,以及如您将看到的,执行许多其他功能.构 ...
 - 【转】开源性能测试工具 - Apache ab 介绍
		
版权声明:本文可以被转载,但是在未经本人许可前,不得用于任何商业用途或其他以盈利为目的的用途.本人保留对本文的一切权利.如需转载,请在转载是保留此版权声明,并保证本文的完整性.也请转贴者理解创作的辛劳 ...
 - Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql、oracle...)间进行数据的传递
		
http://niuzhenxin.iteye.com/blog/1706203 Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql.postgresql.. ...
 - 开源JDBC工具类DbUtils
		
本篇将会详细地介绍Apache公司的JDBC帮助工具类DbUtils以及如何使用.在上一篇中我们已经通过将以前对dao层使用JDBC操作数据库的冗余代码进行了简易封装形成自己的简单工具类JdbcUti ...
 - 8个实用的SVG工具,20 个有用的 SVG 工具,五款超实用的开源SVG工具
		
8个实用的SVG工具 [导读] 你还在为没有好用的SVG工具而发愁吗?开发人员的福音来啦!小编为大家收集罗列了8款实用的SVG工具,让我们一起来看看吧! SVG可缩放矢量图形(Scalable Vec ...
 - 性能测试开源小工具——http_load介绍
		
淘测试 性能测试开源小工具——http_load介绍 meizhu 发表于:2009-07-02 浏览:3552次 评论:1次 所属分类: 性能测试 性能测试开源小工具——http_load介绍 ht ...
 - 开源作业调度工具实现开源的Datax、Sqoop、Kettle等ETL工具的作业批量自动化调度
		
1.阿里开源软件:DataX DataX 是一个异构数据源离线同步工具,致力于实现包括关系型数据库(MySQL.Oracle等).HDFS.Hive.ODPS.HBase.FTP等各种异构数据源之间稳 ...
 
随机推荐
- 做为GPU服务器管理员,当其他用户需要执行某个要root权限的命令时,除了告诉他们root密码,还有没有别的办法?
			
通常一台GPU服务器(这里指linux系统)不可能只有一个帐号能用的,比如当其他用户想要在GPU服务器上安装一些软件的时候,会需要用到apt-get命令,但是apt-get命令需要root用户的操作权 ...
 - Android View 的添加绘制流程 (二)
			
概述 上一篇 Android DecorView 与 Activity 绑定原理分析 分析了在调用 setContentView 之后,DecorView 是如何与 activity 关联在一起的,最 ...
 - 安装anaconda后启动终端头部会有(base)如何解决
			
conda config --show conda config --set auto_activate_base False
 - Java 大黑话讲解设计模式 -- UML类图
			
目录 1.啥是UML类图? 2.UML类图有啥用? 3.正式理解UML类图 4.使用idea画第一个UML类图 5.类之间的关系图[必须牢记] 6.类之间的关系 6.1.依赖 6.2.泛化 6.3.实 ...
 - vue 中使用 watch 的各种问题
			
报错: Method "watch" has type "object" in the component definition. Did you refere ...
 - appium环境的搭建
			
appium环境的搭建,之前看过很多关于appium环境搭建的文章,一个感觉就是“乱”. 所以才想自己来写一篇appium环境的搭建,算是总结和备忘吧. 如下图,其实appium的搭建分三部分完成,各 ...
 - Mac配置Gradle环境
			
下载Gradle 下载地址:https://gradle.org/install 下载最新版本:gradle-3.3 (当前最新版2017年2月8日) 配置Gradle环境 我的本机Gradle存放路 ...
 - 关于layer的基本所有的事件全部失效问题
			
只要在页面中,要是存在id="undefined", layer的基本所有的事件全部失效. <input type="radio" id="un ...
 - C#中的委托和事件(二)
			
引言 如果你看过了 C#中的委托和事件 一文,我想你对委托和事件已经有了一个基本的认识.但那些远不是委托和事件的全部内容,还有很多的地方没有涉及.本文将讨论委托和事件一些更为细节的问题,包括一些大家常 ...
 - python上下文管理器细读
			
test 1 上下文管理器,将生成器转化为上下文管理器 import contextlib @contextlib.contextmanager def a(): print(1) yield pri ...