You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
suricata/src/util-lua-builtins.c

84 lines
2.6 KiB
C

/* Copyright (C) 2025 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "suricata-common.h"
#include "util-lua-builtins.h"
#include "util-lua-base64lib.h"
#include "util-lua-dataset.h"
#include "util-lua-dnp3.h"
#include "util-lua-flowintlib.h"
#include "util-lua-flowvarlib.h"
#include "util-lua-http.h"
#include "util-lua-dns.h"
#include "util-lua-smtp.h"
#include "util-lua-ssh.h"
lua: add initial suricata.flow lib Methods: `get` creates the flow object. `id` returns the flow id. `has_alerts` returns a boolean indicating if the flow triggered alerts. `app_layer_proto` returns various app-layer related fields as 5 strings: alproto, alproto_ts, alproto_tc, alproto_orig, alproto_expect. `stats` returns cnts for bytes and packets to sever and to client, as 4 numbers. `tuple` -- returns various fields: srcip, dstip, proto, sp, dp. `timestamps` returns time as 4 numbers: seconds and microseconds, for first and last packet of the flow. `timestring_legacy` returns the first packet from the flow's timestring as a string (like fastlog). `timestring_iso8601` returns the first packet from the flow's timestring as an iso8601 compat timestring (like eve). Example: ``` name = "lua-scflowstats.log" local flow = require("suricata.flow") function init(args) local needs = {} needs["type"] = "flow" return needs end function setup(args) filename = SCLogPath() .. "/" .. name file = assert(io.open(filename, "a")) SCLogInfo("lua SCFlowStats Log Filename " .. filename) end function log(args) local f = flow.get() timestring = f:timestring_legacy() tscnt, tsbytes, tccnt, tcbytes = f:stats() file:write ("[**] " .. timestring .. "\nSCFlowStats is\nPacket count to server: " .. tscnt .. "\nByte count to server: " .. tsbytes .. "\nPacket count to client: " .. tccnt .. "\nByte count to client: " .. tcbytes .. "\n[**]") file:flush() end function deinit(args) file:close(file) end ``` Task #7489
2 years ago
#include "util-lua-flowlib.h"
#include "util-lua-hashlib.h"
#include "util-lua-tls.h"
#include "util-lua-packetlib.h"
#include "util-lua-rule.h"
#include "util-lua-ja3.h"
#include "util-lua-filelib.h"
#include "lauxlib.h"
static const luaL_Reg builtins[] = {
{ "suricata.base64", SCLuaLoadBase64Lib },
{ "suricata.dataset", LuaLoadDatasetLib },
{ "suricata.dnp3", SCLuaLoadDnp3Lib },
{ "suricata.dns", SCLuaLoadDnsLib },
{ "suricata.file", SCLuaLoadFileLib },
lua: add initial suricata.flow lib Methods: `get` creates the flow object. `id` returns the flow id. `has_alerts` returns a boolean indicating if the flow triggered alerts. `app_layer_proto` returns various app-layer related fields as 5 strings: alproto, alproto_ts, alproto_tc, alproto_orig, alproto_expect. `stats` returns cnts for bytes and packets to sever and to client, as 4 numbers. `tuple` -- returns various fields: srcip, dstip, proto, sp, dp. `timestamps` returns time as 4 numbers: seconds and microseconds, for first and last packet of the flow. `timestring_legacy` returns the first packet from the flow's timestring as a string (like fastlog). `timestring_iso8601` returns the first packet from the flow's timestring as an iso8601 compat timestring (like eve). Example: ``` name = "lua-scflowstats.log" local flow = require("suricata.flow") function init(args) local needs = {} needs["type"] = "flow" return needs end function setup(args) filename = SCLogPath() .. "/" .. name file = assert(io.open(filename, "a")) SCLogInfo("lua SCFlowStats Log Filename " .. filename) end function log(args) local f = flow.get() timestring = f:timestring_legacy() tscnt, tsbytes, tccnt, tcbytes = f:stats() file:write ("[**] " .. timestring .. "\nSCFlowStats is\nPacket count to server: " .. tscnt .. "\nByte count to server: " .. tsbytes .. "\nPacket count to client: " .. tccnt .. "\nByte count to client: " .. tcbytes .. "\n[**]") file:flush() end function deinit(args) file:close(file) end ``` Task #7489
2 years ago
{ "suricata.flow", LuaLoadFlowLib },
{ "suricata.flowint", LuaLoadFlowintLib },
{ "suricata.flowvar", LuaLoadFlowvarLib },
{ "suricata.hashlib", SCLuaLoadHashlib },
{ "suricata.http", SCLuaLoadHttpLib },
{ "suricata.ja3", SCLuaLoadJa3Lib },
{ "suricata.packet", LuaLoadPacketLib },
{ "suricata.rule", SCLuaLoadRuleLib },
{ "suricata.smtp", SCLuaLoadSmtpLib },
{ "suricata.ssh", SCLuaLoadSshLib },
{ "suricata.tls", SCLuaLoadTlsLib },
{ NULL, NULL },
};
/**
* \brief Load a Suricata built-in module in a sand-boxed environment.
*/
bool SCLuaLoadBuiltIns(lua_State *L, const char *name)
{
for (const luaL_Reg *lib = builtins; lib->name; lib++) {
if (strcmp(name, lib->name) == 0) {
lib->func(L);
return true;
}
}
return false;
}
/**
* \brief Register Suricata built-in modules for loading in a
* non-sandboxed environment.
*/
void SCLuaRequirefBuiltIns(lua_State *L)
{
for (const luaL_Reg *lib = builtins; lib->name; lib++) {
luaL_requiref(L, lib->name, lib->func, 0);
lua_pop(L, 1);
}
}