开源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等各种异构数据源之间稳 ...
随机推荐
- 痞子衡嵌入式:恩智浦机器视觉模块OpenMV-RT那些事(1)- 初体验
大家好,我是痞子衡,是正经搞技术的痞子.本系列痞子衡给大家介绍的是机器视觉模块OpenMV-RT初体验. 近些年机器视觉应用一直是个很火的方向,想象一下机器如果能长上"眼睛",是不 ...
- Lab6:进程的调度
CPU调度 从就绪队列中挑选下一个占用CPU运行的进程,从多个可用CPU中挑选就绪进程可使用的CPU资源 调度策略 比较调度算法的准则 CPU使用率 吞吐量 周转时间 就绪等待时间 响应时间 吞吐量与 ...
- selenium webdriver学习--------iframe的处理
有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,代 码也没有任何问题.这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的原因之一.如果你在 ...
- 关于python中的列表遍历注意事项
在开发过程中,很容易出现以下的错误: 可以看出:假如删除列表的元素之后直接执行continue,那么遍历的时候就会落下一个元素. 那么怎么解决这个问题呢? 首先 : 我们尝试把continue去掉: ...
- Chapter 05—Advanced data management(Part 1)
一. R的数学函数,统计函数及字符处理函数 例01:一道实际应用题 一组学生其数学,科学和英语的成绩如下表: 任务:根据成绩,决定对每个学生的单独指导: 前20%的学生的成绩为A,次之为B,以此类推: ...
- react新版本配置代理
新学习react 开始配置react跨域的时候 在网上查看到是在packjson.json里面添加如下代码: "proxy": { "/api": { &quo ...
- mac本地搭建svn
mac系统默认已经安装了svn,我们只需要配置并开启就可以了. 首先我们可以验证一下是否安装了svn,打开终端,输入命令 svnserve —version
- 使用modelarts部署bert命名实体识别模型
模型部署介绍 当我们通过深度学习完成模型训练后,有时希望能将模型落地于生产,能开发API接口被终端调用,这就涉及了模型的部署工作.Modelarts支持对tensorflow,mxnet,pytorc ...
- Nginx 404 Not Found 解决办法
环境:宝塔Nginx面板 解决办法: 宝塔面板--站点设置-配置文件. 去掉: error_page 404 /404.html; 前面的 # 号.
- 【Java库】如何使用优秀的加密库Jasypt来保护你的敏感信息?
1 简介 今天我们介绍一个Java库-Jasypt,全称为Java Simplified Encryption,用于加密解密.它能够让开发者用花费最小的工作而把加密集成到项目中,并且不需要对加密/解密 ...