nginx+lua实现waf

nginx+lua实现waf防火墙


lua简介:
lua是一门简洁、轻量、可扩展的脚本语言(很容易被C/C++代码调用,也可以反过来调用C/C++的函数)

lua安装:

[root@localhost ~]# yum -y install lua

lua的使用:
[root@localhost ~]# lua
Lua 5.1.4 Copyright © 1994-2008 Lua.org, PUC-Rio

print “hello world”
hello world

这种不方便,我们可以把它写在文件里执行:

[root@localhost ~]# which lua
/usr/bin/lua

[root@localhost ~]# vim test.lua
添加:
#!/usr/bin/lua
print("hello world")

[root@localhost ~]# lua test.lua
hello world

lua的注释语法:

基本注释:
–注释内容
范围注释:
–[[
注释内容
–]]

例:
#!/usr/bin/lua
–你好
–[[
test
–]]
print(“hello world”)

[root@localhost ~]# lua test.lua
hello world

Lua变量定义及调用:

#!/usr/bin/lua
–你好
–[[
test
–]]

a = 123
print(a)
print(“test:”,a)

[root@localhost ~]# lua test.lua
123
test: 123

#lua的布尔类型只有nil(空,零)和false,布尔类型就是真和假,也就是true和false.
#数字0,空字符串都是true
#lua的变量全是全局变量

更多的lua操作这里不再说啦。什么while循环,if判断什么的。

Nginx加载Lua环境:
默认情况下nginx不支持Lua模块,需要安装LuaJIT解释器,并且需要重新编译Nginx。 也可以使用春哥开发的openrestry,加载这个非常简单。

我们先来加载Lua:(我这里早已经安装完Nginx,所以重新编译添加模块即可)
下载Luajit和ngx_devel_kit和Lua-nginx-module

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

解压ngx_devel_kit与Lua-nginx-module:

[root@localhost ~]# tar -zxvf v0.2.19.tar.gz -C /usr/src/
[root@localhost ~]# tar -zxvf v0.10.13.tar.gz -C /usr/src/

安装Luajit:

[root@localhost ~]# cd /usr/src/LuaJIT-2.0.4/
[root@localhost LuaJIT-2.0.4]# make && make install
...............
==== Successfully installed LuaJIT 2.0.4 to /usr/local ====   #成功后有Successfully

[root@localhost ~]# echo "/usr/local/lib" >> /etc/ld.so.conf
[root@localhost ~]# ldconfig

因为我这里已经安装过nginx了,所以只需添加模块就可以,无需重新编译安装(如果未安装直接编译安装即可,与以下预编译参数一致即可):

[root@localhost ~]# nginx -V
nginx version: nginx/1.10.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module 

#复制以上预编译参数:

