开源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等各种异构数据源之间稳 ...
随机推荐
- day29 文件的上传和下载 socketserver(并发)
文件上传的讲解: import subprocess res=subprocess.Popen("dir", shell=True, stderr=subprocess.PIPE, ...
- linux中Nginx安装
linux中Nginx安装 编译安装 Nginx的优点太多,这里不再赘述,详情请看这篇博客深入理解nginx. Nginx的安装有rpm包安装.编译安装和docker安装.本文将介绍编译安装方 ...
- Java中父类和子类代码执行顺序
执行顺序:父类静态块-->子类静态块-->父类非静态块-->父类构造方法-->子类非静态块-->子类构造方法 当父类或子类中有多个静态方法时按在代码中的顺序执行 pack ...
- 请停止编写这么多的for循环!
在这篇文章中,我想和你一起回到基础知识,并讨论 Java 中的 for 循环.老实说,我正在为自己写这篇博客文章,因为我也会这样做.从 Java 8 开始,我们不必在 Java 中编写太多 for 循 ...
- mysql配置允许外界连接
1.在mysql的安装目录下找到mysql.ini 找到bind-address=127.0.0.1 ,(有的有,有的没有) 改为 bind-address = 0.0.0.0 2.登录mysql客户 ...
- 【JavaEE】之MyBatis开发DAO
在SSM框架中的DAO层就是MyBatis中的Mapper,Mapper分为两部分:Mapper接口(JAVA文件)和Mapper映射文件(XML文件).DAO开发(Mapper开发)有两种方式:原始 ...
- Java判断字符串相等"=="和"equal"详解
在初学Java时,可能会经常碰到下面的代码: public static void main(String[] args) { //两种声明方式,有所差别 String s1="hello& ...
- MVC参数传递
MVC参数传递 请求参数自动类型转换 JSP页面 form class="loginForm" action="/user/getUser" method=&q ...
- Java的值类型和引用类型
一.问题描述 前几天因为一个需求出现了Bug.说高级点也挺高级,说白点也很简单.其实也就是一个很简单的Java基础入门时候的值类型和引用类型的区别.只是开发的时候由于自己的问题,导致小问题的出现.还好 ...
- 计蒜客-蒜场抽奖(AC自动机+状态压缩DP)
题解:题意不再说了,题目很清楚的. 思路:因为N<=10,所以考虑状态压缩 AC自动机中 val[1<<i]: 表示第i个字符串.AC自动机中fail指针是指当前后缀在其他串里面所能 ...