广告:本站正在使用由 热网互联 提供的云主机(香港CIA线路)

MENU

nginx抵御cc类攻击的一些小技巧

2015 年 04 月 22 日 • 实验室

最简易:通过cookie识别访问者身份结合302 来抵御CC攻击,在 location段加入以下代码:

CC攻击简易预防

    if ($cookie_say != "isuike"){
    add_header Set-Cookie "say=isuike";
    rewrite .* "$scheme://$host$uri" redirect;
}

2.简易增强:如果cc带了cookie,那么可以加上用户ip的识别并写入到cookie验证中,结合302,在 location段加入以下代码:

if ($cookie_say != "isuike$remote_addr"){
    add_header Set-Cookie "say=isuike$remote_addr";
    rewrite .* "$scheme://$host$uri" redirect;
}

3.理论上的完美版:用salt加散列,这里需要nginx_lua模块,同样,加入以下代码:

rewrite_by_lua '
    local say = ngx.md5("isuike" .. ngx.var.remote_addr)
    if (ngx.var.cookie_say ~= say) then
        ngx.header["Set-Cookie"] = "say=" .. say
        return ngx.redirect(ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.uri)
    end
';

这些方法可以解决过CDN,无法识别ip访问频率等因素导致的攻击识别困难的CC类攻击防御。

标签: nginx, cc