42#define PARSE_REGEX "^\\s*(\"\\s*)?([0-9]+)(\\s*\")?\\s*$"
50static void DetectIcmpIdRegisterTests(
void);
54static bool PrefilterIcmpIdIsPrefilterable(
const Signature *s);
76static inline bool GetIcmpId(
Packet *p, uint16_t *
id)
79 if (PacketIsICMPv4(p)) {
89 SCLogDebug(
"ICMPV4_GET_ID(p) %"PRIu16
" (network byte order), "
99 }
else if (PacketIsICMPv6(p)) {
103 SCLogDebug(
"ICMPV6_GET_ID(p) %"PRIu16
" (network byte order), "
138 if (!GetIcmpId(p, &pid))
160 char *substr[3] = {NULL, NULL, NULL};
164 pcre2_match_data *match = NULL;
166 if (ret < 1 || ret > 4) {
173 for (i = 1; i < ret; i++) {
176 SCLogError(
"pcre2_substring_get_bynumber failed");
179 substr[i-1] = (
char *)str_ptr;
187 if (substr[0]!= NULL && strlen(substr[0]) != 0) {
188 if (substr[2] == NULL) {
193 if (substr[2] != NULL) {
208 for (i = 0; i < 3; i++) {
209 if (substr[i] != NULL)
210 pcre2_substring_free((PCRE2_UCHAR8 *)substr[i]);
212 pcre2_match_data_free(match);
217 pcre2_match_data_free(match);
219 for (i = 0; i < 3; i++) {
220 if (substr[i] != NULL)
221 pcre2_substring_free((PCRE2_UCHAR8 *)substr[i]);
242 iid = DetectIcmpIdParse(
de_ctx, icmpidstr);
243 if (iid == NULL)
goto error;
279 if (!GetIcmpId(p, &pid))
282 if (pid ==
ctx->v1.u16[0])
285 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
300 if (v.
u16[0] == a->
id)
308 PrefilterPacketIcmpIdSet, PrefilterPacketIcmpIdCompare, PrefilterPacketIcmpIdMatch);
311static bool PrefilterIcmpIdIsPrefilterable(
const Signature *s)
330static int DetectIcmpIdParseTest01 (
void)
343static int DetectIcmpIdParseTest02 (
void)
356static int DetectIcmpIdParseTest03 (
void)
369static int DetectIcmpIdParseTest04 (
void)
382static int DetectIcmpIdParseTest05 (
void)
394static int DetectIcmpIdMatchTest01 (
void)
419 s = s->
next =
SigInit(
de_ctx,
"alert icmp any any -> any any (icmp_id:21782; sid:2;)");
429 printf(
"sid 1 did not alert, but should have: ");
432 printf(
"sid 2 alerted, but should not have: ");
457static int DetectIcmpIdMatchTest02 (
void)
461 uint8_t raw_icmpv4[] = {
462 0x0b, 0x00, 0x8a, 0xdf, 0x00, 0x00, 0x00, 0x00,
463 0x45, 0x00, 0x00, 0x14, 0x25, 0x0c, 0x00, 0x00,
464 0xff, 0x11, 0x00, 0x00, 0x85, 0x64, 0xea, 0x5b,
465 0x51, 0xa6, 0xbb, 0x35, 0x59, 0x8a, 0x5a, 0xe2,
466 0x00, 0x14, 0x00, 0x00 };
477 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
483 p->
src.addr_data32[0] = 0x01020304;
484 p->
dst.addr_data32[0] = 0x04030201;
486 ip4h.s_ip_src.s_addr = p->
src.addr_data32[0];
487 ip4h.s_ip_dst.s_addr = p->
dst.addr_data32[0];
509 printf(
"sid 1 alerted, but should not have: ");
528static void DetectIcmpIdRegisterTests (
void)
530 UtRegisterTest(
"DetectIcmpIdParseTest01", DetectIcmpIdParseTest01);
531 UtRegisterTest(
"DetectIcmpIdParseTest02", DetectIcmpIdParseTest02);
532 UtRegisterTest(
"DetectIcmpIdParseTest03", DetectIcmpIdParseTest03);
533 UtRegisterTest(
"DetectIcmpIdParseTest04", DetectIcmpIdParseTest04);
534 UtRegisterTest(
"DetectIcmpIdParseTest05", DetectIcmpIdParseTest05);
535 UtRegisterTest(
"DetectIcmpIdMatchTest01", DetectIcmpIdMatchTest01);
536 UtRegisterTest(
"DetectIcmpIdMatchTest02", DetectIcmpIdMatchTest02);
int DecodeICMPV4(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Main ICMPv4 decoding function.
#define ICMP_TIMESTAMPREPLY
#define ICMP_ADDRESSREPLY
#define ICMP_INFO_REQUEST
#define ICMPV6_GET_TYPE(icmp6h)
#define ICMP6_ECHO_REQUEST
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
void SigCleanSignatures(DetectEngineCtx *de_ctx)
int SigGroupCleanup(DetectEngineCtx *de_ctx)
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))
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
void DetectIcmpIdRegister(void)
Registration function for icode: icmp_id.
void DetectIcmpIdFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectIcmpIdData
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)
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
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
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
#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
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
#define PASS
Pass the test.
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Structure to hold thread specific data for all decode modules.
main detection engine ctx
union PacketL4::L4Vars vars
struct Packet_::@33::@40 icmp_s
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.
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
#define SCLogError(...)
Macro used to log ERROR messages.
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
void UTHSetIPV4Hdr(Packet *p, IPV4Hdr *ip4h)