54 CHEN

Nginx防hashdos模块释出

nginx hashdos 2012.1.7 更新
编译的时候推荐使用nginx-1.0以上版本,不要加–with-debug参数编译,(感谢agentzh指出)。

hashdos这个事,严格意义上不是各种语言的错了(不过perl的确处理得很好),但是用nginx来擦屁股要干净些。
借鉴tomcat的作法,实现了下面这个nginx-http-hashdos-module,通过设置hashdos(默认on)的开关和body_max_count(默认值1000),对nginx后面的服务进行安全防护,相比对php或者java进行patch,这或许是最好的办法了。

nginx-http-hashdos-module项目地址 https://github.com/54chen/nginx-http-hashdos-module

如何使用 1.下载zip后保存到一个目录,如~/nginx-http-hashdos-module。
2.cd nginx-1.0.9/
3.重新编译和安装nginx
./configure –prefix=/opt/soft/nginx –with-pcre –user=www –group=www –with-http_stub_status_module –with-http_ssl_module –add-module=/path_to/nginx-http-hashdos-module/ && make && make install
4.配置打开:
hashdos on;
body_max_count 1000;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /opt/soft/nginx/conf/fastcgi_params;
client_body_buffer_size 2m;
client_max_body_size 2m;
}

注意事项 因为计算参数都在内存中进行,所以client_body_buffer_size 与 client_max_body_size 的值一定要相等。推荐2m。

TODO 改进in-file时的post分析。

原理 介于微博上有网友对此文扫一眼之后以为是简单通过client_body_buffer_size和client_max_body_size来做的判断,特加此节。
nginx-reqeust-body在接到请求时,根据header中的声明,判断是保存在内存还是在硬盘中,当大小超过两个buf和client_body_buffer_size大小时,会写入临时文件。
防止hashdos的终极目标是filter用户的输入,所以对用户的输入参数数量进行计数。超过1000(body_max_count的默认数量)时,返回一个413给攻击者。希望不要再渔到各种高级工程师。

https://github.com/54chen/nginx-http-hashdos-module

原创文章如转载,请注明:转载自五四陈科学院[http://www.54chen.com]

Posted by 54chen web相关