ruby-on-rails-4 - rails 4 中的机架攻击和 Allow2Ban 过滤

标签 ruby-on-rails-4 devise rackattack

我正在我的 Rails 应用程序中实现 Kickstarter 的 Rack-attack。

白名单/黑名单过滤工作正常,但我在使用 Allow2Ban 锁定影响我的登录(设计)页面的 IP 地址时遇到问题。注意:我在本地测试这个并且已经从白名单中删除了 localhost。

# Lockout IP addresses that are hammering your login page.
# After 3 requests in 1 minute, block all requests from that IP for 1 hour.
Rack::Attack.blacklist('allow2ban login scrapers') do |req|
  # `filter` returns false value if request is to your login page (but still
  # increments the count) so request below the limit are not blocked until
  # they hit the limit.  At that point, filter will return true and block.
  Rack::Attack::Allow2Ban.filter(req.ip, :maxretry => 3, :findtime => 1.minute, :bantime => 1.hour) do
    # The count for the IP is incremented if the return value is truthy.
    req.path == '/sign_in' and req.post?
  end
end

在Rack-attack文档中,明确指出了节流功能需要缓存,即:

Rack::Attack.throttle('req/ip', :limit => 5, :period => 1.second) do |req| )

,但它没有为 Allow2Ban 说明这一点。任何人都知道 Allow2Ban 是否需要缓存,或者我是否在设计登录页面上使用上面的代码错误地实现了

最佳答案

是的,Allow2Ban 和 Fail2Ban 绝对需要检查(在 https://github.com/kickstarter/rack-attack/blob/master/lib/rack/attack/fail2ban.rb 中您可以看到如何以及为什么)。 顺便提一句。我建议使用 Redis 作为缓存,因为即使您使用多个应用程序节点,它也能确保您的应用程序阻止一个 IP 地址。如果您在多应用程序节点场景中使用 Rails 缓存,您的过滤器将按实例进行管理,这不是我所假设的。

关于ruby-on-rails-4 - rails 4 中的机架攻击和 Allow2Ban 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22721561/

相关文章:

ruby-on-rails - 如何使用 ActionMailer 通过 AWS SES 发送电子邮件

ruby-on-rails - 获取模型 ID where(value==x)

javascript - Ember : Error while loading route: TypeError: Object function… has no method 'create'

ruby-on-rails - 未定义的方法 'env' 为 nil :NilClass

ruby-on-rails - rails 3 + 设计 : on after_inactive_sign_up_path_for() how show the not-yet-confirmed user's email address?

ruby-on-rails - 如何让 Rack::Attack 在负载均衡器后面工作?

ruby-on-rails - Rails是否可以优雅地处理缓存存储中断(memcached)?

使用来自 ruby​​ on rails 的 firebase 的 Android 推送通知

ruby-on-rails - 使用 Hotwire Rails 时如何显示设计登录表单错误