zl程序教程

您现在的位置是:首页 >  其它

当前栏目

openresty实现WAF功能

实现 功能 WAF OpenResty
2023-06-13 09:15:45 时间
 

[root@tiejiang-src1 ~]# ifconfig eth0|grep inet addr|awk -F : {print $2}|awk {print $1}

192.168.83.129

[root@tiejiang-src1 ~]# cat /etc/redhat-release

CentOS release 6.5 (Final)

[root@tiejiang-src1 ~]# uname -r

2.6.32-431.el6.x86_64

2、安装基础依赖包

 

[root@tiejiang-src1 ~]# yum install -y readline-devel pcre-devel openssl-devel

 

3、下载并编译安装openresty

 

[root@tiejiang-src1 ~]# cd /usr/local/src/

[root@tiejiang-src1 src]# wget https://openresty.org/download/ngx_openresty-1.9.3.2.tar.gz

[root@tiejiang-src1 src]# tar zxvf ngx_openresty-1.9.3.2.tar.gz

[root@tiejiang-src1 src]# cd ngx_openresty-1.9.3.2

[root@tiejiang-src1 ngx_openresty-1.9.3.2]# ./configure --prefix=/usr/local/openresty-1.9.3.2 --with-luajit --with-http_stub_status_module --with-pcre --with-pcre-jit

[root@tiejiang-src1 ngx_openresty-1.9.3.2]# gmake gmake install

[root@tiejiang-src1 ngx_openresty-1.9.3.2]# ln -s /usr/local/openresty-1.9.3.2/ /usr/local/openresty

4、测试openresty安装

 

[root@tiejiang-src1 ~]# vim /usr/local/openresty/nginx/conf/nginx.conf

 server {

 location /hello {

 default_type text/html;

 content_by_lua_block {

 ngx.say( HelloWorld )

[root@tiejiang-src1 ~]# /usr/local/openresty/nginx/sbin/nginx -t

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

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

[root@tiejiang-src1 ~]# /usr/local/openresty/nginx/sbin/nginx

5、WAF部署:在github上克隆下代码

 

[root@tiejiang-src1 ~]# yum -y install git

[root@tiejiang-src1 ~]# cd /usr/local/openresty/nginx/conf/

[root@tiejiang-src1 conf]# git clone https://github.com/unixhot/waf.git

6、修改Nginx的配置文件,加入(http字段)以下配置。注意路径,同时WAF日志默认存放在/tmp/日期_waf.log

 

[root@tiejiang-src1 ~]# vim /usr/local/openresty/nginx/conf/nginx.conf

 http {

 include mime.types;

 default_type application/octet-stream;

 #WAF

 lua_shared_dict limit 50m;

 lua_package_path /usr/local/openresty/nginx/conf/waf/?.lua 

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

 access_by_lua_file /usr/local/openresty/nginx/conf/waf/access.lua 

[root@tiejiang-src1 conf]# /usr/local/openresty/nginx/sbin/nginx -t

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

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

[root@tiejiang-src1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload

7、根据日志记录位置,创建日志目录

 

[root@tiejiang-src1 conf]# mkdir /tmp/waf_logs

[root@tiejiang-src1 conf]# chown www.www /tmp/waf_logs

二、启用waf并做测试

1、模拟sql注入即url攻击openresty实现WAF功能

检测顺序:先检查白名单,通过即不检测;再检查黑名单,不通过即拒绝,检查UA,UA不通过即拒绝;检查cookie;URL检查;URL参数检查,post检查;

日志显示如下,记录了UA,匹配规则,URL,客户端类型,攻击的类型,请求的数据


[root@tiejiang-src1 ~]# tail -f /tmp/2018-07-30_waf.log

 { user_agent : Mozilla//5.0 (Windows NT 10.0; Win64; x64) AppleWebKit//537.36 (KHTML, like Gecko) Chrome//67.0.3396.99 Safari//537.36 , rule_tag : //.(bak|inc|old|mdb|sql|backup|java|class|tgz|gz|tar|zip)$ , req_url : //eastmonet.sql , client_ip : 192.168.83.1 , local_time : 2018-07-30 10:46:52 , attack_method : Deny_URL , req_data : - , server_name : localhost }

2、使用ab压力测试工具模拟防CC攻击


[root@tiejiang-src2 ~]# ifconfig eth0|grep inet addr|awk -F : {print $2}|awk {print $1}

192.168.83.131

openresty实现WAF功能

将对方IP放入黑名单

[root@tiejiang-src1 ~]# echo 192.168.83.131 amp;amp;gt; amp;amp;gt; /usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule

[root@tiejiang-src1 ~]# /usr/local/openresty/nginx/sbin/nginx -s reload

再拿192.168.83.131访问的时候就提示403了openresty实现WAF功能

将对方IP放入白名单

[root@tiejiang-src1 ~]# echo 192.168.83.131 amp;amp;gt; amp;amp;gt; /usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule

[root@tiejiang-src1 ~]# /usr/local/openresty/nginx/sbin/nginx -s reload

此时将不对此ip进行任何防护措施,所以sql注入时应该返回404

openresty实现WAF功能


waf目录:/usr/local/openresty/nginx/conf/waf

lua配置文件:/usr/local/openresty/nginx/conf/waf/config.lua

Waf的ip黑名单:/usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule

Waf的ip白名单:/usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule

Waf的规则存放目录:/usr/local/openresty/nginx/conf/waf/rule-config

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/54151.html

centosjavanginxOpenRestywindows