产品信息简介¶
| 名称 | 开源协议 | 开发语言 | 开源时间 | 活跃度 | 代码质量 | 最新版本 | 厂商 |
|---|---|---|---|---|---|---|---|
| ModSecurity | BSD 3-Clause License | Lua | 2013-12-16(2.7.6) | 5.4K⭐️ | A | 2022-05-31 (3.1.7) | Trustwave |
ModSecurity介绍¶
ModSecurity是一个开源的、生产级别的WAF产品,核心组成部分是“规则引擎”和“规则集”,两者的关系有点像杀毒引擎和病毒特征库。
- 规则引擎:实现了自定义的"SecRule"语言,有自己特定的语法。但“SecRule”主要基于正则表达式,还是不够灵活,所以后来引入了Lua,实现了脚本化配置。ModSecurity的规则引擎使用C++11实现,可以从GitHub上下载,然后集成进Nginx。因为他比较庞大,编译很费时间,所以最好编译成动态模块,在配置文件中用指令load_module加载
load_module modules/ngx_http_mod
- 规则集:ModSecurity 源码提供一个基本的规则配置文件“modsecurity.conf-recommended”,使用前要把它的后缀改成“conf”。有了规则集,就可以在 Nginx 配置文件里加载,然后启动规则引擎:
modsecurity on;modsecurity_rules_file /path/to/modsecurity.conf;“modsecurity.conf”文件默认只有检测功能,不提供入侵阻断,这是为了防止误杀误报,把“SecRuleEngine”后面改成“On”就可以开启完全的防护:#SecRuleEngine DetectionOnlySecRuleEngine On除了基本的规则集之外,ModSecurity 还额外提供一个更完善的规则集,为网站提供全面可靠的保护。这个规则集的全名叫“OWASP ModSecurity 核心规则集”CRS
功能¶
- 兼容 ModSecurity (opens new window)。此功能仅限最新的 Current 版本。
- SQL 注入防护(Powered By libinjection (opens new window))。
- XSS 攻击防护(Powered By libinjection (opens new window))。
- 支持 IPV4 和 IPV6。
- 支持开启验证码(CAPTCHA),支持 hCaptcha (opens new window)、reCAPTCHAv2 (opens new window)和 reCAPTCHAv3 (opens new window)。
- 支持识别友好爬虫(如 BaiduSpider)并自动放行(基于 User-Agent 和 IP 的识别)。此功能仅限最新的 Current 版本。
- CC 防御,超出限制后自动拉黑对应 IP 一段时间或者使用验证码做人机识别。
- IP 黑白名单,同时支持类似
192.168.0.0/16和fe80::/10,即支持点分十进制和冒号十六进制表示法和网段划分。 - POST 黑名单。
- URL 黑白名单
- 查询字符串(Query String)黑名单。
- UserAgent 黑名单。
- Cookie 黑名单。
- Referer 黑白名单。
ModSecurity功能¶
- SQL Injection (SQLi):阻止SQL注入
- Cross Site Scripting (XSS):阻止跨站脚本攻击
- Local File Inclusion (LFI):阻止利用本地文件包含漏洞进行攻击
- Remote File Inclusione(RFI):阻止利用远程文件包含漏洞进行攻击
- Remote Code Execution (RCE):阻止利用远程命令执行漏洞进行攻击
- PHP Code Injectiod:阻止PHP代码注入
- HTTP Protocol Violations:阻止违反HTTP协议的恶意访问
- HTTPoxy:阻止利用远程代理感染漏洞进行攻击
- shellshock:阻止利用Shellshock漏洞进行攻击
- Session Fixation:阻止利用Session会话ID不变的漏洞进行攻击
- Scanner Detection:阻止黑客扫描网站
- Metadata/Error Leakages:阻止源代码/错误信息泄露
- Project Honey Pot Blacklist:蜜罐项目黑名单
- GeoIP Country Blocking:根据判断IP地址归属地来进行IP阻断
准备环境¶
- 操作系统:CentOS-7-x86_64-DVD-1810.iso
- ModSecurity:3.0
- Nginx:1.16.1
- 虚拟机:vwareFusion12.2.1 桥接
安装¶
操作系统依赖¶
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¶
cd /usr/local
git clone https://github.com/SpiderLabs/ModSecurity
cd ModSecurity
git checkout -b v3/master origin/v3/master
git submodule init
git submodule update
sh build.sh
./configure
# 输出
ModSecurity - v3.0.7-1-g9755088 for Linux
Mandatory dependencies
+ libInjection ....v3.9.2-46-gbfba51f
+ SecLang tests ....a3d4405
Optional dependencies
+ GeoIP/MaxMind ....found
* (GeoIP) v1.5.0
-lGeoIP , -I/usr/include/
+ LibCURL ....found v7.29.0
-lcurl , -DWITH_CURL
+ YAJL ....found v2.0.4
-lyajl , -DWITH_YAJL
+ LMDB ....disabled
+ LibXML2 ....found v2.9.1
-lxml2 -lz -lm -ldl, -I/usr/include/libxml2 -DWITH_LIBXML2
+ SSDEEP ....found
-lfuzzy -L/usr/lib64/, -DWITH_SSDEEP -I/usr/include
+ LUA ....found v501
-llua-5.1 -L/usr/lib64/, -DWITH_LUA -DWITH_LUA_5_1 -I/usr/include
+ PCRE2 ....not found
Other Options
+ Test Utilities ....enabled
+ SecDebugLog ....enabled
+ afl fuzzer ....disabled
+ library examples ....enabled
+ Building parser ....disabled
+ Treating pm operations as critical section ....disabled
make
make install
安装Nginx与ModSecurity-nginx¶
cd /usr/local
git clone https://github.com/SpiderLabs/ModSecurity-nginx
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -xvzf nginx-1.16.1.tar.gz
cd /usr/local/nginx-1.16.1
./configure --add-module=/usr/local/ModSecurity-nginx
make
make install
验证¶
启动Nginx
/usr/local/nginx/sbin/nginx
验证Nginx服务
[root@localhost sbin]# curl -i http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Thu, 09 Jun 2022 08:24:59 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 09 Jun 2022 08:23:04 GMT
Connection: keep-alive
ETag: "62a1ade8-264"
Accept-Ranges: bytes
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
PS:浏览器访问失败,临时关闭防火墙验证
[root@localhost sbin] systemctl stop firewalld.service
浏览器访问

