Go to the documentation of this file.
67 void *state,
void *txv,
const Signature *s,
71 static void DetectLuaRegisterTests(
void);
74 static int g_smtp_generic_list_id = 0;
102 #define FLAG_DATATYPE_PACKET BIT_U32(0)
103 #define FLAG_DATATYPE_PAYLOAD BIT_U32(1)
104 #define FLAG_DATATYPE_STREAM BIT_U32(2)
105 #define FLAG_DATATYPE_HTTP_URI BIT_U32(3)
106 #define FLAG_DATATYPE_HTTP_URI_RAW BIT_U32(4)
107 #define FLAG_DATATYPE_HTTP_REQUEST_HEADERS BIT_U32(5)
108 #define FLAG_DATATYPE_HTTP_REQUEST_HEADERS_RAW BIT_U32(6)
109 #define FLAG_DATATYPE_HTTP_REQUEST_COOKIE BIT_U32(7)
110 #define FLAG_DATATYPE_HTTP_REQUEST_UA BIT_U32(8)
111 #define FLAG_DATATYPE_HTTP_REQUEST_LINE BIT_U32(9)
112 #define FLAG_DATATYPE_HTTP_REQUEST_BODY BIT_U32(10)
113 #define FLAG_DATATYPE_HTTP_RESPONSE_COOKIE BIT_U32(11)
114 #define FLAG_DATATYPE_HTTP_RESPONSE_BODY BIT_U32(12)
115 #define FLAG_DATATYPE_HTTP_RESPONSE_HEADERS BIT_U32(13)
116 #define FLAG_DATATYPE_HTTP_RESPONSE_HEADERS_RAW BIT_U32(14)
117 #define FLAG_DATATYPE_DNS_RRNAME BIT_U32(15)
118 #define FLAG_DATATYPE_DNS_REQUEST BIT_U32(16)
119 #define FLAG_DATATYPE_DNS_RESPONSE BIT_U32(17)
120 #define FLAG_DATATYPE_SSH BIT_U32(19)
121 #define FLAG_DATATYPE_SMTP BIT_U32(20)
122 #define FLAG_DATATYPE_DNP3 BIT_U32(21)
123 #define FLAG_DATATYPE_BUFFER BIT_U32(22)
124 #define FLAG_ERROR_LOGGED BIT_U32(23)
125 #define FLAG_BLOCKED_FUNCTION_LOGGED BIT_U32(24)
126 #define FLAG_INSTRUCTION_LIMIT_LOGGED BIT_U32(25)
127 #define FLAG_MEMORY_LIMIT_LOGGED BIT_U32(26)
129 #define DEFAULT_LUA_ALLOC_LIMIT 500000
130 #define DEFAULT_LUA_INSTRUCTION_LIMIT 500000
135 int size = lua_gettop(state);
136 printf(
"%s: size %d\n", prefix, size);
138 for (
int i = 1; i <= size; i++) {
139 int type = lua_type(state, i);
140 printf(
"- %s: Stack size=%d, level=%d, type=%d, ", prefix, size, i,
type);
144 printf(
"function %s", lua_tostring(state, i));
147 printf(
"bool %s", lua_toboolean(state, i) ?
"true" :
"false");
150 printf(
"number %g", lua_tonumber(state, i));
153 printf(
"string `%s'", lua_tostring(state, i));
156 printf(
"table `%s'", lua_tostring(state, i));
159 printf(
"other %s", lua_typename(state,
type));
171 static int DetectLuaRunMatch(
177 if (lua_pcall(tlua->
luastate, 1, 1, 0) != 0) {
178 const char *reason = lua_tostring(tlua->
luastate, -1);
189 reason =
"memory limit exceeded";
197 if (!(tlua->
flags & flag)) {
198 SCLogWarning(
"Lua script failed to run successfully: %s", reason);
203 while (lua_gettop(tlua->
luastate) > 0) {
212 if (lua_gettop(tlua->
luastate) > 0) {
214 if (lua_type(tlua->
luastate, 1) == LUA_TNUMBER) {
215 double script_ret = lua_tonumber(tlua->
luastate, 1);
219 if (script_ret == 1.0)
223 }
else if (lua_type(tlua->
luastate, 1) == LUA_TTABLE) {
226 while (lua_next(tlua->
luastate, -2)) {
227 v = lua_tostring(tlua->
luastate, -1);
229 k = lua_tostring(tlua->
luastate, -1);
236 if (strcmp(k,
"retval") == 0) {
240 "for \"retval\" from LUA return table: '%s'",
264 while (lua_gettop(tlua->
luastate) > 0) {
277 if (buffer == NULL || buffer_len == 0)
292 lua_getglobal(tlua->
luastate,
"match");
295 lua_pushliteral(tlua->
luastate,
"offset");
303 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
333 flags = STREAM_TOSERVER;
335 flags = STREAM_TOCLIENT;
354 lua_getglobal(tlua->
luastate,
"match");
359 if (htp_state != NULL && htp_state->
connp != NULL) {
364 for ( ; idx < total_txs; idx++) {
371 lua_pushliteral(tlua->
luastate,
"http.request_line");
381 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
406 lua_getglobal(tlua->
luastate,
"match");
411 if (htp_state != NULL && htp_state->
connp != NULL) {
417 lua_pushliteral(tlua->
luastate,
"http.request_line");
427 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
443 void *state,
void *txv,
const Signature *s,
446 return DetectLuaAppMatchCommon(det_ctx, f,
flags, state, s,
ctx);
452 static const char *ut_script = NULL;
455 static void *DetectLuaThreadInit(
void *data)
485 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
sid));
486 lua_setglobal(t->
luastate,
"SCRuleSid");
487 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
rev));
488 lua_setglobal(t->
luastate,
"SCRuleRev");
489 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
gid));
490 lua_setglobal(t->
luastate,
"SCRuleGid");
494 if (ut_script != NULL) {
495 status = luaL_loadbuffer(t->
luastate, ut_script, strlen(ut_script),
"unittest");
512 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
518 lua_getglobal(t->
luastate,
"thread_init");
519 if (lua_isfunction(t->
luastate, -1)) {
520 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
521 SCLogError(
"couldn't run script 'thread_init' function: %s",
538 static void DetectLuaThreadFree(
void *
ctx)
566 if (strlen(
str) &&
str[0] ==
'!') {
581 DetectLuaFree(
de_ctx, lua);
590 if (luastate == NULL)
593 luaL_openlibs(luastate);
601 if (ut_script != NULL) {
602 status = luaL_loadbuffer(luastate, ut_script, strlen(ut_script),
"unittest");
604 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
609 status = luaL_loadfile(luastate, ld->
filename);
611 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
619 if (lua_pcall(luastate, 0, 0, 0) != 0) {
620 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
624 lua_getglobal(luastate,
"init");
625 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
630 lua_newtable(luastate);
631 if (lua_gettop(luastate) == 0 || lua_type(luastate, 2) != LUA_TTABLE) {
636 lua_pushliteral(luastate,
"script_api_ver");
637 lua_pushnumber (luastate, 1);
638 lua_settable(luastate, -3);
640 if (lua_pcall(luastate, 1, 1, 0) != 0) {
641 SCLogError(
"couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
646 if (lua_gettop(luastate) == 0) {
647 SCLogError(
"init function in script should return table, nothing returned");
650 if (lua_type(luastate, 1) != LUA_TTABLE) {
651 SCLogError(
"init function in script should return table, returned is not table");
655 lua_pushnil(luastate);
657 while (lua_next(luastate, -2)) {
658 k = lua_tostring(luastate, -2);
663 if (strcmp(k,
"flowvar") == 0) {
664 if (lua_istable(luastate, -1)) {
665 lua_pushnil(luastate);
666 while (lua_next(luastate, -2) != 0) {
668 const char *value = lua_tostring(luastate, -1);
671 lua_pop(luastate, 1);
683 lua_pop(luastate, 1);
685 }
else if (strcmp(k,
"flowint") == 0) {
686 if (lua_istable(luastate, -1)) {
687 lua_pushnil(luastate);
688 while (lua_next(luastate, -2) != 0) {
690 const char *value = lua_tostring(luastate, -1);
693 lua_pop(luastate, 1);
705 lua_pop(luastate, 1);
707 }
else if (strcmp(k,
"bytevar") == 0) {
708 if (lua_istable(luastate, -1)) {
709 lua_pushnil(luastate);
710 while (lua_next(luastate, -2) != 0) {
712 const char *value = lua_tostring(luastate, -1);
715 lua_pop(luastate, 1);
724 SCLogError(
"Unknown byte_extract or byte_math var "
725 "requested by lua script - %s",
733 lua_pop(luastate, 1);
737 v = lua_tostring(luastate, -1);
738 lua_pop(luastate, 1);
743 if (strcmp(k,
"packet") == 0 && strcmp(v,
"true") == 0) {
745 }
else if (strcmp(k,
"payload") == 0 && strcmp(v,
"true") == 0) {
747 }
else if (strcmp(k,
"buffer") == 0 && strcmp(v,
"true") == 0) {
755 }
else if (strcmp(k,
"stream") == 0 && strcmp(v,
"true") == 0) {
764 }
else if (strncmp(k,
"http", 4) == 0 && strcmp(v,
"true") == 0) {
767 "can just inspect script against one app layer proto like HTTP at a time");
770 if (ld->
flags != 0) {
771 SCLogError(
"when inspecting HTTP buffers only a single buffer can be inspected");
778 if (strcmp(k,
"http.uri") == 0)
781 else if (strcmp(k,
"http.uri.raw") == 0)
784 else if (strcmp(k,
"http.request_line") == 0)
787 else if (strcmp(k,
"http.request_headers") == 0)
790 else if (strcmp(k,
"http.request_headers.raw") == 0)
793 else if (strcmp(k,
"http.request_cookie") == 0)
796 else if (strcmp(k,
"http.request_user_agent") == 0)
799 else if (strcmp(k,
"http.request_body") == 0)
802 else if (strcmp(k,
"http.response_body") == 0)
805 else if (strcmp(k,
"http.response_cookie") == 0)
808 else if (strcmp(k,
"http.response_headers") == 0)
811 else if (strcmp(k,
"http.response_headers.raw") == 0)
815 SCLogError(
"unsupported http data type %s", k);
824 }
else if (strncmp(k,
"dns", 3) == 0 && strcmp(v,
"true") == 0) {
828 if (strcmp(k,
"dns.rrname") == 0)
830 else if (strcmp(k,
"dns.request") == 0)
832 else if (strcmp(k,
"dns.response") == 0)
836 SCLogError(
"unsupported dns data type %s", k);
844 }
else if (strncmp(k,
"tls", 3) == 0 && strcmp(v,
"true") == 0) {
848 }
else if (strncmp(k,
"ssh", 3) == 0 && strcmp(v,
"true") == 0) {
854 }
else if (strncmp(k,
"smtp", 4) == 0 && strcmp(v,
"true") == 0) {
860 }
else if (strncmp(k,
"dnp3", 4) == 0 && strcmp(v,
"true") == 0) {
873 lua_pop(luastate, 1);
897 (void)
ConfGetBool(
"security.lua.allow-rules", &enabled);
899 SCLogError(
"Lua rules disabled by security configuration: security.lua.allow-rules");
910 (void)
ConfGetInt(
"security.lua.max-bytes", &lua_alloc_limit);
911 (void)
ConfGetInt(
"security.lua.max-instructions", &lua_instruction_limit);
915 int allow_restricted_functions = 0;
916 (void)
ConfGetBool(
"security.lua.allow-restricted-functions", &allow_restricted_functions);
919 if (DetectLuaSetupPrime(
de_ctx, lua, s) == -1) {
924 DetectLuaThreadInit, (
void *)lua,
925 DetectLuaThreadFree, 0);
969 }
else if (lua->
flags &
991 list = g_smtp_generic_list_id;
1012 DetectLuaFree(
de_ctx, lua);
1052 for (uint16_t i = 0; i < lua->
flowints; i++) {
1055 for (uint16_t i = 0; i < lua->
flowvars; i++) {
1069 static int LuaMatchTest01(
void)
1073 const char script[] =
1074 "function init (args)\n"
1075 " local needs = {}\n"
1076 " needs[\"http.request_headers\"] = tostring(true)\n"
1077 " needs[\"flowvar\"] = {\"cnt\"}\n"
1081 "function match(args)\n"
1082 " a = ScFlowvarGet(0)\n"
1084 " a = tostring(tonumber(a)+1)\n"
1086 " ScFlowvarSet(0, a, #a)\n"
1088 " a = tostring(1)\n"
1090 " ScFlowvarSet(0, a, #a)\n"
1093 " print (\"pre check: \" .. (a))\n"
1094 " if tonumber(a) == 2 then\n"
1095 " print \"match\"\n"
1101 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1102 uint8_t httpbuf1[] =
1103 "POST / HTTP/1.1\r\n"
1104 "Host: www.emergingthreats.net\r\n\r\n";
1105 uint8_t httpbuf2[] =
1106 "POST / HTTP/1.1\r\n"
1107 "Host: www.openinfosecfoundation.org\r\n\r\n";
1108 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1109 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1119 memset(&th_v, 0,
sizeof(th_v));
1120 memset(&f, 0,
sizeof(f));
1121 memset(&ssn, 0,
sizeof(ssn));
1128 f.
proto = IPPROTO_TCP;
1194 static int LuaMatchTest01a(
void)
1196 const char script[] =
"function init (args)\n"
1197 " local needs = {}\n"
1198 " needs[\"http.request_headers\"] = tostring(true)\n"
1199 " needs[\"flowvar\"] = {\"cnt\"}\n"
1203 "function match(args)\n"
1204 " a = SCFlowvarGet(0)\n"
1206 " a = tostring(tonumber(a)+1)\n"
1208 " SCFlowvarSet(0, a, #a)\n"
1210 " a = tostring(1)\n"
1212 " SCFlowvarSet(0, a, #a)\n"
1215 " print (\"pre check: \" .. (a))\n"
1216 " if tonumber(a) == 2 then\n"
1217 " print \"match\"\n"
1223 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1224 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1225 "Host: www.emergingthreats.net\r\n\r\n";
1226 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1227 "Host: www.openinfosecfoundation.org\r\n\r\n";
1228 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1229 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1239 memset(&th_v, 0,
sizeof(th_v));
1240 memset(&f, 0,
sizeof(f));
1241 memset(&ssn, 0,
sizeof(ssn));
1248 f.
proto = IPPROTO_TCP;
1315 static int LuaMatchTest02(
void)
1317 const char script[] =
"function init (args)\n"
1318 " local needs = {}\n"
1319 " needs[\"payload\"] = tostring(true)\n"
1320 " needs[\"flowvar\"] = {\"cnt\"}\n"
1324 "function match(args)\n"
1325 " a = ScFlowvarGet(0)\n"
1327 " a = tostring(tonumber(a)+1)\n"
1329 " ScFlowvarSet(0, a, #a)\n"
1331 " a = tostring(1)\n"
1333 " ScFlowvarSet(0, a, #a)\n"
1336 " print (\"pre check: \" .. (a))\n"
1337 " if tonumber(a) == 2 then\n"
1338 " print \"match\"\n"
1344 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1345 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1346 "Host: www.emergingthreats.net\r\n\r\n";
1347 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1348 "Host: www.openinfosecfoundation.org\r\n\r\n";
1349 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1350 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1358 memset(&th_v, 0,
sizeof(th_v));
1359 memset(&f, 0,
sizeof(f));
1360 memset(&ssn, 0,
sizeof(ssn));
1367 f.
proto = IPPROTO_TCP;
1422 static int LuaMatchTest02a(
void)
1424 const char script[] =
"function init (args)\n"
1425 " local needs = {}\n"
1426 " needs[\"payload\"] = tostring(true)\n"
1427 " needs[\"flowvar\"] = {\"cnt\"}\n"
1431 "function match(args)\n"
1432 " a = SCFlowvarGet(0)\n"
1434 " a = tostring(tonumber(a)+1)\n"
1436 " SCFlowvarSet(0, a, #a)\n"
1438 " a = tostring(1)\n"
1440 " SCFlowvarSet(0, a, #a)\n"
1443 " print (\"pre check: \" .. (a))\n"
1444 " if tonumber(a) == 2 then\n"
1445 " print \"match\"\n"
1451 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1452 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1453 "Host: www.emergingthreats.net\r\n\r\n";
1454 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1455 "Host: www.openinfosecfoundation.org\r\n\r\n";
1456 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1457 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1465 memset(&th_v, 0,
sizeof(th_v));
1466 memset(&f, 0,
sizeof(f));
1467 memset(&ssn, 0,
sizeof(ssn));
1474 f.
proto = IPPROTO_TCP;
1528 static int LuaMatchTest03(
void)
1530 const char script[] =
"function init (args)\n"
1531 " local needs = {}\n"
1532 " needs[\"packet\"] = tostring(true)\n"
1533 " needs[\"flowvar\"] = {\"cnt\"}\n"
1537 "function match(args)\n"
1538 " a = ScFlowvarGet(0)\n"
1540 " a = tostring(tonumber(a)+1)\n"
1542 " ScFlowvarSet(0, a, #a)\n"
1544 " a = tostring(1)\n"
1546 " ScFlowvarSet(0, a, #a)\n"
1549 " print (\"pre check: \" .. (a))\n"
1550 " if tonumber(a) == 2 then\n"
1551 " print \"match\"\n"
1557 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1558 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1559 "Host: www.emergingthreats.net\r\n\r\n";
1560 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1561 "Host: www.openinfosecfoundation.org\r\n\r\n";
1562 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1563 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1571 memset(&th_v, 0,
sizeof(th_v));
1572 memset(&f, 0,
sizeof(f));
1573 memset(&ssn, 0,
sizeof(ssn));
1580 f.
proto = IPPROTO_TCP;
1634 static int LuaMatchTest03a(
void)
1636 const char script[] =
"function init (args)\n"
1637 " local needs = {}\n"
1638 " needs[\"packet\"] = tostring(true)\n"
1639 " needs[\"flowvar\"] = {\"cnt\"}\n"
1643 "function match(args)\n"
1644 " a = SCFlowvarGet(0)\n"
1646 " a = tostring(tonumber(a)+1)\n"
1648 " SCFlowvarSet(0, a, #a)\n"
1650 " a = tostring(1)\n"
1652 " SCFlowvarSet(0, a, #a)\n"
1655 " print (\"pre check: \" .. (a))\n"
1656 " if tonumber(a) == 2 then\n"
1657 " print \"match\"\n"
1663 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1664 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1665 "Host: www.emergingthreats.net\r\n\r\n";
1666 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1667 "Host: www.openinfosecfoundation.org\r\n\r\n";
1668 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1669 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1677 memset(&th_v, 0,
sizeof(th_v));
1678 memset(&f, 0,
sizeof(f));
1679 memset(&ssn, 0,
sizeof(ssn));
1686 f.
proto = IPPROTO_TCP;
1739 static int LuaMatchTest04(
void)
1741 const char script[] =
"function init (args)\n"
1742 " local needs = {}\n"
1743 " needs[\"http.request_headers\"] = tostring(true)\n"
1744 " needs[\"flowint\"] = {\"cnt\"}\n"
1748 "function match(args)\n"
1749 " print \"inspecting\""
1750 " a = ScFlowintGet(0)\n"
1752 " ScFlowintSet(0, a + 1)\n"
1754 " ScFlowintSet(0, 1)\n"
1757 " a = ScFlowintGet(0)\n"
1759 " print \"match\"\n"
1765 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1766 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1767 "Host: www.emergingthreats.net\r\n\r\n";
1768 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1769 "Host: www.openinfosecfoundation.org\r\n\r\n";
1770 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1771 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1781 memset(&th_v, 0,
sizeof(th_v));
1782 memset(&f, 0,
sizeof(f));
1783 memset(&ssn, 0,
sizeof(ssn));
1790 f.
proto = IPPROTO_TCP;
1855 static int LuaMatchTest04a(
void)
1857 const char script[] =
"function init (args)\n"
1858 " local needs = {}\n"
1859 " needs[\"http.request_headers\"] = tostring(true)\n"
1860 " needs[\"flowint\"] = {\"cnt\"}\n"
1864 "function match(args)\n"
1865 " print \"inspecting\""
1866 " a = SCFlowintGet(0)\n"
1868 " SCFlowintSet(0, a + 1)\n"
1870 " SCFlowintSet(0, 1)\n"
1873 " a = SCFlowintGet(0)\n"
1875 " print \"match\"\n"
1881 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1882 uint8_t httpbuf1[] =
1883 "POST / HTTP/1.1\r\n"
1884 "Host: www.emergingthreats.net\r\n\r\n";
1885 uint8_t httpbuf2[] =
1886 "POST / HTTP/1.1\r\n"
1887 "Host: www.openinfosecfoundation.org\r\n\r\n";
1888 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1889 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1899 memset(&th_v, 0,
sizeof(th_v));
1900 memset(&f, 0,
sizeof(f));
1901 memset(&ssn, 0,
sizeof(ssn));
1908 f.
proto = IPPROTO_TCP;
1973 static int LuaMatchTest05(
void)
1975 const char script[] =
"function init (args)\n"
1976 " local needs = {}\n"
1977 " needs[\"http.request_headers\"] = tostring(true)\n"
1978 " needs[\"flowint\"] = {\"cnt\"}\n"
1982 "function match(args)\n"
1983 " print \"inspecting\""
1984 " a = ScFlowintIncr(0)\n"
1986 " print \"match\"\n"
1992 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1993 uint8_t httpbuf1[] =
1994 "POST / HTTP/1.1\r\n"
1995 "Host: www.emergingthreats.net\r\n\r\n";
1996 uint8_t httpbuf2[] =
1997 "POST / HTTP/1.1\r\n"
1998 "Host: www.openinfosecfoundation.org\r\n\r\n";
1999 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2000 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2010 memset(&th_v, 0,
sizeof(th_v));
2011 memset(&f, 0,
sizeof(f));
2012 memset(&ssn, 0,
sizeof(ssn));
2019 f.
proto = IPPROTO_TCP;
2084 static int LuaMatchTest05a(
void)
2086 const char script[] =
"function init (args)\n"
2087 " local needs = {}\n"
2088 " needs[\"http.request_headers\"] = tostring(true)\n"
2089 " needs[\"flowint\"] = {\"cnt\"}\n"
2093 "function match(args)\n"
2094 " print \"inspecting\""
2095 " a = SCFlowintIncr(0)\n"
2097 " print \"match\"\n"
2103 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2104 uint8_t httpbuf1[] =
2105 "POST / HTTP/1.1\r\n"
2106 "Host: www.emergingthreats.net\r\n\r\n";
2107 uint8_t httpbuf2[] =
2108 "POST / HTTP/1.1\r\n"
2109 "Host: www.openinfosecfoundation.org\r\n\r\n";
2110 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2111 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2121 memset(&th_v, 0,
sizeof(th_v));
2122 memset(&f, 0,
sizeof(f));
2123 memset(&ssn, 0,
sizeof(ssn));
2130 f.
proto = IPPROTO_TCP;
2195 static int LuaMatchTest06(
void)
2197 const char script[] =
"function init (args)\n"
2198 " local needs = {}\n"
2199 " needs[\"http.request_headers\"] = tostring(true)\n"
2200 " needs[\"flowint\"] = {\"cnt\"}\n"
2204 "function match(args)\n"
2205 " print \"inspecting\""
2206 " a = ScFlowintGet(0)\n"
2207 " if a == nil then\n"
2208 " print \"new var set to 2\""
2209 " ScFlowintSet(0, 2)\n"
2211 " a = ScFlowintDecr(0)\n"
2213 " print \"match\"\n"
2219 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2220 uint8_t httpbuf1[] =
2221 "POST / HTTP/1.1\r\n"
2222 "Host: www.emergingthreats.net\r\n\r\n";
2223 uint8_t httpbuf2[] =
2224 "POST / HTTP/1.1\r\n"
2225 "Host: www.openinfosecfoundation.org\r\n\r\n";
2226 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2227 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2237 memset(&th_v, 0,
sizeof(th_v));
2238 memset(&f, 0,
sizeof(f));
2239 memset(&ssn, 0,
sizeof(ssn));
2246 f.
proto = IPPROTO_TCP;
2311 static int LuaMatchTest06a(
void)
2313 const char script[] =
"function init (args)\n"
2314 " local needs = {}\n"
2315 " needs[\"http.request_headers\"] = tostring(true)\n"
2316 " needs[\"flowint\"] = {\"cnt\"}\n"
2320 "function match(args)\n"
2321 " print \"inspecting\""
2322 " a = SCFlowintGet(0)\n"
2323 " if a == nil then\n"
2324 " print \"new var set to 2\""
2325 " SCFlowintSet(0, 2)\n"
2327 " a = SCFlowintDecr(0)\n"
2329 " print \"match\"\n"
2335 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2336 uint8_t httpbuf1[] =
2337 "POST / HTTP/1.1\r\n"
2338 "Host: www.emergingthreats.net\r\n\r\n";
2339 uint8_t httpbuf2[] =
2340 "POST / HTTP/1.1\r\n"
2341 "Host: www.openinfosecfoundation.org\r\n\r\n";
2342 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2343 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2353 memset(&th_v, 0,
sizeof(th_v));
2354 memset(&f, 0,
sizeof(f));
2355 memset(&ssn, 0,
sizeof(ssn));
2362 f.
proto = IPPROTO_TCP;
2426 void DetectLuaRegisterTests(
void)
#define FLAG_MEMORY_LIMIT_LOGGED
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
union FlowVar_::@109 data
void LuaStateSetThreadVars(lua_State *luastate, ThreadVars *tv)
#define FLAG_DATATYPE_HTTP_RESPONSE_COOKIE
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
#define FLAG_DATATYPE_HTTP_REQUEST_LINE
SigTableElmt * sigmatch_table
void(* Free)(DetectEngineCtx *, void *)
#define FLAG_BLOCKED_FUNCTION_LOGGED
#define FLAG_DATATYPE_SSH
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
void * DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
SCLuaSbState * SCLuaSbGetContext(lua_State *L)
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
#define FLAG_DATATYPE_HTTP_REQUEST_HEADERS
#define FLAG_DATATYPE_DNS_RESPONSE
int allow_restricted_functions
#define FLAG_INSTRUCTION_LIMIT_LOGGED
main detection engine ctx
uint16_t lua_blocked_function_errors
#define DEFAULT_LUA_INSTRUCTION_LIMIT
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
#define DETECT_LUA_MAX_FLOWVARS
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
#define FLOW_PKT_TOSERVER
int ConfSetFinal(const char *name, const char *val)
Set a final configuration value.
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.
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
uint32_t flowvar[DETECT_LUA_MAX_FLOWVARS]
int StringParseInt32(int32_t *res, int base, size_t len, const char *str)
uint32_t VarNameStoreRegister(const char *name, const enum VarTypes type)
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
uint16_t lua_instruction_limit_errors
int DetectBufferGetActiveList(DetectEngineCtx *de_ctx, Signature *s)
#define SIG_FLAG_TOCLIENT
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
#define FLAG_DATATYPE_PACKET
uint64_t AppLayerParserGetTransactionInspectId(AppLayerParserState *pstate, uint8_t direction)
uint8_t DetectByteIndexType
#define FLAG_DATATYPE_HTTP_URI_RAW
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
int DetectBufferTypeGetByName(const char *name)
struct lua_State lua_State
AppLayerParserState * alparser
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
#define FLOW_INITIALIZE(f)
#define SIG_FLAG_TOSERVER
uint32_t VarNameStoreLookupByName(const char *name, const enum VarTypes type)
find name for id+type at packet time.
#define PASS
Pass the test.
#define FLAG_DATATYPE_HTTP_REQUEST_COOKIE
uint64_t instruction_limit
AppLayerParserThreadCtx * alp_tctx
Per thread variable structure.
void DetectLuaRegister(void)
Registration function for keyword: lua.
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
void VarNameStoreUnregister(const uint32_t id, const enum VarTypes type)
#define DETECT_LUA_MAX_BYTEVARS
#define DETECT_LUA_MAX_FLOWINTS
#define FLAG_DATATYPE_PAYLOAD
#define FLAG_DATATYPE_HTTP_REQUEST_HEADERS_RAW
#define SCLogWarning(...)
Macro used to log WARNING messages.
int DetectLuaMatchBuffer(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, const uint8_t *buffer, uint32_t buffer_len, uint32_t offset, Flow *f)
#define FLAG_DATATYPE_DNS_REQUEST
uint32_t bytevar[DETECT_LUA_MAX_BYTEVARS]
uint16_t lua_memory_limit_errors
#define FLAG_DATATYPE_HTTP_REQUEST_UA
#define FLAG_DATATYPE_STREAM
void SCLuaSbStateClose(lua_State *L)
uint32_t flowint[DETECT_LUA_MAX_FLOWINTS]
#define FLAG_ERROR_LOGGED
SignatureInitData * init_data
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
void SCLuaRequirefBuiltIns(lua_State *L)
Register Suricata built-in modules for loading in a non-sandboxed environment.
bool blocked_function_error
#define FLAG_DATATYPE_HTTP_URI
#define FLOW_PKT_TOCLIENT
void LuaExtensionsMatchSetup(lua_State *lua_state, DetectLuaData *ld, DetectEngineThreadCtx *det_ctx, Flow *f, Packet *p, const Signature *s, uint8_t flags)
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
void LuaDumpStack(lua_State *state, const char *prefix)
dump stack from lua state to screen
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
#define FLAG_DATATYPE_HTTP_RESPONSE_HEADERS_RAW
void SCLuaSbLoadLibs(lua_State *L)
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
bool DetectByteRetrieveSMVar(const char *arg, const Signature *s, DetectByteIndexType *index)
Used to retrieve args from BM.
int DetectBufferTypeRegister(const char *name)
void StreamTcpFreeConfig(bool quiet)
int DetectRegisterThreadCtxFuncs(DetectEngineCtx *de_ctx, const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *), int mode)
Register Thread keyword context Funcs.
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
#define FLAG_DATATYPE_SMTP
#define DEFAULT_LUA_ALLOC_LIMIT
void SCLuaSbResetInstructionCounter(lua_State *L)
#define FLAG_DATATYPE_HTTP_RESPONSE_HEADERS
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.
#define FLAG_DATATYPE_HTTP_RESPONSE_BODY
void DetectLuaPostSetup(Signature *s)
post-sig parse function to set the sid,rev,gid into the ctx, as this isn't available yet during parsi...
#define SCLogError(...)
Macro used to log ERROR messages.
#define FLAG_DATATYPE_BUFFER
a single match condition for a signature
const char * AppLayerGetProtoName(AppProto alproto)
Given the internal protocol id, returns a string representation of the protocol.
#define FLAG_DATATYPE_DNP3
#define FLAG_DATATYPE_DNS_RRNAME
#define FLOW_PKT_ESTABLISHED
DetectEngineCtx * DetectEngineCtxInit(void)
int LuaRegisterExtensions(lua_State *lua_state)
Register Suricata Lua functions.
bool instruction_count_error
char * DetectLoadCompleteSigPath(const DetectEngineCtx *de_ctx, const char *sig_file)
Create the path if default-rule-path was specified.
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
lua_State * SCLuaSbStateNew(uint64_t alloclimit, uint64_t instructionlimit)
Allocate a new Lua sandbox.
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
FlowVar * FlowVarGet(Flow *f, uint32_t idx)
get the flowvar with index 'idx' from the flow
int DetectUnregisterThreadCtxFuncs(DetectEngineCtx *de_ctx, void *data, const char *name)
Remove Thread keyword context registration.
#define htp_tx_request_line(tx)
AppProto alproto
application level protocol
uint64_t AppLayerParserGetTxCnt(const Flow *f, void *alstate)
#define FLAG_DATATYPE_HTTP_REQUEST_BODY
void(* RegisterTests)(void)
int LuaPushStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.