suricata
detect-lua.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2025 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  */
24 
25 #include "suricata-common.h"
26 #include "conf.h"
27 
28 #include "decode.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
32 
33 #include "detect-engine.h"
34 #include "detect-engine-buffer.h"
35 #include "detect-engine-mpm.h"
36 #include "detect-engine-build.h"
37 
38 #include "detect-byte.h"
39 
40 #include "flow.h"
41 #include "flow-var.h"
42 #include "flow-util.h"
43 
44 #include "util-byte.h"
45 
46 #include "util-unittest.h"
47 #include "util-unittest-helper.h"
48 
49 #include "app-layer.h"
50 #include "app-layer-parser.h"
51 #include "app-layer-htp.h"
52 #include "app-layer-ssl.h"
53 
54 #include "stream-tcp.h"
55 
56 #include "detect-lua.h"
57 #include "detect-lua-extensions.h"
58 
59 #include "util-var-name.h"
60 
61 #include "util-lua.h"
62 #include "util-lua-builtins.h"
63 #include "util-lua-common.h"
64 #include "util-lua-sandbox.h"
65 
66 static int DetectLuaMatch (DetectEngineThreadCtx *,
67  Packet *, const Signature *, const SigMatchCtx *);
68 static int DetectLuaAppTxMatch (DetectEngineThreadCtx *det_ctx,
69  Flow *f, uint8_t flags,
70  void *state, void *txv, const Signature *s,
71  const SigMatchCtx *ctx);
72 static int DetectLuaSetup (DetectEngineCtx *, Signature *, const char *);
73 #ifdef UNITTESTS
74 static void DetectLuaRegisterTests(void);
75 #endif
76 static void DetectLuaFree(DetectEngineCtx *, void *);
77 static int g_lua_ja3_list_id = 0;
78 static int g_lua_ja3s_list_id = 0;
79 
80 /**
81  * \brief Registration function for keyword: lua
82  */
84 {
86  sigmatch_table[DETECT_LUA].desc = "match via a lua script";
87  sigmatch_table[DETECT_LUA].url = "/rules/rule-lua-scripting.html";
88  sigmatch_table[DETECT_LUA].Match = DetectLuaMatch;
89  sigmatch_table[DETECT_LUA].AppLayerTxMatch = DetectLuaAppTxMatch;
90  sigmatch_table[DETECT_LUA].Setup = DetectLuaSetup;
91  sigmatch_table[DETECT_LUA].Free = DetectLuaFree;
92 #ifdef UNITTESTS
93  sigmatch_table[DETECT_LUA].RegisterTests = DetectLuaRegisterTests;
94 #endif
95 
96  g_lua_ja3_list_id = DetectBufferTypeRegister("ja3.lua");
101 
102  g_lua_ja3s_list_id = DetectBufferTypeRegister("ja3s.lua");
107 
108  SCLogDebug("registering lua rule option");
109 }
110 
111 /* Flags for DetectLuaThreadData. */
112 #define FLAG_DATATYPE_PACKET BIT_U32(0)
113 #define FLAG_DATATYPE_PAYLOAD BIT_U32(1)
114 #define FLAG_DATATYPE_STREAM BIT_U32(2)
115 #define FLAG_LIST_JA3 BIT_U32(3)
116 #define FLAG_LIST_JA3S BIT_U32(4)
117 #define FLAG_DATATYPE_BUFFER BIT_U32(22)
118 #define FLAG_ERROR_LOGGED BIT_U32(23)
119 #define FLAG_BLOCKED_FUNCTION_LOGGED BIT_U32(24)
120 #define FLAG_INSTRUCTION_LIMIT_LOGGED BIT_U32(25)
121 #define FLAG_MEMORY_LIMIT_LOGGED BIT_U32(26)
122 
123 #define DEFAULT_LUA_ALLOC_LIMIT 500000
124 #define DEFAULT_LUA_INSTRUCTION_LIMIT 500000
125 
126 /** \brief dump stack from lua state to screen */
127 void LuaDumpStack(lua_State *state, const char *prefix)
128 {
129  int size = lua_gettop(state);
130  printf("%s: size %d\n", prefix, size);
131 
132  for (int i = 1; i <= size; i++) {
133  int type = lua_type(state, i);
134  printf("- %s: Stack size=%d, level=%d, type=%d, ", prefix, size, i, type);
135 
136  switch (type) {
137  case LUA_TFUNCTION:
138  printf("function %s", lua_tostring(state, i));
139  break;
140  case LUA_TBOOLEAN:
141  printf("bool %s", lua_toboolean(state, i) ? "true" : "false");
142  break;
143  case LUA_TNUMBER:
144  printf("number %g", lua_tonumber(state, i));
145  break;
146  case LUA_TSTRING:
147  printf("string `%s'", lua_tostring(state, i));
148  break;
149  case LUA_TTABLE:
150  printf("table `%s'", lua_tostring(state, i));
151  break;
152  default:
153  printf("other %s", lua_typename(state, type));
154  break;
155 
156  }
157  printf("\n");
158  }
159 }
160 
161 static void LuaStateSetDetectLuaData(lua_State *state, DetectLuaData *data)
162 {
163  lua_pushlightuserdata(state, (void *)&luaext_key_ld);
164  lua_pushlightuserdata(state, (void *)data);
165  lua_settable(state, LUA_REGISTRYINDEX);
166 }
167 
168 /**
169  * \brief Common function to run the Lua match function and process
170  * the return value.
171  */
172 static int DetectLuaRunMatch(
173  DetectEngineThreadCtx *det_ctx, const DetectLuaData *lua, DetectLuaThreadData *tlua)
174 {
175  /* Reset instruction count. */
177 
178  if (lua_pcall(tlua->luastate, 1, 1, 0) != 0) {
179  const char *reason = lua_tostring(tlua->luastate, -1);
180  SCLuaSbState *context = SCLuaSbGetContext(tlua->luastate);
181  uint32_t flag = 0;
182  if (context->blocked_function_error) {
183  StatsIncr(det_ctx->tv, det_ctx->lua_blocked_function_errors);
185  } else if (context->instruction_count_error) {
186  StatsIncr(det_ctx->tv, det_ctx->lua_instruction_limit_errors);
188  } else if (context->memory_limit_error) {
189  StatsIncr(det_ctx->tv, det_ctx->lua_memory_limit_errors);
190  reason = "memory limit exceeded";
192  } else {
193  flag = FLAG_ERROR_LOGGED;
194  }
195 
196  /* Log once per thread per error type, the message from Lua
197  * will include the filename. */
198  if (!(tlua->flags & flag)) {
199  SCLogWarning("Lua script failed to run successfully: %s", reason);
200  tlua->flags |= flag;
201  }
202 
203  StatsIncr(det_ctx->tv, det_ctx->lua_rule_errors);
204  while (lua_gettop(tlua->luastate) > 0) {
205  lua_pop(tlua->luastate, 1);
206  }
207  SCReturnInt(0);
208  }
209 
210  int match = 0;
211 
212  /* process returns from script */
213  if (lua_gettop(tlua->luastate) > 0) {
214  /* script returns a number (return 1 or return 0) */
215  if (lua_type(tlua->luastate, 1) == LUA_TNUMBER) {
216  lua_Integer script_ret = lua_tointeger(tlua->luastate, 1);
217  SCLogDebug("script_ret %lld", script_ret);
218  lua_pop(tlua->luastate, 1);
219  if (script_ret == 1)
220  match = 1;
221  } else {
222  SCLogDebug("Unsupported datatype returned from Lua script");
223  }
224  }
225 
226  if (lua->negated) {
227  if (match == 1)
228  match = 0;
229  else
230  match = 1;
231  }
232 
233  while (lua_gettop(tlua->luastate) > 0) {
234  lua_pop(tlua->luastate, 1);
235  }
236 
237  SCReturnInt(match);
238 }
239 
241  const SigMatchData *smd, const uint8_t *buffer, uint32_t buffer_len, uint32_t offset,
242  Flow *f)
243 {
244  SCEnter();
245 
246  if (buffer == NULL || buffer_len == 0)
247  SCReturnInt(0);
248 
249  DetectLuaData *lua = (DetectLuaData *)smd->ctx;
250  if (lua == NULL)
251  SCReturnInt(0);
252 
253  DetectLuaThreadData *tlua =
255  if (tlua == NULL)
256  SCReturnInt(0);
257 
258  LuaExtensionsMatchSetup(tlua->luastate, lua, det_ctx, f, /* no packet in the ctx */ NULL, s, 0);
259 
260  /* prepare data to pass to script */
261  lua_getglobal(tlua->luastate, "match");
262  lua_newtable(tlua->luastate); /* stack at -1 */
263 
264  lua_pushliteral(tlua->luastate, "offset"); /* stack at -2 */
265  lua_pushnumber(tlua->luastate, (int)(offset + 1));
266  lua_settable(tlua->luastate, -3);
267 
268  lua_pushstring(tlua->luastate, lua->buffername); /* stack at -2 */
269  LuaPushStringBuffer(tlua->luastate, (const uint8_t *)buffer, (size_t)buffer_len);
270  lua_settable(tlua->luastate, -3);
271 
272  SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
273 }
274 
275 /**
276  * \brief match the specified lua script
277  *
278  * \param t thread local vars
279  * \param det_ctx pattern matcher thread local data
280  * \param p packet
281  * \param s signature being inspected
282  * \param m sigmatch that we will cast into DetectLuaData
283  *
284  * \retval 0 no match
285  * \retval 1 match
286  */
287 static int DetectLuaMatch (DetectEngineThreadCtx *det_ctx,
288  Packet *p, const Signature *s, const SigMatchCtx *ctx)
289 {
290  SCEnter();
291  DetectLuaData *lua = (DetectLuaData *)ctx;
292  if (lua == NULL)
293  SCReturnInt(0);
294 
296  if (tlua == NULL)
297  SCReturnInt(0);
298 
299  /* setup extension data for use in lua c functions */
300  uint8_t flags = 0;
301  if (p->flowflags & FLOW_PKT_TOSERVER)
302  flags = STREAM_TOSERVER;
303  else if (p->flowflags & FLOW_PKT_TOCLIENT)
304  flags = STREAM_TOCLIENT;
305 
306  LuaStateSetThreadVars(tlua->luastate, det_ctx->tv);
307 
308  LuaExtensionsMatchSetup(tlua->luastate, lua, det_ctx, p->flow, p, s, flags);
309 
310  if ((tlua->flags & FLAG_DATATYPE_PAYLOAD) && p->payload_len == 0)
311  SCReturnInt(0);
312  if ((tlua->flags & FLAG_DATATYPE_PACKET) && GET_PKT_LEN(p) == 0)
313  SCReturnInt(0);
314 
315  lua_getglobal(tlua->luastate, "match");
316  lua_newtable(tlua->luastate); /* stack at -1 */
317 
318  SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
319 }
320 
321 static int DetectLuaAppMatchCommon (DetectEngineThreadCtx *det_ctx,
322  Flow *f, uint8_t flags, void *state,
323  const Signature *s, const SigMatchCtx *ctx)
324 {
325  SCEnter();
326  DetectLuaData *lua = (DetectLuaData *)ctx;
327  if (lua == NULL)
328  SCReturnInt(0);
329 
331  if (tlua == NULL)
332  SCReturnInt(0);
333 
334  /* setup extension data for use in lua c functions */
335  LuaExtensionsMatchSetup(tlua->luastate, lua, det_ctx, f, NULL, s, flags);
336 
337  lua_getglobal(tlua->luastate, "match");
338  lua_newtable(tlua->luastate); /* stack at -1 */
339 
340  SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
341 }
342 
343 /**
344  * \brief match the specified lua script in a list with a tx
345  *
346  * \param t thread local vars
347  * \param det_ctx pattern matcher thread local data
348  * \param s signature being inspected
349  * \param m sigmatch that we will cast into DetectLuaData
350  *
351  * \retval 0 no match
352  * \retval 1 match
353  */
354 static int DetectLuaAppTxMatch (DetectEngineThreadCtx *det_ctx,
355  Flow *f, uint8_t flags,
356  void *state, void *txv, const Signature *s,
357  const SigMatchCtx *ctx)
358 {
359  return DetectLuaAppMatchCommon(det_ctx, f, flags, state, s, ctx);
360 }
361 
362 #ifdef UNITTESTS
363 /* if this ptr is set the lua setup functions will use this buffer as the
364  * lua script instead of calling luaL_loadfile on the filename supplied. */
365 static const char *ut_script = NULL;
366 #endif
367 
368 static void *DetectLuaThreadInit(void *data)
369 {
370  int status;
371  DetectLuaData *lua = (DetectLuaData *)data;
372  BUG_ON(lua == NULL);
373 
375  if (unlikely(t == NULL)) {
376  SCLogError("couldn't alloc ctx memory");
377  return NULL;
378  }
379 
380  t->flags = lua->flags;
381 
383  if (t->luastate == NULL) {
384  SCLogError("luastate pool depleted");
385  goto error;
386  }
387 
388  if (lua->allow_restricted_functions) {
389  luaL_openlibs(t->luastate);
391  } else {
393  }
394 
395  LuaStateSetDetectLuaData(t->luastate, lua);
396 
397  /* hackish, needed to allow unittests to pass buffers as scripts instead of files */
398 #ifdef UNITTESTS
399  if (ut_script != NULL) {
400  status = luaL_loadbuffer(t->luastate, ut_script, strlen(ut_script), "unittest");
401  if (status) {
402  SCLogError("couldn't load file: %s", lua_tostring(t->luastate, -1));
403  goto error;
404  }
405  } else {
406 #endif
407  status = luaL_loadfile(t->luastate, lua->filename);
408  if (status) {
409  SCLogError("couldn't load file: %s", lua_tostring(t->luastate, -1));
410  goto error;
411  }
412 #ifdef UNITTESTS
413  }
414 #endif
415 
416  /* prime the script (or something) */
417  if (lua_pcall(t->luastate, 0, 0, 0) != 0) {
418  SCLogError("couldn't prime file: %s", lua_tostring(t->luastate, -1));
419  goto error;
420  }
421 
422  /* thread_init call */
423  lua_getglobal(t->luastate, "thread_init");
424  if (lua_isfunction(t->luastate, -1)) {
425  if (lua_pcall(t->luastate, 0, 0, 0) != 0) {
426  SCLogError("couldn't run script 'thread_init' function: %s",
427  lua_tostring(t->luastate, -1));
428  goto error;
429  }
430  } else {
431  lua_pop(t->luastate, 1);
432  }
433 
434  return (void *)t;
435 
436 error:
437  if (t->luastate != NULL)
439  SCFree(t);
440  return NULL;
441 }
442 
443 static void DetectLuaThreadFree(void *ctx)
444 {
445  if (ctx != NULL) {
447  if (t->luastate != NULL)
449  SCFree(t);
450  }
451 }
452 
453 /**
454  * \brief Parse the lua keyword
455  *
456  * \param de_ctx Pointer to the detection engine context
457  * \param str Pointer to the user provided option
458  *
459  * \retval lua pointer to DetectLuaData on success
460  * \retval NULL on failure
461  */
462 static DetectLuaData *DetectLuaParse (DetectEngineCtx *de_ctx, const char *str)
463 {
464  DetectLuaData *lua = NULL;
465 
466  /* We have a correct lua option */
467  lua = SCCalloc(1, sizeof(DetectLuaData));
468  if (unlikely(lua == NULL))
469  goto error;
470 
471  if (strlen(str) && str[0] == '!') {
472  lua->negated = 1;
473  str++;
474  }
475 
476  /* get full filename */
478  if (lua->filename == NULL) {
479  goto error;
480  }
481 
482  return lua;
483 
484 error:
485  DetectLuaFree(de_ctx, lua);
486  return NULL;
487 }
488 
489 static int DetectLuaSetupPrime(DetectEngineCtx *de_ctx, DetectLuaData *ld, const Signature *s)
490 {
491  int status;
492 
494  if (luastate == NULL)
495  return -1;
496  if (ld->allow_restricted_functions) {
497  luaL_openlibs(luastate);
498  SCLuaRequirefBuiltIns(luastate);
499  } else {
500  SCLuaSbLoadLibs(luastate);
501  }
502  LuaStateSetDetectLuaData(luastate, ld);
503 
504  /* hackish, needed to allow unittests to pass buffers as scripts instead of files */
505 #ifdef UNITTESTS
506  if (ut_script != NULL) {
507  status = luaL_loadbuffer(luastate, ut_script, strlen(ut_script), "unittest");
508  if (status) {
509  SCLogError("couldn't load file: %s", lua_tostring(luastate, -1));
510  goto error;
511  }
512  } else {
513 #endif
514  status = luaL_loadfile(luastate, ld->filename);
515  if (status) {
516  SCLogError("couldn't load file: %s", lua_tostring(luastate, -1));
517  goto error;
518  }
519 #ifdef UNITTESTS
520  }
521 #endif
522 
523  /* prime the script (or something) */
524  if (lua_pcall(luastate, 0, 0, 0) != 0) {
525  SCLogError("couldn't prime file: %s", lua_tostring(luastate, -1));
526  goto error;
527  }
528 
529  lua_getglobal(luastate, "init");
530  if (lua_type(luastate, -1) != LUA_TFUNCTION) {
531  SCLogError("no init function in script");
532  goto error;
533  }
534 
535  /* Pass the signature as the first argument, setting up bytevars depends on
536  * access to the signature. */
537  lua_pushlightuserdata(luastate, (void *)s);
538 
539  if (lua_pcall(luastate, 1, 1, 0) != 0) {
540  SCLogError("couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
541  goto error;
542  }
543 
544  /* process returns from script */
545  if (lua_gettop(luastate) == 0) {
546  SCLogError("init function in script should return table, nothing returned");
547  goto error;
548  }
549  if (lua_type(luastate, 1) != LUA_TTABLE) {
550  SCLogError("init function in script should return table, returned is not table");
551  goto error;
552  }
553 
554  lua_pushnil(luastate);
555  const char *k;
556  while (lua_next(luastate, -2)) {
557  k = lua_tostring(luastate, -2);
558  if (k == NULL)
559  continue;
560 
561  /* handle flowvar and bytes separately as they have a table as value */
562  if (strcmp(k, "flowvar") == 0) {
563  if (lua_istable(luastate, -1)) {
564  lua_pushnil(luastate);
565  while (lua_next(luastate, -2) != 0) {
566  /* value at -1, key is at -2 which we ignore */
567  const char *value = lua_tostring(luastate, -1);
568  SCLogDebug("value %s", value);
569  /* removes 'value'; keeps 'key' for next iteration */
570  lua_pop(luastate, 1);
571 
572  if (ld->flowvars == DETECT_LUA_MAX_FLOWVARS) {
573  SCLogError("too many flowvars registered");
574  goto error;
575  }
576 
577  uint32_t idx = VarNameStoreRegister(value, VAR_TYPE_FLOW_VAR);
578  if (unlikely(idx == 0))
579  goto error;
580  ld->flowvar[ld->flowvars++] = idx;
581  SCLogDebug("script uses flowvar %u with script id %u", idx, ld->flowvars - 1);
582  }
583  }
584  lua_pop(luastate, 1);
585  continue;
586  } else if (strcmp(k, "flowint") == 0) {
587  if (lua_istable(luastate, -1)) {
588  lua_pushnil(luastate);
589  while (lua_next(luastate, -2) != 0) {
590  /* value at -1, key is at -2 which we ignore */
591  const char *value = lua_tostring(luastate, -1);
592  SCLogDebug("value %s", value);
593  /* removes 'value'; keeps 'key' for next iteration */
594  lua_pop(luastate, 1);
595 
596  if (ld->flowints == DETECT_LUA_MAX_FLOWINTS) {
597  SCLogError("too many flowints registered");
598  goto error;
599  }
600 
601  uint32_t idx = VarNameStoreRegister(value, VAR_TYPE_FLOW_INT);
602  if (unlikely(idx == 0))
603  goto error;
604  ld->flowint[ld->flowints++] = idx;
605  SCLogDebug("script uses flowint %u with script id %u", idx, ld->flowints - 1);
606  }
607  }
608  lua_pop(luastate, 1);
609  continue;
610  }
611 
612  bool required = lua_toboolean(luastate, -1);
613  lua_pop(luastate, 1);
614  if (!required) {
615  continue;
616  }
617 
618  if (strcmp(k, "ja3") == 0) {
619  ld->flags |= FLAG_LIST_JA3;
620  } else if (strcmp(k, "ja3s") == 0) {
621  ld->flags |= FLAG_LIST_JA3S;
622  } else if (strcmp(k, "packet") == 0) {
624  } else if (strcmp(k, "payload") == 0) {
626  } else if (strcmp(k, "buffer") == 0) {
628 
629  ld->buffername = SCStrdup("buffer");
630  if (ld->buffername == NULL) {
631  SCLogError("alloc error");
632  goto error;
633  }
634  } else if (strcmp(k, "stream") == 0) {
636 
637  ld->buffername = SCStrdup("stream");
638  if (ld->buffername == NULL) {
639  SCLogError("alloc error");
640  goto error;
641  }
642  /* old options no longer supported */
643  } else if (strncmp(k, "http", 4) == 0 || strncmp(k, "dns", 3) == 0 ||
644  strncmp(k, "tls", 3) == 0 || strncmp(k, "ssh", 3) == 0 ||
645  strncmp(k, "smtp", 4) == 0 || strncmp(k, "dnp3", 4) == 0) {
646  SCLogError("data type %s no longer supported, use rule hooks", k);
647  goto error;
648 
649  } else {
650  SCLogError("unsupported data type %s", k);
651  goto error;
652  }
653  }
654 
655  /* pop the table */
656  lua_pop(luastate, 1);
657  SCLuaSbStateClose(luastate);
658  return 0;
659 error:
660  SCLuaSbStateClose(luastate);
661  return -1;
662 }
663 
664 /**
665  * \brief this function is used to parse lua options
666  * \brief into the current signature
667  *
668  * \param de_ctx pointer to the Detection Engine Context
669  * \param s pointer to the Current Signature
670  * \param str pointer to the user provided "lua" option
671  *
672  * \retval 0 on Success
673  * \retval -1 on Failure
674  */
675 static int DetectLuaSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
676 {
677  /* First check if Lua rules are enabled, by default Lua in rules
678  * is disabled. */
679  int enabled = 0;
680  if (SCConfGetBool("security.lua.allow-rules", &enabled) == 1 && !enabled) {
681  SCLogError("Lua rules disabled by security configuration: security.lua.allow-rules");
682  return -1;
683  }
684 
685  DetectLuaData *lua = DetectLuaParse(de_ctx, str);
686  if (lua == NULL)
687  return -1;
688 
689  /* Load lua sandbox configurations */
690  intmax_t lua_alloc_limit = DEFAULT_LUA_ALLOC_LIMIT;
691  intmax_t lua_instruction_limit = DEFAULT_LUA_INSTRUCTION_LIMIT;
692  (void)SCConfGetInt("security.lua.max-bytes", &lua_alloc_limit);
693  (void)SCConfGetInt("security.lua.max-instructions", &lua_instruction_limit);
694  lua->alloc_limit = lua_alloc_limit;
695  lua->instruction_limit = lua_instruction_limit;
696 
697  int allow_restricted_functions = 0;
698  (void)SCConfGetBool("security.lua.allow-restricted-functions", &allow_restricted_functions);
699  lua->allow_restricted_functions = allow_restricted_functions;
700 
701  if (DetectLuaSetupPrime(de_ctx, lua, s) == -1) {
702  goto error;
703  }
704 
706  DetectLuaThreadInit, (void *)lua,
707  DetectLuaThreadFree, 0);
708  if (lua->thread_ctx_id == -1)
709  goto error;
710 
711  int list = DetectBufferGetActiveList(de_ctx, s);
712  SCLogDebug("buffer list %d -> %d", list, s->init_data->list);
713  if (list == -1 || (list == 0 && s->init_data->list == INT_MAX)) {
714  /* what needs to happen here is: we register to the rule hook, so e.g.
715  * http1.request_complete. This means we need a list.
716  *
717  * This includes each pkt, payload, stream, etc. */
718 
720  list = s->init_data->hook.sm_list;
721  SCLogDebug("setting list %d", list);
722  }
723  }
724 
725  if (list == -1) {
726  SCLogError("lua failed to set up");
727  goto error;
728  }
729  if (list == 0) {
730  if (lua->flags & FLAG_LIST_JA3) {
731  list = g_lua_ja3_list_id;
732  } else if (lua->flags & FLAG_LIST_JA3S) {
733  list = g_lua_ja3s_list_id;
734  }
735  }
736 
737  if (SCSigMatchAppendSMToList(de_ctx, s, DETECT_LUA, (SigMatchCtx *)lua, list) == NULL) {
738  goto error;
739  }
740 
741  return 0;
742 
743 error:
744  if (lua != NULL)
745  DetectLuaFree(de_ctx, lua);
746  return -1;
747 }
748 
749 /**
750  * \brief this function will free memory associated with DetectLuaData
751  *
752  * \param ptr pointer to DetectLuaData
753  */
754 static void DetectLuaFree(DetectEngineCtx *de_ctx, void *ptr)
755 {
756  if (ptr != NULL) {
757  DetectLuaData *lua = (DetectLuaData *)ptr;
758 
759  if (lua->buffername)
760  SCFree(lua->buffername);
761  if (lua->filename)
762  SCFree(lua->filename);
763 
764  for (uint16_t i = 0; i < lua->flowints; i++) {
766  }
767  for (uint16_t i = 0; i < lua->flowvars; i++) {
769  }
770  for (uint16_t i = 0; i < lua->bytevars; i++) {
771  SCFree(lua->bytevar[i].name);
772  }
773 
775 
776  SCFree(lua);
777  }
778 }
779 
780 #ifdef UNITTESTS
781 #include "detect-engine-alert.h"
782 
783 /** \test http buffer */
784 static int LuaMatchTest01(void)
785 {
786  SCConfSetFinal("security.lua.allow-rules", "true");
787 
788  const char script[] = "local flowvarlib = require(\"suricata.flowvar\")\n"
789  "function init (args)\n"
790  " flowvarlib.register(\"cnt\")\n"
791  " return {}\n"
792  "end\n"
793  "function thread_init (args)\n"
794  " cnt = flowvarlib.get(\"cnt\")\n"
795  "end\n"
796  "\n"
797  "function match(args)\n"
798  " a = cnt:value()\n"
799  " if a then\n"
800  " a = tostring(tonumber(a)+1)\n"
801  " print (a)\n"
802  " cnt:set(a, #a)\n"
803  " else\n"
804  " a = tostring(1)\n"
805  " print (a)\n"
806  " cnt:set(a, #a)\n"
807  " end\n"
808  " \n"
809  " print (\"pre check: \" .. (a))\n"
810  " if tonumber(a) == 2 then\n"
811  " print \"match\"\n"
812  " return 1\n"
813  " end\n"
814  " return 0\n"
815  "end\n"
816  "return 0\n";
817  char sig[] = "alert http1:request_complete any any -> any any (flow:to_server; lua:unittest; "
818  "sid:1;)";
819  uint8_t httpbuf1[] =
820  "POST / HTTP/1.1\r\n"
821  "Host: www.emergingthreats.net\r\n\r\n";
822  uint8_t httpbuf2[] =
823  "POST / HTTP/1.1\r\n"
824  "Host: www.openinfosecfoundation.org\r\n\r\n";
825  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
826  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
827  TcpSession ssn;
828  Flow f;
829  ThreadVars th_v;
830  DetectEngineThreadCtx *det_ctx;
831 
833 
834  ut_script = script;
835 
836  memset(&th_v, 0, sizeof(th_v));
837  memset(&f, 0, sizeof(f));
838  memset(&ssn, 0, sizeof(ssn));
839 
840  Packet *p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
841  Packet *p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
842 
843  FLOW_INITIALIZE(&f);
844  f.protoctx = (void *)&ssn;
845  f.proto = IPPROTO_TCP;
846  f.flags |= FLOW_IPV4;
848 
849  p1->flow = &f;
853  p2->flow = &f;
857 
858  StreamTcpInitConfig(true);
859 
862  de_ctx->flags |= DE_QUIET;
863 
865  FAIL_IF_NULL(s);
866 
868  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
869 
870  int r = AppLayerParserParse(
871  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
872  FAIL_IF(r != 0);
873  HtpState *http_state = f.alstate;
874  FAIL_IF_NULL(http_state);
875 
876  /* do detect for p1 */
877  SCLogDebug("inspecting p1");
878  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
879  FAIL_IF(PacketAlertCheck(p1, 1));
880 
881  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
882  FAIL_IF(r != 0);
883 
884  /* do detect for p2 */
885  SCLogDebug("inspecting p2");
886  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
888 
889  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_VAR);
890  FAIL_IF(id == 0);
891 
892  FlowVar *fv = FlowVarGet(&f, id);
893  FAIL_IF_NULL(fv);
894  FAIL_IF(fv->data.fv_str.value_len != 1);
895  FAIL_IF(memcmp(fv->data.fv_str.value, "2", 1) != 0);
896 
897  UTHFreePackets(&p1, 1);
898  UTHFreePackets(&p2, 1);
899  FLOW_DESTROY(&f);
900 
902  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
904  StreamTcpFreeConfig(true);
905  StatsThreadCleanup(&th_v);
906  PASS;
907 }
908 
909 static int LuaMatchTest01a(void)
910 {
911  const char script[] = "local flowvarlib = require(\"suricata.flowvar\")\n"
912  "function init (args)\n"
913  " flowvarlib.register(\"cnt\")\n"
914  " return {}\n"
915  "end\n"
916  "function thread_init (args)\n"
917  " cnt = flowvarlib.get(\"cnt\")\n"
918  "end\n"
919  "\n"
920  "function match(args)\n"
921  " a = cnt:value(0)\n"
922  " if a then\n"
923  " a = tostring(tonumber(a)+1)\n"
924  " print (a)\n"
925  " cnt:set(a, #a)\n"
926  " else\n"
927  " a = tostring(1)\n"
928  " print (a)\n"
929  " cnt:set(a, #a)\n"
930  " end\n"
931  " \n"
932  " print (\"pre check: \" .. (a))\n"
933  " if tonumber(a) == 2 then\n"
934  " print \"match\"\n"
935  " return 1\n"
936  " end\n"
937  " return 0\n"
938  "end\n"
939  "return 0\n";
940  char sig[] = "alert http1:request_complete any any -> any any (flow:to_server; lua:unittest; "
941  "sid:1;)";
942  uint8_t httpbuf1[] = "POST / HTTP/1.1\r\n"
943  "Host: www.emergingthreats.net\r\n\r\n";
944  uint8_t httpbuf2[] = "POST / HTTP/1.1\r\n"
945  "Host: www.openinfosecfoundation.org\r\n\r\n";
946  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
947  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
948  TcpSession ssn;
949  Flow f;
950  ThreadVars th_v;
951  DetectEngineThreadCtx *det_ctx;
952 
954 
955  ut_script = script;
956 
957  memset(&th_v, 0, sizeof(th_v));
958  memset(&f, 0, sizeof(f));
959  memset(&ssn, 0, sizeof(ssn));
960 
961  Packet *p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
962  Packet *p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
963 
964  FLOW_INITIALIZE(&f);
965  f.protoctx = (void *)&ssn;
966  f.proto = IPPROTO_TCP;
967  f.flags |= FLOW_IPV4;
969 
970  p1->flow = &f;
974  p2->flow = &f;
978 
979  StreamTcpInitConfig(true);
980 
983  de_ctx->flags |= DE_QUIET;
984 
986  FAIL_IF_NULL(s);
987 
989  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
990 
991  int r = AppLayerParserParse(
992  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
993  FAIL_IF(r != 0);
994 
995  HtpState *http_state = f.alstate;
996  FAIL_IF_NULL(http_state);
997 
998  /* do detect for p1 */
999  SCLogDebug("inspecting p1");
1000  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
1001  FAIL_IF(PacketAlertCheck(p1, 1));
1002 
1003  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
1004  FAIL_IF(r != 0);
1005  /* do detect for p2 */
1006  SCLogDebug("inspecting p2");
1007  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
1008  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
1009 
1010  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_VAR);
1011  FAIL_IF(id == 0);
1012 
1013  FlowVar *fv = FlowVarGet(&f, id);
1014  FAIL_IF_NULL(fv);
1015  FAIL_IF(fv->data.fv_str.value_len != 1);
1016  FAIL_IF(memcmp(fv->data.fv_str.value, "2", 1) != 0);
1017 
1018  UTHFreePackets(&p1, 1);
1019  UTHFreePackets(&p2, 1);
1020  FLOW_DESTROY(&f);
1021 
1023  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1025  StreamTcpFreeConfig(true);
1026  StatsThreadCleanup(&th_v);
1027  PASS;
1028 }
1029 
1030 /** \test payload buffer */
1031 static int LuaMatchTest02(void)
1032 {
1033  const char script[] = "local flowvarlib = require(\"suricata.flowvar\")\n"
1034  "function init (args)\n"
1035  " flowvarlib.register(\"cnt\")\n"
1036  " local needs = {}\n"
1037  " needs[\"payload\"] = tostring(true)\n"
1038  " return needs\n"
1039  "end\n"
1040  "function thread_init (args)\n"
1041  " cnt = flowvarlib.get(\"cnt\")\n"
1042  "end\n"
1043  "\n"
1044  "function match(args)\n"
1045  " a = cnt:value()\n"
1046  " if a then\n"
1047  " a = tostring(tonumber(a)+1)\n"
1048  " print (a)\n"
1049  " cnt:set(a, #a)\n"
1050  " else\n"
1051  " a = tostring(1)\n"
1052  " print (a)\n"
1053  " cnt:set(a, #a)\n"
1054  " end\n"
1055  " \n"
1056  " print (\"pre check: \" .. (a))\n"
1057  " if tonumber(a) == 2 then\n"
1058  " print \"match\"\n"
1059  " return 1\n"
1060  " end\n"
1061  " return 0\n"
1062  "end\n"
1063  "return 0\n";
1064  char sig[] = "alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1065  uint8_t httpbuf1[] = "POST / HTTP/1.1\r\n"
1066  "Host: www.emergingthreats.net\r\n\r\n";
1067  uint8_t httpbuf2[] = "POST / HTTP/1.1\r\n"
1068  "Host: www.openinfosecfoundation.org\r\n\r\n";
1069  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1070  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1071  TcpSession ssn;
1072  Flow f;
1073  ThreadVars th_v;
1074  DetectEngineThreadCtx *det_ctx;
1075 
1076  ut_script = script;
1077 
1078  memset(&th_v, 0, sizeof(th_v));
1079  memset(&f, 0, sizeof(f));
1080  memset(&ssn, 0, sizeof(ssn));
1081 
1082  Packet *p1 = UTHBuildPacket(httpbuf1, httplen1, IPPROTO_TCP);
1083  Packet *p2 = UTHBuildPacket(httpbuf2, httplen2, IPPROTO_TCP);
1084 
1085  FLOW_INITIALIZE(&f);
1086  f.protoctx = (void *)&ssn;
1087  f.proto = IPPROTO_TCP;
1088  f.flags |= FLOW_IPV4;
1089  f.alproto = ALPROTO_HTTP1;
1090 
1091  p1->flow = &f;
1095  p2->flow = &f;
1099 
1100  StreamTcpInitConfig(true);
1101 
1104  de_ctx->flags |= DE_QUIET;
1105 
1107  FAIL_IF_NULL(s);
1108 
1110  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1111 
1112  /* do detect for p1 */
1113  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
1114 
1115  FAIL_IF(PacketAlertCheck(p1, 1));
1116 
1117  /* do detect for p2 */
1118  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
1119 
1120  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
1121 
1122  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_VAR);
1123  FAIL_IF(id == 0);
1124 
1125  FlowVar *fv = FlowVarGet(&f, id);
1126  FAIL_IF_NULL(fv);
1127  FAIL_IF(fv->data.fv_str.value_len != 1);
1128  FAIL_IF(memcmp(fv->data.fv_str.value, "2", 1) != 0);
1129 
1130  UTHFreePackets(&p1, 1);
1131  UTHFreePackets(&p2, 1);
1132  FLOW_DESTROY(&f);
1133 
1134  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1136  StreamTcpFreeConfig(true);
1137  StatsThreadCleanup(&th_v);
1138  PASS;
1139 }
1140 
1141 /** \test payload buffer */
1142 static int LuaMatchTest02a(void)
1143 {
1144  const char script[] = "local flowvarlib = require(\"suricata.flowvar\")\n"
1145  "function init (args)\n"
1146  " flowvarlib.register(\"cnt\")"
1147  " local needs = {}\n"
1148  " needs[\"payload\"] = tostring(true)\n"
1149  " return needs\n"
1150  "end\n"
1151  "function thread_init (args)\n"
1152  " cnt = flowvarlib.get(\"cnt\")"
1153  "end\n"
1154  "\n"
1155  "function match(args)\n"
1156  " a = cnt:value()\n"
1157  " if a then\n"
1158  " a = tostring(tonumber(a)+1)\n"
1159  " print (a)\n"
1160  " cnt:set(a, #a)\n"
1161  " else\n"
1162  " a = tostring(1)\n"
1163  " print (a)\n"
1164  " cnt:set(a, #a)\n"
1165  " end\n"
1166  " \n"
1167  " print (\"pre check: \" .. (a))\n"
1168  " if tonumber(a) == 2 then\n"
1169  " print \"match\"\n"
1170  " return 1\n"
1171  " end\n"
1172  " return 0\n"
1173  "end\n"
1174  "return 0\n";
1175  char sig[] = "alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1176  uint8_t httpbuf1[] = "POST / HTTP/1.1\r\n"
1177  "Host: www.emergingthreats.net\r\n\r\n";
1178  uint8_t httpbuf2[] = "POST / HTTP/1.1\r\n"
1179  "Host: www.openinfosecfoundation.org\r\n\r\n";
1180  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1181  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1182  TcpSession ssn;
1183  Flow f;
1184  ThreadVars th_v;
1185  DetectEngineThreadCtx *det_ctx;
1186 
1187  ut_script = script;
1188 
1189  memset(&th_v, 0, sizeof(th_v));
1190  memset(&f, 0, sizeof(f));
1191  memset(&ssn, 0, sizeof(ssn));
1192 
1193  Packet *p1 = UTHBuildPacket(httpbuf1, httplen1, IPPROTO_TCP);
1194  Packet *p2 = UTHBuildPacket(httpbuf2, httplen2, IPPROTO_TCP);
1195 
1196  FLOW_INITIALIZE(&f);
1197  f.protoctx = (void *)&ssn;
1198  f.proto = IPPROTO_TCP;
1199  f.flags |= FLOW_IPV4;
1200  f.alproto = ALPROTO_HTTP1;
1201 
1202  p1->flow = &f;
1206  p2->flow = &f;
1210 
1211  StreamTcpInitConfig(true);
1212 
1215  de_ctx->flags |= DE_QUIET;
1216 
1218  FAIL_IF_NULL(s);
1219 
1221  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1222 
1223  /* do detect for p1 */
1224  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
1225  FAIL_IF(PacketAlertCheck(p1, 1));
1226 
1227  /* do detect for p2 */
1228  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
1229  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
1230 
1231  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_VAR);
1232  FAIL_IF(id == 0);
1233 
1234  FlowVar *fv = FlowVarGet(&f, id);
1235  FAIL_IF_NULL(fv);
1236  FAIL_IF(fv->data.fv_str.value_len != 1);
1237  FAIL_IF(memcmp(fv->data.fv_str.value, "2", 1) != 0);
1238 
1239  UTHFreePackets(&p1, 1);
1240  UTHFreePackets(&p2, 1);
1241  FLOW_DESTROY(&f);
1242 
1243  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1245  StreamTcpFreeConfig(true);
1246  StatsThreadCleanup(&th_v);
1247  PASS;
1248 }
1249 
1250 /** \test packet buffer */
1251 static int LuaMatchTest03(void)
1252 {
1253  const char script[] = "local flowvarlib = require(\"suricata.flowvar\")\n"
1254  "function init (args)\n"
1255  " flowvarlib.register(\"cnt\")\n"
1256  " local needs = {}\n"
1257  " needs[\"packet\"] = tostring(true)\n"
1258  " return needs\n"
1259  "end\n"
1260  "\n"
1261  "function thread_init (args)\n"
1262  " cnt = flowvarlib.get(\"cnt\")\n"
1263  "end\n"
1264  "\n"
1265  "function match(args)\n"
1266  " a = cnt:value()\n"
1267  " if a then\n"
1268  " a = tostring(tonumber(a)+1)\n"
1269  " print (a)\n"
1270  " cnt:set(a, #a)\n"
1271  " else\n"
1272  " a = tostring(1)\n"
1273  " print (a)\n"
1274  " cnt:set(a, #a)\n"
1275  " end\n"
1276  " \n"
1277  " print (\"pre check: \" .. (a))\n"
1278  " if tonumber(a) == 2 then\n"
1279  " print \"match\"\n"
1280  " return 1\n"
1281  " end\n"
1282  " return 0\n"
1283  "end\n"
1284  "return 0\n";
1285  char sig[] = "alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1286  uint8_t httpbuf1[] = "POST / HTTP/1.1\r\n"
1287  "Host: www.emergingthreats.net\r\n\r\n";
1288  uint8_t httpbuf2[] = "POST / HTTP/1.1\r\n"
1289  "Host: www.openinfosecfoundation.org\r\n\r\n";
1290  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1291  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1292  TcpSession ssn;
1293  Flow f;
1294  ThreadVars th_v;
1295  DetectEngineThreadCtx *det_ctx;
1296 
1297  ut_script = script;
1298 
1299  memset(&th_v, 0, sizeof(th_v));
1300  memset(&f, 0, sizeof(f));
1301  memset(&ssn, 0, sizeof(ssn));
1302 
1303  Packet *p1 = UTHBuildPacket(httpbuf1, httplen1, IPPROTO_TCP);
1304  Packet *p2 = UTHBuildPacket(httpbuf2, httplen2, IPPROTO_TCP);
1305 
1306  FLOW_INITIALIZE(&f);
1307  f.protoctx = (void *)&ssn;
1308  f.proto = IPPROTO_TCP;
1309  f.flags |= FLOW_IPV4;
1310  f.alproto = ALPROTO_HTTP1;
1311 
1312  p1->flow = &f;
1316  p2->flow = &f;
1320 
1321  StreamTcpInitConfig(true);
1322 
1325  de_ctx->flags |= DE_QUIET;
1326 
1328  FAIL_IF_NULL(s);
1329 
1331  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1332 
1333  /* do detect for p1 */
1334  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
1335  FAIL_IF(PacketAlertCheck(p1, 1));
1336 
1337  /* do detect for p2 */
1338  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
1339  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
1340 
1341  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_VAR);
1342  FAIL_IF(id == 0);
1343  FlowVar *fv = FlowVarGet(&f, id);
1344  FAIL_IF_NULL(fv);
1345  FAIL_IF(fv->data.fv_str.value_len != 1);
1346  FAIL_IF(memcmp(fv->data.fv_str.value, "2", 1) != 0);
1347 
1348  UTHFreePackets(&p1, 1);
1349  UTHFreePackets(&p2, 1);
1350  FLOW_DESTROY(&f);
1351 
1352  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1354  StreamTcpFreeConfig(true);
1355  StatsThreadCleanup(&th_v);
1356  PASS;
1357 }
1358 
1359 /** \test packet buffer */
1360 static int LuaMatchTest03a(void)
1361 {
1362  const char script[] = "local flowvarlib = require(\"suricata.flowvar\")\n"
1363  "function init (args)\n"
1364  " flowvarlib.register(\"cnt\")\n"
1365  " local needs = {}\n"
1366  " needs[\"packet\"] = tostring(true)\n"
1367  " return needs\n"
1368  "end\n"
1369  "\n"
1370  "function thread_init (args)\n"
1371  " cnt = flowvarlib.get(\"cnt\")\n"
1372  "end\n"
1373  "\n"
1374  "function match(args)\n"
1375  " a = cnt:value()\n"
1376  " if a then\n"
1377  " a = tostring(tonumber(a)+1)\n"
1378  " print (a)\n"
1379  " cnt:set(a, #a)\n"
1380  " else\n"
1381  " a = tostring(1)\n"
1382  " print (a)\n"
1383  " cnt:set(a, #a)\n"
1384  " end\n"
1385  " \n"
1386  " print (\"pre check: \" .. (a))\n"
1387  " if tonumber(a) == 2 then\n"
1388  " print \"match\"\n"
1389  " return 1\n"
1390  " end\n"
1391  " return 0\n"
1392  "end\n"
1393  "return 0\n";
1394  char sig[] = "alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1395  uint8_t httpbuf1[] = "POST / HTTP/1.1\r\n"
1396  "Host: www.emergingthreats.net\r\n\r\n";
1397  uint8_t httpbuf2[] = "POST / HTTP/1.1\r\n"
1398  "Host: www.openinfosecfoundation.org\r\n\r\n";
1399  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1400  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1401  TcpSession ssn;
1402  Flow f;
1403  ThreadVars th_v;
1404  DetectEngineThreadCtx *det_ctx;
1405 
1406  ut_script = script;
1407 
1408  memset(&th_v, 0, sizeof(th_v));
1409  memset(&f, 0, sizeof(f));
1410  memset(&ssn, 0, sizeof(ssn));
1411 
1412  Packet *p1 = UTHBuildPacket(httpbuf1, httplen1, IPPROTO_TCP);
1413  Packet *p2 = UTHBuildPacket(httpbuf2, httplen2, IPPROTO_TCP);
1414 
1415  FLOW_INITIALIZE(&f);
1416  f.protoctx = (void *)&ssn;
1417  f.proto = IPPROTO_TCP;
1418  f.flags |= FLOW_IPV4;
1419  f.alproto = ALPROTO_HTTP1;
1420 
1421  p1->flow = &f;
1425  p2->flow = &f;
1429 
1430  StreamTcpInitConfig(true);
1431 
1434  de_ctx->flags |= DE_QUIET;
1435 
1437  FAIL_IF_NULL(s);
1438 
1440  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1441 
1442  /* do detect for p1 */
1443  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
1444  FAIL_IF(PacketAlertCheck(p1, 1));
1445 
1446  /* do detect for p2 */
1447  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
1448  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
1449 
1450  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_VAR);
1451  FAIL_IF(id == 0);
1452  FlowVar *fv = FlowVarGet(&f, id);
1453  FAIL_IF_NULL(fv);
1454  FAIL_IF(fv->data.fv_str.value_len != 1);
1455  FAIL_IF(memcmp(fv->data.fv_str.value, "2", 1) != 0);
1456 
1457  UTHFreePackets(&p1, 1);
1458  UTHFreePackets(&p2, 1);
1459  FLOW_DESTROY(&f);
1460 
1461  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1463  StreamTcpFreeConfig(true);
1464  StatsThreadCleanup(&th_v);
1465  PASS;
1466 }
1467 
1468 /** \test http buffer, flowints */
1469 static int LuaMatchTest04(void)
1470 {
1471  const char script[] = "local flowintlib = require(\"suricata.flowint\")\n"
1472  "function init (args)\n"
1473  " flowintlib.register(\"cnt\")\n"
1474  " return {}\n"
1475  "end\n"
1476  "\n"
1477  "function thread_init (args)\n"
1478  " cnt = flowintlib.get(\"cnt\")\n"
1479  "end\n"
1480  "\n"
1481  "function match(args)\n"
1482  " print \"inspecting\""
1483  " a = cnt:value()\n"
1484  " if a then\n"
1485  " cnt:set(a + 1)\n"
1486  " else\n"
1487  " cnt:set(1)\n"
1488  " end\n"
1489  " \n"
1490  " a = cnt:value()\n"
1491  " if a == 2 then\n"
1492  " print \"match\"\n"
1493  " return 1\n"
1494  " end\n"
1495  " return 0\n"
1496  "end\n"
1497  "return 0\n";
1498  char sig[] = "alert http1:request_complete any any -> any any (flow:to_server; lua:unittest; "
1499  "sid:1;)";
1500  uint8_t httpbuf1[] = "POST / HTTP/1.1\r\n"
1501  "Host: www.emergingthreats.net\r\n\r\n";
1502  uint8_t httpbuf2[] = "POST / HTTP/1.1\r\n"
1503  "Host: www.openinfosecfoundation.org\r\n\r\n";
1504  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1505  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1506  TcpSession ssn;
1507  Flow f;
1508  ThreadVars th_v;
1509  DetectEngineThreadCtx *det_ctx;
1510 
1512 
1513  ut_script = script;
1514 
1515  memset(&th_v, 0, sizeof(th_v));
1516  memset(&f, 0, sizeof(f));
1517  memset(&ssn, 0, sizeof(ssn));
1518 
1519  Packet *p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1520  Packet *p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1521 
1522  FLOW_INITIALIZE(&f);
1523  f.protoctx = (void *)&ssn;
1524  f.proto = IPPROTO_TCP;
1525  f.flags |= FLOW_IPV4;
1526  f.alproto = ALPROTO_HTTP1;
1527 
1528  p1->flow = &f;
1532 
1533  p2->flow = &f;
1537 
1538  StreamTcpInitConfig(true);
1539 
1542  de_ctx->flags |= DE_QUIET;
1543 
1545  FAIL_IF_NULL(s);
1546 
1548  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1549 
1550  int r = AppLayerParserParse(
1551  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
1552  FAIL_IF(r != 0);
1553  HtpState *http_state = f.alstate;
1554  FAIL_IF_NULL(http_state);
1555 
1556  /* do detect for p1 */
1557  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
1558  FAIL_IF(PacketAlertCheck(p1, 1));
1559 
1560  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
1561  FAIL_IF(r != 0);
1562 
1563  /* do detect for p2 */
1564  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
1565  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
1566 
1567  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_INT);
1568  FAIL_IF(id == 0);
1569  FlowVar *fv = FlowVarGet(&f, id);
1570  FAIL_IF_NULL(fv);
1571  FAIL_IF(fv->data.fv_int.value != 2);
1572 
1573  UTHFreePackets(&p1, 1);
1574  UTHFreePackets(&p2, 1);
1575  FLOW_DESTROY(&f);
1576 
1578  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1580  StreamTcpFreeConfig(true);
1581  StatsThreadCleanup(&th_v);
1582  PASS;
1583 }
1584 
1585 /** \test http buffer, flowints */
1586 static int LuaMatchTest04a(void)
1587 {
1588  const char script[] = "local flowintlib = require(\"suricata.flowint\")\n"
1589  "function init (args)\n"
1590  " flowintlib.register(\"cnt\")\n"
1591  " return {}\n"
1592  "end\n"
1593  "\n"
1594  "function thread_init (args)\n"
1595  " cnt = flowintlib.get(\"cnt\")\n"
1596  "end\n"
1597  "\n"
1598  "function match(args)\n"
1599  " print \"inspecting\""
1600  " a = cnt:value()\n"
1601  " if a then\n"
1602  " cnt:set(a + 1)\n"
1603  " else\n"
1604  " cnt:set(1)\n"
1605  " end\n"
1606  " \n"
1607  " a = cnt:value()\n"
1608  " if a == 2 then\n"
1609  " print \"match\"\n"
1610  " return 1\n"
1611  " end\n"
1612  " return 0\n"
1613  "end\n"
1614  "return 0\n";
1615  char sig[] = "alert http1:request_complete any any -> any any (flow:to_server; lua:unittest; "
1616  "sid:1;)";
1617  uint8_t httpbuf1[] =
1618  "POST / HTTP/1.1\r\n"
1619  "Host: www.emergingthreats.net\r\n\r\n";
1620  uint8_t httpbuf2[] =
1621  "POST / HTTP/1.1\r\n"
1622  "Host: www.openinfosecfoundation.org\r\n\r\n";
1623  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1624  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1625  TcpSession ssn;
1626  Flow f;
1627  ThreadVars th_v;
1628  DetectEngineThreadCtx *det_ctx;
1629 
1631 
1632  ut_script = script;
1633 
1634  memset(&th_v, 0, sizeof(th_v));
1635  memset(&f, 0, sizeof(f));
1636  memset(&ssn, 0, sizeof(ssn));
1637 
1638  Packet *p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1639  Packet *p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1640 
1641  FLOW_INITIALIZE(&f);
1642  f.protoctx = (void *)&ssn;
1643  f.proto = IPPROTO_TCP;
1644  f.flags |= FLOW_IPV4;
1645  f.alproto = ALPROTO_HTTP1;
1646 
1647  p1->flow = &f;
1651 
1652  p2->flow = &f;
1656 
1657  StreamTcpInitConfig(true);
1658 
1661  de_ctx->flags |= DE_QUIET;
1662 
1664  FAIL_IF_NULL(s);
1665 
1667  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1668 
1669  int r = AppLayerParserParse(
1670  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
1671  FAIL_IF(r != 0);
1672  HtpState *http_state = f.alstate;
1673  FAIL_IF_NULL(http_state);
1674 
1675  /* do detect for p1 */
1676  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
1677  FAIL_IF(PacketAlertCheck(p1, 1));
1678 
1679  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
1680  FAIL_IF(r != 0);
1681 
1682  /* do detect for p2 */
1683  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
1684  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
1685 
1686  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_INT);
1687  FAIL_IF(id == 0);
1688  FlowVar *fv = FlowVarGet(&f, id);
1689  FAIL_IF_NULL(fv);
1690  FAIL_IF(fv->data.fv_int.value != 2);
1691 
1692  UTHFreePackets(&p1, 1);
1693  UTHFreePackets(&p2, 1);
1694  FLOW_DESTROY(&f);
1695 
1697  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1699  StreamTcpFreeConfig(true);
1700  StatsThreadCleanup(&th_v);
1701  PASS;
1702 }
1703 
1704 /** \test http buffer, flowints */
1705 static int LuaMatchTest05(void)
1706 {
1707  const char script[] = "local flowintlib = require(\"suricata.flowint\")\n"
1708  "function init (args)\n"
1709  " flowintlib.register(\"cnt\")\n"
1710  " return {}\n"
1711  "end\n"
1712  "\n"
1713  "function thread_init (args)\n"
1714  " cnt = flowintlib.get(\"cnt\")\n"
1715  "end\n"
1716  "\n"
1717  "function match(args)\n"
1718  " print \"inspecting\""
1719  " a = cnt:incr()\n"
1720  " if a == 2 then\n"
1721  " print \"match\"\n"
1722  " return 1\n"
1723  " end\n"
1724  " return 0\n"
1725  "end\n"
1726  "return 0\n";
1727  char sig[] = "alert http1:request_complete any any -> any any (flow:to_server; lua:unittest; "
1728  "sid:1;)";
1729  uint8_t httpbuf1[] =
1730  "POST / HTTP/1.1\r\n"
1731  "Host: www.emergingthreats.net\r\n\r\n";
1732  uint8_t httpbuf2[] =
1733  "POST / HTTP/1.1\r\n"
1734  "Host: www.openinfosecfoundation.org\r\n\r\n";
1735  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1736  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1737  TcpSession ssn;
1738  Flow f;
1739  ThreadVars th_v;
1740  DetectEngineThreadCtx *det_ctx;
1741 
1743 
1744  ut_script = script;
1745 
1746  memset(&th_v, 0, sizeof(th_v));
1747  memset(&f, 0, sizeof(f));
1748  memset(&ssn, 0, sizeof(ssn));
1749 
1750  Packet *p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1751  Packet *p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1752 
1753  FLOW_INITIALIZE(&f);
1754  f.protoctx = (void *)&ssn;
1755  f.proto = IPPROTO_TCP;
1756  f.flags |= FLOW_IPV4;
1757  f.alproto = ALPROTO_HTTP1;
1758 
1759  p1->flow = &f;
1763 
1764  p2->flow = &f;
1768 
1769  StreamTcpInitConfig(true);
1770 
1773  de_ctx->flags |= DE_QUIET;
1774 
1776  FAIL_IF_NULL(s);
1777 
1779  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1780 
1781  int r = AppLayerParserParse(
1782  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
1783  FAIL_IF(r != 0);
1784  HtpState *http_state = f.alstate;
1785  FAIL_IF_NULL(http_state);
1786 
1787  /* do detect for p1 */
1788  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
1789  FAIL_IF(PacketAlertCheck(p1, 1));
1790 
1791  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
1792  FAIL_IF(r != 0);
1793 
1794  /* do detect for p2 */
1795  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
1796  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
1797 
1798  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_INT);
1799  FAIL_IF(id == 0);
1800  FlowVar *fv = FlowVarGet(&f, id);
1801  FAIL_IF_NULL(fv);
1802  FAIL_IF(fv->data.fv_int.value != 2);
1803 
1804  UTHFreePackets(&p1, 1);
1805  UTHFreePackets(&p2, 1);
1806  FLOW_DESTROY(&f);
1807 
1809  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1811  StreamTcpFreeConfig(true);
1812  StatsThreadCleanup(&th_v);
1813  PASS;
1814 }
1815 
1816 /** \test http buffer, flowints */
1817 static int LuaMatchTest05a(void)
1818 {
1819  const char script[] = "local flowintlib = require(\"suricata.flowint\")\n"
1820  "function init (args)\n"
1821  " flowintlib.register(\"cnt\")\n"
1822  " return {}\n"
1823  "end\n"
1824  "\n"
1825  "function thread_init (args)\n"
1826  " cnt = flowintlib.get(\"cnt\")\n"
1827  "end\n"
1828  "\n"
1829  "function match(args)\n"
1830  " print \"inspecting\""
1831  " a = cnt:incr()\n"
1832  " if a == 2 then\n"
1833  " print \"match\"\n"
1834  " return 1\n"
1835  " end\n"
1836  " return 0\n"
1837  "end\n"
1838  "return 0\n";
1839  char sig[] = "alert http1:request_complete any any -> any any (flow:to_server; lua:unittest; "
1840  "sid:1;)";
1841  uint8_t httpbuf1[] =
1842  "POST / HTTP/1.1\r\n"
1843  "Host: www.emergingthreats.net\r\n\r\n";
1844  uint8_t httpbuf2[] =
1845  "POST / HTTP/1.1\r\n"
1846  "Host: www.openinfosecfoundation.org\r\n\r\n";
1847  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1848  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1849  TcpSession ssn;
1850  Flow f;
1851  ThreadVars th_v;
1852  DetectEngineThreadCtx *det_ctx;
1853 
1855 
1856  ut_script = script;
1857 
1858  memset(&th_v, 0, sizeof(th_v));
1859  memset(&f, 0, sizeof(f));
1860  memset(&ssn, 0, sizeof(ssn));
1861 
1862  Packet *p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1863  Packet *p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1864 
1865  FLOW_INITIALIZE(&f);
1866  f.protoctx = (void *)&ssn;
1867  f.proto = IPPROTO_TCP;
1868  f.flags |= FLOW_IPV4;
1869  f.alproto = ALPROTO_HTTP1;
1870 
1871  p1->flow = &f;
1875 
1876  p2->flow = &f;
1880 
1881  StreamTcpInitConfig(true);
1882 
1885  de_ctx->flags |= DE_QUIET;
1886 
1888  FAIL_IF_NULL(s);
1889 
1891  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1892 
1893  int r = AppLayerParserParse(
1894  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
1895  FAIL_IF(r != 0);
1896  HtpState *http_state = f.alstate;
1897  FAIL_IF_NULL(http_state);
1898 
1899  /* do detect for p1 */
1900  SCLogInfo("p1");
1901  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
1902  FAIL_IF(PacketAlertCheck(p1, 1));
1903 
1904  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
1905  FAIL_IF(r != 0);
1906  /* do detect for p2 */
1907  SCLogInfo("p2");
1908  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
1909 
1910  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
1911 
1912  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_INT);
1913  FAIL_IF(id == 0);
1914  FlowVar *fv = FlowVarGet(&f, id);
1915  FAIL_IF_NULL(fv);
1916  FAIL_IF(fv->data.fv_int.value != 2);
1917 
1918  UTHFreePackets(&p1, 1);
1919  UTHFreePackets(&p2, 1);
1920  FLOW_DESTROY(&f);
1921 
1923  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1925  StreamTcpFreeConfig(true);
1926  StatsThreadCleanup(&th_v);
1927  PASS;
1928 }
1929 
1930 /** \test http buffer, flowints */
1931 static int LuaMatchTest06(void)
1932 {
1933  const char script[] = "local flowintlib = require(\"suricata.flowint\")\n"
1934  "function init (args)\n"
1935  " flowintlib.register(\"cnt\")\n"
1936  " return {}\n"
1937  "end\n"
1938  "\n"
1939  "function thread_init (args)\n"
1940  " cnt = flowintlib.get(\"cnt\")\n"
1941  "end\n"
1942  "\n"
1943  "function match(args)\n"
1944  " print \"inspecting\""
1945  " a = cnt:value()\n"
1946  " if a == nil then\n"
1947  " print \"new var set to 2\""
1948  " cnt:set(2)\n"
1949  " end\n"
1950  " a = cnt:decr()\n"
1951  " if a == 0 then\n"
1952  " print \"match\"\n"
1953  " return 1\n"
1954  " end\n"
1955  " return 0\n"
1956  "end\n"
1957  "return 0\n";
1958  char sig[] = "alert http1:request_complete any any -> any any (flow:to_server; lua:unittest; "
1959  "sid:1;)";
1960  uint8_t httpbuf1[] =
1961  "POST / HTTP/1.1\r\n"
1962  "Host: www.emergingthreats.net\r\n\r\n";
1963  uint8_t httpbuf2[] =
1964  "POST / HTTP/1.1\r\n"
1965  "Host: www.openinfosecfoundation.org\r\n\r\n";
1966  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1967  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1968  TcpSession ssn;
1969  Flow f;
1970  ThreadVars th_v;
1971  DetectEngineThreadCtx *det_ctx;
1972 
1974 
1975  ut_script = script;
1976 
1977  memset(&th_v, 0, sizeof(th_v));
1978  memset(&f, 0, sizeof(f));
1979  memset(&ssn, 0, sizeof(ssn));
1980 
1981  Packet *p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1982  Packet *p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1983 
1984  FLOW_INITIALIZE(&f);
1985  f.protoctx = (void *)&ssn;
1986  f.proto = IPPROTO_TCP;
1987  f.flags |= FLOW_IPV4;
1988  f.alproto = ALPROTO_HTTP1;
1989 
1990  p1->flow = &f;
1994 
1995  p2->flow = &f;
1999 
2000  StreamTcpInitConfig(true);
2001 
2004  de_ctx->flags |= DE_QUIET;
2005 
2007  FAIL_IF_NULL(s);
2008 
2010  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
2011 
2012  int r = AppLayerParserParse(
2013  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
2014  FAIL_IF(r != 0);
2015  HtpState *http_state = f.alstate;
2016  FAIL_IF_NULL(http_state);
2017 
2018  /* do detect for p1 */
2019  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
2020  FAIL_IF(PacketAlertCheck(p1, 1));
2021 
2022  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
2023  FAIL_IF(r != 0);
2024 
2025  /* do detect for p2 */
2026  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
2027  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
2028 
2029  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_INT);
2030  FAIL_IF(id == 0);
2031  FlowVar *fv = FlowVarGet(&f, id);
2032  FAIL_IF_NULL(fv);
2033  FAIL_IF(fv->data.fv_int.value != 0);
2034 
2035  UTHFreePackets(&p1, 1);
2036  UTHFreePackets(&p2, 1);
2037  FLOW_DESTROY(&f);
2038 
2040  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
2042  StreamTcpFreeConfig(true);
2043  StatsThreadCleanup(&th_v);
2044  PASS;
2045 }
2046 
2047 /** \test http buffer, flowints */
2048 static int LuaMatchTest06a(void)
2049 {
2050  const char script[] = "local flowintlib = require(\"suricata.flowint\")\n"
2051  "function init (args)\n"
2052  " flowintlib.register(\"cnt\")\n"
2053  " return {}\n"
2054  "end\n"
2055  "\n"
2056  "function thread_init (args)\n"
2057  " cnt = flowintlib.get(\"cnt\")\n"
2058  "end\n"
2059  "\n"
2060  "function match(args)\n"
2061  " print \"inspecting\""
2062  " a = cnt:value()\n"
2063  " if a == nil then\n"
2064  " print \"new var set to 2\""
2065  " cnt:set(2)\n"
2066  " end\n"
2067  " a = cnt:decr()\n"
2068  " if a == 0 then\n"
2069  " print \"match\"\n"
2070  " return 1\n"
2071  " end\n"
2072  " return 0\n"
2073  "end\n"
2074  "return 0\n";
2075  char sig[] = "alert http1:request_complete any any -> any any (flow:to_server; lua:unittest; "
2076  "sid:1;)";
2077  uint8_t httpbuf1[] =
2078  "POST / HTTP/1.1\r\n"
2079  "Host: www.emergingthreats.net\r\n\r\n";
2080  uint8_t httpbuf2[] =
2081  "POST / HTTP/1.1\r\n"
2082  "Host: www.openinfosecfoundation.org\r\n\r\n";
2083  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
2084  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
2085  TcpSession ssn;
2086  Flow f;
2087  ThreadVars th_v;
2088  DetectEngineThreadCtx *det_ctx;
2089 
2091 
2092  ut_script = script;
2093 
2094  memset(&th_v, 0, sizeof(th_v));
2095  memset(&f, 0, sizeof(f));
2096  memset(&ssn, 0, sizeof(ssn));
2097 
2098  Packet *p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
2099  Packet *p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
2100 
2101  FLOW_INITIALIZE(&f);
2102  f.protoctx = (void *)&ssn;
2103  f.proto = IPPROTO_TCP;
2104  f.flags |= FLOW_IPV4;
2105  f.alproto = ALPROTO_HTTP1;
2106 
2107  p1->flow = &f;
2111 
2112  p2->flow = &f;
2116 
2117  StreamTcpInitConfig(true);
2118 
2121  de_ctx->flags |= DE_QUIET;
2122 
2124  FAIL_IF_NULL(s);
2125 
2127  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
2128 
2129  int r = AppLayerParserParse(
2130  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
2131  FAIL_IF(r != 0);
2132  HtpState *http_state = f.alstate;
2133  FAIL_IF_NULL(http_state);
2134 
2135  /* do detect for p1 */
2136  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
2137  FAIL_IF(PacketAlertCheck(p1, 1));
2138 
2139  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
2140  FAIL_IF(r != 0);
2141 
2142  /* do detect for p2 */
2143  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
2144  FAIL_IF_NOT(PacketAlertCheck(p2, 1));
2145 
2146  uint32_t id = VarNameStoreLookupByName("cnt", VAR_TYPE_FLOW_INT);
2147  FAIL_IF(id == 0);
2148  FlowVar *fv = FlowVarGet(&f, id);
2149  FAIL_IF_NULL(fv);
2150  FAIL_IF(fv->data.fv_int.value != 0);
2151 
2152  UTHFreePackets(&p1, 1);
2153  UTHFreePackets(&p2, 1);
2154  FLOW_DESTROY(&f);
2155 
2157  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
2159  StreamTcpFreeConfig(true);
2160  StatsThreadCleanup(&th_v);
2161  PASS;
2162 }
2163 
2164 void DetectLuaRegisterTests(void)
2165 {
2166  UtRegisterTest("LuaMatchTest01", LuaMatchTest01);
2167  UtRegisterTest("LuaMatchTest01a", LuaMatchTest01a);
2168  UtRegisterTest("LuaMatchTest02", LuaMatchTest02);
2169  UtRegisterTest("LuaMatchTest02a", LuaMatchTest02a);
2170  UtRegisterTest("LuaMatchTest03", LuaMatchTest03);
2171  UtRegisterTest("LuaMatchTest03a", LuaMatchTest03a);
2172  UtRegisterTest("LuaMatchTest04", LuaMatchTest04);
2173  UtRegisterTest("LuaMatchTest04a", LuaMatchTest04a);
2174  UtRegisterTest("LuaMatchTest05", LuaMatchTest05);
2175  UtRegisterTest("LuaMatchTest05a", LuaMatchTest05a);
2176  UtRegisterTest("LuaMatchTest06", LuaMatchTest06);
2177  UtRegisterTest("LuaMatchTest06a", LuaMatchTest06a);
2178 }
2179 #endif
FLAG_MEMORY_LIMIT_LOGGED
#define FLAG_MEMORY_LIMIT_LOGGED
Definition: detect-lua.c:121
util-byte.h
DetectLuaData::bytevars
uint16_t bytevars
Definition: detect-lua.h:54
DetectLuaData
Definition: detect-lua.h:44
SigTableElmt_::url
const char * url
Definition: detect.h:1460
SCLuaSbState::memory_limit_error
bool memory_limit_error
Definition: util-lua-sandbox.h:56
detect-engine.h
LuaStateSetThreadVars
void LuaStateSetThreadVars(lua_State *luastate, ThreadVars *tv)
Definition: util-lua.c:110
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
StatsIncr
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:166
SigTableElmt_::desc
const char * desc
Definition: detect.h:1459
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1268
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
util-lua-common.h
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1444
SignatureHook_::sm_list
int sm_list
Definition: detect.h:573
FLAG_BLOCKED_FUNCTION_LOGGED
#define FLAG_BLOCKED_FUNCTION_LOGGED
Definition: detect-lua.c:119
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1457
stream-tcp.h
DetectThreadCtxGetKeywordThreadCtx
void * DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3724
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:39
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:279
DetectLuaDataBytevarEntry_::name
char * name
Definition: detect-lua.h:40
DetectLuaData::flags
uint32_t flags
Definition: detect-lua.h:48
SCLuaSbGetContext
SCLuaSbState * SCLuaSbGetContext(lua_State *L)
Definition: util-lua-sandbox.c:352
Flow_::proto
uint8_t proto
Definition: flow.h:370
util-lua.h
ALPROTO_QUIC
@ ALPROTO_QUIC
Definition: app-layer-protos.h:57
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:142
FlowVarTypeStr::value_len
uint16_t value_len
Definition: flow-var.h:41
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:368
Packet_::flags
uint32_t flags
Definition: decode.h:544
SCLuaSbState
Definition: util-lua-sandbox.h:40
Flow_
Flow data structure.
Definition: flow.h:348
DetectLuaData::allow_restricted_functions
int allow_restricted_functions
Definition: detect-lua.h:58
FLAG_INSTRUCTION_LIMIT_LOGGED
#define FLAG_INSTRUCTION_LIMIT_LOGGED
Definition: detect-lua.c:120
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:932
DetectEngineThreadCtx_::lua_blocked_function_errors
uint16_t lua_blocked_function_errors
Definition: detect.h:1377
DEFAULT_LUA_INSTRUCTION_LIMIT
#define DEFAULT_LUA_INSTRUCTION_LIMIT
Definition: detect-lua.c:124
FlowVar_::fv_str
FlowVarTypeStr fv_str
Definition: flow-var.h:64
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2634
detect-lua.h
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1422
DETECT_LUA_MAX_FLOWVARS
#define DETECT_LUA_MAX_FLOWVARS
Definition: detect-lua.h:35
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:324
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:225
util-var-name.h
TLS_STATE_SERVER_HELLO_DONE
@ TLS_STATE_SERVER_HELLO_DONE
Definition: app-layer-ssl.h:89
SCConfGetBool
int SCConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
Definition: conf.c:497
DE_QUIET
#define DE_QUIET
Definition: detect.h:330
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:365
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:2416
DetectLuaData::flowvar
uint32_t flowvar[DETECT_LUA_MAX_FLOWVARS]
Definition: detect-lua.h:53
util-lua-builtins.h
VarNameStoreRegister
uint32_t VarNameStoreRegister(const char *name, const enum VarTypes type)
Definition: util-var-name.c:155
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3439
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:532
DetectEngineThreadCtx_::lua_instruction_limit_errors
uint16_t lua_instruction_limit_errors
Definition: detect.h:1380
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:272
Flow_::protoctx
void * protoctx
Definition: flow.h:433
SigMatchData_
Data needed for Match()
Definition: detect.h:365
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1439
FLAG_DATATYPE_PACKET
#define FLAG_DATATYPE_PACKET
Definition: detect-lua.c:112
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:100
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:606
util-unittest.h
HtpState_
Definition: app-layer-htp.h:181
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
DetectLuaThreadData::flags
uint32_t flags
Definition: detect-lua.h:32
DetectLuaThreadData::luastate
lua_State * luastate
Definition: detect-lua.h:31
lua_State
struct lua_State lua_State
Definition: suricata-common.h:523
DetectEngineThreadCtx_::lua_rule_errors
uint16_t lua_rule_errors
Definition: detect.h:1374
FlowVar_::fv_int
FlowVarTypeInt fv_int
Definition: flow-var.h:65
DetectLuaData::buffername
char * buffername
Definition: detect-lua.h:49
StreamTcpInitConfig
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
Definition: stream-tcp.c:496
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:38
DetectLuaData::negated
int negated
Definition: detect-lua.h:46
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:271
app-layer-htp.h
FLAG_LIST_JA3S
#define FLAG_LIST_JA3S
Definition: detect-lua.c:116
VarNameStoreLookupByName
uint32_t VarNameStoreLookupByName(const char *name, const enum VarTypes type)
find name for id+type at packet time. As the active store won't be modified, we don't need locks.
Definition: util-var-name.c:326
decode.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1244
DetectLuaData::thread_ctx_id
int thread_ctx_id
Definition: detect-lua.h:45
DetectLuaData::instruction_limit
uint64_t instruction_limit
Definition: detect-lua.h:57
TLS_STATE_CLIENT_HELLO_DONE
@ TLS_STATE_CLIENT_HELLO_DONE
Definition: app-layer-ssl.h:79
SCConfGetInt
int SCConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:414
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:23
DETECT_LUA
@ DETECT_LUA
Definition: detect-engine-register.h:92
SignatureInitData_::list
int list
Definition: detect.h:628
SCEnter
#define SCEnter(...)
Definition: util-debug.h:281
detect-engine-mpm.h
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:388
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DetectLuaRegister
void DetectLuaRegister(void)
Registration function for keyword: lua.
Definition: detect-lua.c:83
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3364
VarNameStoreUnregister
void VarNameStoreUnregister(const uint32_t id, const enum VarTypes type)
Definition: util-var-name.c:204
DETECT_LUA_MAX_FLOWINTS
#define DETECT_LUA_MAX_FLOWINTS
Definition: detect-lua.h:36
FLAG_DATATYPE_PAYLOAD
#define FLAG_DATATYPE_PAYLOAD
Definition: detect-lua.c:113
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:259
app-layer-parser.h
DetectLuaMatchBuffer
int DetectLuaMatchBuffer(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, const uint8_t *buffer, uint32_t buffer_len, uint32_t offset, Flow *f)
Definition: detect-lua.c:240
SignatureInitData_::hook
SignatureHook hook
Definition: detect.h:590
SIGNATURE_HOOK_TYPE_NOT_SET
@ SIGNATURE_HOOK_TYPE_NOT_SET
Definition: detect.h:547
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
DetectEngineThreadCtx_::lua_memory_limit_errors
uint16_t lua_memory_limit_errors
Definition: detect.h:1383
DetectLuaData::bytevar
DetectLuaDataBytevarEntry bytevar[DETECT_LUA_MAX_BYTEVARS]
Definition: detect-lua.h:55
DetectLuaData::alloc_limit
uint64_t alloc_limit
Definition: detect-lua.h:56
DetectLuaData::flowints
uint16_t flowints
Definition: detect-lua.h:51
Packet_
Definition: decode.h:501
detect-engine-build.h
type
uint16_t type
Definition: decode-vlan.c:106
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:208
FLAG_DATATYPE_STREAM
#define FLAG_DATATYPE_STREAM
Definition: detect-lua.c:114
SCLuaSbStateClose
void SCLuaSbStateClose(lua_State *L)
Definition: util-lua-sandbox.c:361
DetectLuaData::flowint
uint32_t flowint[DETECT_LUA_MAX_FLOWINTS]
Definition: detect-lua.h:50
detect-engine-alert.h
conf.h
FLAG_ERROR_LOGGED
#define FLAG_ERROR_LOGGED
Definition: detect-lua.c:118
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:747
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1419
detect-byte.h
SCLuaRequirefBuiltIns
void SCLuaRequirefBuiltIns(lua_State *L)
Register Suricata built-in modules for loading in a non-sandboxed environment.
Definition: util-lua-builtins.c:85
SCLuaSbState::blocked_function_error
bool blocked_function_error
Definition: util-lua-sandbox.h:54
DetectLuaThreadData
Definition: detect-lua.h:30
FLOW_PKT_TOCLIENT
#define FLOW_PKT_TOCLIENT
Definition: flow.h:226
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:47
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:229
LuaDumpStack
void LuaDumpStack(lua_State *state, const char *prefix)
dump stack from lua state to screen
Definition: detect-lua.c:127
FlowVarTypeInt_::value
uint32_t value
Definition: flow-var.h:46
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2194
SCConfSetFinal
int SCConfSetFinal(const char *name, const char *val)
Set a final configuration value.
Definition: conf.c:318
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:297
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
util-lua-sandbox.h
SCLuaSbLoadLibs
void SCLuaSbLoadLibs(lua_State *L)
Definition: util-lua-sandbox.c:280
FlowVarTypeStr::value
uint8_t * value
Definition: flow-var.h:40
Packet_::flow
struct Flow_ * flow
Definition: decode.h:546
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1214
StreamTcpFreeConfig
void StreamTcpFreeConfig(bool quiet)
Definition: stream-tcp.c:867
flags
uint8_t flags
Definition: decode-gre.h:0
DetectRegisterThreadCtxFuncs
int DetectRegisterThreadCtxFuncs(DetectEngineCtx *de_ctx, const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *), int mode)
Register Thread keyword context Funcs.
Definition: detect-engine.c:3654
AppLayerParserParse
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
Definition: app-layer-parser.c:1277
detect-lua-extensions.h
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
DEFAULT_LUA_ALLOC_LIMIT
#define DEFAULT_LUA_ALLOC_LIMIT
Definition: detect-lua.c:123
SignatureHook_::type
enum SignatureHookType type
Definition: detect.h:572
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3596
detect-engine-buffer.h
SCLuaSbResetInstructionCounter
void SCLuaSbResetInstructionCounter(lua_State *L)
Definition: util-lua-sandbox.c:388
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1946
FlowVar_::data
union FlowVar_::@116 data
DetectLuaData::flowvars
uint16_t flowvars
Definition: detect-lua.h:52
luaext_key_ld
const char luaext_key_ld[]
Definition: detect-lua-extensions.c:45
str
#define str(s)
Definition: suricata-common.h:308
DetectEngineThreadCtx_::tv
ThreadVars * tv
Definition: detect.h:1252
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:271
FLAG_DATATYPE_BUFFER
#define FLAG_DATATYPE_BUFFER
Definition: detect-lua.c:117
SCFree
#define SCFree(p)
Definition: util-mem.h:61
Flow_::alstate
void * alstate
Definition: flow.h:471
Flow_::flags
uint32_t flags
Definition: flow.h:413
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
VAR_TYPE_FLOW_VAR
@ VAR_TYPE_FLOW_VAR
Definition: util-var.h:39
VAR_TYPE_FLOW_INT
@ VAR_TYPE_FLOW_INT
Definition: util-var.h:37
FLOW_PKT_ESTABLISHED
#define FLOW_PKT_ESTABLISHED
Definition: flow.h:227
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2595
SCLuaSbState::instruction_count_error
bool instruction_count_error
Definition: util-lua-sandbox.h:55
DetectLoadCompleteSigPath
char * DetectLoadCompleteSigPath(const DetectEngineCtx *de_ctx, const char *sig_file)
Create the path if default-rule-path was specified.
Definition: detect-engine-loader.c:106
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:273
SCLuaSbStateNew
lua_State * SCLuaSbStateNew(uint64_t alloclimit, uint64_t instructionlimit)
Allocate a new Lua sandbox.
Definition: util-lua-sandbox.c:320
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:934
FlowVarGet
FlowVar * FlowVarGet(Flow *f, uint32_t idx)
get the flowvar with index 'idx' from the flow
Definition: flow-var.c:84
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:60
DetectUnregisterThreadCtxFuncs
int DetectUnregisterThreadCtxFuncs(DetectEngineCtx *de_ctx, void *data, const char *name)
Remove Thread keyword context registration.
Definition: detect-engine.c:3706
TcpSession_
Definition: stream-tcp-private.h:283
flow.h
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:442
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:285
DetectBufferGetActiveList
int DetectBufferGetActiveList(DetectEngineCtx *de_ctx, Signature *s)
Definition: detect-engine-buffer.c:109
StatsThreadCleanup
void StatsThreadCleanup(ThreadVars *tv)
Definition: counters.c:1324
flow-var.h
DetectLuaData::filename
char * filename
Definition: detect-lua.h:47
FlowVar_
Definition: flow-var.h:55
app-layer-ssl.h
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:119
PKT_STREAM_EST
#define PKT_STREAM_EST
Definition: decode.h:1264
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1446
LuaPushStringBuffer
int LuaPushStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)
Definition: util-lua.c:319
app-layer.h
UTHFreePackets
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:456
FLAG_LIST_JA3
#define FLAG_LIST_JA3
Definition: detect-lua.c:115