suricata
detect-lua-extensions.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Functions to expose to the lua scripts.
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "decode.h"
29 #include "detect.h"
30 
31 #include "flow.h"
32 
33 #include "util-debug.h"
34 
35 #include "detect-lua.h"
36 
37 #include "app-layer-parser.h"
38 
39 #include "util-lua.h"
40 #include "util-lua-common.h"
41 #include "util-lua-tls.h"
42 #include "util-lua-smtp.h"
43 #include "util-lua-dnp3.h"
44 #include "detect-lua-extensions.h"
45 
46 /* Lua registry key for DetectLuaData. */
47 const char luaext_key_ld[] = "suricata:luadata";
48 
49 static int GetLuaData(lua_State *luastate, DetectLuaData **ret_ld)
50 {
51  *ret_ld = NULL;
52 
53  DetectLuaData *ld;
54  lua_pushlightuserdata(luastate, (void *)&luaext_key_ld);
55  lua_gettable(luastate, LUA_REGISTRYINDEX);
56  ld = lua_touserdata(luastate, -1);
57  if (ld == NULL) {
58  LUA_ERROR("internal error: no ld");
59  }
60  *ret_ld = ld;
61  return 0;
62 }
63 
64 static int LuaGetByteVar(lua_State *luastate)
65 {
66  DetectLuaData *ld = NULL;
67  DetectEngineThreadCtx *det_ctx = LuaStateGetDetCtx(luastate);
68 
69  if (det_ctx == NULL)
70  return LuaCallbackError(luastate, "internal error: no ldet_ctx");
71 
72  int ret = GetLuaData(luastate, &ld);
73  if (ret != 0)
74  return ret;
75 
76  if (!lua_isnumber(luastate, 1)) {
77  LUA_ERROR("bytevar id not a number");
78  }
79  int id = lua_tonumber(luastate, 1);
80  if (id < 0 || id >= DETECT_LUA_MAX_BYTEVARS) {
81  LUA_ERROR("bytevar id out of range");
82  }
83  uint32_t idx = ld->bytevar[id];
84 
85  lua_pushinteger(luastate, det_ctx->byte_values[idx]);
86 
87  return 1;
88 }
89 
91  DetectEngineThreadCtx *det_ctx, Flow *f, Packet *p, const Signature *s, uint8_t flags)
92 {
93  SCLogDebug("det_ctx %p, f %p", det_ctx, f);
94 
95  LuaStateSetSignature(lua_state, s);
96  LuaStateSetFlow(lua_state, f);
97  LuaStateSetDetCtx(lua_state, det_ctx);
98 
99  if (det_ctx->tx_id_set) {
100  if (f && f->alstate) {
101  void *txptr = AppLayerParserGetTx(f->proto, f->alproto, f->alstate, det_ctx->tx_id);
102  if (txptr) {
103  LuaStateSetTX(lua_state, txptr, det_ctx->tx_id);
104  }
105  }
106  }
107 
108  if (p != NULL)
109  LuaStateSetPacket(lua_state, p);
110 
111  LuaStateSetDirection(lua_state, (flags & STREAM_TOSERVER));
112 }
113 
114 /**
115  * \brief Register Suricata Lua functions
116  */
118 {
119  lua_pushcfunction(lua_state, LuaGetByteVar);
120  lua_setglobal(lua_state, "SCByteVarGet");
121 
122  LuaRegisterFunctions(lua_state);
123  LuaRegisterTlsFunctions(lua_state);
124  return 0;
125 }
DetectEngineThreadCtx_::byte_values
uint64_t * byte_values
Definition: detect.h:1258
LuaStateSetTX
void LuaStateSetTX(lua_State *luastate, void *txptr, const uint64_t tx_id)
Definition: util-lua.c:150
LuaStateSetPacket
void LuaStateSetPacket(lua_State *luastate, Packet *p)
Definition: util-lua.c:126
DetectLuaData
Definition: detect-lua.h:39
LUA_ERROR
#define LUA_ERROR(msg)
Definition: util-lua-common.h:41
util-lua-common.h
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Flow_::proto
uint8_t proto
Definition: flow.h:378
util-lua.h
LuaCallbackError
int LuaCallbackError(lua_State *luastate, const char *msg)
Definition: util-lua-common.c:59
DetectEngineThreadCtx_::tx_id
uint64_t tx_id
Definition: detect.h:1295
Flow_
Flow data structure.
Definition: flow.h:356
detect-lua.h
LuaStateSetFlow
void LuaStateSetFlow(lua_State *luastate, Flow *f)
set a flow pointer in the lua state
Definition: util-lua.c:176
LuaStateGetDetCtx
DetectEngineThreadCtx * LuaStateGetDetCtx(lua_State *luastate)
get DetectEngineThreadCtx pointer from the lua state
Definition: util-lua.c:239
lua_State
struct lua_State lua_State
Definition: suricata-common.h:523
decode.h
util-debug.h
DetectEngineThreadCtx_
Definition: detect.h:1223
detect.h
DETECT_LUA_MAX_BYTEVARS
#define DETECT_LUA_MAX_BYTEVARS
Definition: detect-lua.h:37
app-layer-parser.h
DetectLuaData::bytevar
uint32_t bytevar[DETECT_LUA_MAX_BYTEVARS]
Definition: detect-lua.h:50
LuaStateSetDetCtx
void LuaStateSetDetCtx(lua_State *luastate, DetectEngineThreadCtx *det_ctx)
Definition: util-lua.c:247
Packet_
Definition: decode.h:492
util-lua-dnp3.h
LuaRegisterTlsFunctions
int LuaRegisterTlsFunctions(lua_State *luastate)
register tls lua extensions in a luastate
Definition: util-lua-tls.c:338
LuaExtensionsMatchSetup
void LuaExtensionsMatchSetup(lua_State *lua_state, DetectLuaData *ld, DetectEngineThreadCtx *det_ctx, Flow *f, Packet *p, const Signature *s, uint8_t flags)
Definition: detect-lua-extensions.c:90
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1108
util-lua-smtp.h
flags
uint8_t flags
Definition: decode-gre.h:0
detect-lua-extensions.h
suricata-common.h
luaext_key_ld
const char luaext_key_ld[]
Definition: detect-lua-extensions.c:47
LuaStateSetDirection
void LuaStateSetDirection(lua_State *luastate, int direction)
Definition: util-lua.c:278
Flow_::alstate
void * alstate
Definition: flow.h:479
Signature_
Signature container.
Definition: detect.h:670
util-lua-tls.h
DetectEngineThreadCtx_::tx_id_set
bool tx_id_set
Definition: detect.h:1293
LuaRegisterExtensions
int LuaRegisterExtensions(lua_State *lua_state)
Register Suricata Lua functions.
Definition: detect-lua-extensions.c:117
id
uint32_t id
Definition: detect-flowbits.c:933
flow.h
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:450
LuaStateSetSignature
void LuaStateSetSignature(lua_State *luastate, const Signature *s)
Definition: util-lua.c:215
LuaRegisterFunctions
int LuaRegisterFunctions(lua_State *luastate)
Definition: util-lua-common.c:366