Skip to content

Instantly share code, notes, and snippets.

@samholst
Forked from etdsoft/fail2ban_notifier.rb
Created May 8, 2018 23:00
Show Gist options
  • Save samholst/8e08abb5bc778d8d7a0cacaa4bfe733d to your computer and use it in GitHub Desktop.
Save samholst/8e08abb5bc778d8d7a0cacaa4bfe733d to your computer and use it in GitHub Desktop.
Simple Fail2banNotifier for exception_notification (will submit a pull request and update gist when accepted). See: http://securityroots.com/blog/2014/01/protecting-app-rails-fail2ban/
class Fail2banNotifier
def initialize(options)
@default_options = options
@default_options[:logfile] ||= Rails.root.join('log', 'fail2ban.log')
# Roll over every 30M, keep 10 files
@logger ||= Logger.new(@default_options[:logfile], 10, 30*1024*1024)
end
def call(exception, options={})
env = options[:env]
request = ActionDispatch::Request.new(env)
# <ip> : <exception class> : <method> <path> -- <params>
msg = "%s : %s : %s %s -- %s" % [
request.remote_ip,
exception.class,
request.request_method,
env["PATH_INFO"],
request.filtered_parameters.inspect
]
@logger.error(msg)
end
end
# Custom Rails app jail. Add to /etc/fail2ban/jail.local
[rails-app]
enabled = true
port = http,https
filter = rails-app
logpath = /path/to/app/log/fail2ban.log
bantime = 3600
findtime = 600
maxretry = 10
# Custom Rails app filter. Place in /etc/fail2ban/filter.d/
[Definition]
failregex = : <HOST> :
ignoreregex =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment