53#define PARSE_REGEX "^(?:([\\+\\*!]))?\\s*([MDR]+)"
61#define MODIFIER_PLUS 2
64#define FRAGBITS_HAVE_MF 0x01
65#define FRAGBITS_HAVE_DF 0x02
66#define FRAGBITS_HAVE_RF 0x04
76static bool PrefilterFragBitsIsPrefilterable(
const Signature *s);
78static void FragBitsRegisterTests(
void);
103FragBitsMatch(
const uint8_t pbits,
const uint8_t modifier,
108 if ((pbits & dbits) > 0)
113 if (((pbits & dbits) == dbits) && (((pbits - dbits) > 0)))
118 if ((pbits & dbits) != dbits)
146 if (!
ctx || !PacketIsIPv4(p))
149 uint8_t fragbits = 0;
151 const IPV4Hdr *ip4h = PacketGetIPv4(p);
174 int found = 0, res = 0;
176 const char *str_ptr = NULL;
177 char *args[2] = { NULL, NULL};
180 pcre2_match_data *match = NULL;
184 SCLogError(
"pcre_exec parse error, ret %" PRId32
", string %s", ret, rawstr);
188 for (i = 0; i < (ret - 1); i++) {
191 SCLogError(
"pcre2_substring_get_bynumber failed %d", res);
195 args[i] = (
char *)str_ptr;
198 if (args[1] == NULL) {
209 if (args[0] && strlen(args[0])) {
228 while (*ptr !=
'\0') {
255 for (i = 0; i < 2; i++) {
257 pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
259 pcre2_match_data_free(match);
264 pcre2_match_data_free(match);
266 for (i = 0; i < 2; i++) {
268 pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
291 de = DetectFragBitsParse(rawstr);
327 if (!PacketIsIPv4(p))
330 uint8_t fragbits = 0;
331 const IPV4Hdr *ip4h = PacketGetIPv4(p);
339 if (FragBitsMatch(fragbits,
ctx->v1.u8[0],
ctx->v1.u8[1]))
341 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
368 PrefilterPacketFragBitsSet, PrefilterPacketFragBitsCompare,
369 PrefilterPacketFragBitsMatch);
372static bool PrefilterFragBitsIsPrefilterable(
const Signature *s)
398static int FragBitsTestParse01 (
void)
401 de = DetectFragBitsParse(
"M");
403 DetectFragBitsFree(NULL, de);
416static int FragBitsTestParse02 (
void)
419 de = DetectFragBitsParse(
"G");
421 DetectFragBitsFree(NULL, de);
434static int FragBitsTestParse03 (
void)
436 uint8_t raw_eth[] = {
437 0x00 ,0x40 ,0x33 ,0xd9 ,0x7c ,0xfd ,0x00 ,0x00,
438 0x39 ,0xcf ,0xd9 ,0xcd ,0x08 ,0x00 ,0x45 ,0x00,
439 0x01 ,0x13 ,0x9c ,0x5d ,0x40 ,0x00 ,0xf6 ,0x11,
440 0x44 ,0xca ,0x97 ,0xa4 ,0x01 ,0x08 ,0x0a ,0x00,
441 0x00 ,0x06 ,0x00 ,0x35 ,0x04 ,0x0b ,0x00 ,0xff,
442 0x3c ,0x87 ,0x7d ,0x9e ,0x85 ,0x80 ,0x00 ,0x01,
443 0x00 ,0x01 ,0x00 ,0x05 ,0x00 ,0x05 ,0x06 ,0x70,
444 0x69 ,0x63 ,0x61 ,0x72 ,0x64 ,0x07 ,0x75 ,0x74,
445 0x68 ,0x73 ,0x63 ,0x73 ,0x61 ,0x03 ,0x65 ,0x64,
446 0x75 ,0x00 ,0x00 ,0x01 ,0x00 ,0x01 ,0xc0 ,0x0c,
447 0x00 ,0x01 ,0x00 ,0x01 ,0x00 ,0x00 ,0x0e ,0x10,
448 0x00 ,0x04 ,0x81 ,0x6f ,0x1e ,0x1b ,0x07 ,0x75,
449 0x74 ,0x68 ,0x73 ,0x63 ,0x73 ,0x61 ,0x03 ,0x65,
450 0x64 ,0x75 ,0x00 ,0x00 ,0x02 ,0x00 ,0x01 ,0x00,
451 0x00 ,0x0e ,0x10 ,0x00 ,0x09 ,0x06 ,0x6b ,0x65,
452 0x6e ,0x6f ,0x62 ,0x69 ,0xc0 ,0x34 ,0xc0 ,0x34,
453 0x00 ,0x02 ,0x00 ,0x01 ,0x00 ,0x00 ,0x0e ,0x10,
454 0x00 ,0x07 ,0x04 ,0x6a ,0x69 ,0x6e ,0x6e ,0xc0,
455 0x34 ,0xc0 ,0x34 ,0x00 ,0x02 ,0x00 ,0x01 ,0x00,
456 0x00 ,0x0e ,0x10 ,0x00 ,0x0c ,0x04 ,0x64 ,0x6e,
457 0x73 ,0x31 ,0x04 ,0x6e ,0x6a ,0x69 ,0x74 ,0xc0,
458 0x3c ,0xc0 ,0x34 ,0x00 ,0x02 ,0x00 ,0x01 ,0x00,
459 0x00 ,0x0e ,0x10 ,0x00 ,0x08 ,0x05 ,0x65 ,0x6c,
460 0x7a ,0x69 ,0x70 ,0xc0 ,0x34 ,0xc0 ,0x34 ,0x00,
461 0x02 ,0x00 ,0x01 ,0x00 ,0x00 ,0x0e ,0x10 ,0x00,
462 0x08 ,0x05 ,0x61 ,0x72 ,0x77 ,0x65 ,0x6e ,0xc0,
463 0x34 ,0xc0 ,0x4b ,0x00 ,0x01 ,0x00 ,0x01 ,0x00,
464 0x00 ,0x0e ,0x10 ,0x00 ,0x04 ,0x81 ,0x6f ,0x1a,
465 0x06 ,0xc0 ,0x60 ,0x00 ,0x01 ,0x00 ,0x01 ,0x00,
466 0x00 ,0x0e ,0x10 ,0x00 ,0x04 ,0x81 ,0x6f ,0x1a,
467 0x07 ,0xc0 ,0x73 ,0x00 ,0x01 ,0x00 ,0x01 ,0x00,
468 0x01 ,0x03 ,0x82 ,0x00 ,0x04 ,0x80 ,0xeb ,0xfb,
469 0x0a ,0xc0 ,0x8b ,0x00 ,0x01 ,0x00 ,0x01 ,0x00,
470 0x00 ,0x0e ,0x10 ,0x00 ,0x04 ,0x81 ,0x6f ,0x01,
471 0x0b ,0xc0 ,0x9f ,0x00 ,0x01 ,0x00 ,0x01 ,0x00,
472 0x00 ,0x0e ,0x10 ,0x00 ,0x04 ,0x81 ,0x6f ,0x0b,
490 de = DetectFragBitsParse(
"D");
500 ret = DetectFragBitsMatch(NULL, p, NULL, sm->
ctx);
516static int FragBitsTestParse04 (
void)
518 uint8_t raw_eth[] = {
519 0x00 ,0x40 ,0x33 ,0xd9 ,0x7c ,0xfd ,0x00 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x02 ,0x00 ,0x01 ,0x00,
541 0x00 ,0x0e ,0x10 ,0x00 ,0x08 ,0x05 ,0x65 ,0x6c,
542 0x7a ,0x69 ,0x70 ,0xc0 ,0x34 ,0xc0 ,0x34 ,0x00,
543 0x02 ,0x00 ,0x01 ,0x00 ,0x00 ,0x0e ,0x10 ,0x00,
544 0x08 ,0x05 ,0x61 ,0x72 ,0x77 ,0x65 ,0x6e ,0xc0,
545 0x34 ,0xc0 ,0x4b ,0x00 ,0x01 ,0x00 ,0x01 ,0x00,
546 0x00 ,0x0e ,0x10 ,0x00 ,0x04 ,0x81 ,0x6f ,0x1a,
547 0x06 ,0xc0 ,0x60 ,0x00 ,0x01 ,0x00 ,0x01 ,0x00,
548 0x00 ,0x0e ,0x10 ,0x00 ,0x04 ,0x81 ,0x6f ,0x1a,
549 0x07 ,0xc0 ,0x73 ,0x00 ,0x01 ,0x00 ,0x01 ,0x00,
550 0x01 ,0x03 ,0x82 ,0x00 ,0x04 ,0x80 ,0xeb ,0xfb,
551 0x0a ,0xc0 ,0x8b ,0x00 ,0x01 ,0x00 ,0x01 ,0x00,
552 0x00 ,0x0e ,0x10 ,0x00 ,0x04 ,0x81 ,0x6f ,0x01,
553 0x0b ,0xc0 ,0x9f ,0x00 ,0x01 ,0x00 ,0x01 ,0x00,
554 0x00 ,0x0e ,0x10 ,0x00 ,0x04 ,0x81 ,0x6f ,0x0b,
572 de = DetectFragBitsParse(
"!D");
584 ret = DetectFragBitsMatch(NULL, p, NULL, sm->
ctx);
597static void FragBitsRegisterTests(
void)
AppLayerThreadCtx * AppLayerGetCtxThread(void)
Creates a new app layer thread context.
int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
#define IPV4_GET_RAW_FLAG_DF(ip4h)
#define IPV4_GET_RAW_FLAG_RF(ip4h)
#define IPV4_GET_RAW_FLAG_MF(ip4h)
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
int PrefilterSetupPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, SignatureMask mask, void(*Set)(PrefilterPacketHeaderValue *v, void *), bool(*Compare)(PrefilterPacketHeaderValue v, void *), void(*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
void DetectFragBitsRegister(void)
Registration function for fragbits: keyword.
int SC_Pcre2SubstringGet(pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR **bufferptr, PCRE2_SIZE *bufflen)
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
SigMatch * SigMatchAlloc(void)
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
#define SIG_FLAG_REQUIRE_PACKET
#define SIG_MASK_REQUIRE_REAL_PKT
void FlowInitConfig(bool quiet)
initialize the configuration
void FlowShutdown(void)
shutdown the flow engine
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
void PacketRecycle(Packet *p)
Structure to hold thread specific data for all decode modules.
AppLayerThreadCtx * app_tctx
main detection engine ctx
Container for matching data for a signature group.
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
a single match condition for a signature
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
void(* Free)(DetectEngineCtx *, void *)
void(* RegisterTests)(void)
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
bool(* SupportsPrefilter)(const Signature *s)
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
SignatureInitData * init_data
Per thread variable structure.
#define SCLogError(...)
Macro used to log ERROR messages.
#define DEBUG_VALIDATE_BUG_ON(exp)