WAF简介及ModSecurity-nginx搭建

WAF简介

WAF是(Web Application Firewall)的简称,一般称为web应用防火墙(waf防火墙),用于屏蔽过滤常见漏洞攻击,sql,xml,xss等,针对应用层的入侵检测(只针对应用层)。次要功能是作网站加速用CDN等。
WAF分为三种,三类WAF优缺点均有但工作流程几乎相同。

云WAF(阿里云盾,百度云加速)用户的请求首先发送到云端节点进行检测,如存在异常请求则进行拦截否则将请求转发至真实服务器。

优点:部署简单,兼顾CDN。
缺点:可靠性比硬WAF稍低,受上游厂商影响较大。

硬WAF(nsfocus imperva)硬件Waf通常的安装方式是将Waf串行部署在Web服务器前端,用于检测、阻断异常流量。

优点:高吞吐量,即插即用。
缺点:价格贵。

软WAF(安全狗 D盾)安装在需要防护的服务器上,实现方式通常是WAF监听端口或以Web容器扩展方式进行请求检测和阻断。

优点:免费,开源。
缺点:严重误杀&漏报,内存占用量大。

MoSecurity搭建

本文基于modsecurity,nginx搭建环境
它是一款开源的的三方模块,功能包括http流量日志,实时检测等功能。
ModSecurity核心规则集(CRS)提供以下类别的保户来防止攻击。
官方宣传:
◆HTTP Protection (HTTP防御) - HTTP协议和本地定义使用的detectsviolations策略。
◆Real-time Blacklist Lookups(实时黑名单查询) -利用第三方IP信誉。
◆HTTP Denial of Service Protections(HTTP的拒绝服务保护) -防御HTTP的洪水攻击和HTTP Dos 攻击。
◆Common Web Attacks Protection(常见的Web攻击防护) -检测常见的Web应用程序的安全攻击。
◆Automation Detection(自动化检测) -检测机器人,爬虫,扫描仪和其他表面恶意活动。
◆Integration with AV Scanning for File Uploads(文件上传防病毒扫描) -检测通过Web应用程序上传的恶意文件。
◆Tracking Sensitive Data(跟踪敏感数据) -信用卡通道的使用,并阻止泄漏。
◆Trojan Protection(木马防护) -检测访问木马。
◆Identification of Application Defects (应用程序缺陷的鉴定)-应用程序的错误配置警报。
◆Error Detection and Hiding(错误检测和隐藏) -伪装服务器发送错误消息。

安装过程:

依赖

yum install -y git wget epel-release
yum install -y gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre-devel lmdb-devel libxml2-devel ssdeep-devel lua-devel libtool autoconf automake

克隆ModSecurity

git clone https://github.com/SpiderLabs/ModSecurity

在这里插入图片描述
# cd ModSecurity

# git submodule init //初始化

# git submodule update  //更新

# sh build.sh

# ./configure       //检查配置产出makefile

# make              //编译

# make install      //安装

编译安装比较慢,耐性等待

在这里插入图片描述

下载nginx连接器源码

克隆git

git clone https://github.com/SpiderLabs/ModSecurity-nginx

下载nginx源码并编译安装

wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -xvf nginx-1.16.1.tar.gz

在这里插入图片描述

cd /nginx-1.16.1
./configure --add-module=/root/ModSecurity-nginx
make
make install
/usr/local/nginx/sbin/nginx
nginx环境变量
vim /usr/lib/systemd/system/nginx.service

写入

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

在这里插入图片描述
在这里插入图片描述

配置文件
mkdir /usr/local/nginx/conf/modsecurity
cd ModSecurity
cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity/modsecurity.conf
cp unicode.mapping /usr/local/nginx/conf/modsecurity

在这里插入图片描述

OWASP规则
wget http://www.modsecurity.cn/download/corerule/owasp-modsecurity-crs-3.3-dev.zip
unzip owasp-modsecurity-crs-3.3-dev.zip
cd owasp-modsecurity-crs-3.3-dev
cp crs-setup.conf.example /usr/local/nginx/conf/modsecurity/crs-setup.conf
cp -r rules/ /usr/local/nginx/conf/modsecurity/
cd /usr/local/nginx/conf/modsecurity/rules/
//修改文件名
cp REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example.back
cp RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example.back
cp RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example.back RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
cp REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example.back REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
修改nginx启用waf
vim /usr/local/nginx/conf/nginx.conf

在http或server节点中添加以下内容(在http节点添加表示全局配置,在server节点添加表示为指定网站配置)
在这里插入图片描述

modsecurity on;
modsecurity_rules_file /usr/local/nginx/conf/modsecurity/modsecurity.conf;

编辑modsecurity.conf

SecRuleEngine DetectionOnly改为SecRuleEngine On

同时添加以下内容:

Include /usr/local/nginx/conf/modsecurity/crs-setup.conf
Include /usr/local/nginx/conf/modsecurity/rules/*.conf

在这里插入图片描述
重启服务

在这里插入图片描述

原文链接: https://www.cnblogs.com/cqnswp/p/12782351.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    WAF简介及ModSecurity-nginx搭建

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/376496

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月3日 下午1:03
下一篇 2023年3月3日 下午1:03

相关推荐