zoukankan      html  css  js  c++  java
  • Nginx 支持 WAF 防护功能实战

    WAF(Web Application Firewall),中文名称叫做“Web应用防火墙

    WAF的定义是这样的:Web应用防火墙是通过执行一系列针对HTTP/HTTPS的安全策略来专门为Web应用提供保护的一款产品,通过从上面对WAF的定义中,我们可以很清晰地了解到:WAF是一种工作在应用层的、通过特定的安全策略来专门为Web应用提供安全防护的产品。
    用途:
      用于过滤post,get,cookie方式常见的web攻击
      防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击
      防止svn/备份之类文件泄漏
      防止ApacheBench之类压力测试工具的攻击
      屏蔽常见的扫描黑客工具,扫描器
      屏蔽异常的网络请求
      屏蔽图片附件类目录php执行权限
      防止webshell上传

    安装前准备:

      系统:CentOS6.5

      nginx:/opt/nginx-1.4.4(nginx我之前已经安装,在/opt/nginx/目录,现在只是安装 Nginx 支持 WAF 防护功能实战 )

      yum -y install gcc gcc-c++ autoconf automake make
      yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

    开始安装:

    1.安装LuaJIT 2.0(推荐使用lujit2.1做lua支持)

    下载luajit 2.0并安装
    http://luajit.org/download.html
    tar zxf LuaJIT-2.0.0.tar.gz
    cd LuaJIT-2.0.0
    make && make install
    注:lib和include是直接放在/usr/local/lib和usr/local/include
    

    2.安装ngx_devel_kit

    下载ngx_devel_kit解压
    https://github.com/simpl/ngx_devel_kit/tags
    wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz --no-check-certificate
    tar -zxvf  v0.2.18  
    

    3.安装nginx_lua_module

    下载nginx_lua_module解压
    https://github.com/chaoslawful/lua-nginx-module/tags
    wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.7.18rc2.tar.gz --no-check-certificate
    tar -zxvf v0.7.18rc2
    

    4.导入环境变量,编译

    export LUAJIT_LIB=/usr/local/lib    
    export LUAJIT_INC=/usr/local/include/luajit-2.0
    

    5.进入nginx源码目录对nginx进行重新编译,将waf功能编译进nginx中:

    [root@localhost ~]# cd /root/soft/nginx-1.4.4
    [root@localhost nginx-1.4.4]# ./configure --prefix=/opt/nginx/ --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module 
     --add-module=/root/soft/ngx_devel_kit-0.2.18 
     --add-module=/root/soft/lua-nginx-module-0.7.18rc2 
     --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"
    
    [root@localhost nginx-1.4.4]#make -j2
    [root@localhost nginx-1.4.4]#make install
    

    6.新建/opt/nginx/logs/hack/目录攻击日志,并赋予nginx用户对该目录的写入权限.

    mkdir -p /opt/nginx/logs/hack/
    www账户是跑nginx服务的用户
    chown -R www:www /opt/nginx/logs/hack/
    chmod -R 755 /opt/nginx/logs/hack/
    

    至此nginx支持WAF防护功能已经搭建完成!

    使用说明:

    nginx安装路径假设为:/opt/nginx/conf/

    把ngx_lua_waf模块下载到conf目录下,解压命名为waf

    [root@oracle-demo conf]# mv  ngx_lua_waf-master /opt/nginx/conf/waf

    ngx_lua_waf下载地址:https://github.com/loveshell/ngx_lua_waf

    在nginx.conf的http段添加:

    http
    {
     ...
      lua_package_path "/opt/nginx/conf/waf/?.lua"; 
      lua_shared_dict limit 10m;
      init_by_lua_file  /opt/nginx/conf/waf/init.lua; 
      access_by_lua_file /opt/nginx/conf/waf/waf.lua;
    
    ...
    }
    

    配置config.lua里的waf规则目录(一般在wafconf/目录下)

    RulePath = "/opt/nginx/conf/waf/wafconf/"

    绝对路径如有变动,需对应修改

    然后重启nginx即可

    检查规则是否生效:

    部署完毕可以尝试如下命令:

    curl http://xxxx/test.php?id=../etc/passwd
    返回"Please go away~~"字样,说明规则生效。

    #vim test.php
    <?phpinfo()?>
     

    配置文件详细说明:(vim waf/config.lua )

    RulePath = "/opt/nginx/conf/waf/wafconf/"
        --规则存放目录
        attacklog = "off"
        --是否开启攻击信息记录,需要配置logdir
        logdir = "/opt/nginx/logs/hack/"
        --log存储目录,该目录需要用户自己新建,切需要nginx用户的可写权限
        UrlDeny="on"
        --是否拦截url访问
        Redirect="on"
        --是否拦截后重定向
        CookieMatch = "on"
        --是否拦截cookie攻击
        postMatch = "on" 
        --是否拦截post攻击
        whiteModule = "on" 
        --是否开启URL白名单
        black_fileExt={"php","jsp"}
        --填写不允许上传文件后缀类型
        ipWhitelist={"127.0.0.1"}
        --ip白名单,多个ip用逗号分隔
        ipBlocklist={"1.0.0.1"}
        --ip黑名单,多个ip用逗号分隔
        CCDeny="on"
        --是否开启拦截cc攻击(需要nginx.conf的http段增加lua_shared_dict limit 10m;)
        CCrate = "100/60"
        --设置cc攻击频率,单位为秒.
        --默认1分钟同一个IP只能请求同一个地址100次
        html=[[Please go away~~]]
        --警告内容,可在中括号内自定义
        备注:不要乱动双引号,区分大小写
    

    一些说明:

    过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割

    args   里面的规则get参数进行过滤的
    url   是只在get请求url过滤的规则
    post   是只在post请求过滤的规则
    whitelist 是白名单,里面的url匹配到不做过滤
    user-agent是对user-agent的过滤规则

    默认开启了get和post过滤,需要开启cookie过滤的,编辑waf.lua取消部分--注释即可

    参考文档:https://github.com/loveshell/ngx_lua_waf 

  • 相关阅读:
    洛谷P2568 GCD
    线段树(模板)
    题解 CF1296D 【Fight with Monsters】
    图片针对父元素居中 TileImg
    npm
    echarts线图,柱状图,饼图option
    mac下修改环境变量
    input获取焦点,但不调起键盘
    mac shh 关联git仓库
    根据滚动条触发动画
  • 原文地址:https://www.cnblogs.com/saneri/p/5341224.html
Copyright © 2011-2022 走看看