使用ModSecurity搭建自己的Web应用防火墙(WAF)

搭建环境

ModSecurity介绍
ModSecurity是一个开源的、跨平台的Web应用防火墙(WAF),被称为WAF界的“瑞士军刀”。 它可以通过检查Web服务接收到的数据,以及发送出去的数据来对网站进行安全防护。

SQLi:阻止SQL注入
XSS:阻止跨站脚本攻击
LFI:阻止利用本地文件包含漏洞进行攻击
RFI:阻止利用远程文件包含漏洞进行攻击
RCE:阻止利用远程命令执行漏洞进行攻击
PHP Code :阻止PHP代码注入
HTTP Protocol Violations:阻止违反HTTP协议的恶意访问
HTTPoxy:阻止利用远程代理感染漏洞进行攻击
Sshllshock:阻止利用Shellshock漏洞进行攻击
Session Fixation:阻止利用Session会话ID不变的漏洞进行攻击
Scanner Detection:阻止黑客扫描网站
Metadata/Error Leakages:阻止源代码/错误信息泄露
Project Honey Pot Blacklist:蜜罐项目黑名单
GeoIP Country Blocking:根据判断IP地址归属地来进行IP阻断


安装依赖

yum install -y yajl-devel ssdeep-devel

安装

cd /usr/local
wget https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.5/modsecurity-2.9.5.tar.gz
tar -zxvf modsecurity-2.9.5.tar.gz
cd modsecurity-2.9.5
./configure --with-apxs=/www/server/apache/bin/apxs --with-apr=/www/server/apache/bin/apr-1-config --with-apu=/www/server/apache/bin/apu-1-config
make
make install
图片[1]-使用ModSecurity搭建自己的Web应用防火墙(WAF)-暮间雾博客


​非宝塔环境,也可以按照上面的命令进行安装。需要修改的位置为apxs arp-1-confg``apu-l-config这三个文件的位置。宝塔下默认位置为:/www/server/apache/bin/你可以用find命令进行搜索。

配置规则文件

<!-- wp:paragraph -->
<p>cd /usr/local<br>git clone https://github.com/coreruleset/coreruleset.git</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1>如果本地网站无法连接git官网,可尝试下方的另外两个下载地址</h1>
<!-- /wp:heading -->

<!-- wp:heading {"level":1} -->
<h1>git clone https://hub.fastgit.org/coreruleset/coreruleset.git</h1>
<!-- /wp:heading -->

<!-- wp:heading {"level":1} -->
<h1>git clone https://hub.0z.gs/coreruleset/coreruleset.git</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>mkdir /www/server/apache/conf/modsecurity/</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1>复制ModSecurity相关配置文件</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>cp /usr/local/modsecurity-2.9.5/modsecurity.conf-recommended /www/server/apache/conf/modsecurity/modsecurity.conf<br>cp /usr/local/modsecurity-2.9.5/unicode.mapping /www/server/apache/conf/modsecurity/unicode.mapping</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1>复制OWASP相关规则文件</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>cp /usr/local/coreruleset/crs-setup.conf.example /www/server/apache/conf/modsecurity/crs-setup.conf<br>cp -r /usr/local/coreruleset/rules/ /www/server/apache/conf/modsecurity/</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1>启用白名单及规则禁用文件</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>mv /www/server/apache/conf/modsecurity/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example /www/server/apache/conf/modsecurity/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf<br>mv /www/server/apache/conf/modsecurity/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example /www/server/apache/conf/modsecurity/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf</p>
<!-- /wp:paragraph -->

打开waf
编写httpd.conf,去掉#LoadModule unique_id_module modules/mod_unique_id.so前的注释符#,并添加以下内容

LoadModule security2_module modules/mod_security2.so
Include conf/modsecurity/modsecurity.conf Include conf/modsecurity/crs-setup.conf Include conf/modsecurity/rules/*.conf

编辑/www/server/apache/conf/modsecurity/modsecurity.conf,SecRuleEngine DetectionOnly改为SecRuleEngine On

根据需求,其余配置看需求,关闭即可。

重启apache服务

service restart httpd

自定义403页面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>系统防火墙</title>

<script type="text/javascript">
 function getInterceptUrl(){
  var url = getQueryString('url');
  var domain = getQueryString('intercept_domain');
  var tmpstr = "被拦截URL为:";
  var div = document.getElementById('interceptdiv');
  if(domain != ""){
   tmpstr += domain;
  }
  if(url != "" && url != "/"){
   tmpstr += url;
  }  
  if(domain != "" || url != ""){
   var textnode=document.createTextNode(tmpstr);
   div.appendChild(textnode);
  }
  
 }
 function getQueryString(name) {
    var result = window.location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return result[1];
}
</script>

<style type="text/css"> 
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#fff;}
fieldset{padding:0 15px 10px 15px;border-color: #fff;border: 0px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:2.4em;margin:0;color:#CC0000;text-align: center;} 
h3{font-size:1.7em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#fff;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:50%;margin:0 auto;position:relative;margin-top:100px}
a{text-decoration:none;color:#009cd6}
a:hover{text-decoration:underline;color:#ff0000}
-->
</style>
</head>
<body onload = "getInterceptUrl();">
<div id="content"> 
 <div class="content-container"><fieldset> 
  <br><br><br>
 <img  src="你的图片地址" >
  <h3 style="text-align: center;">当前的操作可能会对网站安全造成威胁,已被服务器防火墙拦截。</h3> 
<br>   
<div id = "interceptdiv" style="color:#F00;text-align: center;font-size: 16px;"></div>
</fieldset>
</div>
</body>
</html>

修改/www/server/apache/conf/modsecurity/crs-setup.conf文件,添加下面代码

SecDefaultAction "phase:1,log,auditlog,redirect:https://blog.bbskali.cn/waf.html?url=%{REQUEST_FILENAME}&intercept_domain=%{request_headers.host}"
SecDefaultAction "phase:2,log,auditlog,redirect:https://blog.bbskali.cn/waf.html?url=%{REQUEST_FILENAME}&intercept_domain=%{request_headers.host}"

将原来的#SecDefaultAction "phase:1,log,auditlog,pass"注释掉。

重启apache,查看实际效果。

图片[2]-使用ModSecurity搭建自己的Web应用防火墙(WAF)-暮间雾博客

© 版权声明
THE END
喜欢本站内容,请点【分享】 【赞】 和 【收藏】
点赞1.5W+ 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容