针对REST API的Web应用防火墙
type
status
date
slug
summary
tags
category
icon
password
前言
个人毕业设计,挺水的。本来想好好弄,把防火墙和机器学习综合一下,结果学校突然通知提前一个月验收,只好匆匆完成,不过最后答辩老师的评价还挺不错,大概是没有细看吧。总之是让大家见笑了。
核心思路
Nginx + ModSecurity + OWASP Core Rule Set + APP Specific Profiles
防火墙引擎采用ModSecurity模块,搭配OWASP Core Rule Set和基于OWASP AppSensor DetectionPoints自行编写的APP Specific Profiles。该防火墙基于Nginx反向代理技术。
具体细节
原理
- 利用Nginx单项代理技术对请求和返回进行处理。ModSecurity作为Apache的一个模块,在被修改后是可以被Nginx使用的。
- REST API现在被广泛使用,具体定义这里就不赘述了,大体是一个类似与http://hostname.com/version1/SchoolID/ClassID/StudentID这样一个URL对资源进行定位,个人感觉对URL这个统一资源定位符是一种新的解读,但这种新相对于旧的过渡却非常的自然。具体可以看这位UCI博士Roy Fielding的论文。http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
- 现有的OWASP Core Rule Set能够防御很多攻击,但是这个规则集并没有针对REST API进行防御。这样一来,一方面我们可以部署这一套规则集,另一方面我们自行编写一些规则来针对REST API。而这些自定义的规则我们可以针对OWASP的AppSensor DetectionPoints来制定。https://www.owasp.org/index.php/AppSensor_DetectionPoints
- 要注意的是Nginx不支持动态模块加载,因此在变异Nginx时就需要把ModSecurity模块编译进去。
部署流程
Nginx
wget http://nginx.org/download/nginx‐1.10.3.tar.gz
ModSecurity
wget https://www.modsecurity.org/tarball/2.9.1/modsecurity-2.9.1.tar.gz
安装ModSecurity编译时所需要的库
apt-get install apache2-threaded-dev libxml2-dev
修改ModSecurity源码使其支持PUT请求
将./nginx/modsecurity/ngx_http_modsecurity.c的第1011行修改为如下代码。
编译ModSecurity模块
安装Nginx
开启反向代理,开启ModSecurity模块
指定nginx.conf对应内容为:
部署Core Rule Set
https://github.com/SpiderLabs/owasp‐modsecurity‐crs
git clone https://github.com/SpiderLabs/owasp‐modsecurity‐crs.git Move the crs‐setup.conf.example file to crs‐setup.conf
root@demo‐64bit:/usr/local/nginx/conf/owasp‐modsecurity‐crs# mv crs‐setup.conf.example crs‐setup.conf Rename rules/REQUEST‐900‐EXCLUSION‐RULES‐BEFORE‐CRS.conf.example and rules/RESPONSE‐999‐ EXCLUSION‐RULES‐AFTER‐CRS.conf.example to remove the '.example' extension.编写APP Specific Profiles
敏感内容,和企业项目有关。可自行根据CRS修改。
修改ModSecurity配置文件
modsecurity.conf
改为
否则会出现:
ModSecurity: Audit log: Failed to lock global mutex: Permission denied [hostname "ubuntu"] [uri "/test.php"] [unique_id "AcccAYAcAcAWycbcA
关闭数据分享(可选,大致是维护方为了提升用户体验做的数据收取)
SecStatusEngine On 改为SecStatusEngine Off
修改日志目录权限
chmod 777 /var/log/
重启Nginx
/usr/local/nginx/sbin/nginx ‐s reload
Loading...