本篇将介绍如何使用OpenResty和ModSecurity 来构建自己的WAF,安装过程整体与Nginx是类似的,但也有些区别,在文中会特别指出,本篇算是用openresty对前面两篇nginx和crs的集中介绍。

Preface

版本信息

  • CentOS Linux release 7.6.1810 (Core)
  • nginx version: openresty/1.13.6.1
  • ModSecurity 3.0

安装依赖

# yum install epel-release -y
# 安装modsecurity依赖
# 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
# 安装nginx/openresty依赖
# yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel -y

下载

# mkdir /opt/waf          #创建一个专属目录
# cd /opt/waf
# git clone --depth -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity      # 下载ModSecurity
# wget https://openresty.org/download/openresty-1.13.6.1.tar.gz                        # 下载openresty
# git clone --depth https://github.com/SpiderLabs/ModSecurity-nginx.git                  # 下载ModSecurity-nginx连接器
# pwd
/opt/waf
# ls
ModSecurity ModSecurity-nginx openresty-1.13.6.1.tar.gz

编译ModSecurity

# cd /opt/waf/
# cd ModSecurity/       # 切换到ModSecurity目录
# git submodule init     # 初始化
# git submodule update    # 更新
# ./build.sh
# ./configure
# make
# make install

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

fatal: No names found, cannot describe anything

编译modsecurity_module

# cd /opt/waf/
# tar xvf openresty-1.13.6.1.tar.gz
# ls
ModSecurity ModSecurity-nginx openresty-1.13.6.1 openresty-1.13.6.1.tar.gz
# cd openresty-1.13.6.1/
# ./configure --with-compat --add-dynamic-module=/opt/waf/ModSecurity-nginx    # 需用绝对路径,相对路径会出问题
# gmake            # 不能使用make modules命令
# gmake install       
... 上一步的输出
cp objs/ngx_http_modsecurity_module.so '/usr/local/openresty/nginx/modules/ngx_http_modsecurity_module.so'
gmake[]: Leaving directory `/opt/waf/openresty-1.13.6.1/build/nginx-1.13.'
gmake[]: Leaving directory `/opt/waf/openresty-1.13.6.1/build/nginx-1.13.'
mkdir -p /usr/local/openresty/site/lualib /usr/local/openresty/site/pod /usr/local/openresty/site/manifest
ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty

  openresty默认安装到/usr/local/openresty,最终的是ngx_http_modsecurity_module.so库文件ok。

配置

基本测试

  为了便于后续操作,创建一个openresty的符号链接。

# ln -s /usr/local/openresty/bin/openresty /usr/bin/openresty
# openresty -v
nginx version: openresty/1.13.6.1
# cd /usr/local/openresty/nginx/conf
# head - nginx.conf
load_module /usr/local/openresty/nginx/modules/ngx_http_modsecurity_module.so; # 首行加入
# openresty             # 启动
# openresty -s reload       # 重载配置(如果已启动则重载配置即可)
# openresty -t           # 测试ok
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful

启用WAF

# cd /usr/local/openresty/nginx
# ls
client_body_temp conf fastcgi_temp html logs modules proxy_temp sbin scgi_temp uwsgi_temp
# mkdir modsec      # 创建一个专属modsecurity的规则文件夹
# cd modsec/
# sudo wget https://raw.githubusercontent.com/SpiderLabs/ModSecurity/v3/master/modsecurity.conf-recommended
# mv modsecurity.conf-recommended modsecurity.conf

  修改modsecurity.conf配置文件

# vim  modsecurity.conf
# -- Rule engine initialization ----------------------------------------------
...
SecRuleEngine On <== 设置为On

  拷贝mapping文件。

# cp /opt/waf/ModSecurity/unicode.mapping .
# ls
main.conf modsecurity.conf unicode.mapping

  创建一个主规则文件main.conf,内容如下。

# cat /etc/nginx/modsec/main.conf
# Include the recommended configuration
Include /usr/local/openresty/nginx/modsec/modsecurity.conf    
# A test rule
SecRule ARGS:testparam "@contains test" "id:1234,deny,log,status:403"

  修改nginx配置文件,简要图示如下:

...
server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main;
modsecurity on;      
modsecurity_rules_file
/usr/local/openresty/nginx/modsec/main.conf;
location / {
default_type text/plain;
return "Thank you for requesting ${request_uri}\n"
;
}

...
  • modsecurity on:启用Nginx WAF;
  • modsecurity_rules_file:指定规则文件路径。

测试WAF

# curl localhost
Thank you for requesting /
[root@localhost conf]# curl localhost/foo?testparam=thisisatestofmodsecurity # 携带恶意参数test
<html>
<head><title> Forbidden</title></head>
<body bgcolor="white">
<center><h1> Forbidden</h1></center>        <<= 禁止访问
<hr><center>openresty/1.13.6.1</center>
</body>
</html>

  可以看到参数testparam中带有test关键字会被拦截。

CRS

  OpenResrty 配置文件nginx.conf请参考:https://www.cnblogs.com/Hi-blog/p/ModSecurity.html#autoid-3-4-0

  CRS请参考:OWASP ModSecurity Core Rule Set (CRS)的基本使用