配置¶
- 创建用于存放配置的文件夹
[root@localhost conf]# mkdir /usr/local/nginx/conf/modsecurity
- 复制并重命名配置文件
[root@localhost conf] cp /usr/local/ModSecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity/
# 重命名配置文件
[root@localhost] cd /usr/local/nginx/conf/modsecurity/ && rename modsecurity.conf-recommended modsecurity.conf *
[root@localhost modsecurity] cp /usr/local/ModSecurity/unicode.mapping /usr/local/nginx/conf/modsecurity/
# 下载规则包
[root@localhost local] wget http://www.modsecurity.cn/download/corerule/owasp-modsecurity-crs-3.3-dev.zip
# 解压
[root@localhost local] unzip owasp-modsecurity-crs-3.3-dev.zip
# 复制crs-setup.conf.example
[root@localhost owasp-modsecurity-crs-3.3-dev] cp crs-setup.conf.example /usr/local/nginx/conf/modsecurity/
# 重命名文件
[root@localhost] cd /usr/local/nginx/conf/modsecurity/ && rename modsecurity.conf.example crs-setup.conf *
# 复制rule文件夹
[root@localhost owasp-modsecurity-crs-3.3-dev] cp -r rules /usr/local/nginx/conf/modsecurity/
# 修改文件名,可以将自己写的规则放在999 900这两个文件中
[root@localhost rules] rename RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf *
[root@localhost rules] rename REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf *
- 编辑nginx.conf 在http或server节点中添加以下内容在http节点添加表示全局配置,在server节点添加表示为指定网站配置:
modsecurity on;
modsecurity_rules_file /usr/local/nginx/conf/modsecurity/modsecurity.conf;
- 编辑modsecurity.conf
vim /usr/local/nginx/conf/modsecurity/modsecurity.conf
# SecRuleEngine DetectionOnly改为SecRuleEngine On
# 添加一下内容
Include /usr/local/nginx/conf/modsecurity/crs-setup.conf
Include /usr/local/nginx/conf/modsecurity/rules/*.conf
- 重新加载Nginx
/usr/local/nginx/sbin/nginx -s reload
- 模拟攻击测试
http://nginx.test/?param=%22%3E%3Cscript%3Ealert(1);%3C/script%3E
进阶¶
Nginx基于IP的安全防护策略¶
由于虚拟机部署的Nginx,Nginx访问日志IP为虚拟机网关IP172.16.244.1
Nginx获取真实源IP¶
todo
Nginx禁止国外IP测试¶
挂代理访问不经过互联网,日志只记录虚拟机网关IP。局域网访问不到虚拟机IP。
TODO
- [x] 通过colab创建VPS部署Nginx ✅ 2022-06-10 💀行不通
SSH VNC¶
Chrome Remote Desktop¶
Google Colab常见问题
Colab 中的资源将优先提供给交互式用例。我们禁止各种涉及批量计算、会对他人造成负面影响,或试图规避我们的政策的操作。Colab 运行时中不允许执行下列操作:
- 文件托管、媒体传送或提供其他与 Colab 的交互式计算无关的网络服务
- 下载种子文件或进行点对点文件共享
- 使用远程桌面或 SSH
- 连接到远程代理
- 加密货币挖矿
- 运行拒绝服务攻击
- 破解密码
- 利用多个帐号绕过访问权限或资源使用情况限制
- 创建深度伪造内容

