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_REQUEST BIT_U32(16)
118 #define FLAG_DATATYPE_DNS_RESPONSE BIT_U32(17)
119 #define FLAG_DATATYPE_SSH BIT_U32(19)
120 #define FLAG_DATATYPE_SMTP BIT_U32(20)
121 #define FLAG_DATATYPE_DNP3 BIT_U32(21)
122 #define FLAG_DATATYPE_BUFFER BIT_U32(22)
123 #define FLAG_ERROR_LOGGED BIT_U32(23)
124 #define FLAG_BLOCKED_FUNCTION_LOGGED BIT_U32(24)
125 #define FLAG_INSTRUCTION_LIMIT_LOGGED BIT_U32(25)
126 #define FLAG_MEMORY_LIMIT_LOGGED BIT_U32(26)
128 #define DEFAULT_LUA_ALLOC_LIMIT 500000
129 #define DEFAULT_LUA_INSTRUCTION_LIMIT 500000
134 int size = lua_gettop(state);
135 printf(
"%s: size %d\n", prefix, size);
137 for (
int i = 1; i <= size; i++) {
138 int type = lua_type(state, i);
139 printf(
"- %s: Stack size=%d, level=%d, type=%d, ", prefix, size, i,
type);
143 printf(
"function %s", lua_tostring(state, i));
146 printf(
"bool %s", lua_toboolean(state, i) ?
"true" :
"false");
149 printf(
"number %g", lua_tonumber(state, i));
152 printf(
"string `%s'", lua_tostring(state, i));
155 printf(
"table `%s'", lua_tostring(state, i));
158 printf(
"other %s", lua_typename(state,
type));
170 static int DetectLuaRunMatch(
176 if (lua_pcall(tlua->
luastate, 1, 1, 0) != 0) {
177 const char *reason = lua_tostring(tlua->
luastate, -1);
188 reason =
"memory limit exceeded";
196 if (!(tlua->
flags & flag)) {
197 SCLogWarning(
"Lua script failed to run successfully: %s", reason);
202 while (lua_gettop(tlua->
luastate) > 0) {
211 if (lua_gettop(tlua->
luastate) > 0) {
213 if (lua_type(tlua->
luastate, 1) == LUA_TNUMBER) {
214 double script_ret = lua_tonumber(tlua->
luastate, 1);
218 if (script_ret == 1.0)
222 }
else if (lua_type(tlua->
luastate, 1) == LUA_TTABLE) {
225 while (lua_next(tlua->
luastate, -2)) {
226 v = lua_tostring(tlua->
luastate, -1);
228 k = lua_tostring(tlua->
luastate, -1);
235 if (strcmp(k,
"retval") == 0) {
239 "for \"retval\" from LUA return table: '%s'",
263 while (lua_gettop(tlua->
luastate) > 0) {
276 if (buffer == NULL || buffer_len == 0)
291 lua_getglobal(tlua->
luastate,
"match");
294 lua_pushliteral(tlua->
luastate,
"offset");
302 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
332 flags = STREAM_TOSERVER;
334 flags = STREAM_TOCLIENT;
353 lua_getglobal(tlua->
luastate,
"match");
358 if (htp_state != NULL && htp_state->
connp != NULL) {
363 for ( ; idx < total_txs; idx++) {
370 lua_pushliteral(tlua->
luastate,
"http.request_line");
380 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
405 lua_getglobal(tlua->
luastate,
"match");
410 if (htp_state != NULL && htp_state->
connp != NULL) {
416 lua_pushliteral(tlua->
luastate,
"http.request_line");
426 SCReturnInt(DetectLuaRunMatch(det_ctx, lua, tlua));
442 void *state,
void *txv,
const Signature *s,
445 return DetectLuaAppMatchCommon(det_ctx, f,
flags, state, s,
ctx);
451 static const char *ut_script = NULL;
454 static void *DetectLuaThreadInit(
void *data)
484 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
sid));
485 lua_setglobal(t->
luastate,
"SCRuleSid");
486 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
rev));
487 lua_setglobal(t->
luastate,
"SCRuleRev");
488 lua_pushinteger(t->
luastate, (lua_Integer)(lua->
gid));
489 lua_setglobal(t->
luastate,
"SCRuleGid");
493 if (ut_script != NULL) {
494 status = luaL_loadbuffer(t->
luastate, ut_script, strlen(ut_script),
"unittest");
511 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
517 lua_getglobal(t->
luastate,
"thread_init");
518 if (lua_isfunction(t->
luastate, -1)) {
519 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
520 SCLogError(
"couldn't run script 'thread_init' function: %s",
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);
600 if (ut_script != NULL) {
601 status = luaL_loadbuffer(luastate, ut_script, strlen(ut_script),
"unittest");
603 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
608 status = luaL_loadfile(luastate, ld->
filename);
610 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
618 if (lua_pcall(luastate, 0, 0, 0) != 0) {
619 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
623 lua_getglobal(luastate,
"init");
624 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
629 if (lua_pcall(luastate, 0, 1, 0) != 0) {
630 SCLogError(
"couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
635 if (lua_gettop(luastate) == 0) {
636 SCLogError(
"init function in script should return table, nothing returned");
639 if (lua_type(luastate, 1) != LUA_TTABLE) {
640 SCLogError(
"init function in script should return table, returned is not table");
644 lua_pushnil(luastate);
646 while (lua_next(luastate, -2)) {
647 k = lua_tostring(luastate, -2);
652 if (strcmp(k,
"flowvar") == 0) {
653 if (lua_istable(luastate, -1)) {
654 lua_pushnil(luastate);
655 while (lua_next(luastate, -2) != 0) {
657 const char *value = lua_tostring(luastate, -1);
660 lua_pop(luastate, 1);
672 lua_pop(luastate, 1);
674 }
else if (strcmp(k,
"flowint") == 0) {
675 if (lua_istable(luastate, -1)) {
676 lua_pushnil(luastate);
677 while (lua_next(luastate, -2) != 0) {
679 const char *value = lua_tostring(luastate, -1);
682 lua_pop(luastate, 1);
694 lua_pop(luastate, 1);
696 }
else if (strcmp(k,
"bytevar") == 0) {
697 if (lua_istable(luastate, -1)) {
698 lua_pushnil(luastate);
699 while (lua_next(luastate, -2) != 0) {
701 const char *value = lua_tostring(luastate, -1);
704 lua_pop(luastate, 1);
713 SCLogError(
"Unknown byte_extract or byte_math var "
714 "requested by lua script - %s",
722 lua_pop(luastate, 1);
726 bool required = lua_toboolean(luastate, -1);
727 lua_pop(luastate, 1);
732 if (strcmp(k,
"packet") == 0) {
734 }
else if (strcmp(k,
"payload") == 0) {
736 }
else if (strcmp(k,
"buffer") == 0) {
744 }
else if (strcmp(k,
"stream") == 0) {
753 }
else if (strncmp(k,
"http", 4) == 0) {
756 "can just inspect script against one app layer proto like HTTP at a time");
759 if (ld->
flags != 0) {
760 SCLogError(
"when inspecting HTTP buffers only a single buffer can be inspected");
767 if (strcmp(k,
"http.uri") == 0)
770 else if (strcmp(k,
"http.uri.raw") == 0)
773 else if (strcmp(k,
"http.request_line") == 0)
776 else if (strcmp(k,
"http.request_headers") == 0)
779 else if (strcmp(k,
"http.request_headers.raw") == 0)
782 else if (strcmp(k,
"http.request_cookie") == 0)
785 else if (strcmp(k,
"http.request_user_agent") == 0)
788 else if (strcmp(k,
"http.request_body") == 0)
791 else if (strcmp(k,
"http.response_body") == 0)
794 else if (strcmp(k,
"http.response_cookie") == 0)
797 else if (strcmp(k,
"http.response_headers") == 0)
800 else if (strcmp(k,
"http.response_headers.raw") == 0)
804 SCLogError(
"unsupported http data type %s", k);
813 }
else if (strncmp(k,
"dns", 3) == 0) {
817 if (strcmp(k,
"dns.request") == 0)
819 else if (strcmp(k,
"dns.response") == 0)
823 SCLogError(
"unsupported dns data type %s", k);
831 }
else if (strncmp(k,
"tls", 3) == 0) {
835 }
else if (strncmp(k,
"ssh", 3) == 0) {
841 }
else if (strncmp(k,
"smtp", 4) == 0) {
847 }
else if (strncmp(k,
"dnp3", 4) == 0) {
860 lua_pop(luastate, 1);
884 (void)
ConfGetBool(
"security.lua.allow-rules", &enabled);
886 SCLogError(
"Lua rules disabled by security configuration: security.lua.allow-rules");
897 (void)
ConfGetInt(
"security.lua.max-bytes", &lua_alloc_limit);
898 (void)
ConfGetInt(
"security.lua.max-instructions", &lua_instruction_limit);
902 int allow_restricted_functions = 0;
903 (void)
ConfGetBool(
"security.lua.allow-restricted-functions", &allow_restricted_functions);
906 if (DetectLuaSetupPrime(
de_ctx, lua, s) == -1) {
911 DetectLuaThreadInit, (
void *)lua,
912 DetectLuaThreadFree, 0);
956 }
else if (lua->
flags &
976 list = g_smtp_generic_list_id;
997 DetectLuaFree(
de_ctx, lua);
1037 for (uint16_t i = 0; i < lua->
flowints; i++) {
1040 for (uint16_t i = 0; i < lua->
flowvars; i++) {
1054 static int LuaMatchTest01(
void)
1058 const char script[] =
1059 "function init (args)\n"
1060 " local needs = {}\n"
1061 " needs[\"http.request_headers\"] = tostring(true)\n"
1062 " needs[\"flowvar\"] = {\"cnt\"}\n"
1066 "function match(args)\n"
1067 " a = ScFlowvarGet(0)\n"
1069 " a = tostring(tonumber(a)+1)\n"
1071 " ScFlowvarSet(0, a, #a)\n"
1073 " a = tostring(1)\n"
1075 " ScFlowvarSet(0, a, #a)\n"
1078 " print (\"pre check: \" .. (a))\n"
1079 " if tonumber(a) == 2 then\n"
1080 " print \"match\"\n"
1086 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1087 uint8_t httpbuf1[] =
1088 "POST / HTTP/1.1\r\n"
1089 "Host: www.emergingthreats.net\r\n\r\n";
1090 uint8_t httpbuf2[] =
1091 "POST / HTTP/1.1\r\n"
1092 "Host: www.openinfosecfoundation.org\r\n\r\n";
1093 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1094 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1104 memset(&th_v, 0,
sizeof(th_v));
1105 memset(&f, 0,
sizeof(f));
1106 memset(&ssn, 0,
sizeof(ssn));
1113 f.
proto = IPPROTO_TCP;
1179 static int LuaMatchTest01a(
void)
1181 const char script[] =
"function init (args)\n"
1182 " local needs = {}\n"
1183 " needs[\"http.request_headers\"] = tostring(true)\n"
1184 " needs[\"flowvar\"] = {\"cnt\"}\n"
1188 "function match(args)\n"
1189 " a = SCFlowvarGet(0)\n"
1191 " a = tostring(tonumber(a)+1)\n"
1193 " SCFlowvarSet(0, a, #a)\n"
1195 " a = tostring(1)\n"
1197 " SCFlowvarSet(0, a, #a)\n"
1200 " print (\"pre check: \" .. (a))\n"
1201 " if tonumber(a) == 2 then\n"
1202 " print \"match\"\n"
1208 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1209 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1210 "Host: www.emergingthreats.net\r\n\r\n";
1211 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1212 "Host: www.openinfosecfoundation.org\r\n\r\n";
1213 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1214 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1224 memset(&th_v, 0,
sizeof(th_v));
1225 memset(&f, 0,
sizeof(f));
1226 memset(&ssn, 0,
sizeof(ssn));
1233 f.
proto = IPPROTO_TCP;
1300 static int LuaMatchTest02(
void)
1302 const char script[] =
"function init (args)\n"
1303 " local needs = {}\n"
1304 " needs[\"payload\"] = tostring(true)\n"
1305 " needs[\"flowvar\"] = {\"cnt\"}\n"
1309 "function match(args)\n"
1310 " a = ScFlowvarGet(0)\n"
1312 " a = tostring(tonumber(a)+1)\n"
1314 " ScFlowvarSet(0, a, #a)\n"
1316 " a = tostring(1)\n"
1318 " ScFlowvarSet(0, a, #a)\n"
1321 " print (\"pre check: \" .. (a))\n"
1322 " if tonumber(a) == 2 then\n"
1323 " print \"match\"\n"
1329 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1330 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1331 "Host: www.emergingthreats.net\r\n\r\n";
1332 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1333 "Host: www.openinfosecfoundation.org\r\n\r\n";
1334 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1335 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1343 memset(&th_v, 0,
sizeof(th_v));
1344 memset(&f, 0,
sizeof(f));
1345 memset(&ssn, 0,
sizeof(ssn));
1352 f.
proto = IPPROTO_TCP;
1407 static int LuaMatchTest02a(
void)
1409 const char script[] =
"function init (args)\n"
1410 " local needs = {}\n"
1411 " needs[\"payload\"] = tostring(true)\n"
1412 " needs[\"flowvar\"] = {\"cnt\"}\n"
1416 "function match(args)\n"
1417 " a = SCFlowvarGet(0)\n"
1419 " a = tostring(tonumber(a)+1)\n"
1421 " SCFlowvarSet(0, a, #a)\n"
1423 " a = tostring(1)\n"
1425 " SCFlowvarSet(0, a, #a)\n"
1428 " print (\"pre check: \" .. (a))\n"
1429 " if tonumber(a) == 2 then\n"
1430 " print \"match\"\n"
1436 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1437 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1438 "Host: www.emergingthreats.net\r\n\r\n";
1439 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1440 "Host: www.openinfosecfoundation.org\r\n\r\n";
1441 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1442 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1450 memset(&th_v, 0,
sizeof(th_v));
1451 memset(&f, 0,
sizeof(f));
1452 memset(&ssn, 0,
sizeof(ssn));
1459 f.
proto = IPPROTO_TCP;
1513 static int LuaMatchTest03(
void)
1515 const char script[] =
"function init (args)\n"
1516 " local needs = {}\n"
1517 " needs[\"packet\"] = tostring(true)\n"
1518 " needs[\"flowvar\"] = {\"cnt\"}\n"
1522 "function match(args)\n"
1523 " a = ScFlowvarGet(0)\n"
1525 " a = tostring(tonumber(a)+1)\n"
1527 " ScFlowvarSet(0, a, #a)\n"
1529 " a = tostring(1)\n"
1531 " ScFlowvarSet(0, a, #a)\n"
1534 " print (\"pre check: \" .. (a))\n"
1535 " if tonumber(a) == 2 then\n"
1536 " print \"match\"\n"
1542 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1543 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1544 "Host: www.emergingthreats.net\r\n\r\n";
1545 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1546 "Host: www.openinfosecfoundation.org\r\n\r\n";
1547 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1548 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1556 memset(&th_v, 0,
sizeof(th_v));
1557 memset(&f, 0,
sizeof(f));
1558 memset(&ssn, 0,
sizeof(ssn));
1565 f.
proto = IPPROTO_TCP;
1619 static int LuaMatchTest03a(
void)
1621 const char script[] =
"function init (args)\n"
1622 " local needs = {}\n"
1623 " needs[\"packet\"] = tostring(true)\n"
1624 " needs[\"flowvar\"] = {\"cnt\"}\n"
1628 "function match(args)\n"
1629 " a = SCFlowvarGet(0)\n"
1631 " a = tostring(tonumber(a)+1)\n"
1633 " SCFlowvarSet(0, a, #a)\n"
1635 " a = tostring(1)\n"
1637 " SCFlowvarSet(0, a, #a)\n"
1640 " print (\"pre check: \" .. (a))\n"
1641 " if tonumber(a) == 2 then\n"
1642 " print \"match\"\n"
1648 char sig[] =
"alert tcp any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1649 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1650 "Host: www.emergingthreats.net\r\n\r\n";
1651 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1652 "Host: www.openinfosecfoundation.org\r\n\r\n";
1653 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1654 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1662 memset(&th_v, 0,
sizeof(th_v));
1663 memset(&f, 0,
sizeof(f));
1664 memset(&ssn, 0,
sizeof(ssn));
1671 f.
proto = IPPROTO_TCP;
1724 static int LuaMatchTest04(
void)
1726 const char script[] =
"function init (args)\n"
1727 " local needs = {}\n"
1728 " needs[\"http.request_headers\"] = tostring(true)\n"
1729 " needs[\"flowint\"] = {\"cnt\"}\n"
1733 "function match(args)\n"
1734 " print \"inspecting\""
1735 " a = ScFlowintGet(0)\n"
1737 " ScFlowintSet(0, a + 1)\n"
1739 " ScFlowintSet(0, 1)\n"
1742 " a = ScFlowintGet(0)\n"
1744 " print \"match\"\n"
1750 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1751 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n"
1752 "Host: www.emergingthreats.net\r\n\r\n";
1753 uint8_t httpbuf2[] =
"POST / HTTP/1.1\r\n"
1754 "Host: www.openinfosecfoundation.org\r\n\r\n";
1755 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1756 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1766 memset(&th_v, 0,
sizeof(th_v));
1767 memset(&f, 0,
sizeof(f));
1768 memset(&ssn, 0,
sizeof(ssn));
1775 f.
proto = IPPROTO_TCP;
1840 static int LuaMatchTest04a(
void)
1842 const char script[] =
"function init (args)\n"
1843 " local needs = {}\n"
1844 " needs[\"http.request_headers\"] = tostring(true)\n"
1845 " needs[\"flowint\"] = {\"cnt\"}\n"
1849 "function match(args)\n"
1850 " print \"inspecting\""
1851 " a = SCFlowintGet(0)\n"
1853 " SCFlowintSet(0, a + 1)\n"
1855 " SCFlowintSet(0, 1)\n"
1858 " a = SCFlowintGet(0)\n"
1860 " print \"match\"\n"
1866 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1867 uint8_t httpbuf1[] =
1868 "POST / HTTP/1.1\r\n"
1869 "Host: www.emergingthreats.net\r\n\r\n";
1870 uint8_t httpbuf2[] =
1871 "POST / HTTP/1.1\r\n"
1872 "Host: www.openinfosecfoundation.org\r\n\r\n";
1873 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1874 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1884 memset(&th_v, 0,
sizeof(th_v));
1885 memset(&f, 0,
sizeof(f));
1886 memset(&ssn, 0,
sizeof(ssn));
1893 f.
proto = IPPROTO_TCP;
1958 static int LuaMatchTest05(
void)
1960 const char script[] =
"function init (args)\n"
1961 " local needs = {}\n"
1962 " needs[\"http.request_headers\"] = tostring(true)\n"
1963 " needs[\"flowint\"] = {\"cnt\"}\n"
1967 "function match(args)\n"
1968 " print \"inspecting\""
1969 " a = ScFlowintIncr(0)\n"
1971 " print \"match\"\n"
1977 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
1978 uint8_t httpbuf1[] =
1979 "POST / HTTP/1.1\r\n"
1980 "Host: www.emergingthreats.net\r\n\r\n";
1981 uint8_t httpbuf2[] =
1982 "POST / HTTP/1.1\r\n"
1983 "Host: www.openinfosecfoundation.org\r\n\r\n";
1984 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1985 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1995 memset(&th_v, 0,
sizeof(th_v));
1996 memset(&f, 0,
sizeof(f));
1997 memset(&ssn, 0,
sizeof(ssn));
2004 f.
proto = IPPROTO_TCP;
2069 static int LuaMatchTest05a(
void)
2071 const char script[] =
"function init (args)\n"
2072 " local needs = {}\n"
2073 " needs[\"http.request_headers\"] = tostring(true)\n"
2074 " needs[\"flowint\"] = {\"cnt\"}\n"
2078 "function match(args)\n"
2079 " print \"inspecting\""
2080 " a = SCFlowintIncr(0)\n"
2082 " print \"match\"\n"
2088 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2089 uint8_t httpbuf1[] =
2090 "POST / HTTP/1.1\r\n"
2091 "Host: www.emergingthreats.net\r\n\r\n";
2092 uint8_t httpbuf2[] =
2093 "POST / HTTP/1.1\r\n"
2094 "Host: www.openinfosecfoundation.org\r\n\r\n";
2095 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2096 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2106 memset(&th_v, 0,
sizeof(th_v));
2107 memset(&f, 0,
sizeof(f));
2108 memset(&ssn, 0,
sizeof(ssn));
2115 f.
proto = IPPROTO_TCP;
2180 static int LuaMatchTest06(
void)
2182 const char script[] =
"function init (args)\n"
2183 " local needs = {}\n"
2184 " needs[\"http.request_headers\"] = tostring(true)\n"
2185 " needs[\"flowint\"] = {\"cnt\"}\n"
2189 "function match(args)\n"
2190 " print \"inspecting\""
2191 " a = ScFlowintGet(0)\n"
2192 " if a == nil then\n"
2193 " print \"new var set to 2\""
2194 " ScFlowintSet(0, 2)\n"
2196 " a = ScFlowintDecr(0)\n"
2198 " print \"match\"\n"
2204 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2205 uint8_t httpbuf1[] =
2206 "POST / HTTP/1.1\r\n"
2207 "Host: www.emergingthreats.net\r\n\r\n";
2208 uint8_t httpbuf2[] =
2209 "POST / HTTP/1.1\r\n"
2210 "Host: www.openinfosecfoundation.org\r\n\r\n";
2211 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2212 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2222 memset(&th_v, 0,
sizeof(th_v));
2223 memset(&f, 0,
sizeof(f));
2224 memset(&ssn, 0,
sizeof(ssn));
2231 f.
proto = IPPROTO_TCP;
2296 static int LuaMatchTest06a(
void)
2298 const char script[] =
"function init (args)\n"
2299 " local needs = {}\n"
2300 " needs[\"http.request_headers\"] = tostring(true)\n"
2301 " needs[\"flowint\"] = {\"cnt\"}\n"
2305 "function match(args)\n"
2306 " print \"inspecting\""
2307 " a = SCFlowintGet(0)\n"
2308 " if a == nil then\n"
2309 " print \"new var set to 2\""
2310 " SCFlowintSet(0, 2)\n"
2312 " a = SCFlowintDecr(0)\n"
2314 " print \"match\"\n"
2320 char sig[] =
"alert http any any -> any any (flow:to_server; lua:unittest; sid:1;)";
2321 uint8_t httpbuf1[] =
2322 "POST / HTTP/1.1\r\n"
2323 "Host: www.emergingthreats.net\r\n\r\n";
2324 uint8_t httpbuf2[] =
2325 "POST / HTTP/1.1\r\n"
2326 "Host: www.openinfosecfoundation.org\r\n\r\n";
2327 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
2328 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
2338 memset(&th_v, 0,
sizeof(th_v));
2339 memset(&f, 0,
sizeof(f));
2340 memset(&ssn, 0,
sizeof(ssn));
2347 f.
proto = IPPROTO_TCP;
2411 void DetectLuaRegisterTests(
void)
#define FLAG_MEMORY_LIMIT_LOGGED
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
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)
union FlowVar_::@107 data
#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 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.