42 static void TransformLuaxform(
45 #define LUAXFORM_MAX_ARGS 10
64 static void DetectTransformLuaxformId(
const uint8_t **data, uint32_t *length,
void *context)
68 *data = (uint8_t *)lua->
id_data;
95 static int DetectTransformLuaxformSetupPrime(
102 luaL_openlibs(luastate);
108 int status = luaL_loadfile(luastate, ld->
filename);
110 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
115 if (lua_pcall(luastate, 0, 0, 0) != 0) {
116 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
120 lua_getglobal(luastate,
"transform");
121 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
122 SCLogError(
"no transform function in script");
125 lua_pop(luastate, 1);
142 FatalError(
"unable to allocate memory for Lua transform: %s", optsstr);
145 lua->
copystr = strdup(optsstr);
146 lua->
id_data = strdup(optsstr);
148 FatalError(
"unable to allocate memory for Lua transform: %s", optsstr);
154 char *saveptr = NULL;
155 char *token = strtok_r(lua->
copystr,
",", &saveptr);
157 lua->
args[count++] = token;
158 token = strtok_r(NULL,
",", &saveptr);
178 DetectTransformLuaxformFree(
de_ctx, lua);
182 static void *DetectLuaxformThreadInit(
void *data)
189 FatalError(
"unable to allocate luaxform context memory");
214 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
220 lua_getglobal(t->
luastate,
"thread_init");
221 if (lua_isfunction(t->
luastate, -1)) {
222 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
223 SCLogError(
"couldn't run script 'thread_init' function: %s",
240 static void DetectLuaxformThreadFree(
void *
ctx)
268 SCLogError(
"Lua rules disabled by security configuration: security.lua.allow-rules");
279 int allow_restricted_functions = 0;
280 (void)
SCConfGetInt(
"security.lua.max-bytes", &lua_alloc_limit);
281 (void)
SCConfGetInt(
"security.lua.max-instructions", &lua_instruction_limit);
282 (void)
SCConfGetBool(
"security.lua.allow-restricted-functions", &allow_restricted_functions);
288 if (DetectTransformLuaxformSetupPrime(
de_ctx, lua, s) == -1) {
293 de_ctx,
"luaxform", DetectLuaxformThreadInit, (
void *)lua, DetectLuaxformThreadFree, 0);
303 DetectTransformLuaxformFree(
de_ctx, lua);
307 static void TransformLuaxform(
321 lua_getglobal(tlua->
luastate,
"transform");
323 const uint8_t *input = buffer->
inspect;
335 for (
int i = 1; i < lua->
arg_count + 1; i++) {
343 if (LUA_OK != lua_pcall(tlua->
luastate, 2, 2, 0)) {
347 int return_value_count = lua_gettop(tlua->
luastate);
348 if (return_value_count != 2) {
349 SCLogDebug(
"Error: expected 2 return values but got %d", return_value_count);
353 if (lua_isstring(tlua->
luastate, -2)) {
354 const char *transformed_buffer = lua_tostring(tlua->
luastate, -2);
355 lua_Integer transformed_buffer_byte_count = lua_tointeger(tlua->
luastate, -1);
356 if (transformed_buffer != NULL && transformed_buffer_byte_count > 0)
358 (uint32_t)transformed_buffer_byte_count);
359 SCLogDebug(
"transform returns [nbytes %d] \"%p\"",
360 (uint32_t)transformed_buffer_byte_count, transformed_buffer);
365 while (lua_gettop(tlua->
luastate) > 0) {
374 "pass inspection buffer to a Lua function along with "
375 "arguments supplied to the transform";