zoukankan      html  css  js  c++  java
  • 40、Nginx 配置支持 WAF

    40.1 waf说明

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

    2 WAF的定义是这样的:Web应用防火墙是通过执行一系列针对HTTP/HTTPS的安全策略来专门为Web应用提供保护的一款产品。

    3 通过从上面对WAF的定义中,我们可以很清晰地了解到:WAF是一种工作在应用层的、通过特定的安全策略来专门为Web应用

    提供安全防护的产品。

    4 ngx_lua_waf 是一个基于ngx_lua的web应用防火墙。

    5 ngx_lua_waf用途:

    (1) 防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击

    (2) 防止svn/备份之类文件泄露

    (3) 防止ApacheBench之类的压力测试工具的攻击

    (4) 屏蔽常见的黑客扫描工具,扫描器

    (5) 屏蔽异常的网络请求

    (6) 屏蔽图片附件类目录PHP执行权限

    (7) 防止webshell上传

    6 什么是lua

    Lua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序

    提供灵活的扩展和定制功能。

    7 部署环境

    # cat /etc/redhat-release

    CentOS Linux release 7.5.1804 (Core)

    # uname -r

    3.10.0-862.el7.x86_64

    40.2 安装waf

    1 安装依赖包

    # yum -y install gcc gcc-c++ autoconf automake make unzip

    # yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

    2 安装LuaJIT2.0

    LuaJIT是采用C语言写的Lua代码的解释器 这里推荐使用lujit2.1做lua支持。

    # wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz

    # tar -xzf LuaJIT-2.0.5.tar.gz

    # cd LuaJIT-2.0.5/

    # make && make install

    3 安装ngx_devel_kit

    NDK(nginx development kit)模块是一个拓展nginx服务器核心功能的模块,第三方模块开发可以基于它来快速实现。

    # wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz

    # tar -xzf v0.3.0.tar.gz

    # mv ngx_devel_kit-0.3.0/ /usr/local/src/

    4 安装nginx_lua_module

    # wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz

    # tar -xzf v0.10.13.tar.gz

    # mv lua-nginx-module-0.10.13/ /usr/local/src/

    5 导入环境变量

    # echo "export LUAJIT_LIB=/usr/local/lib" >> /etc/profile

    # echo "export LUAJIT_INC=/usr/local/include/luajit-2.0" >> /etc/profile

    # source /etc/profile

    6 编译安装nginx

    # wget http://nginx.org/download/nginx-1.14.0.tar.gz

    # tar -xzf nginx-1.14.0.tar.gz

    # cd nginx-1.14.0/

    # useradd -s /sbin/nologin -M nginx

    # ./configure --user=nginx --group=nginx

    --prefix=/usr/local/nginx-1.14.0

    --with-http_stub_status_module

    --with-http_ssl_module

    --with-http_gzip_static_module

    --pid-path=/usr/local/nginx-1.14.0/nginx.pid

    --with-http_realip_module

    --add-module=/usr/local/src/ngx_devel_kit-0.3.0

    --add-module=/usr/local/src/lua-nginx-module-0.10.13

    --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"

    # make -j2 && make install

    # ln -s /usr/local/nginx-1.14.0 /usr/local/nginx

    # ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

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

    # mkdir -p /usr/local/nginx/logs/hack/

    # chown -R nginx.nginx /usr/local/nginx/logs/hack/

    8 配置ngx_lun_waf

    把ngx_lua_waf下载到nginx的conf目录下,解压命名为waf

    # wget https://github.com/loveshell/ngx_lua_waf/archive/master.zip

    # unzip master.zip -d /usr/local/nginx/conf/

    # mv /usr/local/nginx/conf/ngx_lua_waf-master /usr/local/nginx/conf/waf

    9 在nginx.conf的http段添加如下内容

    # vim /usr/local/nginx/conf/nginx.conf

    http {

    ......

    lua_package_path "/usr/local/nginx/conf/waf/?.lua";

    lua_shared_dict limit 10m;

    init_by_lua_file /usr/local/nginx/conf/waf/init.lua;

    access_by_lua_file /usr/local/nginx/conf/waf/waf.lua;

    ......

    }

    # /usr/local/nginx/sbin/nginx -t

    nginx: the configuration file /usr/local/nginx-1.14.0/conf/nginx.conf syntax is ok

    nginx: configuration file /usr/local/nginx-1.14.0/conf/nginx.conf test is successful

    40.3 测试

    1 启动nginx

    # /usr/local/nginx/sbin/nginx

    2 访问url地址出现如下结果则说明规则生效

    http://172.16.1.122/test.tex?id=../etc/passwd

    40.4 waf配置文件说明

    # cat /usr/local/nginx/conf/waf/config.lua

    RulePath = "/usr/local/nginx/conf/waf/wafconf/"

    --规则存放目录,绝对路径如有变动,需对应修改

    attacklog = "on"

    --是否开启攻击信息记录,需要配置logdir

    logdir = "/usr/local/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=[[

    <html xmlns="http://www.w3.org/1999/xhtml"><head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <title>网站防火墙</title>

    ......

    ]]

    --警告内容,可在中括号内自定义

    备注:

    (1) 不要乱动双引号,配置文件区分大小写。

    (2) 默认开启了get、post、cookie过滤。

    (3) 日志文件名称格式为"虚拟主机名_日期_sec.log"。

    (4) 默认,黑白名单不过滤,可自行调整config.lun配置,使用"--"注释其中一个,另一个

    起作用。

    40.5 过滤规则

    # ls /usr/local/nginx/conf/waf/

    config.lua init.lua install.sh README.md wafconf waf.lua

    (1) config.lua # 配置文件

    (2) init.lua # 规则函数

    (3) waf.lua # 逻辑关系

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

    # ls /usr/local/nginx/conf/waf/wafconf/

    args cookie post url user-agent whiteurl

    (1) args # 里面的规则get参数进行过滤的

    (2) url # 是只在get请求url过滤的规则

    (3) post # 是只在post请求过滤的规则

    (4) whiteurl # 是白名单,里面的url匹配到不做过滤

    (5) user-agent # 是对user-agent的过滤规则

    (6) cookie # cookie过滤

  • 相关阅读:
    String类型和包装类型作为参数传递时,是属于值传递还是引用传递呢?
    RPC 框架
    大型分布式电商系统架构演进史?
    框架演变
    SSM和springboot对比
    什么是微服务,SpringBoot和SpringCloud的关系和区别
    Springboot,SSM及SSH的概念、优点、区别及缺点
    Spring Cloud 和dubbo
    LeetCode 048 Rotate Image
    LeetCode 047 Permutations II
  • 原文地址:https://www.cnblogs.com/LiuChang-blog/p/14641963.html
Copyright © 2011-2022 走看看