-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
detect/lua: add initial suricata.packet lib
Example: ``` local packet = require "suricata.packet" function init (args) local needs = {} return needs end function match (args) p = packet.get() payload = p:payload() for line in payload:gmatch("([^\r\n]*)[\r\n]+") do if line == "GET /index.html HTTP/1.0" then SCLogNotice(p:pcap_cnt() .. ": matching line! " .. line) return 1 end end return 0 end ``` Methods: `get` creates the packet object. `payload` returns the packet payload as a buffer `packet` returns the whole packet (includes headers) `pcap_cnt` returns the `pcap_cnt` (pcap file mode only)
- Loading branch information
1 parent
e0386fc
commit 48a5783
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* 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. | ||
*/ | ||
|
||
/** | ||
* \file | ||
* | ||
* Packet API for Lua. | ||
* | ||
* local packet = require("suricata.packet") | ||
*/ | ||
|
||
#include "suricata-common.h" | ||
|
||
#include "util-lua-packet.h" | ||
|
||
#include "app-layer-protos.h" /* Required by util-lua-common. */ | ||
#include "util-lua-common.h" | ||
#include "util-lua.h" | ||
#include "util-debug.h" | ||
|
||
/* key for p (packet) pointer */ | ||
extern const char lua_ext_key_p[]; | ||
|
||
struct LuaPacket { | ||
Packet *p; | ||
}; | ||
|
||
static int LuaPacketGC(lua_State *luastate) | ||
{ | ||
SCLogDebug("gc:start"); | ||
struct LuaPacket *s = (struct LuaPacket *)lua_touserdata(luastate, 1); | ||
SCLogDebug("packet %p", s->p); | ||
s->p = NULL; | ||
SCLogDebug("gc:done"); | ||
return 0; | ||
} | ||
|
||
static int LuaPacketPayload(lua_State *luastate) | ||
{ | ||
struct LuaPacket *s = (struct LuaPacket *)lua_touserdata(luastate, 1); | ||
if (s == NULL) { | ||
LUA_ERROR("failed to get packet"); | ||
} | ||
if (s->p == NULL) { | ||
LUA_ERROR("failed to get packet"); | ||
} | ||
|
||
LuaPushStringBuffer (luastate, (const uint8_t *)s->p->payload, (size_t)s->p->payload_len); /* stack at -3 */ | ||
return 1; | ||
} | ||
|
||
static int LuaPacketPacket(lua_State *luastate) | ||
{ | ||
struct LuaPacket *s = (struct LuaPacket *)lua_touserdata(luastate, 1); | ||
if (s == NULL) { | ||
LUA_ERROR("failed to get packet"); | ||
} | ||
if (s->p == NULL) { | ||
LUA_ERROR("failed to get packet"); | ||
} | ||
|
||
LuaPushStringBuffer (luastate, (const uint8_t *)GET_PKT_DATA(s->p), (size_t)GET_PKT_LEN(s->p)); /* stack at -3 */ | ||
return 1; | ||
} | ||
|
||
static int LuaPacketPcapCnt(lua_State *luastate) | ||
{ | ||
struct LuaPacket *s = (struct LuaPacket *)lua_touserdata(luastate, 1); | ||
if (s == NULL) { | ||
LUA_ERROR("failed to get packet"); | ||
} | ||
if (s->p == NULL) { | ||
LUA_ERROR("failed to get packet"); | ||
} | ||
|
||
lua_pushinteger(luastate, s->p->pcap_cnt); | ||
return 1; | ||
} | ||
|
||
static int LuaPacketGet(lua_State *luastate) | ||
{ | ||
Packet *p = LuaStateGetPacket(luastate); | ||
if (p == NULL) { | ||
LUA_ERROR("failed to get packet"); | ||
} | ||
|
||
SCLogDebug("new:start"); | ||
struct LuaPacket *s = (struct LuaPacket *)lua_newuserdata(luastate, sizeof(*s)); | ||
if (s == NULL) { | ||
LUA_ERROR("failed to get userdata"); | ||
} | ||
s->p = p; | ||
luaL_getmetatable(luastate, "packet::metatable"); | ||
lua_setmetatable(luastate, -2); | ||
SCLogDebug("new:done"); | ||
return 1; | ||
} | ||
|
||
// clang-format off | ||
static const luaL_Reg packetlib[] = { | ||
{ "get", LuaPacketGet }, | ||
{ "packet", LuaPacketPacket }, | ||
{ "payload", LuaPacketPayload }, | ||
{ "pcap_cnt", LuaPacketPcapCnt }, | ||
{ "__gc", LuaPacketGC }, | ||
{ NULL, NULL } | ||
}; | ||
// clang-format on | ||
|
||
void LuaLoadPacketLib(lua_State *luastate) | ||
{ | ||
luaL_newmetatable(luastate, "packet::metatable"); | ||
lua_pushvalue(luastate, -1); | ||
lua_setfield(luastate, -2, "__index"); | ||
luaL_setfuncs(luastate, packetlib, 0); | ||
luaL_newlib(luastate, packetlib); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* 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. | ||
*/ | ||
|
||
#ifndef SURICATA_UTIL_LUA_PACKET_H | ||
#define SURICATA_UTIL_LUA_PACKET_H | ||
|
||
#include "lua.h" | ||
|
||
void LuaLoadPacketLib(lua_State *luastate); | ||
|
||
#endif /* SURICATA_UTIL_LUA_DATASET_H */ |
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