Skip to content

Instantly share code, notes, and snippets.

@B0fH
Last active April 26, 2018 01:42
Show Gist options
  • Save B0fH/c4bcb88a7611965ad093ddf63d36d224 to your computer and use it in GitHub Desktop.
Save B0fH/c4bcb88a7611965ad093ddf63d36d224 to your computer and use it in GitHub Desktop.
A SnortSam integration script for Suricata
--[[
samblock.lua: A SnortSam integration script for Suricata
Written by Elazar Broad
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
]]--
function init (args)
local needs = {}
needs["type"] = "packet"
needs["filter"] = "alerts"
return needs
end
function setup (args)
-- TODO: Move all of this to a config file.
-- Modify as necessary
snortsam_host = '127.0.0.1'
snortsam_port = 898
snortsam_password = '<you SnortSam password here>'
samtool_path = '/usr/local/bin/samtool'
--[[
Modify as necessary
Format is: [<sig id>] = { ip='src|dst', direction='in|out|full', duration='n s[econds]|m[inutes]|h[ours]|d[ays]|0 for permanent' }
]]--
sid_block_map = {
[2009700] = { ip='dst', direction='in', duration='12h' },
[2008578] = { ip='src', direction='in', duration='60d' },
[2017162] = { ip='src', direction='in', duration='60d' },
[2009699] = { ip='src', direction='in', duration='60d'}
}
end
function log (args)
sid, rev, gid = SCRuleIds()
if sid ~= nil then
local block_data = sid_block_map[sid]
if block_data ~= nil then
ipver, srcip, dstip, proto, sp, dp = SCPacketTuple()
local ip_to_block = srcip
if block_data.ip == 'dst' then
ip_to_block = dstip
end
execute_samtool(ip_to_block, sid, block_data.direction, block_data.duration)
end
end
end
function execute_samtool(ip, sid, direction, duration)
-- TODO: Sanitize arguments
local samblock_command = string.format('%s -block -ip %s -sid %s -direction %s -duration %s %s:%s/%s',
samtool_path,
ip,
sid,
direction,
duration,
snortsam_host,
snortsam_port,
snortsam_password)
local ret = os.execute(samblock_command)
if ret == 0 then
SCLogInfo(string.format('Blocked IP %s for %s.', ip, duration))
else
SCLogWarning(string.format('Blocking IP %s failed with exit code %s!', ip, ret))
end
end
function deinit (args)
SCLogInfo('samblock deinit')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment