Giter VIP home page Giter VIP logo

nginx-waf's Introduction

waf

使用Nginx+Lua实现自定义WAF(Web application firewall)

##版权声明 严重参考(照抄)https://github.com/loveshell/ngx_lua_waf 严重参考(照抄)https://github.com/unixhot/waf ####功能列表:

  1. 支持IP白名单和黑名单功能,直接将黑名单的IP访问拒绝。
  2. 支持URL白名单,将不需要过滤的URL进行定义。
  3. 支持User-Agent的过滤,匹配自定义规则中的条目,然后进行处理(返回403)。
  4. 支持Cookie过滤,匹配自定义规则中的条目,然后进行处理(返回403)。
  5. 支持URL过滤,匹配自定义规则中的条目,如果用户请求的URL包含这些,返回403。
  6. 支持URL参数过滤,原理同上。
  7. 支持日志记录,将所有拒绝的操作,记录到日志中去。

####WAF实现 WAF一句话描述,就是解析HTTP请求(协议解析模块),规则检测(规则模块),做不同的防御动作(动作模块),并将防御过程(日志模块)记录下来。所以本文中的WAF的实现由五个模块(配置模块、协议解析模块、规则模块、动作模块、错误处理模块)组成。

####Nginx + Lua部署

环境准备 [root@nginx-lua ~]# cd /usr/local/src 首先,现在Nginx安装必备的Nginx和PCRE软件包。

[root@nginx-lua src]# wget http://nginx.org/download/nginx-1.9.4.tar.gz
[root@nginx-lua src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz

其次,下载当前最新的luajit和ngx_devel_kit (NDK),以及春哥(章)编写的lua-nginx-module

  [root@nginx-lua src]# wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
  [root@nginx-lua src]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz
  [root@nginx-lua src]# wget https://github.com/openresty/lua-nginx-module/archive/v0.9.16.tar.gz

最后,创建Nginx运行的普通用户 [root@nginx-lua src]# useradd -s /sbin/nologin -M www

解压NDK和lua-nginx-module

    [root@openstack-compute-node5 src]# tar zxvf v0.2.19 解压后为ngx_devel_kit-0.2.19
    [root@openstack-compute-node5 src]# tar zxvf v0.9.10解压后为lua-nginx-module-0.9.16

安装LuaJIT Luajit是Lua即时编译器。

  [root@openstack-compute-node5 src]# tar zxvf LuaJIT-2.0.3.tar.gz 
  [root@openstack-compute-node5 src]# cd LuaJIT-2.0.3
  [root@openstack-compute-node5 LuaJIT-2.0.3]# make && make install

安装Nginx并加载模块

  [root@openstack-compute-node5 src]# tar zxvf nginx-1.9.4.tar.gz 
  [root@openstack-compute-node5 src]# cd nginx-1.9.4
  [root@openstack-compute-node5 nginx-1.9.4]# export LUAJIT_LIB=/usr/local/lib
  [root@openstack-compute-node5 nginx-1.9.4]# export LUAJIT_INC=/usr/local/include/luajit-2.0
  [root@openstack-compute-node5 nginx-1.9.4]# ./configure --prefix=/usr/local/nginx --user=www --group=www     --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module --add-module=../ngx_devel_kit-0.2.19/ --add-module=../lua-nginx-module-0.9.16/ --with-pcre=/usr/local/src/pcre-8.37 
  [root@openstack-compute-node5 nginx-1.5.12]# make -j2 && make install

  [root@openstack-compute-node5 ~]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

如果不创建符号链接,可能出现以下异常: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

#####测试安装 安装完毕后,下面可以测试安装了,修改nginx.conf 增加第一个配置。

        location /hello {
                default_type 'text/plain';
                content_by_lua 'ngx.say("hello,lua")';
        }
    
[root@openstack-compute-node5 ~]# /usr/local/nginx-1.9.4/sbin/nginx –t
[root@openstack-compute-node5 ~]# /usr/local/nginx-1.9.4/sbin/nginx

然后访问http://xxx.xxx.xxx.xxx/hello,如果出现hello,lua。表示安装完成,然后就可以。 ####WAF部署

#cd /usr/local/nginx/conf
#git clone https://github.com/tluolovembtan/jsh_waf


修改Nginx的配置文件,加入以下配置。注意路径,同时WAF日志默认存放在/tmp/日期_waf.log
#WAF
    lua_shared_dict limit 50m;
    lua_package_path "/usr/local/nginx/conf/jsh_waf/?.lua";
    init_by_lua_file "/usr/local/nginx/conf/jsh_waf/init.lua";
    access_by_lua_file "/usr/local/nginx/conf/jsh_waf/waf.lua";

[root@openstack-compute-node5 ~]# /usr/local/nginx/sbin/nginx
#WAF 我的无坑
mkdir -p /data/src && cd /data/src
tar -vxzf pcre-8.37.tar.gz
cd pcre-8.37/
./configure
make && make install
cd ..

tar -vxzf lua-nginx-module-0.10.14.tar.gz
tar -vxzf ngx_devel_kit-0.2.19.tar.gz

tar -vxzf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make && make install
export LUAJIT_LIB=/usr/local/lib #可以写/etc/profile
export LUAJIT_INC=/usr/local/include/luajit-2.0 #可以写/etc/profile
ln -s /usr/local/lib/libluajit-5.1.so.2.0.4 /lib64/libluajit-5.1.so.2.0.4
cd ..

tar -vxzf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --user=nobody --group=nobody --with-http_realip_module --with-pcre --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --add-module=/data/src/ngx_devel_kit-0.2.19 --add-module=/data/src/lua-nginx-module-0.10.14 --with-ld-opt=-Wl,-rpath,$LUAJIT_LIB
make
#如果是添加插件不要安装

记得修改配置文件,他的配置路径和文档有点不一样

nginx-waf's People

Contributors

seedc avatar

Watchers

 avatar

Forkers

heartshare

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.