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攻击
检测顺序:先检查白名单,通过即不检测;再检查黑名单,不通过即拒绝,检查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
[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了
[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
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相关文章
- Android–SQLite数据库实现增删改查
- 【说站】Java数组的静态初始化实现
- 【说站】Python+OpenCV实现图像识别替换功能详解
- 解析分布式锁之Zookeeper实现(一)详解架构师
- RRegionServer Splitting 实现过程详解大数据
- 使用socket实现聊天功能详解编程语言
- Java对象表示方式2:XStream实现对对象的XML化详解编程语言
- Linux U盘自动挂载实现方法(linuxu盘自动挂载)
- MongoDB分片查询:实现大数据的突破(mongodb分片查询)
- Linux下安装Flash:轻松实现一键上手(linux安装flash)
- Oracle函数如何实现判断功能(oracle函数判断)
- 实现稳定的Linux UDP通信(linuxudp通信)
- 利用Oracle实现系统时间同步的方法(oracle同步时间)
- Linux下命令行实现安全关机(命令行关机linux)
- 单机Redis如何实现高并发(单机redis并发)
- Redis配置实现自动启动(redis 配置自动启动)
- Redis集群架构实现JWT认证(redis集群jwt)
- Oracle3表关联实现复杂查询功能基表研究(oracle3表关联基表)
- PHP图片验证码制作实现分享(全)
- JS自定义功能函数实现动态添加网址参数修改网址参数值
- ThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调整