Go to the documentation of this file.
66 void *state,
void *txv,
const Signature *s,
70 static void DetectLuaRegisterTests(
void);
73 static int g_smtp_generic_list_id = 0;
101 #define FLAG_DATATYPE_PACKET BIT_U32(0)
102 #define FLAG_DATATYPE_PAYLOAD BIT_U32(1)
103 #define FLAG_DATATYPE_STREAM BIT_U32(2)
104 #define FLAG_DATATYPE_HTTP_URI BIT_U32(3)
105 #define FLAG_DATATYPE_HTTP_URI_RAW BIT_U32(4)
106 #define FLAG_DATATYPE_HTTP_REQUEST_HEADERS BIT_U32(5)
107 #define FLAG_DATATYPE_HTTP_REQUEST_HEADERS_RAW BIT_U32(6)
108 #define FLAG_DATATYPE_HTTP_REQUEST_COOKIE BIT_U32(7)
109 #define FLAG_DATATYPE_HTTP_REQUEST_UA BIT_U32(8)
110 #define FLAG_DATATYPE_HTTP_REQUEST_LINE BIT_U32(9)
111 #define FLAG_DATATYPE_HTTP_REQUEST_BODY BIT_U32(10)
112 #define FLAG_DATATYPE_HTTP_RESPONSE_COOKIE BIT_U32(11)
113 #define FLAG_DATATYPE_HTTP_RESPONSE_BODY BIT_U32(12)
114 #define FLAG_DATATYPE_HTTP_RESPONSE_HEADERS BIT_U32(13)
115 #define FLAG_DATATYPE_HTTP_RESPONSE_HEADERS_RAW BIT_U32(14)
116 #define FLAG_DATATYPE_DNS_RRNAME BIT_U32(15)
117 #define FLAG_DATATYPE_DNS_REQUEST BIT_U32(16)
118 #define FLAG_DATATYPE_DNS_RESPONSE BIT_U32(17)
119 #define FLAG_DATATYPE_TLS BIT_U32(18)
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
136 int size = lua_gettop(state);
139 for (i = 1; i <= size; i++) {
140 int type = lua_type(state, i);
141 printf(
"Stack size=%d, level=%d, type=%d, ", size, i,
type);
145 printf(
"function %s", lua_tostring(state, i) ?
"true" :
"false");
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));
173 static int DetectLuaRunMatch(
179 if (lua_pcall(tlua->
luastate, 1, 1, 0) != 0) {
180 const char *reason = lua_tostring(tlua->
luastate, -1);
191 reason =
"memory limit exceeded";
199 if (!(tlua->
flags & flag)) {
200 SCLogWarning(
"Lua script failed to run successfully: %s", reason);
205 while (lua_gettop(tlua->
luastate) > 0) {
214 if (lua_gettop(tlua->
luastate) > 0) {
216 if (lua_type(tlua->
luastate, 1) == LUA_TNUMBER) {
217 double script_ret = lua_tonumber(tlua->
luastate, 1);
221 if (script_ret == 1.0)
225 }
else if (lua_type(tlua->
luastate, 1) == LUA_TTABLE) {
228 while (lua_next(tlua->
luastate, -2)) {
229 v = lua_tostring(tlua->
luastate, -1);
231 k = lua_tostring(tlua->
luastate, -1);
238 if (strcmp(k,
"retval") == 0) {
242 "for \"retval\" from LUA return table: '%s'",
266 while (lua_gettop(tlua->
luastate) > 0) {
279 if (buffer == NULL || buffer_len == 0)
294 lua_getglobal(tlua->
luastate,
"match");
297 lua_pushliteral(tlua->
luastate,
"offset");
305 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
335 flags = STREAM_TOSERVER;
337 flags = STREAM_TOCLIENT;
356 lua_getglobal(tlua->
luastate,
"match");
360 lua_pushliteral(tlua->
luastate,
"payload");
365 lua_pushliteral(tlua->
luastate,
"packet");
371 if (htp_state != NULL && htp_state->
connp != NULL) {
376 for ( ; idx < total_txs; idx++) {
382 bstr_len(tx->request_line) > 0) {
383 lua_pushliteral(tlua->
luastate,
"http.request_line");
385 (
const uint8_t *)bstr_ptr(tx->request_line),
386 bstr_len(tx->request_line));
393 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
418 lua_getglobal(tlua->
luastate,
"match");
423 if (htp_state != NULL && htp_state->
connp != NULL) {
428 bstr_len(tx->request_line) > 0) {
429 lua_pushliteral(tlua->
luastate,
"http.request_line");
431 (
const uint8_t *)bstr_ptr(tx->request_line),
432 bstr_len(tx->request_line));
439 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
455 void *state,
void *txv,
const Signature *s,
458 return DetectLuaAppMatchCommon(det_ctx, f,
flags, state, s,
ctx);
464 static const char *ut_script = NULL;
467 static void *DetectLuaThreadInit(
void *data)
496 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
sid));
497 lua_setglobal(t->
luastate,
"SCRuleSid");
498 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
rev));
499 lua_setglobal(t->
luastate,
"SCRuleRev");
500 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
gid));
501 lua_setglobal(t->
luastate,
"SCRuleGid");
505 if (ut_script != NULL) {
506 status = luaL_loadbuffer(t->
luastate, ut_script, strlen(ut_script),
"unittest");
523 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
537 static void DetectLuaThreadFree(
void *
ctx)
565 if (strlen(
str) &&
str[0] ==
'!') {
580 DetectLuaFree(
de_ctx, lua);
589 if (luastate == NULL)
592 luaL_openlibs(luastate);
599 if (ut_script != NULL) {
600 status = luaL_loadbuffer(luastate, ut_script, strlen(ut_script),
"unittest");
602 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
607 status = luaL_loadfile(luastate, ld->
filename);
609 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
617 if (lua_pcall(luastate, 0, 0, 0) != 0) {
618 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
622 lua_getglobal(luastate,
"init");
623 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
628 lua_newtable(luastate);
629 if (lua_gettop(luastate) == 0 || lua_type(luastate, 2) != LUA_TTABLE) {
634 lua_pushliteral(luastate,
"script_api_ver");
635 lua_pushnumber (luastate, 1);
636 lua_settable(luastate, -3);
638 if (lua_pcall(luastate, 1, 1, 0) != 0) {
639 SCLogError(
"couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
644 if (lua_gettop(luastate) == 0) {
645 SCLogError(
"init function in script should return table, nothing returned");
648 if (lua_type(luastate, 1) != LUA_TTABLE) {
649 SCLogError(
"init function in script should return table, returned is not table");
653 lua_pushnil(luastate);
655 while (lua_next(luastate, -2)) {
656 k = lua_tostring(luastate, -2);
661 if (strcmp(k,
"flowvar") == 0) {
662 if (lua_istable(luastate, -1)) {
663 lua_pushnil(luastate);
664 while (lua_next(luastate, -2) != 0) {
666 const char *value = lua_tostring(luastate, -1);
669 lua_pop(luastate, 1);
681 lua_pop(luastate, 1);
683 }
else if (strcmp(k,
"flowint") == 0) {
684 if (lua_istable(luastate, -1)) {
685 lua_pushnil(luastate);
686 while (lua_next(luastate, -2) != 0) {
688 const char *value = lua_tostring(luastate, -1);
691 lua_pop(luastate, 1);
703 lua_pop(luastate, 1);
705 }
else if (strcmp(k,
"bytevar") == 0) {
706 if (lua_istable(luastate, -1)) {
707 lua_pushnil(luastate);
708 while (lua_next(luastate, -2) != 0) {
710 const char *value = lua_tostring(luastate, -1);
713 lua_pop(luastate, 1);
722 SCLogError(
"Unknown byte_extract or byte_math var "
723 "requested by lua script - %s",
731 lua_pop(luastate, 1);
735 v = lua_tostring(luastate, -1);
736 lua_pop(luastate, 1);
741 if (strcmp(k,
"packet") == 0 && strcmp(v,
"true") == 0) {
743 }
else if (strcmp(k,
"payload") == 0 && strcmp(v,
"true") == 0) {
745 }
else if (strcmp(k,
"buffer") == 0 && strcmp(v,
"true") == 0) {
753 }
else if (strcmp(k,
"stream") == 0 && strcmp(v,
"true") == 0) {
762 }
else if (strncmp(k,
"http", 4) == 0 && strcmp(v,
"true") == 0) {
765 "can just inspect script against one app layer proto like HTTP at a time");
768 if (ld->
flags != 0) {
769 SCLogError(
"when inspecting HTTP buffers only a single buffer can be inspected");
776 if (strcmp(k,
"http.uri") == 0)
779 else if (strcmp(k,
"http.uri.raw") == 0)
782 else if (strcmp(k,
"http.request_line") == 0)
785 else if (strcmp(k,
"http.request_headers") == 0)
788 else if (strcmp(k,
"http.request_headers.raw") == 0)
791 else if (strcmp(k,
"http.request_cookie") == 0)
794 else if (strcmp(k,
"http.request_user_agent") == 0)
797 else if (strcmp(k,
"http.request_body") == 0)
800 else if (strcmp(k,
"http.response_body") == 0)
803 else if (strcmp(k,
"http.response_cookie") == 0)
806 else if (strcmp(k,
"http.response_headers") == 0)
809 else if (strcmp(k,
"http.response_headers.raw") == 0)
813 SCLogError(
"unsupported http data type %s", k);
822 }
else if (strncmp(k,
"dns", 3) == 0 && strcmp(v,
"true") == 0) {
826 if (strcmp(k,
"dns.rrname") == 0)
828 else if (strcmp(k,
"dns.request") == 0)
830 else if (strcmp(k,
"dns.response") == 0)
834 SCLogError(
"unsupported dns data type %s", k);
842 }
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);
899 (void)
ConfGetBool(
"security.lua.allow-rules", &enabled);
901 SCLogError(
"Lua rules disabled by security configuration: security.lua.allow-rules");
912 (void)
ConfGetInt(
"security.lua.max-bytes", &lua_alloc_limit);
913 (void)
ConfGetInt(
"security.lua.max-instructions", &lua_instruction_limit);
917 int allow_restricted_functions = 0;
918 (void)
ConfGetBool(
"security.lua.allow-restricted-functions", &allow_restricted_functions);
921 if (DetectLuaSetupPrime(
de_ctx, lua, s) == -1) {
926 DetectLuaThreadInit, (
void *)lua,
927 DetectLuaThreadFree, 0);
971 }
else if (lua->
flags &
993 list = g_smtp_generic_list_id;
1014 DetectLuaFree(
de_ctx, lua);
1054 for (uint16_t i = 0; i < lua->
flowints; i++) {
1057 for (uint16_t i = 0; i < lua->
flowvars; i++) {
1071 static int LuaMatchTest01(
void)
1075 const char script[] =
1076 "function init (args)\n"
1077 " local needs = {}\n"
1078 " needs[\"http.request_headers\"] = tostring(true)\n"
1079 " needs[\"flowvar\"] = {\"cnt\"}\n"
1083 "function match(args)\n"
1084 " a = ScFlowvarGet(0)\n"
1086 " a = tostring(tonumber(a)+1)\n"
1088 " ScFlowvarSet(0, a, #a)\n"
1090 " a = tostring(1)\n"
1092 " ScFlowvarSet(0, a, #a)\n"
1095 " print (\"pre check: \" .. (a))\n"
1096 " if tonumber(a) == 2 then\n"
1097 " print \"match\"\n"
1103 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1104 uint8_t httpbuf1[] =
1105 "POST / HTTP/1.1\r\n"
1106 "Host: www.emergingthreats.net\r\n\r\n";
1107 uint8_t httpbuf2[] =
1108 "POST / HTTP/1.1\r\n"
1109 "Host: www.openinfosecfoundation.org\r\n\r\n";
1110 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1111 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1121 memset(&th_v, 0,
sizeof(th_v));
1122 memset(&f, 0,
sizeof(f));
1123 memset(&ssn, 0,
sizeof(ssn));
1130 f.
proto = IPPROTO_TCP;
1196 static int LuaMatchTest01a(
void)
1198 const char script[] =
"function init (args)\n"
1199 " local needs = {}\n"
1200 " needs[\"http.request_headers\"] = tostring(true)\n"
1201 " needs[\"flowvar\"] = {\"cnt\"}\n"
1205 "function match(args)\n"
1206 " a = SCFlowvarGet(0)\n"
1208 " a = tostring(tonumber(a)+1)\n"
1210 " SCFlowvarSet(0, a, #a)\n"
1212 " a = tostring(1)\n"
1214 " SCFlowvarSet(0, a, #a)\n"
1217 " print (\"pre check: \" .. (a))\n"
1218 " if tonumber(a) == 2 then\n"
1219 " print \"match\"\n"
1225 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1226 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1227 "Host: www.emergingthreats.net\r\n\r\n";
1228 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1229 "Host: www.openinfosecfoundation.org\r\n\r\n";
1230 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1231 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1241 memset(&th_v, 0,
sizeof(th_v));
1242 memset(&f, 0,
sizeof(f));
1243 memset(&ssn, 0,
sizeof(ssn));
1250 f.
proto = IPPROTO_TCP;
1317 static int LuaMatchTest02(
void)
1319 const char script[] =
"function init (args)\n"
1320 " local needs = {}\n"
1321 " needs[\"payload\"] = tostring(true)\n"
1322 " needs[\"flowvar\"] = {\"cnt\"}\n"
1326 "function match(args)\n"
1327 " a = ScFlowvarGet(0)\n"
1329 " a = tostring(tonumber(a)+1)\n"
1331 " ScFlowvarSet(0, a, #a)\n"
1333 " a = tostring(1)\n"
1335 " ScFlowvarSet(0, a, #a)\n"
1338 " print (\"pre check: \" .. (a))\n"
1339 " if tonumber(a) == 2 then\n"
1340 " print \"match\"\n"
1346 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1347 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1348 "Host: www.emergingthreats.net\r\n\r\n";
1349 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1350 "Host: www.openinfosecfoundation.org\r\n\r\n";
1351 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1352 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1360 memset(&th_v, 0,
sizeof(th_v));
1361 memset(&f, 0,
sizeof(f));
1362 memset(&ssn, 0,
sizeof(ssn));
1369 f.
proto = IPPROTO_TCP;
1424 static int LuaMatchTest02a(
void)
1426 const char script[] =
"function init (args)\n"
1427 " local needs = {}\n"
1428 " needs[\"payload\"] = tostring(true)\n"
1429 " needs[\"flowvar\"] = {\"cnt\"}\n"
1433 "function match(args)\n"
1434 " a = SCFlowvarGet(0)\n"
1436 " a = tostring(tonumber(a)+1)\n"
1438 " SCFlowvarSet(0, a, #a)\n"
1440 " a = tostring(1)\n"
1442 " SCFlowvarSet(0, a, #a)\n"
1445 " print (\"pre check: \" .. (a))\n"
1446 " if tonumber(a) == 2 then\n"
1447 " print \"match\"\n"
1453 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1454 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1455 "Host: www.emergingthreats.net\r\n\r\n";
1456 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1457 "Host: www.openinfosecfoundation.org\r\n\r\n";
1458 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1459 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1467 memset(&th_v, 0,
sizeof(th_v));
1468 memset(&f, 0,
sizeof(f));
1469 memset(&ssn, 0,
sizeof(ssn));
1476 f.
proto = IPPROTO_TCP;
1530 static int LuaMatchTest03(
void)
1532 const char script[] =
"function init (args)\n"
1533 " local needs = {}\n"
1534 " needs[\"packet\"] = tostring(true)\n"
1535 " needs[\"flowvar\"] = {\"cnt\"}\n"
1539 "function match(args)\n"
1540 " a = ScFlowvarGet(0)\n"
1542 " a = tostring(tonumber(a)+1)\n"
1544 " ScFlowvarSet(0, a, #a)\n"
1546 " a = tostring(1)\n"
1548 " ScFlowvarSet(0, a, #a)\n"
1551 " print (\"pre check: \" .. (a))\n"
1552 " if tonumber(a) == 2 then\n"
1553 " print \"match\"\n"
1559 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1560 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1561 "Host: www.emergingthreats.net\r\n\r\n";
1562 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1563 "Host: www.openinfosecfoundation.org\r\n\r\n";
1564 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1565 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1573 memset(&th_v, 0,
sizeof(th_v));
1574 memset(&f, 0,
sizeof(f));
1575 memset(&ssn, 0,
sizeof(ssn));
1582 f.
proto = IPPROTO_TCP;
1636 static int LuaMatchTest03a(
void)
1638 const char script[] =
"function init (args)\n"
1639 " local needs = {}\n"
1640 " needs[\"packet\"] = tostring(true)\n"
1641 " needs[\"flowvar\"] = {\"cnt\"}\n"
1645 "function match(args)\n"
1646 " a = SCFlowvarGet(0)\n"
1648 " a = tostring(tonumber(a)+1)\n"
1650 " SCFlowvarSet(0, a, #a)\n"
1652 " a = tostring(1)\n"
1654 " SCFlowvarSet(0, a, #a)\n"
1657 " print (\"pre check: \" .. (a))\n"
1658 " if tonumber(a) == 2 then\n"
1659 " print \"match\"\n"
1665 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1666 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1667 "Host: www.emergingthreats.net\r\n\r\n";
1668 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1669 "Host: www.openinfosecfoundation.org\r\n\r\n";
1670 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1671 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1679 memset(&th_v, 0,
sizeof(th_v));
1680 memset(&f, 0,
sizeof(f));
1681 memset(&ssn, 0,
sizeof(ssn));
1688 f.
proto = IPPROTO_TCP;
1741 static int LuaMatchTest04(
void)
1743 const char script[] =
"function init (args)\n"
1744 " local needs = {}\n"
1745 " needs[\"http.request_headers\"] = tostring(true)\n"
1746 " needs[\"flowint\"] = {\"cnt\"}\n"
1750 "function match(args)\n"
1751 " print \"inspecting\""
1752 " a = ScFlowintGet(0)\n"
1754 " ScFlowintSet(0, a + 1)\n"
1756 " ScFlowintSet(0, 1)\n"
1759 " a = ScFlowintGet(0)\n"
1761 " print \"match\"\n"
1767 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1768 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1769 "Host: www.emergingthreats.net\r\n\r\n";
1770 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1771 "Host: www.openinfosecfoundation.org\r\n\r\n";
1772 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1773 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1783 memset(&th_v, 0,
sizeof(th_v));
1784 memset(&f, 0,
sizeof(f));
1785 memset(&ssn, 0,
sizeof(ssn));
1792 f.
proto = IPPROTO_TCP;
1857 static int LuaMatchTest04a(
void)
1859 const char script[] =
"function init (args)\n"
1860 " local needs = {}\n"
1861 " needs[\"http.request_headers\"] = tostring(true)\n"
1862 " needs[\"flowint\"] = {\"cnt\"}\n"
1866 "function match(args)\n"
1867 " print \"inspecting\""
1868 " a = SCFlowintGet(0)\n"
1870 " SCFlowintSet(0, a + 1)\n"
1872 " SCFlowintSet(0, 1)\n"
1875 " a = SCFlowintGet(0)\n"
1877 " print \"match\"\n"
1883 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1884 uint8_t httpbuf1[] =
1885 "POST / HTTP/1.1\r\n"
1886 "Host: www.emergingthreats.net\r\n\r\n";
1887 uint8_t httpbuf2[] =
1888 "POST / HTTP/1.1\r\n"
1889 "Host: www.openinfosecfoundation.org\r\n\r\n";
1890 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1891 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1901 memset(&th_v, 0,
sizeof(th_v));
1902 memset(&f, 0,
sizeof(f));
1903 memset(&ssn, 0,
sizeof(ssn));
1910 f.
proto = IPPROTO_TCP;
1975 static int LuaMatchTest05(
void)
1977 const char script[] =
"function init (args)\n"
1978 " local needs = {}\n"
1979 " needs[\"http.request_headers\"] = tostring(true)\n"
1980 " needs[\"flowint\"] = {\"cnt\"}\n"
1984 "function match(args)\n"
1985 " print \"inspecting\""
1986 " a = ScFlowintIncr(0)\n"
1988 " print \"match\"\n"
1994 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1995 uint8_t httpbuf1[] =
1996 "POST / HTTP/1.1\r\n"
1997 "Host: www.emergingthreats.net\r\n\r\n";
1998 uint8_t httpbuf2[] =
1999 "POST / HTTP/1.1\r\n"
2000 "Host: www.openinfosecfoundation.org\r\n\r\n";
2001 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2002 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2012 memset(&th_v, 0,
sizeof(th_v));
2013 memset(&f, 0,
sizeof(f));
2014 memset(&ssn, 0,
sizeof(ssn));
2021 f.
proto = IPPROTO_TCP;
2086 static int LuaMatchTest05a(
void)
2088 const char script[] =
"function init (args)\n"
2089 " local needs = {}\n"
2090 " needs[\"http.request_headers\"] = tostring(true)\n"
2091 " needs[\"flowint\"] = {\"cnt\"}\n"
2095 "function match(args)\n"
2096 " print \"inspecting\""
2097 " a = SCFlowintIncr(0)\n"
2099 " print \"match\"\n"
2105 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2106 uint8_t httpbuf1[] =
2107 "POST / HTTP/1.1\r\n"
2108 "Host: www.emergingthreats.net\r\n\r\n";
2109 uint8_t httpbuf2[] =
2110 "POST / HTTP/1.1\r\n"
2111 "Host: www.openinfosecfoundation.org\r\n\r\n";
2112 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2113 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2123 memset(&th_v, 0,
sizeof(th_v));
2124 memset(&f, 0,
sizeof(f));
2125 memset(&ssn, 0,
sizeof(ssn));
2132 f.
proto = IPPROTO_TCP;
2197 static int LuaMatchTest06(
void)
2199 const char script[] =
"function init (args)\n"
2200 " local needs = {}\n"
2201 " needs[\"http.request_headers\"] = tostring(true)\n"
2202 " needs[\"flowint\"] = {\"cnt\"}\n"
2206 "function match(args)\n"
2207 " print \"inspecting\""
2208 " a = ScFlowintGet(0)\n"
2209 " if a == nil then\n"
2210 " print \"new var set to 2\""
2211 " ScFlowintSet(0, 2)\n"
2213 " a = ScFlowintDecr(0)\n"
2215 " print \"match\"\n"
2221 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2222 uint8_t httpbuf1[] =
2223 "POST / HTTP/1.1\r\n"
2224 "Host: www.emergingthreats.net\r\n\r\n";
2225 uint8_t httpbuf2[] =
2226 "POST / HTTP/1.1\r\n"
2227 "Host: www.openinfosecfoundation.org\r\n\r\n";
2228 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2229 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2239 memset(&th_v, 0,
sizeof(th_v));
2240 memset(&f, 0,
sizeof(f));
2241 memset(&ssn, 0,
sizeof(ssn));
2248 f.
proto = IPPROTO_TCP;
2313 static int LuaMatchTest06a(
void)
2315 const char script[] =
"function init (args)\n"
2316 " local needs = {}\n"
2317 " needs[\"http.request_headers\"] = tostring(true)\n"
2318 " needs[\"flowint\"] = {\"cnt\"}\n"
2322 "function match(args)\n"
2323 " print \"inspecting\""
2324 " a = SCFlowintGet(0)\n"
2325 " if a == nil then\n"
2326 " print \"new var set to 2\""
2327 " SCFlowintSet(0, 2)\n"
2329 " a = SCFlowintDecr(0)\n"
2331 " print \"match\"\n"
2337 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2338 uint8_t httpbuf1[] =
2339 "POST / HTTP/1.1\r\n"
2340 "Host: www.emergingthreats.net\r\n\r\n";
2341 uint8_t httpbuf2[] =
2342 "POST / HTTP/1.1\r\n"
2343 "Host: www.openinfosecfoundation.org\r\n\r\n";
2344 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2345 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2355 memset(&th_v, 0,
sizeof(th_v));
2356 memset(&f, 0,
sizeof(f));
2357 memset(&ssn, 0,
sizeof(ssn));
2364 f.
proto = IPPROTO_TCP;
2428 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)
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.
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.