• nginx + lua 构建网站防护waf(一)


    最近在帮朋友维护一个站点。这个站点是一个Php网站。坑爹的是用IIS做代理。出了无数问题之后忍无可忍,于是要我帮他切换到nginx上面,前期被不断的扫描和CC。最后找到了waf这样一个解决方案缓解一下。话不多说直接开始。

    waf的作用:

    防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击
    防止svn/备份之类文件泄漏
    防止ApacheBench之类压力测试工具的攻击
    屏蔽常见的扫描黑客工具,扫描器
    屏蔽异常的网络请求
    屏蔽图片附件类目录php执行权限
    防止webshell上传

    nginx 的话我选择春哥开源的:OpenResty一个伟大的项目。

    好了步骤开始:

    1、安装Luagit:

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

    # tar -xvf LuaJIT-2.1.0-beta1.tar.gz

    # cd LuaJIT-2.1.0-beta1

    # make

    # make install

    #ln -sf luajit-2.1.0-beta1 /usr/local/bin/luajit

    2、安装openresty:

     ./configure --prefix=/opt/openresty   --with-luajit     --without-http_redis2_module       --with-http_iconv_module

    gmake

    gmake install

    3、测试openresty:

    [root@www ngx_lua_waf]# cd /opt/openresty/nginx/conf/

    [root@www conf]# cat nginx.conf

    http {

        server { 

            listen 80;

            location / {

                     default_type text/html;

                  content_by_lua_block {

                      ngx.say("HelloWorld")

                                }

                     }

         }

    }

    ###

    测试一下访问是否输出hello world,后面应该会有一些列的简介。

    [root@www conf]# curl localhost

    HelloWorld

    4、下载开源项目:

    [root@www nginx]# cd /opt/openresty/nginx/

    [root@www nginx]# git clone https://github.com/loveshell/ngx_lua_waf.git

    5、然后修改nginx添加配置,支持lua脚本地址,在http段位置:

    lua_package_path "/opt/openresty/nginx/ngx_lua_waf/?.lua";  ###相关项目存放地址

    lua_shared_dict limit 10m;                       ###存放limit表的大小

    init_by_lua_file  /opt/openresty/nginx/ngx_lua_waf/init.lua; ###相应地址

    access_by_lua_file /opt/openresty/nginx/ngx_lua_waf/waf.lua; ##相应地址

    6、修改ngx_lua_waf相关配置:

    [root@www ngx_lua_waf]# vim config.lua 

    RulePath = "/opt/openresty/nginx/ngx_lua_waf/wafconf/"   ##指定相应位置

    attacklog = "on"                            ##开启日志

    logdir = "/opt/openresty/nginx/logs/hack/"           ##日志存放位置

    UrlDeny="on"                              ##是否开启URL防护

    Redirect="on"                             ##地址重定向

    CookieMatch="on"                           ##cookie拦截

    postMatch="on"                            ##post拦截

    whiteModule="on"                           ##白名单

    black_fileExt={"php","jsp"}                        

    ipWhitelist={"127.0.0.1"}                    ##白名单IP

    ipBlocklist={"1.0.0.1"}                     ##黑名单IP

    CCDeny="on"                             ##开启CC防护        

    CCrate="100/60"                          ##60秒内允许同一个IP访问100次

    7、创建日志存放目录:

    [root@www ngx_lua_waf]#mkdir /opt/openresty/nginx/logs/hack/

    [root@www ngx_lua_waf]#chown -R nobody:nobody /opt/openresty/nginx/logs/hack/

    8、启动nginx测试:

    [root@www logs]# /opt/openresty/nginx/sbin/nginx 

    9、网页访问一条测试:

    face/6HHjsZ4KasDNjpi7sj5TtEEWwesApXbD.png

    10、压力测试CC攻击:

    把congfig.lua的频率改成如下:

    CCDeny="on"

    CCrate="50/60"

    测试结果:

    [root@www ngx_lua_waf]# ab -c 100 -n 100 http://192.168.63.242/index.heml

    This is ApacheBench, Version 2.3

    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

    Licensed to The Apache Software Foundation, http://www.apache.org/

    Benchmarking 192.168.63.242 (be patient).....done

    Server Software:        openresty/1.11.2.2

    Server Hostname:        192.168.63.242

    Server Port:            80

    Document Path:          /index.heml

    Document Length:        2078 bytes

    Concurrency Level:      100

    Time taken for tests:   0.052 seconds

    Complete requests:      100

    Failed requests:        49      ###因为做了现在,所以这么多是失败的。

    到处已经构建成功了一套waf防御系统,非常感谢loveshell提供这么棒的waf开源项目,还有春哥的openresty.

    原文地址:http://www.roncoo.com/article/detail/126294

  • 相关阅读:
    图书管理系统(view)前后端调联 (copy)
    图书管理系统(modelviewset)前后端调联
    工单系统表的设计
    初始化工单项目及配置
    重要概念 什么是web容器 Linux下安装部署njinx+uwsgi+django+vue
    ubuntu下安装docker django使用whoosh搜索引擎 使用es(elasticsearch)代替whoosh
    RBAC权限管理基本概念与实现
    Django支付宝(沙箱)后端接口
    使用七牛云上传 并将本地图片视屏上传
    展示课程
  • 原文地址:https://www.cnblogs.com/hk315523748/p/6150560.html
Copyright © 2020-2023  润新知