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.
303 lines
8.2 KiB
C
303 lines
8.2 KiB
C
15 years ago
|
/* Copyright (C) 2007-2010 Open Information Security Foundation
|
||
|
*
|
||
|
* 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.
|
||
|
*/
|
||
16 years ago
|
|
||
|
/**
|
||
|
* \file
|
||
15 years ago
|
*
|
||
16 years ago
|
* \author Brian Rectanus <brectanu@gmail.com>
|
||
|
*
|
||
15 years ago
|
* Implements the seq keyword.
|
||
16 years ago
|
*/
|
||
|
|
||
16 years ago
|
#include "suricata-common.h"
|
||
16 years ago
|
#include "debug.h"
|
||
|
#include "decode.h"
|
||
|
#include "detect.h"
|
||
|
|
||
|
#include "detect-parse.h"
|
||
|
#include "detect-engine.h"
|
||
9 years ago
|
#include "detect-engine-prefilter.h"
|
||
|
#include "detect-engine-prefilter-common.h"
|
||
16 years ago
|
|
||
6 years ago
|
#include "detect-tcp-seq.h"
|
||
16 years ago
|
|
||
|
#include "util-byte.h"
|
||
|
#include "util-unittest.h"
|
||
16 years ago
|
#include "util-unittest-helper.h"
|
||
16 years ago
|
#include "util-debug.h"
|
||
16 years ago
|
|
||
8 years ago
|
static int DetectSeqSetup(DetectEngineCtx *, Signature *, const char *);
|
||
16 years ago
|
static int DetectSeqMatch(ThreadVars *, DetectEngineThreadCtx *,
|
||
9 years ago
|
Packet *, const Signature *, const SigMatchCtx *);
|
||
16 years ago
|
static void DetectSeqRegisterTests(void);
|
||
16 years ago
|
static void DetectSeqFree(void *);
|
||
8 years ago
|
static int PrefilterSetupTcpSeq(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
|
||
9 years ago
|
static _Bool PrefilterTcpSeqIsPrefilterable(const Signature *s);
|
||
16 years ago
|
|
||
11 years ago
|
void DetectSeqRegister(void)
|
||
|
{
|
||
6 years ago
|
sigmatch_table[DETECT_SEQ].name = "tcp.seq";
|
||
|
sigmatch_table[DETECT_SEQ].alias = "seq";
|
||
13 years ago
|
sigmatch_table[DETECT_SEQ].desc = "check for a specific TCP sequence number";
|
||
9 years ago
|
sigmatch_table[DETECT_SEQ].url = DOC_URL DOC_VERSION "/rules/header-keywords.html#seq";
|
||
16 years ago
|
sigmatch_table[DETECT_SEQ].Match = DetectSeqMatch;
|
||
|
sigmatch_table[DETECT_SEQ].Setup = DetectSeqSetup;
|
||
|
sigmatch_table[DETECT_SEQ].Free = DetectSeqFree;
|
||
|
sigmatch_table[DETECT_SEQ].RegisterTests = DetectSeqRegisterTests;
|
||
9 years ago
|
|
||
|
sigmatch_table[DETECT_SEQ].SupportsPrefilter = PrefilterTcpSeqIsPrefilterable;
|
||
|
sigmatch_table[DETECT_SEQ].SetupPrefilter = PrefilterSetupTcpSeq;
|
||
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* \internal
|
||
|
* \brief This function is used to match packets with a given Seq number
|
||
|
*
|
||
|
* \param t pointer to thread vars
|
||
|
* \param det_ctx pointer to the pattern matcher thread
|
||
|
* \param p pointer to the current packet
|
||
|
* \param m pointer to the sigmatch that we will cast into DetectSeqData
|
||
|
*
|
||
|
* \retval 0 no match
|
||
|
* \retval 1 match
|
||
|
*/
|
||
|
static int DetectSeqMatch(ThreadVars *t, DetectEngineThreadCtx *det_ctx,
|
||
9 years ago
|
Packet *p, const Signature *s, const SigMatchCtx *ctx)
|
||
16 years ago
|
{
|
||
11 years ago
|
const DetectSeqData *data = (const DetectSeqData *)ctx;
|
||
16 years ago
|
|
||
|
/* This is only needed on TCP packets */
|
||
15 years ago
|
if (!(PKT_IS_TCP(p)) || PKT_IS_PSEUDOPKT(p)) {
|
||
16 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
16 years ago
|
return (data->seq == TCP_GET_SEQ(p)) ? 1 : 0;
|
||
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* \internal
|
||
|
* \brief this function is used to add the seq option into the signature
|
||
|
*
|
||
|
* \param de_ctx pointer to the Detection Engine Context
|
||
|
* \param s pointer to the Current Signature
|
||
|
* \param optstr pointer to the user provided options
|
||
|
*
|
||
|
* \retval 0 on Success
|
||
|
* \retval -1 on Failure
|
||
|
*/
|
||
8 years ago
|
static int DetectSeqSetup (DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
|
||
16 years ago
|
{
|
||
12 years ago
|
DetectSeqData *data = NULL;
|
||
16 years ago
|
SigMatch *sm = NULL;
|
||
|
|
||
16 years ago
|
data = SCMalloc(sizeof(DetectSeqData));
|
||
13 years ago
|
if (unlikely(data == NULL))
|
||
16 years ago
|
goto error;
|
||
|
|
||
|
sm = SigMatchAlloc();
|
||
12 years ago
|
if (sm == NULL)
|
||
16 years ago
|
goto error;
|
||
|
|
||
|
sm->type = DETECT_SEQ;
|
||
|
|
||
16 years ago
|
if (-1 == ByteExtractStringUint32(&data->seq, 10, 0, optstr)) {
|
||
16 years ago
|
goto error;
|
||
|
}
|
||
11 years ago
|
sm->ctx = (SigMatchCtx*)data;
|
||
16 years ago
|
|
||
14 years ago
|
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_MATCH);
|
||
14 years ago
|
s->flags |= SIG_FLAG_REQUIRE_PACKET;
|
||
16 years ago
|
|
||
|
return 0;
|
||
|
|
||
|
error:
|
||
12 years ago
|
if (data)
|
||
|
SCFree(data);
|
||
|
if (sm)
|
||
|
SigMatchFree(sm);
|
||
16 years ago
|
return -1;
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* \internal
|
||
|
* \brief this function will free memory associated with seq option
|
||
|
*
|
||
|
* \param data pointer to seq configuration data
|
||
|
*/
|
||
|
static void DetectSeqFree(void *ptr)
|
||
|
{
|
||
16 years ago
|
DetectSeqData *data = (DetectSeqData *)ptr;
|
||
16 years ago
|
SCFree(data);
|
||
16 years ago
|
}
|
||
|
|
||
9 years ago
|
/* prefilter code */
|
||
|
|
||
|
static void
|
||
|
PrefilterPacketSeqMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
|
||
|
{
|
||
|
const PrefilterPacketHeaderCtx *ctx = pectx;
|
||
|
|
||
9 years ago
|
if (PrefilterPacketHeaderExtraMatch(ctx, p) == FALSE)
|
||
|
return;
|
||
|
|
||
9 years ago
|
if ((p->proto) == IPPROTO_TCP && !(PKT_IS_PSEUDOPKT(p)) &&
|
||
|
(p->tcph != NULL) && (TCP_GET_SEQ(p) == ctx->v1.u32[0]))
|
||
|
{
|
||
|
SCLogDebug("packet matches TCP seq %u", ctx->v1.u32[0]);
|
||
|
PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
PrefilterPacketSeqSet(PrefilterPacketHeaderValue *v, void *smctx)
|
||
|
{
|
||
|
const DetectSeqData *a = smctx;
|
||
|
v->u32[0] = a->seq;
|
||
|
}
|
||
|
|
||
|
static _Bool
|
||
|
PrefilterPacketSeqCompare(PrefilterPacketHeaderValue v, void *smctx)
|
||
|
{
|
||
|
const DetectSeqData *a = smctx;
|
||
|
if (v.u32[0] == a->seq)
|
||
|
return TRUE;
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
8 years ago
|
static int PrefilterSetupTcpSeq(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
|
||
9 years ago
|
{
|
||
8 years ago
|
return PrefilterSetupPacketHeader(de_ctx, sgh, DETECT_SEQ,
|
||
9 years ago
|
PrefilterPacketSeqSet,
|
||
|
PrefilterPacketSeqCompare,
|
||
|
PrefilterPacketSeqMatch);
|
||
|
}
|
||
|
|
||
|
static _Bool PrefilterTcpSeqIsPrefilterable(const Signature *s)
|
||
|
{
|
||
|
const SigMatch *sm;
|
||
9 years ago
|
for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
|
||
9 years ago
|
switch (sm->type) {
|
||
|
case DETECT_SEQ:
|
||
|
return TRUE;
|
||
|
}
|
||
|
}
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
16 years ago
|
|
||
|
#ifdef UNITTESTS
|
||
16 years ago
|
|
||
16 years ago
|
/**
|
||
16 years ago
|
* \test DetectSeqSigTest01 tests parses
|
||
16 years ago
|
*/
|
||
16 years ago
|
static int DetectSeqSigTest01(void)
|
||
16 years ago
|
{
|
||
|
int result = 0;
|
||
|
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
|
||
16 years ago
|
if (de_ctx == NULL)
|
||
16 years ago
|
goto end;
|
||
|
|
||
|
/* These three are crammed in here as there is no Parse */
|
||
|
if (SigInit(de_ctx,
|
||
|
"alert tcp any any -> any any "
|
||
|
"(msg:\"Testing seq\";seq:foo;sid:1;)") != NULL)
|
||
|
{
|
||
|
printf("invalid seq accepted: ");
|
||
16 years ago
|
goto cleanup;
|
||
16 years ago
|
}
|
||
|
if (SigInit(de_ctx,
|
||
|
"alert tcp any any -> any any "
|
||
|
"(msg:\"Testing seq\";seq:9999999999;sid:1;)") != NULL)
|
||
|
{
|
||
|
printf("overflowing seq accepted: ");
|
||
16 years ago
|
goto cleanup;
|
||
16 years ago
|
}
|
||
|
if (SigInit(de_ctx,
|
||
|
"alert tcp any any -> any any "
|
||
|
"(msg:\"Testing seq\";seq:-100;sid:1;)") != NULL)
|
||
|
{
|
||
|
printf("negative seq accepted: ");
|
||
|
goto cleanup;
|
||
|
}
|
||
|
result = 1;
|
||
|
|
||
|
cleanup:
|
||
16 years ago
|
if (de_ctx) {
|
||
|
SigGroupCleanup(de_ctx);
|
||
|
SigCleanSignatures(de_ctx);
|
||
|
DetectEngineCtxFree(de_ctx);
|
||
|
}
|
||
16 years ago
|
end:
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
16 years ago
|
* \test DetectSeqSigTest02 tests seq keyword
|
||
16 years ago
|
*/
|
||
16 years ago
|
static int DetectSeqSigTest02(void)
|
||
16 years ago
|
{
|
||
16 years ago
|
int result = 0;
|
||
|
uint8_t *buf = (uint8_t *)"Hi all!";
|
||
|
uint16_t buflen = strlen((char *)buf);
|
||
|
Packet *p[3];
|
||
|
p[0] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
|
||
|
p[1] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
|
||
|
p[2] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_ICMP);
|
||
|
if (p[0] == NULL || p[1] == NULL ||p[2] == NULL)
|
||
|
goto end;
|
||
16 years ago
|
|
||
16 years ago
|
/* TCP w/seq=42 */
|
||
|
p[0]->tcph->th_seq = htonl(42);
|
||
16 years ago
|
|
||
16 years ago
|
/* TCP w/seq=100 */
|
||
|
p[1]->tcph->th_seq = htonl(100);
|
||
|
|
||
8 years ago
|
const char *sigs[2];
|
||
16 years ago
|
sigs[0]= "alert tcp any any -> any any (msg:\"Testing seq\"; seq:41; sid:1;)";
|
||
|
sigs[1]= "alert tcp any any -> any any (msg:\"Testing seq\"; seq:42; sid:2;)";
|
||
|
|
||
|
uint32_t sid[2] = {1, 2};
|
||
|
|
||
|
uint32_t results[3][2] = {
|
||
|
/* packet 0 match sid 1 but should not match sid 2 */
|
||
|
{0, 1},
|
||
|
/* packet 1 should not match */
|
||
|
{0, 0},
|
||
|
/* packet 2 should not match */
|
||
|
{0, 0} };
|
||
|
|
||
|
result = UTHGenericTest(p, 3, sigs, sid, (uint32_t *) results, 2);
|
||
|
UTHFreePackets(p, 3);
|
||
|
end:
|
||
|
return result;
|
||
16 years ago
|
}
|
||
|
|
||
|
#endif /* UNITTESTS */
|
||
|
|
||
|
/**
|
||
|
* \internal
|
||
|
* \brief This function registers unit tests for DetectSeq
|
||
|
*/
|
||
|
static void DetectSeqRegisterTests(void)
|
||
|
{
|
||
|
#ifdef UNITTESTS
|
||
9 years ago
|
UtRegisterTest("DetectSeqSigTest01", DetectSeqSigTest01);
|
||
|
UtRegisterTest("DetectSeqSigTest02", DetectSeqSigTest02);
|
||
16 years ago
|
#endif /* UNITTESTS */
|
||
|
}
|