Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track Allow2Ban #51

Closed
sushengloong opened this issue Mar 26, 2014 · 1 comment
Closed

Track Allow2Ban #51

sushengloong opened this issue Mar 26, 2014 · 1 comment

Comments

@sushengloong
Copy link

I have configured an Allow2Ban rule to filter any clients with say, more than 20 requests from the same IP within 5 seconds and block them for 1 hour. Instead of implementing this in production straightaway, we would like to track and monitor the events without really blocking the users first. However, I can't find a rack.attack.match_type for Allow2Ban event. Ideally, we would like to log every single Allow2Ban occurrence and its details. Any pointers? Thanks.

Rack::Attack.track('allow2ban scraper') do |req|
  Rack::Attack::Allow2Ban.filter(req.ip, maxretry: 20, findtime: 5.seconds, bantime: 1.hour) do
    true
  end
end

ActiveSupport::Notifications.subscribe("rack.attack") do |name, start, finish, request_id, req|
   # how to filter only the Allow2Ban event?
end
@ktheory
Copy link
Collaborator

ktheory commented Apr 4, 2014

Good question!

Here's the best approach that comes to mind. It's a little manual...but not too bad.

# Set up a typical blacklist with Allow2Ban filter:
Rack::Attack.blacklist('allow2ban scraper') do |req|
  result = Rack::Attack::Allow2Ban.filter(req.ip, maxretry: 20, findtime: 5.seconds, bantime: 1.hour) do
    true
  end

  # But instead of returning the result of Allow2Ban.filter, we track it
  if result
   # Log it or whatever
   puts "This request would have been blocked"
  end

  # Return false so the request is not blocked.
  false

end

Then when you're ready to enable it in production, you just delete the extra bit after the Allow2Ban.filter block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants