Last active
August 29, 2015 14:24
-
-
Save vavrusa/f40c4df934583f2efd30 to your computer and use it in GitHub Desktop.
DNS slowdrip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local slowdrip = { | |
tracked = {}, | |
blocked = {}, | |
window = 60, -- Length of the tracking window | |
threshold = 100, -- Number of NXDOMAINs before blocking | |
-- Track suffixes of names leading to NXDOMAIN | |
layer = { | |
finish = function(state, req, answer) | |
local parent = answer:qname() | |
parent = parent:sub(parent:find('.',0,true), -1) | |
if answer:rcode() == kres.rcode.NXDOMAIN then | |
local count = (slowdrip.tracked[parent] or 0) + 1 | |
if count == slowdrip.threshold then | |
table.insert(slowdrip.blocked, parent) | |
end | |
slowdrip.tracked[parent] = count | |
end | |
return state | |
end | |
}, | |
-- Set up suffix tracking and periodic flushing | |
init = function(modules) | |
block:add(block.suffix_common(block.DROP, slowdrip.blocked)) | |
slowdrip.ev = event.recurrent(slowdrip.window * sec, function (ev) | |
local count = #slowdrip.blocked | |
for i=1, count do slowdrip.blocked[i] = nil end | |
slowdrip.tracked = {} | |
end) | |
end, | |
-- Stop tracking on unload | |
deinit = function(modules) | |
event.cancel(slowdrip.ev) | |
end | |
} | |
return slowdrip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment