mirror of https://github.com/OISF/suricata
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1865 lines
54 KiB
C
1865 lines
54 KiB
C
12 years ago
|
/* Copyright (C) 2007-2013 Open Information Security Foundation
|
||
13 years ago
|
*
|
||
|
* You can copy, redistribute or modify this Program under the terms of
|
||
|
* the GNU General Public License version 2 as published by the Free
|
||
|
* Software Foundation.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* version 2 along with this program; if not, write to the Free Software
|
||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||
|
* 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* \file
|
||
|
*
|
||
|
* \author Victor Julien <victor@inliniac.net>
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include "suricata-common.h"
|
||
13 years ago
|
#include "conf.h"
|
||
|
|
||
13 years ago
|
#include "threads.h"
|
||
|
#include "debug.h"
|
||
|
#include "decode.h"
|
||
|
|
||
|
#include "detect.h"
|
||
|
#include "detect-parse.h"
|
||
|
|
||
|
#include "detect-engine.h"
|
||
|
#include "detect-engine-mpm.h"
|
||
|
#include "detect-engine-state.h"
|
||
|
|
||
|
#include "flow.h"
|
||
|
#include "flow-var.h"
|
||
|
#include "flow-util.h"
|
||
|
|
||
|
#include "util-debug.h"
|
||
|
#include "util-spm-bm.h"
|
||
|
#include "util-print.h"
|
||
|
|
||
|
#include "util-unittest.h"
|
||
|
#include "util-unittest-helper.h"
|
||
|
|
||
|
#include "app-layer.h"
|
||
12 years ago
|
#include "app-layer-parser.h"
|
||
13 years ago
|
|
||
|
#include "stream-tcp.h"
|
||
|
|
||
11 years ago
|
#include "detect-lua.h"
|
||
|
#include "detect-lua-extensions.h"
|
||
13 years ago
|
|
||
|
#include "queue.h"
|
||
13 years ago
|
#include "util-cpu.h"
|
||
12 years ago
|
#include "util-var-name.h"
|
||
13 years ago
|
|
||
11 years ago
|
#ifndef HAVE_LUA
|
||
13 years ago
|
|
||
11 years ago
|
static int DetectLuaSetupNoSupport (DetectEngineCtx *a, Signature *b, char *c)
|
||
11 years ago
|
{
|
||
11 years ago
|
SCLogError(SC_ERR_NO_LUA_SUPPORT, "no Lua support built in, needed for lua/luajit keyword");
|
||
13 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* \brief Registration function for keyword: luajit
|
||
|
*/
|
||
11 years ago
|
void DetectLuaRegister(void)
|
||
11 years ago
|
{
|
||
11 years ago
|
sigmatch_table[DETECT_LUA].name = "lua";
|
||
|
sigmatch_table[DETECT_LUA].alias = "luajit";
|
||
|
sigmatch_table[DETECT_LUA].Setup = DetectLuaSetupNoSupport;
|
||
|
sigmatch_table[DETECT_LUA].Free = NULL;
|
||
|
sigmatch_table[DETECT_LUA].RegisterTests = NULL;
|
||
|
sigmatch_table[DETECT_LUA].flags = SIGMATCH_NOT_BUILT;
|
||
|
|
||
|
SCLogDebug("registering lua rule option");
|
||
13 years ago
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
#else /* HAVE_LUA */
|
||
13 years ago
|
|
||
11 years ago
|
#ifdef HAVE_LUAJIT
|
||
13 years ago
|
#include "util-pool.h"
|
||
|
|
||
11 years ago
|
/** \brief lua_State pool
|
||
|
*
|
||
11 years ago
|
* Lua requires states to be alloc'd in memory <2GB. For this reason we
|
||
11 years ago
|
* prealloc the states early during engine startup so we have a better chance
|
||
|
* of getting the states. We protect the pool with a lock as the detect
|
||
|
* threads access it during their init and cleanup.
|
||
|
*
|
||
|
* Pool size is automagically determined based on number of keyword occurences,
|
||
|
* cpus/cores and rule reloads being enabled or not.
|
||
|
*
|
||
|
* Alternatively, the "detect-engine.luajit-states" var can be set.
|
||
|
*/
|
||
|
static Pool *luajit_states = NULL;
|
||
|
static pthread_mutex_t luajit_states_lock = SCMUTEX_INITIALIZER;
|
||
|
|
||
|
#endif /* HAVE_LUAJIT */
|
||
|
|
||
11 years ago
|
#include "util-lua.h"
|
||
|
|
||
11 years ago
|
static int DetectLuaMatch (ThreadVars *, DetectEngineThreadCtx *,
|
||
13 years ago
|
Packet *, Signature *, SigMatch *);
|
||
11 years ago
|
static int DetectLuaSetup (DetectEngineCtx *, Signature *, char *);
|
||
|
static void DetectLuaRegisterTests(void);
|
||
|
static void DetectLuaFree(void *);
|
||
13 years ago
|
|
||
|
/**
|
||
|
* \brief Registration function for keyword: luajit
|
||
|
*/
|
||
11 years ago
|
void DetectLuaRegister(void)
|
||
11 years ago
|
{
|
||
11 years ago
|
sigmatch_table[DETECT_LUA].name = "lua";
|
||
|
sigmatch_table[DETECT_LUA].alias = "luajit";
|
||
|
sigmatch_table[DETECT_LUA].desc = "match via a luajit script";
|
||
|
sigmatch_table[DETECT_LUA].url = "https://redmine.openinfosecfoundation.org/projects/suricata/wiki/Lua_scripting";
|
||
|
sigmatch_table[DETECT_LUA].Match = DetectLuaMatch;
|
||
|
sigmatch_table[DETECT_LUA].Setup = DetectLuaSetup;
|
||
|
sigmatch_table[DETECT_LUA].Free = DetectLuaFree;
|
||
|
sigmatch_table[DETECT_LUA].RegisterTests = DetectLuaRegisterTests;
|
||
13 years ago
|
|
||
|
SCLogDebug("registering luajit rule option");
|
||
|
return;
|
||
|
}
|
||
|
|
||
13 years ago
|
#define DATATYPE_PACKET (1<<0)
|
||
|
#define DATATYPE_PAYLOAD (1<<1)
|
||
|
#define DATATYPE_STREAM (1<<2)
|
||
|
|
||
|
#define DATATYPE_HTTP_URI (1<<3)
|
||
|
#define DATATYPE_HTTP_URI_RAW (1<<4)
|
||
|
|
||
|
#define DATATYPE_HTTP_REQUEST_HEADERS (1<<5)
|
||
|
#define DATATYPE_HTTP_REQUEST_HEADERS_RAW (1<<6)
|
||
|
#define DATATYPE_HTTP_REQUEST_COOKIE (1<<7)
|
||
|
#define DATATYPE_HTTP_REQUEST_UA (1<<8)
|
||
|
|
||
|
#define DATATYPE_HTTP_REQUEST_LINE (1<<9)
|
||
|
#define DATATYPE_HTTP_REQUEST_BODY (1<<10)
|
||
|
|
||
|
#define DATATYPE_HTTP_RESPONSE_COOKIE (1<<11)
|
||
|
#define DATATYPE_HTTP_RESPONSE_BODY (1<<12)
|
||
13 years ago
|
|
||
13 years ago
|
#define DATATYPE_HTTP_RESPONSE_HEADERS (1<<13)
|
||
|
#define DATATYPE_HTTP_RESPONSE_HEADERS_RAW (1<<14)
|
||
|
|
||
11 years ago
|
#ifdef HAVE_LUAJIT
|
||
11 years ago
|
static void *LuaStatePoolAlloc(void)
|
||
|
{
|
||
13 years ago
|
return luaL_newstate();
|
||
|
}
|
||
|
|
||
11 years ago
|
static void LuaStatePoolFree(void *d)
|
||
|
{
|
||
13 years ago
|
lua_State *s = (lua_State *)d;
|
||
|
if (s != NULL)
|
||
|
lua_close(s);
|
||
|
}
|
||
|
|
||
|
/** \brief Populate lua states pool
|
||
|
*
|
||
|
* \param num keyword instances
|
||
|
* \param reloads bool indicating we have rule reloads enabled
|
||
|
*/
|
||
11 years ago
|
int DetectLuajitSetupStatesPool(int num, int reloads)
|
||
|
{
|
||
13 years ago
|
int retval = 0;
|
||
|
pthread_mutex_lock(&luajit_states_lock);
|
||
|
|
||
|
if (luajit_states == NULL) {
|
||
|
int cnt = 0;
|
||
|
char *conf_val = NULL;
|
||
|
|
||
|
if ((ConfGet("detect-engine.luajit-states", &conf_val)) == 1) {
|
||
|
cnt = (int)atoi(conf_val);
|
||
|
} else {
|
||
|
int cpus = UtilCpuGetNumProcessorsOnline();
|
||
|
if (cpus == 0) {
|
||
|
cpus = 10;
|
||
|
}
|
||
|
cnt = num * cpus;
|
||
13 years ago
|
cnt *= 3; /* assume 3 threads per core */
|
||
13 years ago
|
|
||
|
/* alloc a bunch extra so reload can add new rules/instances */
|
||
|
if (reloads)
|
||
|
cnt *= 5;
|
||
|
}
|
||
|
|
||
13 years ago
|
luajit_states = PoolInit(0, cnt, 0, LuaStatePoolAlloc, NULL, NULL, NULL, LuaStatePoolFree);
|
||
13 years ago
|
if (luajit_states == NULL) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "luastate pool init failed, lua/luajit keywords won't work");
|
||
13 years ago
|
retval = -1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pthread_mutex_unlock(&luajit_states_lock);
|
||
|
return retval;
|
||
|
}
|
||
11 years ago
|
#endif /* HAVE_LUAJIT */
|
||
13 years ago
|
|
||
11 years ago
|
static lua_State *DetectLuaGetState(void)
|
||
11 years ago
|
{
|
||
13 years ago
|
|
||
|
lua_State *s = NULL;
|
||
11 years ago
|
#ifdef HAVE_LUAJIT
|
||
13 years ago
|
pthread_mutex_lock(&luajit_states_lock);
|
||
|
if (luajit_states != NULL)
|
||
|
s = (lua_State *)PoolGet(luajit_states);
|
||
|
pthread_mutex_unlock(&luajit_states_lock);
|
||
11 years ago
|
#else
|
||
|
s = luaL_newstate();
|
||
|
#endif
|
||
13 years ago
|
return s;
|
||
|
}
|
||
|
|
||
11 years ago
|
static void DetectLuaReturnState(lua_State *s)
|
||
11 years ago
|
{
|
||
13 years ago
|
if (s != NULL) {
|
||
11 years ago
|
#ifdef HAVE_LUAJIT
|
||
13 years ago
|
pthread_mutex_lock(&luajit_states_lock);
|
||
|
PoolReturn(luajit_states, (void *)s);
|
||
|
pthread_mutex_unlock(&luajit_states_lock);
|
||
11 years ago
|
#else
|
||
|
lua_close(s);
|
||
|
#endif
|
||
13 years ago
|
}
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
|
/** \brief dump stack from lua state to screen */
|
||
11 years ago
|
void LuaDumpStack(lua_State *state)
|
||
|
{
|
||
13 years ago
|
int size = lua_gettop(state);
|
||
|
int i;
|
||
|
|
||
|
for (i = 1; i <= size; i++) {
|
||
|
int type = lua_type(state, i);
|
||
|
printf("Stack size=%d, level=%d, type=%d, ", size, i, type);
|
||
|
|
||
|
switch (type) {
|
||
|
case LUA_TFUNCTION:
|
||
|
printf("function %s", lua_tostring(state, i) ? "true" : "false");
|
||
|
break;
|
||
|
case LUA_TBOOLEAN:
|
||
|
printf("bool %s", lua_toboolean(state, i) ? "true" : "false");
|
||
|
break;
|
||
|
case LUA_TNUMBER:
|
||
|
printf("number %g", lua_tonumber(state, i));
|
||
|
break;
|
||
|
case LUA_TSTRING:
|
||
|
printf("string `%s'", lua_tostring(state, i));
|
||
|
break;
|
||
|
case LUA_TTABLE:
|
||
|
printf("table `%s'", lua_tostring(state, i));
|
||
|
break;
|
||
|
default:
|
||
|
printf("other %s", lua_typename(state, type));
|
||
|
break;
|
||
|
|
||
|
}
|
||
|
printf("\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
int DetectLuaMatchBuffer(DetectEngineThreadCtx *det_ctx, Signature *s, SigMatch *sm,
|
||
12 years ago
|
uint8_t *buffer, uint32_t buffer_len, uint32_t offset,
|
||
11 years ago
|
Flow *f, int flow_lock)
|
||
12 years ago
|
{
|
||
13 years ago
|
SCEnter();
|
||
|
int ret = 0;
|
||
|
|
||
|
if (buffer == NULL || buffer_len == 0)
|
||
|
SCReturnInt(0);
|
||
|
|
||
11 years ago
|
DetectLuaData *luajit = (DetectLuaData *)sm->ctx;
|
||
13 years ago
|
if (luajit == NULL)
|
||
|
SCReturnInt(0);
|
||
|
|
||
11 years ago
|
DetectLuaThreadData *tluajit = (DetectLuaThreadData *)DetectThreadCtxGetKeywordThreadCtx(det_ctx, luajit->thread_ctx_id);
|
||
13 years ago
|
if (tluajit == NULL)
|
||
|
SCReturnInt(0);
|
||
|
|
||
12 years ago
|
/* setup extension data for use in lua c functions */
|
||
11 years ago
|
LuaExtensionsMatchSetup(tluajit->luastate, luajit, det_ctx, f, flow_lock);
|
||
12 years ago
|
|
||
|
/* prepare data to pass to script */
|
||
13 years ago
|
lua_getglobal(tluajit->luastate, "match");
|
||
|
lua_newtable(tluajit->luastate); /* stack at -1 */
|
||
|
|
||
|
lua_pushliteral (tluajit->luastate, "offset"); /* stack at -2 */
|
||
13 years ago
|
lua_pushnumber (tluajit->luastate, (int)(offset + 1));
|
||
13 years ago
|
lua_settable(tluajit->luastate, -3);
|
||
|
|
||
|
lua_pushstring (tluajit->luastate, luajit->buffername); /* stack at -2 */
|
||
11 years ago
|
LuaPushStringBuffer(tluajit->luastate, (const uint8_t *)buffer, (size_t)buffer_len);
|
||
13 years ago
|
lua_settable(tluajit->luastate, -3);
|
||
|
|
||
|
int retval = lua_pcall(tluajit->luastate, 1, 1, 0);
|
||
|
if (retval != 0) {
|
||
|
SCLogInfo("failed to run script: %s", lua_tostring(tluajit->luastate, -1));
|
||
|
}
|
||
|
|
||
|
/* process returns from script */
|
||
|
if (lua_gettop(tluajit->luastate) > 0) {
|
||
|
/* script returns a number (return 1 or return 0) */
|
||
|
if (lua_type(tluajit->luastate, 1) == LUA_TNUMBER) {
|
||
|
double script_ret = lua_tonumber(tluajit->luastate, 1);
|
||
|
SCLogDebug("script_ret %f", script_ret);
|
||
|
lua_pop(tluajit->luastate, 1);
|
||
|
|
||
|
if (script_ret == 1.0)
|
||
|
ret = 1;
|
||
|
|
||
|
/* script returns a table */
|
||
|
} else if (lua_type(tluajit->luastate, 1) == LUA_TTABLE) {
|
||
|
lua_pushnil(tluajit->luastate);
|
||
|
const char *k, *v;
|
||
|
while (lua_next(tluajit->luastate, -2)) {
|
||
|
v = lua_tostring(tluajit->luastate, -1);
|
||
|
lua_pop(tluajit->luastate, 1);
|
||
|
k = lua_tostring(tluajit->luastate, -1);
|
||
|
|
||
|
if (!k || !v)
|
||
|
continue;
|
||
|
|
||
|
SCLogDebug("k='%s', v='%s'", k, v);
|
||
|
|
||
|
if (strcmp(k, "retval") == 0) {
|
||
|
if (atoi(v) == 1)
|
||
|
ret = 1;
|
||
|
} else {
|
||
|
/* set flow var? */
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* pop the table */
|
||
|
lua_pop(tluajit->luastate, 1);
|
||
|
}
|
||
|
} else {
|
||
|
SCLogDebug("no stack");
|
||
|
}
|
||
|
|
||
12 years ago
|
/* clear the stack */
|
||
|
while (lua_gettop(tluajit->luastate) > 0) {
|
||
|
lua_pop(tluajit->luastate, 1);
|
||
|
}
|
||
|
|
||
13 years ago
|
if (luajit->negated) {
|
||
|
if (ret == 1)
|
||
|
ret = 0;
|
||
|
else
|
||
|
ret = 1;
|
||
|
}
|
||
|
|
||
|
SCReturnInt(ret);
|
||
|
|
||
|
}
|
||
|
|
||
13 years ago
|
/**
|
||
|
* \brief match the specified luajit
|
||
|
*
|
||
|
* \param t thread local vars
|
||
|
* \param det_ctx pattern matcher thread local data
|
||
|
* \param p packet
|
||
|
* \param s signature being inspected
|
||
11 years ago
|
* \param m sigmatch that we will cast into DetectLuaData
|
||
13 years ago
|
*
|
||
|
* \retval 0 no match
|
||
|
* \retval 1 match
|
||
|
*/
|
||
11 years ago
|
static int DetectLuaMatch (ThreadVars *tv, DetectEngineThreadCtx *det_ctx,
|
||
13 years ago
|
Packet *p, Signature *s, SigMatch *m)
|
||
|
{
|
||
|
SCEnter();
|
||
|
int ret = 0;
|
||
11 years ago
|
DetectLuaData *luajit = (DetectLuaData *)m->ctx;
|
||
13 years ago
|
if (luajit == NULL)
|
||
|
SCReturnInt(0);
|
||
|
|
||
11 years ago
|
DetectLuaThreadData *tluajit = (DetectLuaThreadData *)DetectThreadCtxGetKeywordThreadCtx(det_ctx, luajit->thread_ctx_id);
|
||
13 years ago
|
if (tluajit == NULL)
|
||
|
SCReturnInt(0);
|
||
|
|
||
12 years ago
|
/* setup extension data for use in lua c functions */
|
||
11 years ago
|
LuaExtensionsMatchSetup(tluajit->luastate, luajit, det_ctx, p->flow, /* flow not locked */LUA_FLOW_NOT_LOCKED_BY_PARENT);
|
||
12 years ago
|
|
||
13 years ago
|
if ((tluajit->flags & DATATYPE_PAYLOAD) && p->payload_len == 0)
|
||
13 years ago
|
SCReturnInt(0);
|
||
13 years ago
|
if ((tluajit->flags & DATATYPE_PACKET) && GET_PKT_LEN(p) == 0)
|
||
13 years ago
|
SCReturnInt(0);
|
||
13 years ago
|
if (tluajit->alproto != ALPROTO_UNKNOWN) {
|
||
13 years ago
|
if (p->flow == NULL)
|
||
|
SCReturnInt(0);
|
||
|
|
||
|
FLOWLOCK_RDLOCK(p->flow);
|
||
|
int alproto = p->flow->alproto;
|
||
|
FLOWLOCK_UNLOCK(p->flow);
|
||
|
|
||
|
if (tluajit->alproto != alproto)
|
||
|
SCReturnInt(0);
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
lua_getglobal(tluajit->luastate, "match");
|
||
|
lua_newtable(tluajit->luastate); /* stack at -1 */
|
||
|
|
||
13 years ago
|
if ((tluajit->flags & DATATYPE_PAYLOAD) && p->payload_len) {
|
||
13 years ago
|
lua_pushliteral(tluajit->luastate, "payload"); /* stack at -2 */
|
||
11 years ago
|
LuaPushStringBuffer (tluajit->luastate, (const uint8_t *)p->payload, (size_t)p->payload_len); /* stack at -3 */
|
||
13 years ago
|
lua_settable(tluajit->luastate, -3);
|
||
|
}
|
||
13 years ago
|
if ((tluajit->flags & DATATYPE_PACKET) && GET_PKT_LEN(p)) {
|
||
13 years ago
|
lua_pushliteral(tluajit->luastate, "packet"); /* stack at -2 */
|
||
11 years ago
|
LuaPushStringBuffer (tluajit->luastate, (const uint8_t *)GET_PKT_DATA(p), (size_t)GET_PKT_LEN(p)); /* stack at -3 */
|
||
13 years ago
|
lua_settable(tluajit->luastate, -3);
|
||
|
}
|
||
13 years ago
|
if (tluajit->alproto == ALPROTO_HTTP) {
|
||
|
FLOWLOCK_RDLOCK(p->flow);
|
||
|
HtpState *htp_state = p->flow->alstate;
|
||
12 years ago
|
if (htp_state != NULL && htp_state->connp != NULL) {
|
||
12 years ago
|
htp_tx_t *tx = NULL;
|
||
12 years ago
|
uint64_t idx = AppLayerParserGetTransactionInspectId(p->flow->alparser,
|
||
|
STREAM_TOSERVER);
|
||
|
uint64_t total_txs= AppLayerParserGetTxCnt(IPPROTO_TCP, ALPROTO_HTTP, htp_state);
|
||
12 years ago
|
for ( ; idx < total_txs; idx++) {
|
||
12 years ago
|
tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP, htp_state, idx);
|
||
12 years ago
|
if (tx == NULL)
|
||
|
continue;
|
||
|
|
||
|
if ((tluajit->flags & DATATYPE_HTTP_REQUEST_LINE) && tx->request_line != NULL &&
|
||
|
bstr_len(tx->request_line) > 0) {
|
||
|
lua_pushliteral(tluajit->luastate, "http.request_line"); /* stack at -2 */
|
||
11 years ago
|
LuaPushStringBuffer(tluajit->luastate,
|
||
|
(const uint8_t *)bstr_ptr(tx->request_line),
|
||
12 years ago
|
bstr_len(tx->request_line));
|
||
|
lua_settable(tluajit->luastate, -3);
|
||
13 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
FLOWLOCK_UNLOCK(p->flow);
|
||
|
}
|
||
13 years ago
|
|
||
|
int retval = lua_pcall(tluajit->luastate, 1, 1, 0);
|
||
|
if (retval != 0) {
|
||
|
SCLogInfo("failed to run script: %s", lua_tostring(tluajit->luastate, -1));
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
/* process returns from script */
|
||
|
if (lua_gettop(tluajit->luastate) > 0) {
|
||
|
|
||
|
/* script returns a number (return 1 or return 0) */
|
||
|
if (lua_type(tluajit->luastate, 1) == LUA_TNUMBER) {
|
||
|
double script_ret = lua_tonumber(tluajit->luastate, 1);
|
||
|
SCLogDebug("script_ret %f", script_ret);
|
||
|
lua_pop(tluajit->luastate, 1);
|
||
|
|
||
|
if (script_ret == 1.0)
|
||
|
ret = 1;
|
||
|
|
||
|
/* script returns a table */
|
||
|
} else if (lua_type(tluajit->luastate, 1) == LUA_TTABLE) {
|
||
|
lua_pushnil(tluajit->luastate);
|
||
|
const char *k, *v;
|
||
|
while (lua_next(tluajit->luastate, -2)) {
|
||
|
v = lua_tostring(tluajit->luastate, -1);
|
||
|
lua_pop(tluajit->luastate, 1);
|
||
|
k = lua_tostring(tluajit->luastate, -1);
|
||
|
|
||
|
if (!k || !v)
|
||
|
continue;
|
||
|
|
||
|
SCLogDebug("k='%s', v='%s'", k, v);
|
||
|
|
||
|
if (strcmp(k, "retval") == 0) {
|
||
|
if (atoi(v) == 1)
|
||
|
ret = 1;
|
||
|
} else {
|
||
|
/* set flow var? */
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* pop the table */
|
||
|
lua_pop(tluajit->luastate, 1);
|
||
|
}
|
||
|
}
|
||
12 years ago
|
while (lua_gettop(tluajit->luastate) > 0) {
|
||
|
lua_pop(tluajit->luastate, 1);
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
if (luajit->negated) {
|
||
|
if (ret == 1)
|
||
|
ret = 0;
|
||
|
else
|
||
|
ret = 1;
|
||
|
}
|
||
13 years ago
|
|
||
|
SCReturnInt(ret);
|
||
|
}
|
||
|
|
||
12 years ago
|
#ifdef UNITTESTS
|
||
|
/* if this ptr is set the luajit setup functions will use this buffer as the
|
||
|
* lua script instead of calling luaL_loadfile on the filename supplied. */
|
||
|
static const char *ut_script = NULL;
|
||
|
#endif
|
||
|
|
||
11 years ago
|
static void *DetectLuaThreadInit(void *data)
|
||
11 years ago
|
{
|
||
12 years ago
|
int status;
|
||
11 years ago
|
DetectLuaData *luajit = (DetectLuaData *)data;
|
||
13 years ago
|
BUG_ON(luajit == NULL);
|
||
13 years ago
|
|
||
11 years ago
|
DetectLuaThreadData *t = SCMalloc(sizeof(DetectLuaThreadData));
|
||
13 years ago
|
if (unlikely(t == NULL)) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "couldn't alloc ctx memory");
|
||
13 years ago
|
return NULL;
|
||
|
}
|
||
11 years ago
|
memset(t, 0x00, sizeof(DetectLuaThreadData));
|
||
13 years ago
|
|
||
13 years ago
|
t->alproto = luajit->alproto;
|
||
|
t->flags = luajit->flags;
|
||
13 years ago
|
|
||
11 years ago
|
t->luastate = DetectLuaGetState();
|
||
13 years ago
|
if (t->luastate == NULL) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "luastate pool depleted");
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
luaL_openlibs(t->luastate);
|
||
13 years ago
|
|
||
11 years ago
|
LuaRegisterExtensions(t->luastate);
|
||
12 years ago
|
|
||
12 years ago
|
lua_pushinteger(t->luastate, (lua_Integer)(luajit->sid));
|
||
|
lua_setglobal(t->luastate, "SCRuleSid");
|
||
|
lua_pushinteger(t->luastate, (lua_Integer)(luajit->rev));
|
||
|
lua_setglobal(t->luastate, "SCRuleRev");
|
||
|
lua_pushinteger(t->luastate, (lua_Integer)(luajit->gid));
|
||
|
lua_setglobal(t->luastate, "SCRuleGid");
|
||
|
|
||
12 years ago
|
/* hackish, needed to allow unittests to pass buffers as scripts instead of files */
|
||
|
#ifdef UNITTESTS
|
||
|
if (ut_script != NULL) {
|
||
|
status = luaL_loadbuffer(t->luastate, ut_script, strlen(ut_script), "unittest");
|
||
|
if (status) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "couldn't load file: %s", lua_tostring(t->luastate, -1));
|
||
12 years ago
|
goto error;
|
||
|
}
|
||
|
} else {
|
||
|
#endif
|
||
|
status = luaL_loadfile(t->luastate, luajit->filename);
|
||
|
if (status) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "couldn't load file: %s", lua_tostring(t->luastate, -1));
|
||
12 years ago
|
goto error;
|
||
|
}
|
||
|
#ifdef UNITTESTS
|
||
13 years ago
|
}
|
||
12 years ago
|
#endif
|
||
13 years ago
|
|
||
13 years ago
|
/* prime the script (or something) */
|
||
|
if (lua_pcall(t->luastate, 0, 0, 0) != 0) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "couldn't prime file: %s", lua_tostring(t->luastate, -1));
|
||
13 years ago
|
goto error;
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
13 years ago
|
return (void *)t;
|
||
13 years ago
|
|
||
|
error:
|
||
13 years ago
|
if (t->luastate != NULL)
|
||
11 years ago
|
DetectLuaReturnState(t->luastate);
|
||
13 years ago
|
SCFree(t);
|
||
|
return NULL;
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
static void DetectLuaThreadFree(void *ctx)
|
||
11 years ago
|
{
|
||
13 years ago
|
if (ctx != NULL) {
|
||
11 years ago
|
DetectLuaThreadData *t = (DetectLuaThreadData *)ctx;
|
||
13 years ago
|
if (t->luastate != NULL)
|
||
11 years ago
|
DetectLuaReturnState(t->luastate);
|
||
13 years ago
|
SCFree(t);
|
||
|
}
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
/**
|
||
|
* \brief Parse the luajit keyword
|
||
|
*
|
||
13 years ago
|
* \param str Pointer to the user provided option
|
||
13 years ago
|
*
|
||
11 years ago
|
* \retval luajit pointer to DetectLuaData on success
|
||
13 years ago
|
* \retval NULL on failure
|
||
|
*/
|
||
11 years ago
|
static DetectLuaData *DetectLuaParse (char *str)
|
||
13 years ago
|
{
|
||
11 years ago
|
DetectLuaData *luajit = NULL;
|
||
13 years ago
|
|
||
|
/* We have a correct luajit option */
|
||
11 years ago
|
luajit = SCMalloc(sizeof(DetectLuaData));
|
||
13 years ago
|
if (unlikely(luajit == NULL))
|
||
13 years ago
|
goto error;
|
||
|
|
||
11 years ago
|
memset(luajit, 0x00, sizeof(DetectLuaData));
|
||
13 years ago
|
|
||
|
if (strlen(str) && str[0] == '!') {
|
||
|
luajit->negated = 1;
|
||
|
str++;
|
||
|
}
|
||
|
|
||
|
/* get full filename */
|
||
13 years ago
|
luajit->filename = DetectLoadCompleteSigPath(str);
|
||
|
if (luajit->filename == NULL) {
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
|
return luajit;
|
||
|
|
||
|
error:
|
||
|
if (luajit != NULL)
|
||
11 years ago
|
DetectLuaFree(luajit);
|
||
13 years ago
|
return NULL;
|
||
|
}
|
||
|
|
||
11 years ago
|
static int DetectLuaSetupPrime(DetectEngineCtx *de_ctx, DetectLuaData *ld)
|
||
11 years ago
|
{
|
||
12 years ago
|
int status;
|
||
|
|
||
13 years ago
|
lua_State *luastate = luaL_newstate();
|
||
|
if (luastate == NULL)
|
||
|
goto error;
|
||
|
luaL_openlibs(luastate);
|
||
|
|
||
12 years ago
|
/* hackish, needed to allow unittests to pass buffers as scripts instead of files */
|
||
|
#ifdef UNITTESTS
|
||
|
if (ut_script != NULL) {
|
||
|
status = luaL_loadbuffer(luastate, ut_script, strlen(ut_script), "unittest");
|
||
|
if (status) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "couldn't load file: %s", lua_tostring(luastate, -1));
|
||
12 years ago
|
goto error;
|
||
|
}
|
||
|
} else {
|
||
|
#endif
|
||
|
status = luaL_loadfile(luastate, ld->filename);
|
||
|
if (status) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "couldn't load file: %s", lua_tostring(luastate, -1));
|
||
12 years ago
|
goto error;
|
||
|
}
|
||
|
#ifdef UNITTESTS
|
||
13 years ago
|
}
|
||
12 years ago
|
#endif
|
||
13 years ago
|
|
||
|
/* prime the script (or something) */
|
||
|
if (lua_pcall(luastate, 0, 0, 0) != 0) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "couldn't prime file: %s", lua_tostring(luastate, -1));
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
|
lua_getglobal(luastate, "init");
|
||
|
if (lua_type(luastate, -1) != LUA_TFUNCTION) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "no init function in script");
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
|
lua_newtable(luastate); /* stack at -1 */
|
||
|
if (lua_gettop(luastate) == 0 || lua_type(luastate, 2) != LUA_TTABLE) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "no table setup");
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
|
lua_pushliteral(luastate, "script_api_ver"); /* stack at -2 */
|
||
|
lua_pushnumber (luastate, 1); /* stack at -3 */
|
||
|
lua_settable(luastate, -3);
|
||
|
|
||
|
if (lua_pcall(luastate, 1, 1, 0) != 0) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
|
/* process returns from script */
|
||
|
if (lua_gettop(luastate) == 0) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "init function in script should return table, nothing returned");
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
if (lua_type(luastate, 1) != LUA_TTABLE) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "init function in script should return table, returned is not table");
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
|
lua_pushnil(luastate);
|
||
|
const char *k, *v;
|
||
|
while (lua_next(luastate, -2)) {
|
||
12 years ago
|
k = lua_tostring(luastate, -2);
|
||
|
if (k == NULL)
|
||
|
continue;
|
||
|
|
||
|
/* handle flowvar separately as it has a table as value */
|
||
|
if (strcmp(k, "flowvar") == 0) {
|
||
|
if (lua_istable(luastate, -1)) {
|
||
|
lua_pushnil(luastate);
|
||
|
while (lua_next(luastate, -2) != 0) {
|
||
|
/* value at -1, key is at -2 which we ignore */
|
||
|
const char *value = lua_tostring(luastate, -1);
|
||
|
SCLogDebug("value %s", value);
|
||
|
/* removes 'value'; keeps 'key' for next iteration */
|
||
|
lua_pop(luastate, 1);
|
||
|
|
||
|
if (ld->flowvars == DETECT_LUAJIT_MAX_FLOWVARS) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "too many flowvars registered");
|
||
12 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
|
uint16_t idx = VariableNameGetIdx(de_ctx, (char *)value, DETECT_FLOWVAR);
|
||
|
ld->flowvar[ld->flowvars++] = idx;
|
||
|
SCLogDebug("script uses flowvar %u with script id %u", idx, ld->flowvars - 1);
|
||
|
}
|
||
|
}
|
||
|
lua_pop(luastate, 1);
|
||
|
continue;
|
||
12 years ago
|
} else if (strcmp(k, "flowint") == 0) {
|
||
|
if (lua_istable(luastate, -1)) {
|
||
|
lua_pushnil(luastate);
|
||
|
while (lua_next(luastate, -2) != 0) {
|
||
|
/* value at -1, key is at -2 which we ignore */
|
||
|
const char *value = lua_tostring(luastate, -1);
|
||
|
SCLogDebug("value %s", value);
|
||
|
/* removes 'value'; keeps 'key' for next iteration */
|
||
|
lua_pop(luastate, 1);
|
||
|
|
||
|
if (ld->flowints == DETECT_LUAJIT_MAX_FLOWINTS) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "too many flowints registered");
|
||
12 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
|
uint16_t idx = VariableNameGetIdx(de_ctx, (char *)value, DETECT_FLOWINT);
|
||
|
ld->flowint[ld->flowints++] = idx;
|
||
|
SCLogDebug("script uses flowint %u with script id %u", idx, ld->flowints - 1);
|
||
|
}
|
||
|
}
|
||
|
lua_pop(luastate, 1);
|
||
|
continue;
|
||
12 years ago
|
}
|
||
|
|
||
13 years ago
|
v = lua_tostring(luastate, -1);
|
||
|
lua_pop(luastate, 1);
|
||
12 years ago
|
if (v == NULL)
|
||
13 years ago
|
continue;
|
||
|
|
||
|
SCLogDebug("k='%s', v='%s'", k, v);
|
||
|
if (strcmp(k, "packet") == 0 && strcmp(v, "true") == 0) {
|
||
|
ld->flags |= DATATYPE_PACKET;
|
||
|
} else if (strcmp(k, "payload") == 0 && strcmp(v, "true") == 0) {
|
||
|
ld->flags |= DATATYPE_PAYLOAD;
|
||
|
} else if (strncmp(k, "http", 4) == 0 && strcmp(v, "true") == 0) {
|
||
13 years ago
|
if (ld->alproto != ALPROTO_UNKNOWN && ld->alproto != ALPROTO_HTTP) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "can just inspect script against one app layer proto like HTTP at a time");
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
if (ld->flags != 0) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "when inspecting HTTP buffers only a single buffer can be inspected");
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
|
/* http types */
|
||
|
ld->alproto = ALPROTO_HTTP;
|
||
|
|
||
|
if (strcmp(k, "http.uri") == 0)
|
||
|
ld->flags |= DATATYPE_HTTP_URI;
|
||
13 years ago
|
|
||
13 years ago
|
else if (strcmp(k, "http.uri.raw") == 0)
|
||
|
ld->flags |= DATATYPE_HTTP_URI_RAW;
|
||
13 years ago
|
|
||
13 years ago
|
else if (strcmp(k, "http.request_line") == 0)
|
||
|
ld->flags |= DATATYPE_HTTP_REQUEST_LINE;
|
||
13 years ago
|
|
||
13 years ago
|
else if (strcmp(k, "http.request_headers") == 0)
|
||
|
ld->flags |= DATATYPE_HTTP_REQUEST_HEADERS;
|
||
13 years ago
|
|
||
13 years ago
|
else if (strcmp(k, "http.request_headers.raw") == 0)
|
||
|
ld->flags |= DATATYPE_HTTP_REQUEST_HEADERS_RAW;
|
||
13 years ago
|
|
||
13 years ago
|
else if (strcmp(k, "http.request_cookie") == 0)
|
||
|
ld->flags |= DATATYPE_HTTP_REQUEST_COOKIE;
|
||
13 years ago
|
|
||
13 years ago
|
else if (strcmp(k, "http.request_user_agent") == 0)
|
||
13 years ago
|
ld->flags |= DATATYPE_HTTP_REQUEST_UA;
|
||
|
|
||
|
else if (strcmp(k, "http.request_body") == 0)
|
||
13 years ago
|
ld->flags |= DATATYPE_HTTP_REQUEST_BODY;
|
||
13 years ago
|
|
||
13 years ago
|
else if (strcmp(k, "http.response_body") == 0)
|
||
13 years ago
|
ld->flags |= DATATYPE_HTTP_RESPONSE_BODY;
|
||
13 years ago
|
|
||
13 years ago
|
else if (strcmp(k, "http.response_cookie") == 0)
|
||
|
ld->flags |= DATATYPE_HTTP_RESPONSE_COOKIE;
|
||
13 years ago
|
|
||
13 years ago
|
else if (strcmp(k, "http.response_headers") == 0)
|
||
|
ld->flags |= DATATYPE_HTTP_RESPONSE_HEADERS;
|
||
|
|
||
|
else if (strcmp(k, "http.response_headers.raw") == 0)
|
||
|
ld->flags |= DATATYPE_HTTP_RESPONSE_HEADERS_RAW;
|
||
|
|
||
13 years ago
|
else {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "unsupported http data type %s", k);
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
13 years ago
|
ld->buffername = SCStrdup(k);
|
||
|
if (ld->buffername == NULL) {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "alloc error");
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
13 years ago
|
} else {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "unsupported data type %s", k);
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* pop the table */
|
||
|
lua_pop(luastate, 1);
|
||
|
lua_close(luastate);
|
||
|
return 0;
|
||
|
error:
|
||
|
lua_close(luastate);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
13 years ago
|
/**
|
||
|
* \brief this function is used to parse luajit options
|
||
|
* \brief into the current signature
|
||
|
*
|
||
|
* \param de_ctx pointer to the Detection Engine Context
|
||
|
* \param s pointer to the Current Signature
|
||
|
* \param str pointer to the user provided "luajit" option
|
||
|
*
|
||
|
* \retval 0 on Success
|
||
|
* \retval -1 on Failure
|
||
|
*/
|
||
11 years ago
|
static int DetectLuaSetup (DetectEngineCtx *de_ctx, Signature *s, char *str)
|
||
13 years ago
|
{
|
||
11 years ago
|
DetectLuaData *luajit = NULL;
|
||
13 years ago
|
SigMatch *sm = NULL;
|
||
|
|
||
11 years ago
|
luajit = DetectLuaParse(str);
|
||
13 years ago
|
if (luajit == NULL)
|
||
|
goto error;
|
||
|
|
||
12 years ago
|
if (DetectLuaSetupPrime(de_ctx, luajit) == -1) {
|
||
13 years ago
|
goto error;
|
||
|
}
|
||
|
|
||
13 years ago
|
luajit->thread_ctx_id = DetectRegisterThreadCtxFuncs(de_ctx, "luajit",
|
||
11 years ago
|
DetectLuaThreadInit, (void *)luajit,
|
||
|
DetectLuaThreadFree, 0);
|
||
13 years ago
|
if (luajit->thread_ctx_id == -1)
|
||
|
goto error;
|
||
|
|
||
13 years ago
|
if (luajit->alproto != ALPROTO_UNKNOWN) {
|
||
|
if (s->alproto != ALPROTO_UNKNOWN && luajit->alproto != s->alproto) {
|
||
|
goto error;
|
||
|
}
|
||
|
s->alproto = luajit->alproto;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* Okay so far so good, lets get this into a SigMatch
|
||
|
* and put it in the Signature. */
|
||
|
sm = SigMatchAlloc();
|
||
|
if (sm == NULL)
|
||
|
goto error;
|
||
|
|
||
11 years ago
|
sm->type = DETECT_LUA;
|
||
13 years ago
|
sm->ctx = (void *)luajit;
|
||
|
|
||
13 years ago
|
if (luajit->alproto == ALPROTO_UNKNOWN)
|
||
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_MATCH);
|
||
|
else if (luajit->alproto == ALPROTO_HTTP) {
|
||
|
if (luajit->flags & DATATYPE_HTTP_RESPONSE_BODY)
|
||
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_HSBDMATCH);
|
||
|
else if (luajit->flags & DATATYPE_HTTP_REQUEST_BODY)
|
||
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_HCBDMATCH);
|
||
|
else if (luajit->flags & DATATYPE_HTTP_URI)
|
||
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_UMATCH);
|
||
13 years ago
|
else if (luajit->flags & DATATYPE_HTTP_URI_RAW)
|
||
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_HRUDMATCH);
|
||
|
else if (luajit->flags & DATATYPE_HTTP_REQUEST_COOKIE)
|
||
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_HCDMATCH);
|
||
|
else if (luajit->flags & DATATYPE_HTTP_REQUEST_UA)
|
||
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_HUADMATCH);
|
||
13 years ago
|
else if (luajit->flags & (DATATYPE_HTTP_REQUEST_HEADERS|DATATYPE_HTTP_RESPONSE_HEADERS))
|
||
13 years ago
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_HHDMATCH);
|
||
13 years ago
|
else if (luajit->flags & (DATATYPE_HTTP_REQUEST_HEADERS_RAW|DATATYPE_HTTP_RESPONSE_HEADERS_RAW))
|
||
13 years ago
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_HRHDMATCH);
|
||
|
else if (luajit->flags & DATATYPE_HTTP_RESPONSE_COOKIE)
|
||
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_HCDMATCH);
|
||
13 years ago
|
else
|
||
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_AMATCH);
|
||
12 years ago
|
} else {
|
||
11 years ago
|
SCLogError(SC_ERR_LUA_ERROR, "luajit can't be used with protocol %s",
|
||
12 years ago
|
AppLayerGetProtoName(luajit->alproto));
|
||
12 years ago
|
goto error;
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
|
de_ctx->detect_luajit_instances++;
|
||
13 years ago
|
return 0;
|
||
|
|
||
|
error:
|
||
|
if (luajit != NULL)
|
||
11 years ago
|
DetectLuaFree(luajit);
|
||
13 years ago
|
if (sm != NULL)
|
||
|
SCFree(sm);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
12 years ago
|
/** \brief post-sig parse function to set the sid,rev,gid into the
|
||
|
* ctx, as this isn't available yet during parsing.
|
||
|
*/
|
||
11 years ago
|
void DetectLuaPostSetup(Signature *s)
|
||
11 years ago
|
{
|
||
12 years ago
|
int i;
|
||
|
SigMatch *sm;
|
||
|
|
||
|
for (i = 0; i < DETECT_SM_LIST_MAX; i++) {
|
||
|
for (sm = s->sm_lists[i]; sm != NULL; sm = sm->next) {
|
||
11 years ago
|
if (sm->type != DETECT_LUA)
|
||
12 years ago
|
continue;
|
||
|
|
||
11 years ago
|
DetectLuaData *ld = sm->ctx;
|
||
12 years ago
|
ld->sid = s->id;
|
||
|
ld->rev = s->rev;
|
||
|
ld->gid = s->gid;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
/**
|
||
11 years ago
|
* \brief this function will free memory associated with DetectLuaData
|
||
13 years ago
|
*
|
||
11 years ago
|
* \param luajit pointer to DetectLuaData
|
||
13 years ago
|
*/
|
||
11 years ago
|
static void DetectLuaFree(void *ptr)
|
||
11 years ago
|
{
|
||
13 years ago
|
if (ptr != NULL) {
|
||
11 years ago
|
DetectLuaData *luajit = (DetectLuaData *)ptr;
|
||
13 years ago
|
|
||
|
if (luajit->buffername)
|
||
|
SCFree(luajit->buffername);
|
||
12 years ago
|
if (luajit->filename)
|
||
|
SCFree(luajit->filename);
|
||
13 years ago
|
|
||
13 years ago
|
SCFree(luajit);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#ifdef UNITTESTS
|
||
12 years ago
|
/** \test http buffer */
|
||
11 years ago
|
static int LuaMatchTest01(void)
|
||
11 years ago
|
{
|
||
12 years ago
|
const char script[] =
|
||
|
"function init (args)\n"
|
||
|
" local needs = {}\n"
|
||
|
" needs[\"http.request_headers\"] = tostring(true)\n"
|
||
|
" needs[\"flowvar\"] = {\"cnt\"}\n"
|
||
|
" return needs\n"
|
||
|
"end\n"
|
||
|
"\n"
|
||
|
"function match(args)\n"
|
||
|
" a = ScFlowvarGet(0)\n"
|
||
|
" if a then\n"
|
||
|
" a = tostring(tonumber(a)+1)\n"
|
||
|
" print (a)\n"
|
||
|
" ScFlowvarSet(0, a, #a)\n"
|
||
|
" else\n"
|
||
|
" a = tostring(1)\n"
|
||
|
" print (a)\n"
|
||
|
" ScFlowvarSet(0, a, #a)\n"
|
||
|
" end\n"
|
||
|
" \n"
|
||
|
" print (\"pre check: \" .. (a))\n"
|
||
|
" if tonumber(a) == 2 then\n"
|
||
|
" print \"match\"\n"
|
||
|
" return 1\n"
|
||
|
" end\n"
|
||
|
" return 0\n"
|
||
|
"end\n"
|
||
|
"return 0\n";
|
||
|
char sig[] = "alert http any any -> any any (flow:to_server; luajit:unittest; sid:1;)";
|
||
|
int result = 0;
|
||
|
uint8_t httpbuf1[] =
|
||
|
"POST / HTTP/1.1\r\n"
|
||
12 years ago
|
"Host: www.emergingthreats.net\r\n\r\n";
|
||
12 years ago
|
uint8_t httpbuf2[] =
|
||
12 years ago
|
"POST / HTTP/1.1\r\n"
|
||
|
"Host: www.openinfosecfoundation.org\r\n\r\n";
|
||
12 years ago
|
uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
|
||
|
uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
|
||
|
TcpSession ssn;
|
||
|
Packet *p1 = NULL;
|
||
|
Packet *p2 = NULL;
|
||
|
Flow f;
|
||
|
Signature *s = NULL;
|
||
|
ThreadVars th_v;
|
||
|
DetectEngineThreadCtx *det_ctx;
|
||
|
|
||
12 years ago
|
AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc();
|
||
12 years ago
|
|
||
12 years ago
|
ut_script = script;
|
||
|
|
||
|
memset(&th_v, 0, sizeof(th_v));
|
||
|
memset(&f, 0, sizeof(f));
|
||
|
memset(&ssn, 0, sizeof(ssn));
|
||
|
|
||
|
p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
|
||
|
p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
|
||
|
|
||
|
FLOW_INITIALIZE(&f);
|
||
|
f.protoctx = (void *)&ssn;
|
||
12 years ago
|
f.proto = IPPROTO_TCP;
|
||
12 years ago
|
f.flags |= FLOW_IPV4;
|
||
|
f.alproto = ALPROTO_HTTP;
|
||
|
|
||
|
p1->flow = &f;
|
||
|
p1->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p1->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p1->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
p2->flow = &f;
|
||
|
p2->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p2->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p2->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
|
||
|
StreamTcpInitConfig(TRUE);
|
||
|
|
||
|
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
|
||
|
if (de_ctx == NULL) {
|
||
|
goto end;
|
||
|
}
|
||
|
de_ctx->flags |= DE_QUIET;
|
||
|
|
||
|
s = DetectEngineAppendSig(de_ctx, sig);
|
||
|
if (s == NULL) {
|
||
|
printf("sig parse failed: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
SigGroupBuild(de_ctx);
|
||
|
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
|
||
|
|
||
12 years ago
|
SCMutexLock(&f.m);
|
||
12 years ago
|
int r = AppLayerParserParse(alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, httpbuf1, httplen1);
|
||
12 years ago
|
if (r != 0) {
|
||
|
printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
goto end;
|
||
|
}
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
HtpState *http_state = f.alstate;
|
||
|
if (http_state == NULL) {
|
||
|
printf("no http state: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
/* do detect for p1 */
|
||
12 years ago
|
SCLogDebug("inspecting p1");
|
||
12 years ago
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
|
||
|
|
||
|
if ((PacketAlertCheck(p1, 1))) {
|
||
|
printf("sid 1 didn't match on p1 but should have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
12 years ago
|
SCMutexLock(&f.m);
|
||
12 years ago
|
r = AppLayerParserParse(alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, httpbuf2, httplen2);
|
||
12 years ago
|
if (r != 0) {
|
||
|
printf("toserver chunk 2 returned %" PRId32 ", expected 0: ", r);
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
goto end;
|
||
|
}
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
/* do detect for p2 */
|
||
12 years ago
|
SCLogDebug("inspecting p2");
|
||
12 years ago
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
|
||
|
|
||
|
if (!(PacketAlertCheck(p2, 1))) {
|
||
|
printf("sid 1 didn't match on p2 but should have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
FlowVar *fv = FlowVarGet(&f, 1);
|
||
|
if (fv == NULL) {
|
||
|
printf("no flowvar: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
if (fv->data.fv_str.value_len != 1) {
|
||
|
printf("%u != %u: ", fv->data.fv_str.value_len, 1);
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
if (memcmp(fv->data.fv_str.value, "2", 1) != 0) {
|
||
|
PrintRawDataFp(stdout, fv->data.fv_str.value, fv->data.fv_str.value_len);
|
||
|
|
||
|
printf("buffer mismatch: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
result = 1;
|
||
|
end:
|
||
12 years ago
|
if (alp_tctx != NULL)
|
||
12 years ago
|
AppLayerParserThreadCtxFree(alp_tctx);
|
||
12 years ago
|
if (de_ctx != NULL)
|
||
|
DetectEngineCtxFree(de_ctx);
|
||
|
|
||
|
StreamTcpFreeConfig(TRUE);
|
||
|
FLOW_DESTROY(&f);
|
||
|
UTHFreePackets(&p1, 1);
|
||
|
UTHFreePackets(&p2, 1);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/** \test payload buffer */
|
||
11 years ago
|
static int LuaMatchTest02(void)
|
||
11 years ago
|
{
|
||
12 years ago
|
const char script[] =
|
||
|
"function init (args)\n"
|
||
|
" local needs = {}\n"
|
||
|
" needs[\"payload\"] = tostring(true)\n"
|
||
|
" needs[\"flowvar\"] = {\"cnt\"}\n"
|
||
|
" return needs\n"
|
||
|
"end\n"
|
||
|
"\n"
|
||
|
"function match(args)\n"
|
||
|
" a = ScFlowvarGet(0)\n"
|
||
|
" if a then\n"
|
||
|
" a = tostring(tonumber(a)+1)\n"
|
||
|
" print (a)\n"
|
||
|
" ScFlowvarSet(0, a, #a)\n"
|
||
|
" else\n"
|
||
|
" a = tostring(1)\n"
|
||
|
" print (a)\n"
|
||
|
" ScFlowvarSet(0, a, #a)\n"
|
||
|
" end\n"
|
||
|
" \n"
|
||
|
" print (\"pre check: \" .. (a))\n"
|
||
|
" if tonumber(a) == 2 then\n"
|
||
|
" print \"match\"\n"
|
||
|
" return 1\n"
|
||
|
" end\n"
|
||
|
" return 0\n"
|
||
|
"end\n"
|
||
|
"return 0\n";
|
||
|
char sig[] = "alert tcp any any -> any any (flow:to_server; luajit:unittest; sid:1;)";
|
||
|
int result = 0;
|
||
|
uint8_t httpbuf1[] =
|
||
|
"POST / HTTP/1.1\r\n"
|
||
12 years ago
|
"Host: www.emergingthreats.net\r\n\r\n";
|
||
12 years ago
|
uint8_t httpbuf2[] =
|
||
12 years ago
|
"POST / HTTP/1.1\r\n"
|
||
|
"Host: www.openinfosecfoundation.org\r\n\r\n";
|
||
12 years ago
|
uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
|
||
|
uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
|
||
|
TcpSession ssn;
|
||
|
Packet *p1 = NULL;
|
||
|
Packet *p2 = NULL;
|
||
|
Flow f;
|
||
|
Signature *s = NULL;
|
||
|
ThreadVars th_v;
|
||
|
DetectEngineThreadCtx *det_ctx;
|
||
|
|
||
|
ut_script = script;
|
||
|
|
||
|
memset(&th_v, 0, sizeof(th_v));
|
||
|
memset(&f, 0, sizeof(f));
|
||
|
memset(&ssn, 0, sizeof(ssn));
|
||
|
|
||
|
p1 = UTHBuildPacket(httpbuf1, httplen1, IPPROTO_TCP);
|
||
|
p2 = UTHBuildPacket(httpbuf2, httplen2, IPPROTO_TCP);
|
||
|
|
||
|
FLOW_INITIALIZE(&f);
|
||
|
f.protoctx = (void *)&ssn;
|
||
12 years ago
|
f.proto = IPPROTO_TCP;
|
||
12 years ago
|
f.flags |= FLOW_IPV4;
|
||
|
f.alproto = ALPROTO_HTTP;
|
||
|
|
||
|
p1->flow = &f;
|
||
|
p1->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p1->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p1->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
p2->flow = &f;
|
||
|
p2->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p2->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p2->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
|
||
|
StreamTcpInitConfig(TRUE);
|
||
|
|
||
|
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
|
||
|
if (de_ctx == NULL) {
|
||
|
goto end;
|
||
|
}
|
||
|
de_ctx->flags |= DE_QUIET;
|
||
|
|
||
|
s = DetectEngineAppendSig(de_ctx, sig);
|
||
|
if (s == NULL) {
|
||
|
printf("sig parse failed: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
SigGroupBuild(de_ctx);
|
||
|
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
|
||
|
|
||
|
/* do detect for p1 */
|
||
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
|
||
|
|
||
|
if ((PacketAlertCheck(p1, 1))) {
|
||
|
printf("sid 1 didn't match on p1 but should have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
/* do detect for p2 */
|
||
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
|
||
|
|
||
|
if (!(PacketAlertCheck(p2, 1))) {
|
||
|
printf("sid 1 didn't match on p2 but should have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
FlowVar *fv = FlowVarGet(&f, 1);
|
||
|
if (fv == NULL) {
|
||
|
printf("no flowvar: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
if (fv->data.fv_str.value_len != 1) {
|
||
|
printf("%u != %u: ", fv->data.fv_str.value_len, 1);
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
if (memcmp(fv->data.fv_str.value, "2", 1) != 0) {
|
||
|
PrintRawDataFp(stdout, fv->data.fv_str.value, fv->data.fv_str.value_len);
|
||
|
|
||
|
printf("buffer mismatch: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
result = 1;
|
||
|
end:
|
||
|
if (de_ctx != NULL)
|
||
|
DetectEngineCtxFree(de_ctx);
|
||
|
|
||
|
StreamTcpFreeConfig(TRUE);
|
||
|
FLOW_DESTROY(&f);
|
||
|
UTHFreePackets(&p1, 1);
|
||
|
UTHFreePackets(&p2, 1);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/** \test packet buffer */
|
||
11 years ago
|
static int LuaMatchTest03(void)
|
||
11 years ago
|
{
|
||
12 years ago
|
const char script[] =
|
||
|
"function init (args)\n"
|
||
|
" local needs = {}\n"
|
||
|
" needs[\"packet\"] = tostring(true)\n"
|
||
|
" needs[\"flowvar\"] = {\"cnt\"}\n"
|
||
|
" return needs\n"
|
||
|
"end\n"
|
||
|
"\n"
|
||
|
"function match(args)\n"
|
||
|
" a = ScFlowvarGet(0)\n"
|
||
|
" if a then\n"
|
||
|
" a = tostring(tonumber(a)+1)\n"
|
||
|
" print (a)\n"
|
||
|
" ScFlowvarSet(0, a, #a)\n"
|
||
|
" else\n"
|
||
|
" a = tostring(1)\n"
|
||
|
" print (a)\n"
|
||
|
" ScFlowvarSet(0, a, #a)\n"
|
||
|
" end\n"
|
||
|
" \n"
|
||
|
" print (\"pre check: \" .. (a))\n"
|
||
|
" if tonumber(a) == 2 then\n"
|
||
|
" print \"match\"\n"
|
||
|
" return 1\n"
|
||
|
" end\n"
|
||
|
" return 0\n"
|
||
|
"end\n"
|
||
|
"return 0\n";
|
||
|
char sig[] = "alert tcp any any -> any any (flow:to_server; luajit:unittest; sid:1;)";
|
||
|
int result = 0;
|
||
|
uint8_t httpbuf1[] =
|
||
|
"POST / HTTP/1.1\r\n"
|
||
12 years ago
|
"Host: www.emergingthreats.net\r\n\r\n";
|
||
12 years ago
|
uint8_t httpbuf2[] =
|
||
12 years ago
|
"POST / HTTP/1.1\r\n"
|
||
|
"Host: www.openinfosecfoundation.org\r\n\r\n";
|
||
12 years ago
|
uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
|
||
|
uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
|
||
|
TcpSession ssn;
|
||
|
Packet *p1 = NULL;
|
||
|
Packet *p2 = NULL;
|
||
|
Flow f;
|
||
|
Signature *s = NULL;
|
||
|
ThreadVars th_v;
|
||
|
DetectEngineThreadCtx *det_ctx;
|
||
|
|
||
|
ut_script = script;
|
||
|
|
||
|
memset(&th_v, 0, sizeof(th_v));
|
||
|
memset(&f, 0, sizeof(f));
|
||
|
memset(&ssn, 0, sizeof(ssn));
|
||
|
|
||
|
p1 = UTHBuildPacket(httpbuf1, httplen1, IPPROTO_TCP);
|
||
|
p2 = UTHBuildPacket(httpbuf2, httplen2, IPPROTO_TCP);
|
||
|
|
||
|
FLOW_INITIALIZE(&f);
|
||
|
f.protoctx = (void *)&ssn;
|
||
12 years ago
|
f.proto = IPPROTO_TCP;
|
||
12 years ago
|
f.flags |= FLOW_IPV4;
|
||
|
f.alproto = ALPROTO_HTTP;
|
||
|
|
||
|
p1->flow = &f;
|
||
|
p1->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p1->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p1->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
p2->flow = &f;
|
||
|
p2->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p2->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p2->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
|
||
|
StreamTcpInitConfig(TRUE);
|
||
|
|
||
|
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
|
||
|
if (de_ctx == NULL) {
|
||
|
goto end;
|
||
|
}
|
||
|
de_ctx->flags |= DE_QUIET;
|
||
|
|
||
|
s = DetectEngineAppendSig(de_ctx, sig);
|
||
|
if (s == NULL) {
|
||
|
printf("sig parse failed: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
SigGroupBuild(de_ctx);
|
||
|
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
|
||
|
|
||
|
/* do detect for p1 */
|
||
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
|
||
|
|
||
|
if ((PacketAlertCheck(p1, 1))) {
|
||
|
printf("sid 1 didn't match on p1 but should have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
/* do detect for p2 */
|
||
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
|
||
|
|
||
|
if (!(PacketAlertCheck(p2, 1))) {
|
||
|
printf("sid 1 didn't match on p2 but should have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
FlowVar *fv = FlowVarGet(&f, 1);
|
||
|
if (fv == NULL) {
|
||
|
printf("no flowvar: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
if (fv->data.fv_str.value_len != 1) {
|
||
|
printf("%u != %u: ", fv->data.fv_str.value_len, 1);
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
if (memcmp(fv->data.fv_str.value, "2", 1) != 0) {
|
||
|
PrintRawDataFp(stdout, fv->data.fv_str.value, fv->data.fv_str.value_len);
|
||
|
|
||
|
printf("buffer mismatch: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
result = 1;
|
||
|
end:
|
||
|
if (de_ctx != NULL)
|
||
|
DetectEngineCtxFree(de_ctx);
|
||
|
|
||
|
StreamTcpFreeConfig(TRUE);
|
||
|
FLOW_DESTROY(&f);
|
||
|
UTHFreePackets(&p1, 1);
|
||
|
UTHFreePackets(&p2, 1);
|
||
|
return result;
|
||
13 years ago
|
}
|
||
12 years ago
|
|
||
|
/** \test http buffer, flowints */
|
||
11 years ago
|
static int LuaMatchTest04(void)
|
||
11 years ago
|
{
|
||
12 years ago
|
const char script[] =
|
||
|
"function init (args)\n"
|
||
|
" local needs = {}\n"
|
||
|
" needs[\"http.request_headers\"] = tostring(true)\n"
|
||
|
" needs[\"flowint\"] = {\"cnt\"}\n"
|
||
|
" return needs\n"
|
||
|
"end\n"
|
||
|
"\n"
|
||
|
"function match(args)\n"
|
||
|
" print \"inspecting\""
|
||
|
" a = ScFlowintGet(0)\n"
|
||
|
" if a then\n"
|
||
|
" ScFlowintSet(0, a + 1)\n"
|
||
|
" else\n"
|
||
|
" ScFlowintSet(0, 1)\n"
|
||
|
" end\n"
|
||
|
" \n"
|
||
|
" a = ScFlowintGet(0)\n"
|
||
|
" if a == 2 then\n"
|
||
|
" print \"match\"\n"
|
||
|
" return 1\n"
|
||
|
" end\n"
|
||
|
" return 0\n"
|
||
|
"end\n"
|
||
|
"return 0\n";
|
||
|
char sig[] = "alert http any any -> any any (flow:to_server; luajit:unittest; sid:1;)";
|
||
|
int result = 0;
|
||
|
uint8_t httpbuf1[] =
|
||
|
"POST / HTTP/1.1\r\n"
|
||
12 years ago
|
"Host: www.emergingthreats.net\r\n\r\n";
|
||
12 years ago
|
uint8_t httpbuf2[] =
|
||
12 years ago
|
"POST / HTTP/1.1\r\n"
|
||
|
"Host: www.openinfosecfoundation.org\r\n\r\n";
|
||
12 years ago
|
uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
|
||
|
uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
|
||
|
TcpSession ssn;
|
||
|
Packet *p1 = NULL;
|
||
|
Packet *p2 = NULL;
|
||
|
Flow f;
|
||
|
Signature *s = NULL;
|
||
|
ThreadVars th_v;
|
||
|
DetectEngineThreadCtx *det_ctx;
|
||
|
|
||
12 years ago
|
AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc();
|
||
12 years ago
|
|
||
12 years ago
|
ut_script = script;
|
||
|
|
||
|
memset(&th_v, 0, sizeof(th_v));
|
||
|
memset(&f, 0, sizeof(f));
|
||
|
memset(&ssn, 0, sizeof(ssn));
|
||
|
|
||
|
p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
|
||
|
p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
|
||
|
|
||
|
FLOW_INITIALIZE(&f);
|
||
|
f.protoctx = (void *)&ssn;
|
||
12 years ago
|
f.proto = IPPROTO_TCP;
|
||
12 years ago
|
f.flags |= FLOW_IPV4;
|
||
|
f.alproto = ALPROTO_HTTP;
|
||
|
|
||
|
p1->flow = &f;
|
||
|
p1->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p1->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p1->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
|
||
|
p2->flow = &f;
|
||
|
p2->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p2->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p2->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
|
||
|
StreamTcpInitConfig(TRUE);
|
||
|
|
||
|
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
|
||
|
if (de_ctx == NULL) {
|
||
|
goto end;
|
||
|
}
|
||
|
de_ctx->flags |= DE_QUIET;
|
||
|
|
||
|
s = DetectEngineAppendSig(de_ctx, sig);
|
||
|
if (s == NULL) {
|
||
|
printf("sig parse failed: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
SigGroupBuild(de_ctx);
|
||
|
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
|
||
|
|
||
12 years ago
|
SCMutexLock(&f.m);
|
||
12 years ago
|
int r = AppLayerParserParse(alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, httpbuf1, httplen1);
|
||
12 years ago
|
if (r != 0) {
|
||
|
printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
goto end;
|
||
|
}
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
HtpState *http_state = f.alstate;
|
||
|
if (http_state == NULL) {
|
||
|
printf("no http state: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
/* do detect for p1 */
|
||
|
SCLogInfo("p1");
|
||
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
|
||
|
|
||
|
if (PacketAlertCheck(p1, 1)) {
|
||
|
printf("sid 1 matched on p1 but should not have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
12 years ago
|
SCMutexLock(&f.m);
|
||
12 years ago
|
r = AppLayerParserParse(alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, httpbuf2, httplen2);
|
||
12 years ago
|
if (r != 0) {
|
||
|
printf("toserver chunk 2 returned %" PRId32 ", expected 0: ", r);
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
goto end;
|
||
|
}
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
/* do detect for p2 */
|
||
|
SCLogInfo("p2");
|
||
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
|
||
|
|
||
|
if (!(PacketAlertCheck(p2, 1))) {
|
||
|
printf("sid 1 didn't match on p2 but should have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
FlowVar *fv = FlowVarGet(&f, 1);
|
||
|
if (fv == NULL) {
|
||
|
printf("no flowvar: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
if (fv->data.fv_int.value != 2) {
|
||
|
printf("%u != %u: ", fv->data.fv_int.value, 2);
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
result = 1;
|
||
|
end:
|
||
12 years ago
|
if (alp_tctx != NULL)
|
||
12 years ago
|
AppLayerParserThreadCtxFree(alp_tctx);
|
||
12 years ago
|
if (de_ctx != NULL)
|
||
|
DetectEngineCtxFree(de_ctx);
|
||
|
|
||
|
StreamTcpFreeConfig(TRUE);
|
||
|
FLOW_DESTROY(&f);
|
||
|
UTHFreePackets(&p1, 1);
|
||
|
UTHFreePackets(&p2, 1);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
12 years ago
|
/** \test http buffer, flowints */
|
||
11 years ago
|
static int LuaMatchTest05(void)
|
||
11 years ago
|
{
|
||
12 years ago
|
const char script[] =
|
||
|
"function init (args)\n"
|
||
|
" local needs = {}\n"
|
||
|
" needs[\"http.request_headers\"] = tostring(true)\n"
|
||
|
" needs[\"flowint\"] = {\"cnt\"}\n"
|
||
|
" return needs\n"
|
||
|
"end\n"
|
||
|
"\n"
|
||
|
"function match(args)\n"
|
||
|
" print \"inspecting\""
|
||
|
" a = ScFlowintIncr(0)\n"
|
||
|
" if a == 2 then\n"
|
||
|
" print \"match\"\n"
|
||
|
" return 1\n"
|
||
|
" end\n"
|
||
|
" return 0\n"
|
||
|
"end\n"
|
||
|
"return 0\n";
|
||
|
char sig[] = "alert http any any -> any any (flow:to_server; luajit:unittest; sid:1;)";
|
||
|
int result = 0;
|
||
|
uint8_t httpbuf1[] =
|
||
|
"POST / HTTP/1.1\r\n"
|
||
12 years ago
|
"Host: www.emergingthreats.net\r\n\r\n";
|
||
12 years ago
|
uint8_t httpbuf2[] =
|
||
12 years ago
|
"POST / HTTP/1.1\r\n"
|
||
|
"Host: www.openinfosecfoundation.org\r\n\r\n";
|
||
12 years ago
|
uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
|
||
|
uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
|
||
|
TcpSession ssn;
|
||
|
Packet *p1 = NULL;
|
||
|
Packet *p2 = NULL;
|
||
|
Flow f;
|
||
|
Signature *s = NULL;
|
||
|
ThreadVars th_v;
|
||
|
DetectEngineThreadCtx *det_ctx;
|
||
|
|
||
12 years ago
|
AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc();
|
||
12 years ago
|
|
||
12 years ago
|
ut_script = script;
|
||
|
|
||
|
memset(&th_v, 0, sizeof(th_v));
|
||
|
memset(&f, 0, sizeof(f));
|
||
|
memset(&ssn, 0, sizeof(ssn));
|
||
|
|
||
|
p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
|
||
|
p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
|
||
|
|
||
|
FLOW_INITIALIZE(&f);
|
||
|
f.protoctx = (void *)&ssn;
|
||
12 years ago
|
f.proto = IPPROTO_TCP;
|
||
12 years ago
|
f.flags |= FLOW_IPV4;
|
||
|
f.alproto = ALPROTO_HTTP;
|
||
|
|
||
|
p1->flow = &f;
|
||
|
p1->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p1->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p1->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
|
||
|
p2->flow = &f;
|
||
|
p2->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p2->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p2->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
|
||
|
StreamTcpInitConfig(TRUE);
|
||
|
|
||
|
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
|
||
|
if (de_ctx == NULL) {
|
||
|
goto end;
|
||
|
}
|
||
|
de_ctx->flags |= DE_QUIET;
|
||
|
|
||
|
s = DetectEngineAppendSig(de_ctx, sig);
|
||
|
if (s == NULL) {
|
||
|
printf("sig parse failed: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
SigGroupBuild(de_ctx);
|
||
|
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
|
||
|
|
||
12 years ago
|
SCMutexLock(&f.m);
|
||
12 years ago
|
int r = AppLayerParserParse(alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, httpbuf1, httplen1);
|
||
12 years ago
|
if (r != 0) {
|
||
|
printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
goto end;
|
||
|
}
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
HtpState *http_state = f.alstate;
|
||
|
if (http_state == NULL) {
|
||
|
printf("no http state: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
/* do detect for p1 */
|
||
|
SCLogInfo("p1");
|
||
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
|
||
|
|
||
|
if (PacketAlertCheck(p1, 1)) {
|
||
|
printf("sid 1 matched on p1 but should not have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
12 years ago
|
SCMutexLock(&f.m);
|
||
12 years ago
|
r = AppLayerParserParse(alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, httpbuf2, httplen2);
|
||
12 years ago
|
if (r != 0) {
|
||
|
printf("toserver chunk 2 returned %" PRId32 ", expected 0: ", r);
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
goto end;
|
||
|
}
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
/* do detect for p2 */
|
||
|
SCLogInfo("p2");
|
||
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
|
||
|
|
||
|
if (!(PacketAlertCheck(p2, 1))) {
|
||
|
printf("sid 1 didn't match on p2 but should have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
FlowVar *fv = FlowVarGet(&f, 1);
|
||
|
if (fv == NULL) {
|
||
|
printf("no flowvar: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
if (fv->data.fv_int.value != 2) {
|
||
|
printf("%u != %u: ", fv->data.fv_int.value, 2);
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
result = 1;
|
||
|
end:
|
||
12 years ago
|
if (alp_tctx != NULL)
|
||
12 years ago
|
AppLayerParserThreadCtxFree(alp_tctx);
|
||
12 years ago
|
if (de_ctx != NULL)
|
||
|
DetectEngineCtxFree(de_ctx);
|
||
|
|
||
|
StreamTcpFreeConfig(TRUE);
|
||
|
FLOW_DESTROY(&f);
|
||
|
UTHFreePackets(&p1, 1);
|
||
|
UTHFreePackets(&p2, 1);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/** \test http buffer, flowints */
|
||
11 years ago
|
static int LuaMatchTest06(void)
|
||
11 years ago
|
{
|
||
12 years ago
|
const char script[] =
|
||
|
"function init (args)\n"
|
||
|
" local needs = {}\n"
|
||
|
" needs[\"http.request_headers\"] = tostring(true)\n"
|
||
|
" needs[\"flowint\"] = {\"cnt\"}\n"
|
||
|
" return needs\n"
|
||
|
"end\n"
|
||
|
"\n"
|
||
|
"function match(args)\n"
|
||
|
" print \"inspecting\""
|
||
|
" a = ScFlowintGet(0)\n"
|
||
|
" if a == nil then\n"
|
||
|
" print \"new var set to 2\""
|
||
|
" ScFlowintSet(0, 2)\n"
|
||
|
" end\n"
|
||
|
" a = ScFlowintDecr(0)\n"
|
||
|
" if a == 0 then\n"
|
||
|
" print \"match\"\n"
|
||
|
" return 1\n"
|
||
|
" end\n"
|
||
|
" return 0\n"
|
||
|
"end\n"
|
||
|
"return 0\n";
|
||
|
char sig[] = "alert http any any -> any any (flow:to_server; luajit:unittest; sid:1;)";
|
||
|
int result = 0;
|
||
|
uint8_t httpbuf1[] =
|
||
|
"POST / HTTP/1.1\r\n"
|
||
12 years ago
|
"Host: www.emergingthreats.net\r\n\r\n";
|
||
12 years ago
|
uint8_t httpbuf2[] =
|
||
12 years ago
|
"POST / HTTP/1.1\r\n"
|
||
|
"Host: www.openinfosecfoundation.org\r\n\r\n";
|
||
12 years ago
|
uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
|
||
|
uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
|
||
|
TcpSession ssn;
|
||
|
Packet *p1 = NULL;
|
||
|
Packet *p2 = NULL;
|
||
|
Flow f;
|
||
|
Signature *s = NULL;
|
||
|
ThreadVars th_v;
|
||
|
DetectEngineThreadCtx *det_ctx;
|
||
|
|
||
12 years ago
|
AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc();
|
||
12 years ago
|
|
||
12 years ago
|
ut_script = script;
|
||
|
|
||
|
memset(&th_v, 0, sizeof(th_v));
|
||
|
memset(&f, 0, sizeof(f));
|
||
|
memset(&ssn, 0, sizeof(ssn));
|
||
|
|
||
|
p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
|
||
|
p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
|
||
|
|
||
|
FLOW_INITIALIZE(&f);
|
||
|
f.protoctx = (void *)&ssn;
|
||
12 years ago
|
f.proto = IPPROTO_TCP;
|
||
12 years ago
|
f.flags |= FLOW_IPV4;
|
||
|
f.alproto = ALPROTO_HTTP;
|
||
|
|
||
|
p1->flow = &f;
|
||
|
p1->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p1->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p1->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
|
||
|
p2->flow = &f;
|
||
|
p2->flowflags |= FLOW_PKT_TOSERVER;
|
||
|
p2->flowflags |= FLOW_PKT_ESTABLISHED;
|
||
|
p2->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
|
||
|
|
||
|
StreamTcpInitConfig(TRUE);
|
||
|
|
||
|
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
|
||
|
if (de_ctx == NULL) {
|
||
|
goto end;
|
||
|
}
|
||
|
de_ctx->flags |= DE_QUIET;
|
||
|
|
||
|
s = DetectEngineAppendSig(de_ctx, sig);
|
||
|
if (s == NULL) {
|
||
|
printf("sig parse failed: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
SigGroupBuild(de_ctx);
|
||
|
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
|
||
|
|
||
12 years ago
|
SCMutexLock(&f.m);
|
||
12 years ago
|
int r = AppLayerParserParse(alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, httpbuf1, httplen1);
|
||
12 years ago
|
if (r != 0) {
|
||
|
printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
goto end;
|
||
|
}
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
HtpState *http_state = f.alstate;
|
||
|
if (http_state == NULL) {
|
||
|
printf("no http state: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
/* do detect for p1 */
|
||
|
SCLogInfo("p1");
|
||
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
|
||
|
|
||
|
if (PacketAlertCheck(p1, 1)) {
|
||
|
printf("sid 1 matched on p1 but should not have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
12 years ago
|
SCMutexLock(&f.m);
|
||
12 years ago
|
r = AppLayerParserParse(alp_tctx, &f, ALPROTO_HTTP, STREAM_TOSERVER, httpbuf2, httplen2);
|
||
12 years ago
|
if (r != 0) {
|
||
|
printf("toserver chunk 2 returned %" PRId32 ", expected 0: ", r);
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
goto end;
|
||
|
}
|
||
12 years ago
|
SCMutexUnlock(&f.m);
|
||
12 years ago
|
/* do detect for p2 */
|
||
|
SCLogInfo("p2");
|
||
|
SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
|
||
|
|
||
|
if (!(PacketAlertCheck(p2, 1))) {
|
||
|
printf("sid 1 didn't match on p2 but should have: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
FlowVar *fv = FlowVarGet(&f, 1);
|
||
|
if (fv == NULL) {
|
||
|
printf("no flowvar: ");
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
if (fv->data.fv_int.value != 0) {
|
||
|
printf("%u != %u: ", fv->data.fv_int.value, 0);
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
result = 1;
|
||
|
end:
|
||
12 years ago
|
if (alp_tctx != NULL)
|
||
12 years ago
|
AppLayerParserThreadCtxFree(alp_tctx);
|
||
12 years ago
|
if (de_ctx != NULL)
|
||
|
DetectEngineCtxFree(de_ctx);
|
||
|
|
||
|
StreamTcpFreeConfig(TRUE);
|
||
|
FLOW_DESTROY(&f);
|
||
|
UTHFreePackets(&p1, 1);
|
||
|
UTHFreePackets(&p2, 1);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
13 years ago
|
#endif
|
||
|
|
||
11 years ago
|
void DetectLuaRegisterTests(void)
|
||
11 years ago
|
{
|
||
13 years ago
|
#ifdef UNITTESTS
|
||
11 years ago
|
UtRegisterTest("LuaMatchTest01", LuaMatchTest01, 1);
|
||
|
UtRegisterTest("LuaMatchTest02", LuaMatchTest02, 1);
|
||
|
UtRegisterTest("LuaMatchTest03", LuaMatchTest03, 1);
|
||
|
UtRegisterTest("LuaMatchTest04", LuaMatchTest04, 1);
|
||
|
UtRegisterTest("LuaMatchTest05", LuaMatchTest05, 1);
|
||
|
UtRegisterTest("LuaMatchTest06", LuaMatchTest06, 1);
|
||
13 years ago
|
#endif
|
||
|
}
|
||
|
|
||
|
#endif /* HAVE_LUAJIT */
|