[root@localhost ~]# cd /usr/src/nginx-1.10.3/
[root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --add-module=../ngx_devel_kit-0.2.19 --add-module=../lua-nginx-module-0.10.13     #添加这两个新参数,这两个模块为我们要添加的模块ngx_devel_kit与Lua-nginx-module
[root@localhost nginx-1.10.3]# make
[root@localhost nginx-1.10.3]# cp objs/nginx /usr/local/nginx/sbin/nginx #覆盖
[root@localhost nginx-1.10.3]# rm -rf /usr/bin/nginx   #删除之前的软连接
[root@localhost nginx-1.10.3]# ln -s /usr/local/nginx/sbin/* /usr/bin/  #做新的软连接

验证是否成功加载lua模块:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /test {
                default_type text/html;
                content_by_lua_block {
                        ngx.say("hello world")
                }
        }

重启验证:
在这里插入图片描述

#成功

#也可以部署openrestry,这个比较简单,Lua和openrestry二选一。这里不写openrestry部署啦

nginx+lua实现waf防火墙:
随着网络的发展,安全也成为啦非常重要的一方面。网络有许多的攻击手段,例如爬虫、sql注入等。

搭建lnmp模拟sql注入,这里已经有nginx+lua环境啦,我们安装mysql与php即可。我这里使用Yum安装。(需要用到网络源)

[root@localhost ~]# yum -y install mariadb mariadb-server php php-fpm php-mysql

location / {
            root   html;
            index  index.html index.php;
        }

location ~ \.php$ {
                root html;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
                include fastcgi_params;


        }
[root@localhost ~]# systemctl start php-fpm
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# nginx -s reload
[root@localhost ~]# vim /usr/local/nginx/html/index.php
添加:
<?php
        phpinfo();
?>

访问验证即可。

配置mysql:

mysqladmin -uroot password 123.com
进入数据库
MariaDB [(none)]> create database info;

MariaDB [(none)]> use info;
MariaDB [info]> create table user(id int,username varchar(64),password varchar(64),email varchar(64));

MariaDB [info]> insert into user values(1,'zs',('123'),'123@qq.com');
Query OK, 1 row affected (0.00 sec)

MariaDB [info]> insert into user values(2,'ww',('456'),'456@qq.com');
Query OK, 1 row affected (0.00 sec)

编写文件,为sql注入做准备:

[root@localhost ~]# vi /usr/local/nginx/html/login.html
添加:
<html>
<head>
        <title> 测试Sql注入 </title>
        <meta http-equiv="content-type"content="text/html;charset=utf-8">
</head>
<body>
<form action="sql.php" method="post">
<table>
        <tr>
                <td> 用户: </td>
                <td><input type="text" name="username"></td>
        </tr>


        <tr>
                <td> 密码: </td>
                <td><input type="text" name="password"></td>
        </tr>
        <tr>
                <td><input type="submit" value="提交"></td>
                <td><input type="reset" value="重置"></td>
        </tr>
                </table>
        </form>
</body>
</html>
[root@localhost ~]# vi /usr/local/nginx/html/sql.php
添加:
<?php
        $conn = mysql_connect("localhost",'root','123.com') or die("数据库连接失败! ");
        mysql_select_db("info",$conn) or die ("您选择的数据库不存在");
        $name=$_POST['username'];
        $pwd=$_POST['password'];
        $sql="select * from user where username='$name' and password='$pwd'";
        echo $sql."<br />";
        $query=mysql_query($sql);
        $arr=mysql_fetch_array($query);
        if($arr) {
                echo "login success!<br />";
                echo $arr[1];
                echo $arr[3]."<br /><br />";
        }else{
                echo "login failed!";
        }
?>

浏览器访问login.html验证:
在用户那块填写’ or 1=1#’ 提交

为啦防止sql注入,我们用nginx+lua部署waf防火墙:

[root@localhost ~]# yum -y install git
[root@localhost ~]# git clone https://github.com/Loveshell/ngx_lua_waf.git
[root@localhost ~]# cp -r ngx_lua_waf/ /usr/local/nginx/conf/waf
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在http标签内添加:   #路径一定要对应上面的waf路径
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;

[root@localhost ~]# nginx -s reload
[root@localhost ~]# vim /usr/local/nginx/conf/waf/config.lua
RulePath = "/usr/local/nginx/conf/waf/wafconf/"

[root@localhost ~]# vim /usr/local/nginx/conf/waf/wafconf/post
在第一行添加:
\sor\s+

再次进行sql注入测试:

我克隆过来的waf策略它默认的策略基本够使用了。

配置waf防止cc攻击:

[root@localhost ~]# vim /usr/local/nginx/conf/waf/config.lua
CCDeny="on"             #将此选项打开
CCrate="600/60"          #配置同个ip每60秒最多发送600次请求,超过之后60秒内此IP将不能再访问,其他IP可以

[root@localhost ~]# nginx -s reload

再开一台机器使用ab测试:

[root@localhost ~]# ab -n 2000 -c 200 http://192.168.10.3/login.html

命令完成后访问:
[root@localhost ~]# curl http://192.168.10.3/login.html
<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body bgcolor="white">
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

60秒后可再次访问:

[root@localhost ~]# curl http://192.168.10.3/login.html
<html>
<head>
        <title> 测试Sql注入 </title>
        <meta http-equiv="content-type"content="text/html;charset=utf-8">
</head>
<body>
<form action="sql.php" method="post">
<table>

完成。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值