ModSecurity

在Ubuntu和Nginx上安装,nginx版本为1.14.0。

安装需要包

  1. apt-get install -y apt-utils autoconf automake build-essential git libcurl4-openssl-dev libgeoip-dev liblmdb-dev libpcre++-dev libtool libxml2-dev libyajl-dev pkgconf wget zlib1g-dev

下载编译ModSecurity 3.0源代码

  1. git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity

进入目录运行编译

  1. ./build.sh
  2. git submodule init
  3. git submodule update
  4. ./configure
  5. make
  6. make install

如果出现fatal: No names found, cannot describe anything.,是可以忽略的。

ModSecurity连接器

下载连接器代码

  1. git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git

根据已安装的nginx版本安装需要的nginx原代码

  1. wget http://nginx.org/download/nginx-1.14.0.tar.gz
  2. tar zxvf nginx-1.14.0.tar.gz

需要把连接器编译为动态模块到nginx中,到nginx目录下,其中的编译命令根据nginx -V来决定。

  1. ./configure --with-compat --add-dynamic-module=../ModSecurity-nginx
  2. make modules
  3. mkdir /etc/nginx/modules
  4. cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules/

PS:运行nginx -t测试nginx运行,如果出现报错ModSecurity二进制文件不匹配,则可能是nginx版本验证不匹配,那就需要源码编译安装nginx,同时把ModSecurity一起编译。

加载nginx动态模块

编译完成后的动态模块需要到配置文件中启动加载,修改配置文件/etc/nginx/nginx.conf

  1. load_module /etc/nginx/modules/ngx_http_modsecurity_module.so;

启用和测试规则

安装Spiderlabs的规则

  1. mkdir /etc/nginx/modsec
  2. wget -P /etc/nginx/modsec/ https://raw.githubusercontent.com/SpiderLabs/ModSecurity/v3/master/modsecurity.conf-recommended
  3. mv /etc/nginx/modsec/modsecurity.conf-recommended /etc/nginx/modsec/modsecurity.conf

把ModSecurity中的unicode.mapping文件复制到以上目录

  1. cp ModSecurity/unicode.mapping /etc/nginx/modsec

修改配置文件,其中配置部分被注释,需要安需要启用。

  1. SecRuleEngine DetectionOnly #仅检测恶意流量,改为on为丢弃恶意流量

添加owasp的规则,在nginx下创建目录

  1. mkdir owasp-modsecurity-crs
  2. cd owasp-modsecurity-crs
  3. wget https://github.com/coreruleset/coreruleset/archive/v3.3.0.tar.gz
  4. mv crs-setup.conf.example crs-setup.conf
  5. mv rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
  6. mv rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf

