Rack-attackとのやり取りはこれが初めてなので、コードに誤りがある可能性がある場合は遠慮なく指摘してください。私が試みているのは、次のようなルートにアクセスしようとしている人をブラックリストに登録するip
ことです攻撃は、上記の URL にアクセスしようとするものをブロックしていません。"/azenv.php", "/setup.php" etc..
.php
ip
私が実行しようとしているコードのブロックは次のとおりです。
class Rack::Attack
Rack::Attack.whitelist('allow from localhost') do |req|
# Requests are allowed if the return value is truthy
'127.0.0.1' == req.ip || '::1' == req.ip || '122.166.130.230' == req.ip
end
Rack::Attack.blacklist("Block Referrer Analytics Spam") do |request|
spammerList = ENV.has_key?("spammerList") ? ENV["spammerList"].split(',') : []
spammerList.find { |spammer| request.referer =~ %r{#{spammer}} }
end
Rack::Attack.blacklist('bad_login_ip') do |req|
(req.post? && req.path == "/users/sign_in" && IPCat.datacenter?(req.ip))
end
Rack::Attack.blacklist('Stupid IP for PHP') do |req|
if req.path == "/azenv.php" || req.path == "/testproxy.php" || req.path == "//web/scripts/setup.php"
req.ip
end
# req.path.include?(".php")
end
Rack::Attack.throttle('req/ip', limit: 300, period: 5.minutes) do |req|
req.remote_ip if ['/assets', '/check'].any? {|path| req.path.starts_with? path }
end
Rack::Attack.throttle('req/ip', :limit => 5, :period => 20.seconds) do |req|
if req.path == '/users/sign_in' && req.post?
req.ip
end
end
Rack::Attack.throttle("logins/email", :limit => 5, :period => 20.seconds) do |req|
if req.path == '/users/sign_in' && req.post?
# return the email if present, nil otherwise
req.params['email'].presence
end
end
end
ブロックのいくつかは、Wiki 自体からのものです。ここで何か間違っている場合は修正してください。
アップデート
これは、与えられた例から試しているコードです:
Rack::Attack.blacklist('fail2ban pentesters') do |req|
# `filter` returns truthy value if request fails, or if it's from a previously banned IP
# so the request is blocked
Rack::Attack::Fail2Ban.filter("pentesters-#{req.ip}", :maxretry => 1, :findtime => 10.minutes, :bantime => 5.minutes) do
# The count for the IP is incremented if the return value is truthy
req.path.include?('/testproxy.php') ||
req.path.include?('azenv.php')
end
end