针对REST API的Web应用防火墙

前言

个人毕业设计,挺水的。本来想好好弄,把防火墙和机器学习综合一下,结果学校突然通知提前一个月验收,只好匆匆完成,不过最后答辩老师的评价还挺不错,大概是没有细看吧。总之是让大家见笑了。

核心思路

Nginx + ModSecurity + OWASP Core Rule Set + APP Specific Profiles

防火墙引擎采用ModSecurity模块,搭配OWASP Core Rule Set和基于OWASP AppSensor DetectionPoints自行编写的APP Specific Profiles。该防火墙基于Nginx反向代理技术。

具体细节

原理

  1. 利用Nginx单项代理技术对请求和返回进行处理。ModSecurity作为Apache的一个模块,在被修改后是可以被Nginx使用的。
  2. REST API现在被广泛使用,具体定义这里就不赘述了,大体是一个类似与http://hostname.com/version1/SchoolID/ClassID/StudentID这样一个URL对资源进行定位,个人感觉对URL这个统一资源定位符是一种新的解读,但这种新相对于旧的过渡却非常的自然。具体可以看这位UCI博士Roy Fielding的论文。http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
  3. 现有的OWASP Core Rule Set能够防御很多攻击,但是这个规则集并没有针对REST API进行防御。这样一来,一方面我们可以部署这一套规则集,另一方面我们自行编写一些规则来针对REST API。而这些自定义的规则我们可以针对OWASP的AppSensor DetectionPoints来制定。https://www.owasp.org/index.php/AppSensor_DetectionPoints
  4. 要注意的是Nginx不支持动态模块加载,因此在变异Nginx时就需要把ModSecurity模块编译进去。

部署流程

Nginx

http://nginx.org/download/nginx‐1.10.3.tar.gz

wget http://nginx.org/download/nginx‐1.10.3.tar.gz

ModSecurity

https://modsecurity.org/download.html

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行修改为如下代码。

if ((r‐>method == NGX_HTTP_POST || r‐>method == NGX_HTTP_PUT) && modsecIsRequestBodyAccessEnabled(ctx‐>req) ) {
编译ModSecurity模块
checking if libcurl is linked with gnutls... no
sudo apt‐get install gnutls‐bin
sudo apt‐get install libyajl‐dev libcurl4‐openssl‐dev liblua5.1‐dev
./configure ‐‐enable‐standalone‐module ‐‐with‐yajl ‐‐disable‐mlogc
安装Nginx
./configure ‐‐add‐module=../modsecurity‐2.9.1/nginx/modsecurity
make
make install
开启反向代理,开启ModSecurity模块

指定nginx.conf对应内容为:

location / 
{ 
    root html;
    index index.html index.htm; 
    ModSecurityEnabled on; 
    ModSecurityConfig modsec_includes.conf; 
    # Only needed if including proxies 
    proxy_pass http://localhost:5000; 
    proxy_read_timeout 180s; 
    proxy_force_ranges on;}
部署Core Rule Set
include modsecurity.conf
include owasp‐modsecurity‐crs/crs‐setup.conf
include owasp‐modsecurity‐crs/rules/*.conf
include rest‐modsecurity‐rules/*.conf
cp /home/modsecurity/modsecurity‐2.9.1/modsecurity.conf‐recommended /usr/local/nginx/conf/modsecurity.conf
cp /home/modsecurity/modsecurity‐2.9.1/unicode.mapping /usr/local/nginx/conf/

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

1. SecAuditLogType Serial
2. SecAuditLog /var/log/modsec_audit.log

改为

1. SecAuditLogDirMode 0777
2. SecAuditLogFileMode 0550
3. SecAuditLogType Concurrent
4. SecAuditLog /var/log/modsec_audit.log

否则会出现:
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

Leave a Reply

Your email address will not be published. Required fields are marked *