• 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
    

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

    在这里插入图片描述

  • 相关阅读:
    ORACLE--Connect By、Level、Start With的使用(Hierarchical query-层次查询)
    小工具-ANT
    测试类。。。重写篇
    java代码----求最大值,平均值。。。
    java代码,输入n多个数,求其平均值,虽有重复,但是第二次,我就乱写了
    java代码求输入数的平均值~~~~
    java代码求输入的三个数的最大值
    java代码switch语句求分数等级
    java代码求分数等级的输出~~~
    java代码 求和1+1/2+1/3+1/4+1/5+1/6+.......+1/n 的值~~~~
  • 原文地址:https://www.cnblogs.com/cqnswp/p/12782351.html
Copyright © 2020-2023  润新知