介绍
ModSecurity是Apache的插件模块,可像防火墙一样工作。它通过规则集起作用,允许您自定义和配置服务器安全性。
ModSecurity还可以实时监视Web流量,并帮助您检测和响应入侵。它可以与Apache,Nginx和IIS一起使用,并且与Debian,Ubuntu和CentOS兼容。
本教程说明了 如何在Apache Web服务器上安装和配置ModSecurity。
先决条件
在LAMP堆栈(Linux操作系统,Apache,MySQL和PHP)安装和配置
使用sudo或root特权访问用户帐户
程序包管理器(APT或YUM),默认包含
命令行/终端窗口(Ctrl-Alt-T,Ctrl-Alt-F1)
文本编辑器,例如nano
步骤1:更新软件存储库
打开一个终端窗口,然后输入以下内容:
在Debian / Ubuntu上
sudo apt update -y
在CentOS上
sudo yum update -y
步骤2:在Apache上安装ModSecurity
在Debian上安装ModSecurity
1.在终端窗口中,输入以下内容:
sudo apt install libapache2-modsecurity
如果出现提示,请y按下并按Enter键以完成该过程。
2.重新启动Apache服务:
sudo systemctl restart apache2
如果Apache成功重新启动,将没有输出。
3.检查软件版本(应为2.8.0或更高版本):
apt-cache show libapache2-modsecurity
注意: Ubuntu的ModSecurity软件包的语法略有不同。
在Ubuntu 18.04上安装ModSecurity
1.在终端窗口中,输入:
sudo apt install libapache2-mod-security2
如果出现提示,请y按下并按Enter键以完成该过程。
2.重新启动Apache服务:
sudo systemctl restart apache2
如果Apache成功重新启动,将没有输出。
3.检查软件版本(应为2.8.0或更高版本):
apt-cache show libapache2-mod-security2
在CentOS 7上安装ModSecurity
1.在终端窗口中输入以下内容:
sudo yum install mod_security
如果出现提示,请y按下并按Enter键以完成该过程。
2.重新启动Apache服务:
sudo systemctl restart httpd.service
3.检查软件版本(应为2.8.0或更高版本):
yum info mod_security
步骤:3配置ModSecurity
安装后,将ModSecurity设置为根据默认规则记录事件。您需要编辑配置文件以调整规则以检测和阻止流量。
默认配置文件是 /etc/modsecurity/modsecurity.conf-recommended.
1.复制并重命名文件:
sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
2.接下来,更改ModSecurity检测模式。首先,移至/ etc / modsecurity文件夹:
sudo cd /etc/modsecurity
3.在文本编辑器中打开配置文件(我们将使用nano):
sudo nano modsecurity.conf
在顶部附近,您应该看到一个标签为:
SecRuleEngine DetectionOnly
将其更改为如下:
SecRuleEngine On
配置SecRuleEngine选项以设置ModSecurity。
4.使用CTRL+X 退出,然后按y然后回车保存更改。
5.导航到/ etc / modsecurity文件夹:
cd
6.重新启动Apache:
在Debian / Ubuntu上
sudo systemctl restart apache2
重新启动Apache以加载更新的配置文件。
在CentOS上
sudo systemctl restart httpd.service
这将使用基本默认规则打开ModSecurity。在某些版本的Linux中,这包括OWASP核心规则集。但是,这可能与开发人员维护的最新版本不同。
步骤4:下载最新的OWASP ModSecurity规则
适用于ModSecurity的最新核心规则集(CRS)在GitHub上维护。
1. 如果尚未安装Git,请安装它。
在Debian / Ubuntu上安装Git:
sudo apt install git
在CentOS上安装Git:
sudo yum install git
2.下载CRS的副本:
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
克隆OWASP GitHub存储库。
这会将目录的副本放置为当前工作位置的子目录。
3.打开一个新目录:
cd owasp-modsecurity-crs
4.移动crs-setup文件:
sudo mv crs-setup.conf.example /etc/modsecurity/crs-setup.conf
移动OWASP csr设置文件的命令的图像。
5.然后移动rules /目录:
sudo mv rules/ /etc/modsecurity
如果在尝试移动此目录时遇到错误,请输入:
sudo mkdir /etc/modsecurity/rules
cd rules
sudo cp *.* /etc/modsecurity/rules
6.接下来,检查您的security2.conf文件以确认它已设置为加载ModSecurity规则:
sudo nano /etc/apache2/mods-enabled/security2.conf
编辑OWASP scp配置文件。
验证是否包含以下行并且未对其进行注释:
IncludeOptional /etc/modsecurity/*.conf
Include /etc/modsecurity/rules/*.conf
如果不存在,请添加它们。不要复制它们,否则您可能会禁用Apache服务。
OWASP配置文件。
7.重新启动Apache服务:
在Debian / Ubuntu上
sudo systemctl restart apache2
在CentOS上
sudo systemctl restart httpd.service
步骤5:测试Apache配置
1.打开默认的Apache配置文件:
sudo nano /etc/apache2/sites-available/000-default.conf
2.找到</VirtualHost>底部的标签并添加以下行:
SecRuleEngine On
SecRule ARGS:testparam "@contains test" "id:1234,deny,status:403,msg:'phoenixNAP test rule was triggered'"
您可以将更msg 改为自己喜欢的任何一种。
Nano编辑器显示apache配置文件。
保存并退出该文件( > >输入)。CTRL+Xy
3.重新启动Apache服务:
在Debian / Ubuntu上
sudo systemctl restart apache2
在CentOS上
sudo systemctl restart httpd.service
4.然后,输入以下命令:
curl localhost/index.html?testparam=test
系统通过尝试显示默认网页进行响应。它会在标签内生成错误代码和消息,而不是内容:
终端中的ModSecurity错误消息。
5.您可以通过使用以下命令在Apache错误日志中查找代码403来确认ModSecurity是否正常工作:
sudo tail -f /var/log/apache2/error.log
最底部的条目之一应该是ModSecurity错误代码:
使用Bash脚本测试ModSecurity和OWASP CRS
可以用来测试ModSecurity的另一种方法是使用Bash脚本。
1.在终端中输入以下命令:
curl localhost/index.html?exec=/bin/bash
输出显示与上次相同的错误消息。
2.再次查看Apache error.log文件,您会发现该规则已启动:
sudo tail -f /var/log/apache2/error.log
Apache日志中的OWASP CRS错误消息
输出显示与OWASP相关的ModSecurity错误消息。
步骤6:创建ModSecurity规则
下面是一个测试示例,如何使用ModSecurity阻止PHP表单上的特定关键字。
1.使用以下命令在html目录中创建一个PHP文件:
sudo nano /var/www//html/test.php
2.在文件中输入以下代码:
<html>
<body>
<?php
if(isset($_POST['data']))
echo $_POST['data'];
else
{
?>
<form method="post" action="">
Enter text here:<textarea name="data"></textarea>
<input type="submit"/>
</form>
<?php
}
?>
</body>
</html>
保存文件并退出。
3.接下来,创建一个新的ModSecurity自定义规则文件:
sudo nano /etc/modsecurity/modsecurity_custom_rules.conf
添加以下行:
SecRule REQUEST_FILENAME "test.php" "id:'400001',chain,deny,log,msg:'Spam detected'"
SecRule REQUEST_METHOD "POST" chain
SecRule REQUEST_BODY "@rx (?i:(enlarge|Nigerian|gold))"
当然,请将最后一行中的关键字更改为所需的任何内容。
保存文件并退出。
4.重新加载Apache服务:
在Debian / Ubuntu上
sudo systemctl restart apache2
在CentOS上
sudo systemctl restart httpd.service
5.在Web浏览器中启动表单
localhost/test.php
浏览器显示php测试表单。
6.在规则中输入规则中的关键字之一。在此示例中:enlarge|Nigerian|gold。
您应该收到403禁止错误消息。
测试表单后,浏览器窗口显示禁止消息。
您也可以检查/var/log/apache2/error.log文件以验证ModSecurity的操作。
注意:我们不需要将此custom_rules文件添加到security2.conf文件,因为我们指定了通配符(IncludeOptional /etc/modsecurity/*.conf)。如果我们指定了一个单独的.conf文件,则需要将此custom_rules文件添加到security2.conf文件中。
结论
您现在应该对如何在Apache上安装,设置和配置ModSecurity有深刻的了解。在执行本指南中的步骤之前,请确保正确安装了LAMP堆栈。