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_TLS BIT_U32(18)
121 #define FLAG_DATATYPE_SSH BIT_U32(19)
122 #define FLAG_DATATYPE_SMTP BIT_U32(20)
123 #define FLAG_DATATYPE_DNP3 BIT_U32(21)
124 #define FLAG_DATATYPE_BUFFER BIT_U32(22)
125 #define FLAG_ERROR_LOGGED BIT_U32(23)
126 #define FLAG_BLOCKED_FUNCTION_LOGGED BIT_U32(24)
127 #define FLAG_INSTRUCTION_LIMIT_LOGGED BIT_U32(25)
128 #define FLAG_MEMORY_LIMIT_LOGGED BIT_U32(26)
130 #define DEFAULT_LUA_ALLOC_LIMIT 500000
131 #define DEFAULT_LUA_INSTRUCTION_LIMIT 500000
136 int size = lua_gettop(state);
137 printf(
"%s: size %d\n", prefix, size);
139 for (
int i = 1; i <= size; i++) {
140 int type = lua_type(state, i);
141 printf(
"- %s: Stack size=%d, level=%d, type=%d, ", prefix, size, i,
type);
145 printf(
"function %s", lua_tostring(state, i));
148 printf(
"bool %s", lua_toboolean(state, i) ?
"true" :
"false");
151 printf(
"number %g", lua_tonumber(state, i));
154 printf(
"string `%s'", lua_tostring(state, i));
157 printf(
"table `%s'", lua_tostring(state, i));
160 printf(
"other %s", lua_typename(state,
type));
172 static int DetectLuaRunMatch(
178 if (lua_pcall(tlua->
luastate, 1, 1, 0) != 0) {
179 const char *reason = lua_tostring(tlua->
luastate, -1);
190 reason =
"memory limit exceeded";
198 if (!(tlua->
flags & flag)) {
199 SCLogWarning(
"Lua script failed to run successfully: %s", reason);
204 while (lua_gettop(tlua->
luastate) > 0) {
213 if (lua_gettop(tlua->
luastate) > 0) {
215 if (lua_type(tlua->
luastate, 1) == LUA_TNUMBER) {
216 double script_ret = lua_tonumber(tlua->
luastate, 1);
220 if (script_ret == 1.0)
224 }
else if (lua_type(tlua->
luastate, 1) == LUA_TTABLE) {
227 while (lua_next(tlua->
luastate, -2)) {
228 v = lua_tostring(tlua->
luastate, -1);
230 k = lua_tostring(tlua->
luastate, -1);
237 if (strcmp(k,
"retval") == 0) {
241 "for \"retval\" from LUA return table: '%s'",
265 while (lua_gettop(tlua->
luastate) > 0) {
278 if (buffer == NULL || buffer_len == 0)
293 lua_getglobal(tlua->
luastate,
"match");
296 lua_pushliteral(tlua->
luastate,
"offset");
304 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
334 flags = STREAM_TOSERVER;
336 flags = STREAM_TOCLIENT;
355 lua_getglobal(tlua->
luastate,
"match");
359 lua_pushliteral(tlua->
luastate,
"payload");
364 lua_pushliteral(tlua->
luastate,
"packet");
370 if (htp_state != NULL && htp_state->
connp != NULL) {
375 for ( ; idx < total_txs; idx++) {
382 lua_pushliteral(tlua->
luastate,
"http.request_line");
392 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
417 lua_getglobal(tlua->
luastate,
"match");
422 if (htp_state != NULL && htp_state->
connp != NULL) {
428 lua_pushliteral(tlua->
luastate,
"http.request_line");
438 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
454 void *state,
void *txv,
const Signature *s,
457 return DetectLuaAppMatchCommon(det_ctx, f,
flags, state, s,
ctx);
463 static const char *ut_script = NULL;
466 static void *DetectLuaThreadInit(
void *data)
495 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
sid));
496 lua_setglobal(t->
luastate,
"SCRuleSid");
497 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
rev));
498 lua_setglobal(t->
luastate,
"SCRuleRev");
499 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
gid));
500 lua_setglobal(t->
luastate,
"SCRuleGid");
504 if (ut_script != NULL) {
505 status = luaL_loadbuffer(t->
luastate, ut_script, strlen(ut_script),
"unittest");
522 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
528 lua_getglobal(t->
luastate,
"thread_init");
529 if (lua_isfunction(t->
luastate, -1)) {
530 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
531 SCLogError(
"couldn't run script 'thread_init' function: %s",
548 static void DetectLuaThreadFree(
void *
ctx)
576 if (strlen(
str) &&
str[0] ==
'!') {
591 DetectLuaFree(
de_ctx, lua);
600 if (luastate == NULL)
603 luaL_openlibs(luastate);
610 if (ut_script != NULL) {
611 status = luaL_loadbuffer(luastate, ut_script, strlen(ut_script),
"unittest");
613 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
618 status = luaL_loadfile(luastate, ld->
filename);
620 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
628 if (lua_pcall(luastate, 0, 0, 0) != 0) {
629 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
633 lua_getglobal(luastate,
"init");
634 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
639 lua_newtable(luastate);
640 if (lua_gettop(luastate) == 0 || lua_type(luastate, 2) != LUA_TTABLE) {
645 lua_pushliteral(luastate,
"script_api_ver");
646 lua_pushnumber (luastate, 1);
647 lua_settable(luastate, -3);
649 if (lua_pcall(luastate, 1, 1, 0) != 0) {
650 SCLogError(
"couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
655 if (lua_gettop(luastate) == 0) {
656 SCLogError(
"init function in script should return table, nothing returned");
659 if (lua_type(luastate, 1) != LUA_TTABLE) {
660 SCLogError(
"init function in script should return table, returned is not table");
664 lua_pushnil(luastate);
666 while (lua_next(luastate, -2)) {
667 k = lua_tostring(luastate, -2);
672 if (strcmp(k,
"flowvar") == 0) {
673 if (lua_istable(luastate, -1)) {
674 lua_pushnil(luastate);
675 while (lua_next(luastate, -2) != 0) {
677 const char *value = lua_tostring(luastate, -1);
680 lua_pop(luastate, 1);
692 lua_pop(luastate, 1);
694 }
else if (strcmp(k,
"flowint") == 0) {
695 if (lua_istable(luastate, -1)) {
696 lua_pushnil(luastate);
697 while (lua_next(luastate, -2) != 0) {
699 const char *value = lua_tostring(luastate, -1);
702 lua_pop(luastate, 1);
714 lua_pop(luastate, 1);
716 }
else if (strcmp(k,
"bytevar") == 0) {
717 if (lua_istable(luastate, -1)) {
718 lua_pushnil(luastate);
719 while (lua_next(luastate, -2) != 0) {
721 const char *value = lua_tostring(luastate, -1);
724 lua_pop(luastate, 1);
733 SCLogError(
"Unknown byte_extract or byte_math var "
734 "requested by lua script - %s",
742 lua_pop(luastate, 1);
746 v = lua_tostring(luastate, -1);
747 lua_pop(luastate, 1);
752 if (strcmp(k,
"packet") == 0 && strcmp(v,
"true") == 0) {
754 }
else if (strcmp(k,
"payload") == 0 && strcmp(v,
"true") == 0) {
756 }
else if (strcmp(k,
"buffer") == 0 && strcmp(v,
"true") == 0) {
764 }
else if (strcmp(k,
"stream") == 0 && strcmp(v,
"true") == 0) {
773 }
else if (strncmp(k,
"http", 4) == 0 && strcmp(v,
"true") == 0) {
776 "can just inspect script against one app layer proto like HTTP at a time");
779 if (ld->
flags != 0) {
780 SCLogError(
"when inspecting HTTP buffers only a single buffer can be inspected");
787 if (strcmp(k,
"http.uri") == 0)
790 else if (strcmp(k,
"http.uri.raw") == 0)
793 else if (strcmp(k,
"http.request_line") == 0)
796 else if (strcmp(k,
"http.request_headers") == 0)
799 else if (strcmp(k,
"http.request_headers.raw") == 0)
802 else if (strcmp(k,
"http.request_cookie") == 0)
805 else if (strcmp(k,
"http.request_user_agent") == 0)
808 else if (strcmp(k,
"http.request_body") == 0)
811 else if (strcmp(k,
"http.response_body") == 0)
814 else if (strcmp(k,
"http.response_cookie") == 0)
817 else if (strcmp(k,
"http.response_headers") == 0)
820 else if (strcmp(k,
"http.response_headers.raw") == 0)
824 SCLogError(
"unsupported http data type %s", k);
833 }
else if (strncmp(k,
"dns", 3) == 0 && strcmp(v,
"true") == 0) {
837 if (strcmp(k,
"dns.rrname") == 0)
839 else if (strcmp(k,
"dns.request") == 0)
841 else if (strcmp(k,
"dns.response") == 0)
845 SCLogError(
"unsupported dns data type %s", k);
853 }
else if (strncmp(k,
"tls", 3) == 0 && strcmp(v,
"true") == 0) {
859 }
else if (strncmp(k,
"ssh", 3) == 0 && strcmp(v,
"true") == 0) {
865 }
else if (strncmp(k,
"smtp", 4) == 0 && strcmp(v,
"true") == 0) {
871 }
else if (strncmp(k,
"dnp3", 4) == 0 && strcmp(v,
"true") == 0) {
884 lua_pop(luastate, 1);
910 (void)
ConfGetBool(
"security.lua.allow-rules", &enabled);
912 SCLogError(
"Lua rules disabled by security configuration: security.lua.allow-rules");
923 (void)
ConfGetInt(
"security.lua.max-bytes", &lua_alloc_limit);
924 (void)
ConfGetInt(
"security.lua.max-instructions", &lua_instruction_limit);
928 int allow_restricted_functions = 0;
929 (void)
ConfGetBool(
"security.lua.allow-restricted-functions", &allow_restricted_functions);
932 if (DetectLuaSetupPrime(
de_ctx, lua, s) == -1) {
937 DetectLuaThreadInit, (
void *)lua,
938 DetectLuaThreadFree, 0);
982 }
else if (lua->
flags &
1004 list = g_smtp_generic_list_id;
1025 DetectLuaFree(
de_ctx, lua);
1065 for (uint16_t i = 0; i < lua->
flowints; i++) {
1068 for (uint16_t i = 0; i < lua->
flowvars; i++) {
1082 static int LuaMatchTest01(
void)
1086 const char script[] =
1087 "function init (args)\n"
1088 " local needs = {}\n"
1089 " needs[\"http.request_headers\"] = tostring(true)\n"
1090 " needs[\"flowvar\"] = {\"cnt\"}\n"
1094 "function match(args)\n"
1095 " a = ScFlowvarGet(0)\n"
1097 " a = tostring(tonumber(a)+1)\n"
1099 " ScFlowvarSet(0, a, #a)\n"
1101 " a = tostring(1)\n"
1103 " ScFlowvarSet(0, a, #a)\n"
1106 " print (\"pre check: \" .. (a))\n"
1107 " if tonumber(a) == 2 then\n"
1108 " print \"match\"\n"
1114 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1115 uint8_t httpbuf1[] =
1116 "POST / HTTP/1.1\r\n"
1117 "Host: www.emergingthreats.net\r\n\r\n";
1118 uint8_t httpbuf2[] =
1119 "POST / HTTP/1.1\r\n"
1120 "Host: www.openinfosecfoundation.org\r\n\r\n";
1121 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1122 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1132 memset(&th_v, 0,
sizeof(th_v));
1133 memset(&f, 0,
sizeof(f));
1134 memset(&ssn, 0,
sizeof(ssn));
1141 f.
proto = IPPROTO_TCP;
1207 static int LuaMatchTest01a(
void)
1209 const char script[] =
"function init (args)\n"
1210 " local needs = {}\n"
1211 " needs[\"http.request_headers\"] = tostring(true)\n"
1212 " needs[\"flowvar\"] = {\"cnt\"}\n"
1216 "function match(args)\n"
1217 " a = SCFlowvarGet(0)\n"
1219 " a = tostring(tonumber(a)+1)\n"
1221 " SCFlowvarSet(0, a, #a)\n"
1223 " a = tostring(1)\n"
1225 " SCFlowvarSet(0, a, #a)\n"
1228 " print (\"pre check: \" .. (a))\n"
1229 " if tonumber(a) == 2 then\n"
1230 " print \"match\"\n"
1236 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1237 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1238 "Host: www.emergingthreats.net\r\n\r\n";
1239 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1240 "Host: www.openinfosecfoundation.org\r\n\r\n";
1241 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1242 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1252 memset(&th_v, 0,
sizeof(th_v));
1253 memset(&f, 0,
sizeof(f));
1254 memset(&ssn, 0,
sizeof(ssn));
1261 f.
proto = IPPROTO_TCP;
1328 static int LuaMatchTest02(
void)
1330 const char script[] =
"function init (args)\n"
1331 " local needs = {}\n"
1332 " needs[\"payload\"] = tostring(true)\n"
1333 " needs[\"flowvar\"] = {\"cnt\"}\n"
1337 "function match(args)\n"
1338 " a = ScFlowvarGet(0)\n"
1340 " a = tostring(tonumber(a)+1)\n"
1342 " ScFlowvarSet(0, a, #a)\n"
1344 " a = tostring(1)\n"
1346 " ScFlowvarSet(0, a, #a)\n"
1349 " print (\"pre check: \" .. (a))\n"
1350 " if tonumber(a) == 2 then\n"
1351 " print \"match\"\n"
1357 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1358 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1359 "Host: www.emergingthreats.net\r\n\r\n";
1360 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1361 "Host: www.openinfosecfoundation.org\r\n\r\n";
1362 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1363 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1371 memset(&th_v, 0,
sizeof(th_v));
1372 memset(&f, 0,
sizeof(f));
1373 memset(&ssn, 0,
sizeof(ssn));
1380 f.
proto = IPPROTO_TCP;
1435 static int LuaMatchTest02a(
void)
1437 const char script[] =
"function init (args)\n"
1438 " local needs = {}\n"
1439 " needs[\"payload\"] = tostring(true)\n"
1440 " needs[\"flowvar\"] = {\"cnt\"}\n"
1444 "function match(args)\n"
1445 " a = SCFlowvarGet(0)\n"
1447 " a = tostring(tonumber(a)+1)\n"
1449 " SCFlowvarSet(0, a, #a)\n"
1451 " a = tostring(1)\n"
1453 " SCFlowvarSet(0, a, #a)\n"
1456 " print (\"pre check: \" .. (a))\n"
1457 " if tonumber(a) == 2 then\n"
1458 " print \"match\"\n"
1464 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1465 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1466 "Host: www.emergingthreats.net\r\n\r\n";
1467 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1468 "Host: www.openinfosecfoundation.org\r\n\r\n";
1469 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1470 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1478 memset(&th_v, 0,
sizeof(th_v));
1479 memset(&f, 0,
sizeof(f));
1480 memset(&ssn, 0,
sizeof(ssn));
1487 f.
proto = IPPROTO_TCP;
1541 static int LuaMatchTest03(
void)
1543 const char script[] =
"function init (args)\n"
1544 " local needs = {}\n"
1545 " needs[\"packet\"] = tostring(true)\n"
1546 " needs[\"flowvar\"] = {\"cnt\"}\n"
1550 "function match(args)\n"
1551 " a = ScFlowvarGet(0)\n"
1553 " a = tostring(tonumber(a)+1)\n"
1555 " ScFlowvarSet(0, a, #a)\n"
1557 " a = tostring(1)\n"
1559 " ScFlowvarSet(0, a, #a)\n"
1562 " print (\"pre check: \" .. (a))\n"
1563 " if tonumber(a) == 2 then\n"
1564 " print \"match\"\n"
1570 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1571 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1572 "Host: www.emergingthreats.net\r\n\r\n";
1573 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1574 "Host: www.openinfosecfoundation.org\r\n\r\n";
1575 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1576 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1584 memset(&th_v, 0,
sizeof(th_v));
1585 memset(&f, 0,
sizeof(f));
1586 memset(&ssn, 0,
sizeof(ssn));
1593 f.
proto = IPPROTO_TCP;
1647 static int LuaMatchTest03a(
void)
1649 const char script[] =
"function init (args)\n"
1650 " local needs = {}\n"
1651 " needs[\"packet\"] = tostring(true)\n"
1652 " needs[\"flowvar\"] = {\"cnt\"}\n"
1656 "function match(args)\n"
1657 " a = SCFlowvarGet(0)\n"
1659 " a = tostring(tonumber(a)+1)\n"
1661 " SCFlowvarSet(0, a, #a)\n"
1663 " a = tostring(1)\n"
1665 " SCFlowvarSet(0, a, #a)\n"
1668 " print (\"pre check: \" .. (a))\n"
1669 " if tonumber(a) == 2 then\n"
1670 " print \"match\"\n"
1676 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1677 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1678 "Host: www.emergingthreats.net\r\n\r\n";
1679 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1680 "Host: www.openinfosecfoundation.org\r\n\r\n";
1681 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1682 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1690 memset(&th_v, 0,
sizeof(th_v));
1691 memset(&f, 0,
sizeof(f));
1692 memset(&ssn, 0,
sizeof(ssn));
1699 f.
proto = IPPROTO_TCP;
1752 static int LuaMatchTest04(
void)
1754 const char script[] =
"function init (args)\n"
1755 " local needs = {}\n"
1756 " needs[\"http.request_headers\"] = tostring(true)\n"
1757 " needs[\"flowint\"] = {\"cnt\"}\n"
1761 "function match(args)\n"
1762 " print \"inspecting\""
1763 " a = ScFlowintGet(0)\n"
1765 " ScFlowintSet(0, a + 1)\n"
1767 " ScFlowintSet(0, 1)\n"
1770 " a = ScFlowintGet(0)\n"
1772 " print \"match\"\n"
1778 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1779 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1780 "Host: www.emergingthreats.net\r\n\r\n";
1781 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1782 "Host: www.openinfosecfoundation.org\r\n\r\n";
1783 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1784 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1794 memset(&th_v, 0,
sizeof(th_v));
1795 memset(&f, 0,
sizeof(f));
1796 memset(&ssn, 0,
sizeof(ssn));
1803 f.
proto = IPPROTO_TCP;
1868 static int LuaMatchTest04a(
void)
1870 const char script[] =
"function init (args)\n"
1871 " local needs = {}\n"
1872 " needs[\"http.request_headers\"] = tostring(true)\n"
1873 " needs[\"flowint\"] = {\"cnt\"}\n"
1877 "function match(args)\n"
1878 " print \"inspecting\""
1879 " a = SCFlowintGet(0)\n"
1881 " SCFlowintSet(0, a + 1)\n"
1883 " SCFlowintSet(0, 1)\n"
1886 " a = SCFlowintGet(0)\n"
1888 " print \"match\"\n"
1894 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1895 uint8_t httpbuf1[] =
1896 "POST / HTTP/1.1\r\n"
1897 "Host: www.emergingthreats.net\r\n\r\n";
1898 uint8_t httpbuf2[] =
1899 "POST / HTTP/1.1\r\n"
1900 "Host: www.openinfosecfoundation.org\r\n\r\n";
1901 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1902 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1912 memset(&th_v, 0,
sizeof(th_v));
1913 memset(&f, 0,
sizeof(f));
1914 memset(&ssn, 0,
sizeof(ssn));
1921 f.
proto = IPPROTO_TCP;
1986 static int LuaMatchTest05(
void)
1988 const char script[] =
"function init (args)\n"
1989 " local needs = {}\n"
1990 " needs[\"http.request_headers\"] = tostring(true)\n"
1991 " needs[\"flowint\"] = {\"cnt\"}\n"
1995 "function match(args)\n"
1996 " print \"inspecting\""
1997 " a = ScFlowintIncr(0)\n"
1999 " print \"match\"\n"
2005 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2006 uint8_t httpbuf1[] =
2007 "POST / HTTP/1.1\r\n"
2008 "Host: www.emergingthreats.net\r\n\r\n";
2009 uint8_t httpbuf2[] =
2010 "POST / HTTP/1.1\r\n"
2011 "Host: www.openinfosecfoundation.org\r\n\r\n";
2012 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2013 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2023 memset(&th_v, 0,
sizeof(th_v));
2024 memset(&f, 0,
sizeof(f));
2025 memset(&ssn, 0,
sizeof(ssn));
2032 f.
proto = IPPROTO_TCP;
2097 static int LuaMatchTest05a(
void)
2099 const char script[] =
"function init (args)\n"
2100 " local needs = {}\n"
2101 " needs[\"http.request_headers\"] = tostring(true)\n"
2102 " needs[\"flowint\"] = {\"cnt\"}\n"
2106 "function match(args)\n"
2107 " print \"inspecting\""
2108 " a = SCFlowintIncr(0)\n"
2110 " print \"match\"\n"
2116 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2117 uint8_t httpbuf1[] =
2118 "POST / HTTP/1.1\r\n"
2119 "Host: www.emergingthreats.net\r\n\r\n";
2120 uint8_t httpbuf2[] =
2121 "POST / HTTP/1.1\r\n"
2122 "Host: www.openinfosecfoundation.org\r\n\r\n";
2123 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2124 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2134 memset(&th_v, 0,
sizeof(th_v));
2135 memset(&f, 0,
sizeof(f));
2136 memset(&ssn, 0,
sizeof(ssn));
2143 f.
proto = IPPROTO_TCP;
2208 static int LuaMatchTest06(
void)
2210 const char script[] =
"function init (args)\n"
2211 " local needs = {}\n"
2212 " needs[\"http.request_headers\"] = tostring(true)\n"
2213 " needs[\"flowint\"] = {\"cnt\"}\n"
2217 "function match(args)\n"
2218 " print \"inspecting\""
2219 " a = ScFlowintGet(0)\n"
2220 " if a == nil then\n"
2221 " print \"new var set to 2\""
2222 " ScFlowintSet(0, 2)\n"
2224 " a = ScFlowintDecr(0)\n"
2226 " print \"match\"\n"
2232 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2233 uint8_t httpbuf1[] =
2234 "POST / HTTP/1.1\r\n"
2235 "Host: www.emergingthreats.net\r\n\r\n";
2236 uint8_t httpbuf2[] =
2237 "POST / HTTP/1.1\r\n"
2238 "Host: www.openinfosecfoundation.org\r\n\r\n";
2239 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2240 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2250 memset(&th_v, 0,
sizeof(th_v));
2251 memset(&f, 0,
sizeof(f));
2252 memset(&ssn, 0,
sizeof(ssn));
2259 f.
proto = IPPROTO_TCP;
2324 static int LuaMatchTest06a(
void)
2326 const char script[] =
"function init (args)\n"
2327 " local needs = {}\n"
2328 " needs[\"http.request_headers\"] = tostring(true)\n"
2329 " needs[\"flowint\"] = {\"cnt\"}\n"
2333 "function match(args)\n"
2334 " print \"inspecting\""
2335 " a = SCFlowintGet(0)\n"
2336 " if a == nil then\n"
2337 " print \"new var set to 2\""
2338 " SCFlowintSet(0, 2)\n"
2340 " a = SCFlowintDecr(0)\n"
2342 " print \"match\"\n"
2348 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2349 uint8_t httpbuf1[] =
2350 "POST / HTTP/1.1\r\n"
2351 "Host: www.emergingthreats.net\r\n\r\n";
2352 uint8_t httpbuf2[] =
2353 "POST / HTTP/1.1\r\n"
2354 "Host: www.openinfosecfoundation.org\r\n\r\n";
2355 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2356 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2366 memset(&th_v, 0,
sizeof(th_v));
2367 memset(&f, 0,
sizeof(f));
2368 memset(&ssn, 0,
sizeof(ssn));
2375 f.
proto = IPPROTO_TCP;
2439 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 *)
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.
#define FLAG_DATATYPE_TLS
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.