OpenResty + ModSecurity + OWASP CRS的更多相关文章

  1. 网站waf检测

    WAFW00F WAFW00F识别和指纹Web应用防火墙(WAF)产品. 其工作原理是首先通过发送一个正常http请求,然后观察其返回有没有一些特征字符,若没有在通过发送一个恶意的请求触发waf拦截来 ...

  2. kali linux之防火墙识别

    通过检查回包,可能识别端口是否经过防火墙过滤,设备多种多样,结果存在一定的误差 Send Response   Type SYN   NO    Filtered(先发送syn 如果不给回复 防火墙可 ...

  3. 针对Web的信息搜集

    信息收集(Information Gathering),信息收集是指通过各种方式获取所需要的信息,在整个渗透测试环节中,信息搜集是整个渗透过程中最为重要的一环,信息搜集可占据整个渗透测试80%左右的工 ...

  4. OWASP ModSecurity Core Rule Set (CRS)的基本使用

    Preface 前述文章开源WAF工具ModSecurity,介绍了ModSecurity作为Nginx的动态加载模块的基本安装和使用. 本篇简单介绍ModSecurity CRS规则集的使用. # ...

  5. ModSecurity:一款优秀的开源WAF

    一.ModSecurity3.0介绍 ModSecurity是一个开源的跨平台Web应用程序防火墙(WAF)引擎,用于Apache,IIS和Nginx,由Trustwave的SpiderLabs开发. ...

  6. Azure WAF防火墙工作原理分析和配置向导

    Azure WAF工作原理分析和配置向导 本文博客地址为:http://www.cnblogs.com/taosha/p/6716434.html ,转载请保留出处,多谢! 本地数据中心往云端迁移的的 ...

  7. [security] security engine things

    1. luarock luarock 之于 lua,就好比 pip 之于 python https://luarocks.org/ 2.  lua的库 [root@base package]# ls ...

  8. modsecurity3.0 nginx 安装

    备注: 使用的是modsecurity 3.0 的版本,也是nginx 官方推荐使用的,同时使用的是nginx 的dynamic module 1. 环境准备 https://github.com/S ...

  9. Azure上通过haproxy实现APP Gateway或WAF的http跳转https

    Azure上的APP Gateway是七层负载均衡服务,WAF是APP Gateway服务的扩展.在实现七层负载均衡的同时,增加了WAF的功能,可以对后台的HTTP服务进行保护. Azure WAF采 ...

随机推荐

  1. nginx中proxy_pass小斜杠

    nginx中proxy_pass小斜杠 1. 故事背景 相信做微信公众号开发的朋友都知道,要想在微信中预览效果,必须使用域名访问.很多朋友使用内网穿透工具.不仅不好用还不稳定.所以,发挥脸厚吃天下的态 ...

  2. WPF 使用Win32API 让控件置于WebBrowser上方

    WPF中Webbrowser控件使用HwndHost所以webbrowser会在所有控件的前方.所以webbrowser会覆盖所有同级的控件. 现在通过使用Win32API 可以避免这个情况. 最主要 ...

  3. Spring Boot 2.0 学习笔记(一)——JAVA EE简介

    本章内容:JAVA EE>Spring>Spring Boot 一.JAVA EE简介 1.1 Java ee优点:结束了Web开发的技术无序状态,让程序员.架构师用同一种思维去思考如何架 ...

  4. 堡垒机WebSSH进阶之实时监控和强制下线

    这个功能我可以不用,但你不能没有 前几篇文章实现了对物理机.虚拟机以及Kubernetes中Pod的WebSSH操作,可以方便的在web端对系统进行管理,同时也支持对所有操作进行全程录像,以方便后续的 ...

  5. 使用app测试Modelarts在线服务

    1. 基础准备 本demo代码已上传github地址为 https://github.com/zxzxzxygithub/hwmodelartdemo clone下来之后导入android studi ...

  6. PyTorch最佳实践,怎样才能写出一手风格优美的代码

    [摘要] PyTorch是最优秀的深度学习框架之一,它简单优雅,非常适合入门.本文将介绍PyTorch的最佳实践和代码风格都是怎样的. 虽然这是一个非官方的 PyTorch 指南,但本文总结了一年多使 ...

  7. 洛谷 P2342 叠积木 题解

    本蒟蒻又来发题解了 这题是不是有点像并查集,但是那个询问的个数是不是有点骚: 所以,普通的并查集是无法解决这个问题的,这个时候就需要用到带权并查集了: 每次跑的时候都记录下它的下面有几个点,然后询问的 ...

  8. android 点击无效验证

    背景 在写一个东西滑动删除列表的时候,出现了一个问题.我的需求是,左滑然后出现delete,然后点击delete,让该滑块消失. 我在点列表的第一行的时候,左滑,出现delete,点击删除,ok的,完 ...

  9. dubbo 订阅 RPC 服务

    Dubbo 订阅 RPC 服务 建立消费者者项目 pom.xml <?xml version="1.0" encoding="UTF-8"?> &l ...

  10. 如何在ASP.NET Core 中快速构建PDF文档

    比如我们需要ASP.NET Core 中需要通过PDF来进行某些简单的报表开发,随着这并不难,但还是会手忙脚乱的去搜索一些资料,那么恭喜您,这篇帖子会帮助到您,我们就不会再去浪费一些宝贵的时间. 在本 ...