|
|
|
/* Copyright (C) 2007-2020 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* \author Breno Silva <breno.silva@gmail.com>
|
|
|
|
* \author Victor Julien <victor@inliniac.net>
|
|
|
|
*
|
|
|
|
* Implements fragbits keyword
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "suricata-common.h"
|
|
|
|
#include "suricata.h"
|
|
|
|
#include "decode.h"
|
|
|
|
|
|
|
|
#include "detect.h"
|
|
|
|
#include "detect-parse.h"
|
|
|
|
#include "detect-engine-prefilter.h"
|
|
|
|
#include "detect-engine-prefilter-common.h"
|
|
|
|
|
|
|
|
#include "flow-var.h"
|
|
|
|
#include "decode-events.h"
|
App layer API rewritten. The main files in question are:
app-layer.[ch], app-layer-detect-proto.[ch] and app-layer-parser.[ch].
Things addressed in this commit:
- Brings out a proper separation between protocol detection phase and the
parser phase.
- The dns app layer now is registered such that we don't use "dnstcp" and
"dnsudp" in the rules. A user who previously wrote a rule like this -
"alert dnstcp....." or
"alert dnsudp....."
would now have to use,
alert dns (ipproto:tcp;) or
alert udp (app-layer-protocol:dns;) or
alert ip (ipproto:udp; app-layer-protocol:dns;)
The same rules extend to other another such protocol, dcerpc.
- The app layer parser api now takes in the ipproto while registering
callbacks.
- The app inspection/detection engine also takes an ipproto.
- All app layer parser functions now take direction as STREAM_TOSERVER or
STREAM_TOCLIENT, as opposed to 0 or 1, which was taken by some of the
functions.
- FlowInitialize() and FlowRecycle() now resets proto to 0. This is
needed by unittests, which would try to clean the flow, and that would
call the api, AppLayerParserCleanupParserState(), which would try to
clean the app state, but the app layer now needs an ipproto to figure
out which api to internally call to clean the state, and if the ipproto
is 0, it would return without trying to clean the state.
- A lot of unittests are now updated where if they are using a flow and
they need to use the app layer, we would set a flow ipproto.
- The "app-layer" section in the yaml conf has also been updated as well.
12 years ago
|
|
|
#include "app-layer.h"
|
|
|
|
#include "app-layer-detect-proto.h"
|
|
|
|
|
|
|
|
#include "detect-fragbits.h"
|
|
|
|
#include "util-unittest.h"
|
|
|
|
#include "util-debug.h"
|
|
|
|
|
|
|
|
#include "pkt-var.h"
|
|
|
|
#include "host.h"
|
|
|
|
#include "util-profiling.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regex
|
|
|
|
* fragbits: [!+*](MDR)
|
|
|
|
*/
|
|
|
|
#define PARSE_REGEX "^(?:([\\+\\*!]))?\\s*([MDR]+)"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FragBits args[0] *(3) +(2) !(1)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MODIFIER_NOT 1
|
|
|
|
#define MODIFIER_PLUS 2
|
|
|
|
#define MODIFIER_ANY 3
|
|
|
|
|
|
|
|
#define FRAGBITS_HAVE_MF 0x01
|
|
|
|
#define FRAGBITS_HAVE_DF 0x02
|
|
|
|
#define FRAGBITS_HAVE_RF 0x04
|
|
|
|
|
|
|
|
static DetectParseRegex parse_regex;
|
|
|
|
|
|
|
|
static int DetectFragBitsMatch (DetectEngineThreadCtx *, Packet *,
|
|
|
|
const Signature *, const SigMatchCtx *);
|
|
|
|
static int DetectFragBitsSetup (DetectEngineCtx *, Signature *, const char *);
|
|
|
|
static void DetectFragBitsFree(DetectEngineCtx *, void *);
|
|
|
|
|
|
|
|
static int PrefilterSetupFragBits(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
|
|
|
|
static bool PrefilterFragBitsIsPrefilterable(const Signature *s);
|
|
|
|
#ifdef UNITTESTS
|
|
|
|
static void FragBitsRegisterTests(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Registration function for fragbits: keyword
|
|
|
|
*/
|
|
|
|
|
|
|
|
void DetectFragBitsRegister (void)
|
|
|
|
{
|
|
|
|
sigmatch_table[DETECT_FRAGBITS].name = "fragbits";
|
|
|
|
sigmatch_table[DETECT_FRAGBITS].desc = "check if the fragmentation and reserved bits are set in the IP header";
|
|
|
|
sigmatch_table[DETECT_FRAGBITS].url = "/rules/header-keywords.html#fragbits-ip-fragmentation";
|
|
|
|
sigmatch_table[DETECT_FRAGBITS].Match = DetectFragBitsMatch;
|
|
|
|
sigmatch_table[DETECT_FRAGBITS].Setup = DetectFragBitsSetup;
|
|
|
|
sigmatch_table[DETECT_FRAGBITS].Free = DetectFragBitsFree;
|
|
|
|
#ifdef UNITTESTS
|
|
|
|
sigmatch_table[DETECT_FRAGBITS].RegisterTests = FragBitsRegisterTests;
|
|
|
|
#endif
|
|
|
|
sigmatch_table[DETECT_FRAGBITS].SetupPrefilter = PrefilterSetupFragBits;
|
|
|
|
sigmatch_table[DETECT_FRAGBITS].SupportsPrefilter = PrefilterFragBitsIsPrefilterable;
|
|
|
|
|
|
|
|
DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
FragBitsMatch(const uint8_t pbits, const uint8_t modifier,
|
|
|
|
const uint8_t dbits)
|
|
|
|
{
|
|
|
|
switch (modifier) {
|
|
|
|
case MODIFIER_ANY:
|
|
|
|
if ((pbits & dbits) > 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case MODIFIER_PLUS:
|
|
|
|
if (((pbits & dbits) == dbits) && (((pbits - dbits) > 0)))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case MODIFIER_NOT:
|
|
|
|
if ((pbits & dbits) != dbits)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (pbits == dbits)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \internal
|
|
|
|
* \brief This function is used to match fragbits on a packet with those passed via fragbits:
|
|
|
|
*
|
|
|
|
* \param t pointer to thread vars
|
|
|
|
* \param det_ctx pointer to the pattern matcher thread
|
|
|
|
* \param p pointer to the current packet
|
|
|
|
* \param s pointer to the Signature
|
|
|
|
* \param m pointer to the sigmatch
|
|
|
|
*
|
|
|
|
* \retval 0 no match
|
|
|
|
* \retval 1 match
|
|
|
|
*/
|
|
|
|
static int DetectFragBitsMatch (DetectEngineThreadCtx *det_ctx,
|
|
|
|
Packet *p, const Signature *s, const SigMatchCtx *ctx)
|
|
|
|
{
|
|
|
|
DEBUG_VALIDATE_BUG_ON(PKT_IS_PSEUDOPKT(p));
|
|
|
|
if (!ctx || !PacketIsIPv4(p))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint8_t fragbits = 0;
|
|
|
|
const DetectFragBitsData *de = (const DetectFragBitsData *)ctx;
|
|
|
|
const IPV4Hdr *ip4h = PacketGetIPv4(p);
|
|
|
|
if (IPV4_GET_RAW_FLAG_MF(ip4h))
|
|
|
|
fragbits |= FRAGBITS_HAVE_MF;
|
|
|
|
if (IPV4_GET_RAW_FLAG_DF(ip4h))
|
|
|
|
fragbits |= FRAGBITS_HAVE_DF;
|
|
|
|
if (IPV4_GET_RAW_FLAG_RF(ip4h))
|
|
|
|
fragbits |= FRAGBITS_HAVE_RF;
|
|
|
|
|
|
|
|
return FragBitsMatch(fragbits, de->modifier, de->fragbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \internal
|
|
|
|
* \brief This function is used to parse fragbits options passed via fragbits: keyword
|
|
|
|
*
|
|
|
|
* \param rawstr Pointer to the user provided fragbits options
|
|
|
|
*
|
|
|
|
* \retval de pointer to DetectFragBitsData on success
|
|
|
|
* \retval NULL on failure
|
|
|
|
*/
|
|
|
|
static DetectFragBitsData *DetectFragBitsParse (const char *rawstr)
|
|
|
|
{
|
|
|
|
DetectFragBitsData *de = NULL;
|
|
|
|
int found = 0, res = 0;
|
|
|
|
size_t pcre2_len;
|
|
|
|
const char *str_ptr = NULL;
|
|
|
|
char *args[2] = { NULL, NULL};
|
|
|
|
char *ptr;
|
|
|
|
int i;
|
|
|
|
pcre2_match_data *match = NULL;
|
|
|
|
|
|
|
|
int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
|
|
|
|
if (ret < 1) {
|
|
|
|
SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < (ret - 1); i++) {
|
|
|
|
res = SC_Pcre2SubstringGet(match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
|
|
|
|
if (res < 0) {
|
|
|
|
SCLogError("pcre2_substring_get_bynumber failed %d", res);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
args[i] = (char *)str_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[1] == NULL) {
|
|
|
|
SCLogError("invalid value");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
de = SCCalloc(1, sizeof(DetectFragBitsData));
|
|
|
|
if (unlikely(de == NULL))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/** First parse args[0] */
|
|
|
|
|
|
|
|
if (args[0] && strlen(args[0])) {
|
|
|
|
ptr = args[0];
|
|
|
|
switch (*ptr) {
|
|
|
|
case '!':
|
|
|
|
de->modifier = MODIFIER_NOT;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
de->modifier = MODIFIER_PLUS;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
de->modifier = MODIFIER_ANY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Second parse first set of fragbits */
|
|
|
|
|
|
|
|
ptr = args[1];
|
|
|
|
|
|
|
|
while (*ptr != '\0') {
|
|
|
|
switch (*ptr) {
|
|
|
|
case 'M':
|
|
|
|
case 'm':
|
|
|
|
de->fragbits |= FRAGBITS_HAVE_MF;
|
|
|
|
found++;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
case 'd':
|
|
|
|
de->fragbits |= FRAGBITS_HAVE_DF;
|
|
|
|
found++;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
case 'r':
|
|
|
|
de->fragbits |= FRAGBITS_HAVE_RF;
|
|
|
|
found++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
found = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(found == 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (args[i] != NULL)
|
|
|
|
pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
|
|
|
|
}
|
|
|
|
pcre2_match_data_free(match);
|
|
|
|
return de;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (match) {
|
|
|
|
pcre2_match_data_free(match);
|
|
|
|
}
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (args[i] != NULL)
|
|
|
|
pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
|
|
|
|
}
|
|
|
|
if (de != NULL)
|
|
|
|
SCFree(de);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \internal
|
|
|
|
* \brief this function is used to add the parsed fragbits into the current signature
|
|
|
|
*
|
|
|
|
* \param de_ctx pointer to the Detection Engine Context
|
|
|
|
* \param s pointer to the Current Signature
|
|
|
|
* \param m pointer to the Current SigMatch
|
|
|
|
* \param rawstr pointer to the user provided fragbits options
|
|
|
|
*
|
|
|
|
* \retval 0 on Success
|
|
|
|
* \retval -1 on Failure
|
|
|
|
*/
|
|
|
|
static int DetectFragBitsSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
|
|
|
|
{
|
|
|
|
DetectFragBitsData *de = NULL;
|
|
|
|
|
|
|
|
de = DetectFragBitsParse(rawstr);
|
|
|
|
if (de == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (SCSigMatchAppendSMToList(
|
|
|
|
de_ctx, s, DETECT_FRAGBITS, (SigMatchCtx *)de, DETECT_SM_LIST_MATCH) == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
s->flags |= SIG_FLAG_REQUIRE_PACKET;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (de)
|
|
|
|
SCFree(de);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \internal
|
|
|
|
* \brief this function will free memory associated with DetectFragBitsData
|
|
|
|
*
|
|
|
|
* \param de pointer to DetectFragBitsData
|
|
|
|
*/
|
|
|
|
static void DetectFragBitsFree(DetectEngineCtx *de_ctx, void *de_ptr)
|
|
|
|
{
|
|
|
|
DetectFragBitsData *de = (DetectFragBitsData *)de_ptr;
|
|
|
|
if(de) SCFree(de);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
PrefilterPacketFragBitsMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
|
|
|
|
{
|
|
|
|
DEBUG_VALIDATE_BUG_ON(PKT_IS_PSEUDOPKT(p));
|
|
|
|
const PrefilterPacketHeaderCtx *ctx = pectx;
|
|
|
|
|
|
|
|
if (!PacketIsIPv4(p))
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint8_t fragbits = 0;
|
|
|
|
const IPV4Hdr *ip4h = PacketGetIPv4(p);
|
|
|
|
if (IPV4_GET_RAW_FLAG_MF(ip4h))
|
|
|
|
fragbits |= FRAGBITS_HAVE_MF;
|
|
|
|
if (IPV4_GET_RAW_FLAG_DF(ip4h))
|
|
|
|
fragbits |= FRAGBITS_HAVE_DF;
|
|
|
|
if (IPV4_GET_RAW_FLAG_RF(ip4h))
|
|
|
|
fragbits |= FRAGBITS_HAVE_RF;
|
|
|
|
|
|
|
|
if (FragBitsMatch(fragbits, ctx->v1.u8[0], ctx->v1.u8[1]))
|
|
|
|
{
|
|
|
|
PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
PrefilterPacketFragBitsSet(PrefilterPacketHeaderValue *v, void *smctx)
|
|
|
|
{
|
|
|
|
const DetectFragBitsData *fb = smctx;
|
|
|
|
v->u8[0] = fb->modifier;
|
|
|
|
v->u8[1] = fb->fragbits;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
PrefilterPacketFragBitsCompare(PrefilterPacketHeaderValue v, void *smctx)
|
|
|
|
{
|
|
|
|
const DetectFragBitsData *fb = smctx;
|
|
|
|
if (v.u8[0] == fb->modifier &&
|
|
|
|
v.u8[1] == fb->fragbits)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int PrefilterSetupFragBits(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
|
|
|
|
{
|
|
|
|
return PrefilterSetupPacketHeader(de_ctx, sgh, DETECT_FRAGBITS, SIG_MASK_REQUIRE_REAL_PKT,
|
|
|
|
PrefilterPacketFragBitsSet, PrefilterPacketFragBitsCompare,
|
|
|
|
PrefilterPacketFragBitsMatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PrefilterFragBitsIsPrefilterable(const Signature *s)
|
|
|
|
{
|
|
|
|
const SigMatch *sm;
|
|
|
|
for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
|
|
|
|
switch (sm->type) {
|
|
|
|
case DETECT_FRAGBITS:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ONLY TESTS BELOW THIS COMMENT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef UNITTESTS
|
|
|
|
#include "util-unittest-helper.h"
|
|
|
|
#include "packet.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \test FragBitsTestParse01 is a test for a valid fragbits value
|
|
|
|
*
|
|
|
|
* \retval 1 on success
|
|
|
|
* \retval 0 on failure
|
|
|
|
*/
|
|
|
|
static int FragBitsTestParse01 (void)
|
|
|
|
{
|
|
|
|
DetectFragBitsData *de = NULL;
|
|
|
|
de = DetectFragBitsParse("M");
|
|
|
|
if (de && (de->fragbits == FRAGBITS_HAVE_MF) ) {
|
|
|
|
DetectFragBitsFree(NULL, de);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \test FragBitsTestParse02 is a test for an invalid fragbits value
|
|
|
|
*
|
|
|
|
* \retval 1 on success
|
|
|
|
* \retval 0 on failure
|
|
|
|
*/
|
|
|
|
static int FragBitsTestParse02 (void)
|
|
|
|
{
|
|
|
|
DetectFragBitsData *de = NULL;
|
|
|
|
de = DetectFragBitsParse("G");
|
|
|
|
if (de) {
|
|
|
|
DetectFragBitsFree(NULL, de);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \test FragBitsTestParse03 test if DONT FRAG is set. Must return success
|
|
|
|
*
|
|
|
|
* \retval 1 on success
|
|
|
|
* \retval 0 on failure
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,0x04 ,0x81 ,0x6f ,0x0b,
|
|
|
|
0x51};
|
|
|
|
Packet *p = PacketGetFromAlloc();
|
|
|
|
FAIL_IF(unlikely(p == NULL));
|
|
|
|
ThreadVars tv;
|
|
|
|
DecodeThreadVars dtv;
|
|
|
|
int ret = 0;
|
|
|
|
DetectFragBitsData *de = NULL;
|
|
|
|
SigMatch *sm = NULL;
|
|
|
|
|
|
|
|
memset(&tv, 0, sizeof(ThreadVars));
|
|
|
|
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
|
|
|
dtv.app_tctx = AppLayerGetCtxThread();
|
|
|
|
|
|
|
|
FlowInitConfig(FLOW_QUIET);
|
|
|
|
|
|
|
|
DecodeEthernet(&tv, &dtv, p, raw_eth, sizeof(raw_eth));
|
|
|
|
|
|
|
|
de = DetectFragBitsParse("D");
|
|
|
|
|
|
|
|
FAIL_IF(de == NULL || (de->fragbits != FRAGBITS_HAVE_DF));
|
|
|
|
|
|
|
|
sm = SigMatchAlloc();
|
|
|
|
FAIL_IF(sm == NULL);
|
|
|
|
|
|
|
|
sm->type = DETECT_FRAGBITS;
|
|
|
|
sm->ctx = (SigMatchCtx *)de;
|
|
|
|
|
|
|
|
ret = DetectFragBitsMatch(NULL, p, NULL, sm->ctx);
|
|
|
|
FAIL_IF(ret == 0);
|
|
|
|
|
|
|
|
FlowShutdown();
|
|
|
|
SCFree(de);
|
|
|
|
SCFree(sm);
|
|
|
|
SCFree(p);
|
|
|
|
PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \test FragBitsTestParse04 test if DONT FRAG is not set. Must fails.
|
|
|
|
*
|
|
|
|
* \retval 1 on success
|
|
|
|
* \retval 0 on failure
|
|
|
|
*/
|
|
|
|
static int FragBitsTestParse04 (void)
|
|
|
|
{
|
|
|
|
uint8_t raw_eth[] = {
|
|
|
|
0x00 ,0x40 ,0x33 ,0xd9 ,0x7c ,0xfd ,0x00 ,0x00,
|
|
|
|
0x39 ,0xcf ,0xd9 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|
|
|
|
DetectFragBitsData *de = NULL;
|
|
|
|
SigMatch *sm = NULL;
|
|
|
|
|
|
|
|
memset(&tv, 0, sizeof(ThreadVars));
|
|
|
|
memset(&dtv, 0, sizeof(DecodeThreadVars));
|
|
|
|
dtv.app_tctx = AppLayerGetCtxThread();
|
|
|
|
|
|
|
|
FlowInitConfig(FLOW_QUIET);
|
|
|
|
|
|
|
|
DecodeEthernet(&tv, &dtv, p, raw_eth, sizeof(raw_eth));
|
|
|
|
|
|
|
|
de = DetectFragBitsParse("!D");
|
|
|
|
|
|
|
|
FAIL_IF(de == NULL);
|
|
|
|
FAIL_IF(de->fragbits != FRAGBITS_HAVE_DF);
|
|
|
|
FAIL_IF(de->modifier != MODIFIER_NOT);
|
|
|
|
|
|
|
|
sm = SigMatchAlloc();
|
|
|
|
FAIL_IF(sm == NULL);
|
|
|
|
|
|
|
|
sm->type = DETECT_FRAGBITS;
|
|
|
|
sm->ctx = (SigMatchCtx *)de;
|
|
|
|
|
|
|
|
ret = DetectFragBitsMatch(NULL, p, NULL, sm->ctx);
|
|
|
|
FAIL_IF(ret);
|
|
|
|
SCFree(de);
|
|
|
|
SCFree(sm);
|
|
|
|
PacketRecycle(p);
|
|
|
|
FlowShutdown();
|
|
|
|
SCFree(p);
|
|
|
|
PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief this function registers unit tests for FragBits
|
|
|
|
*/
|
|
|
|
static void FragBitsRegisterTests(void)
|
|
|
|
{
|
|
|
|
UtRegisterTest("FragBitsTestParse01", FragBitsTestParse01);
|
|
|
|
UtRegisterTest("FragBitsTestParse02", FragBitsTestParse02);
|
|
|
|
UtRegisterTest("FragBitsTestParse03", FragBitsTestParse03);
|
|
|
|
UtRegisterTest("FragBitsTestParse04", FragBitsTestParse04);
|
|
|
|
}
|
|
|
|
#endif /* UNITTESTS */
|