在modsec中修改modsecurity.conf,添加配置文件,同时把rules目录下的配置文件添加进去

  1. Include /etc/nginx/modsec/owasp-modsecurity-crs/coreruleset-3.3.0/crs-setup.conf
  2. Include /etc/nginx/modsec/owasp-modsecurity-crs/coreruleset-3.3.0/rules/*.conf

在nginx配置文件中添加此配置

  1. vi sites-enabled/default
  2. server {
  3. listen 80;
  4. modsecurity on;
  5. modsecurity_rules_file /etc/nginx/modsec/modsecurity.conf;
  6. }

可以到crs配置文件中找到对应的规则来启用,比如如下启用id为900240的后缀文件检测

  1. C:\Users\user>curl http://192.168.120.142/admin.db
  2. <html>
  3. <head><title>403 Forbidden</title></head>
  4. <body bgcolor="white">
  5. <center><h1>403 Forbidden</h1></center>
  6. <hr><center>nginx/1.14.0</center>
  7. </body>
  8. </html>

默认情况下会拦截常见的web攻击,比如

  1. C:\Users\user>curl http://192.168.120.142/admin.php?id=1%20and%201=1
  2. <html>
  3. <head><title>403 Forbidden</title></head>
  4. <body bgcolor="white">
  5. <center><h1>403 Forbidden</h1></center>
  6. <hr><center>nginx/1.14.0</center>
  7. </body>
  8. </html>

配置新的conf文件

如果默认的规则文件不符合需求,获取由于其他原因需要添加规则。先看一个普通的规则是怎么样的

  1. SecRule REQUEST_FILENAME "@endsWith /wp-login.php" \
  2. "id:1003,\
  3. phase:2,\
  4. pass,\
  5. nolog,\
  6. ctl:ruleRemoveTargetByTag=OWASP_CRS;ARGS:pwd"

相当于在wp-login.php页面中,禁用对参数pwd的拦截检查。其中的格式是

  1. SecRule VARIABLES OPERATOR ACTIONS
  2. VARIABLES :代表HTTP包中的标识项,规定了安全规则针对的对象。
  3. OPERATOR:代表操作符,一般用来定义安全规则的匹配条件。
  4. ACTIONS:代表响应动作,一般用来定义数据包被规则命中后的响应动作。

比如,我们打算写一个只允许特定IP访问的路径。这时候需要两条规则来判断,需要chain来连接动作。把它保存到上面crs配置文件路径。重启nginx

  1. SecRule REQUEST_FILENAME "@endsWith /login.php" \
  2. "id:91002,\
  3. phase:1,\
  4. pass,\
  5. nolog,\
  6. deny,\
  7. chain"
  8. SecRule REMOTE_ADDR "!@ipMatch 192.168.120.1" \
  9. "t:none"

配置文档参考:http://www.modsecurity.cn/chm/ConfigurationDirectives.html

VeryNginx

VeryNginx 基于 lua_nginx_module(openrestry) 开发。 集成在 Nginx 中运行,扩展了 Nginx 本身的功能,并提供了友好的 Web 交互界面。本质上这并不是一个WAF,只是openrestry的界面化。但是可以通过界面化的配置规则来实现waf的功能。

https://github.com/alexazhou/VeryNginx

一键配置

克隆 VeryNginx 仓库到本地, 然后进入仓库目录,执行以下命令

  1. python install.py install

即可一键安装 VeryNginx 和 以及依赖的 OpenResty,可以先修改其中的版本为新版本地址再安装。

手动安装

手动安装,需要本地的nginx有openrestry,所以先安装。

  1. apt-get install libreadline-dev libpcre3-dev libssl-dev perl
  2. wget https://openresty.org/download/openresty-1.17.8.2.tar.gz
  3. ./configure -j2
  4. make -j2
  5. sudo make install
  6. export PATH=/usr/local/openresty/bin:$PATH

这时候会把openrestry加入快捷命令,如果直接执行openrestry将开启自带的nginx。

再来安装verynginx

  1. python install.py install verynginx

在nginx的配置文件中添加以下规则,需要配置到不同的地方。

  1. include /opt/verynginx/verynginx/nginx_conf/in_external.conf; #http外部
  2. include /opt/verynginx/verynginx/nginx_conf/in_http_block.conf; #http内部
  3. include /opt/verynginx/verynginx/nginx_conf/in_server_block.conf; #server内部

在地址/verynginx/index_zh.html下即可访问到页面。

默认的账号密码都是verynginx / verynginx。添加规则的方式比modsecurity简单,但自带的规则过于简单,需要自己添加规则。

已有的规则可以参考:https://github.com/unixhot/waf

JxWaf

https://github.com/jx-sec/jxwaf

jxwaf(锦衣盾)是一款开源WEB应用防火墙,分为客户端和服务端,客户端也是openrestry开发而来。

服务端安装

采用私有化部署

  1. docker run -d -p 80:80 jxwaf/jxwaf-server:latest

拉取后,访问地址邮箱验证码随便填写,注册账号在全局配置页面获取”api key”和”api password”

客户端安装

  1. git clone https://github.com/jx-sec/jxwaf.git
  2. cd jxwaf
  3. sh install_waf.sh

在下载的文件内:

  1. cd tools
  2. python jxwaf_init.py --api_key=xxxx --api_password=xxxx --waf_server=http://192.168.1.1
  3. /opt/jxwaf/nginx/sbin/nginx

在网站管理中添加对应的域名和IP端口

Janusec

可多节点布置的应用网关。https://github.com/Janusec/janusec

  1. wget https://www.janusec.com/download/janusec-latest.tar.gz
  2. tar zxf ./janusec-latest.tar.gz
  3. cd janusec-0.9.9
  4. ./install.sh

此处选择的是1,主节点。默认安装的路径为 /usr/local/janusec/

由于需要postgresql

  1. sudo apt-get install postgresql postgresql-client
  2. sudo -i -u postgres
  3. create user janusec with password 'janusec';
  4. create database janusec owner janusec;
  5. grant all on database janusec to janusec;
  6. \q

然后编辑 /usr/local/janusec/config.json ,尝试开启waf

  1. systemctl start janusec

设置中管理太台监听设置为true,则使用http:9080端口,此处修改为false,直接在目录janusec-admin/

默认用户名:admin,默认口令:J@nusec123。在应用管理中添加IP端口信息即可。





# Open Source Security