Go to the documentation of this file.
68 "(?:\\s*,\\s*([^\\s,]+))?(?:\\s*)?" \
69 "(?:\\s*,\\s*([^,\\s]+))?(?:\\s*)?" \
252 return DetectHostbitMatchIsset(p,xd);
254 return DetectHostbitMatchIsnotset(p,xd);
256 return DetectHostbitMatchSet(p,xd);
258 return DetectHostbitMatchUnset(p,xd);
260 return DetectHostbitMatchToggle(p,xd);
285 static int DetectHostbitParse(
const char *
str,
char *cmd,
int cmd_len,
286 char *name,
int name_len,
char *dir,
int dir_len)
291 pcre2_match_data *match = NULL;
293 if (count != 2 && count != 3 && count != 4) {
294 SCLogError(
"\"%s\" is not a valid setting for hostbits.",
str);
299 rc = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)cmd, &pcre2len);
301 SCLogError(
"pcre2_substring_copy_bynumber failed");
307 rc = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)name, &pcre2len);
309 SCLogError(
"pcre2_substring_copy_bynumber failed");
314 rc = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)dir, &pcre2len);
316 SCLogError(
"pcre2_substring_copy_bynumber failed");
322 pcre2_match_data_free(match);
327 pcre2_match_data_free(match);
337 char fb_cmd_str[16] =
"", fb_name[256] =
"";
338 char hb_dir_str[16] =
"";
340 if (!DetectHostbitParse(rawstr, fb_cmd_str,
sizeof(fb_cmd_str),
341 fb_name,
sizeof(fb_name), hb_dir_str,
sizeof(hb_dir_str))) {
345 if (strlen(hb_dir_str) > 0) {
346 if (strcmp(hb_dir_str,
"src") == 0)
348 else if (strcmp(hb_dir_str,
"dst") == 0)
350 else if (strcmp(hb_dir_str,
"both") == 0) {
360 if (strcmp(fb_cmd_str,
"noalert") == 0) {
362 }
else if (strcmp(fb_cmd_str,
"isset") == 0) {
364 }
else if (strcmp(fb_cmd_str,
"isnotset") == 0) {
366 }
else if (strcmp(fb_cmd_str,
"set") == 0) {
368 }
else if (strcmp(fb_cmd_str,
"unset") == 0) {
370 }
else if (strcmp(fb_cmd_str,
"toggle") == 0) {
373 SCLogError(
"ERROR: flowbits action \"%s\" is not supported.", fb_cmd_str);
379 if (strlen(fb_name) != 0)
389 if (strlen(fb_name) == 0)
404 SCLogDebug(
"idx %" PRIu32
", cmd %s, name %s",
405 cd->
idx, fb_cmd_str, strlen(fb_name) ? fb_name :
"(none)");
459 static void HostBitsTestSetup(
void)
467 static void HostBitsTestShutdown(
void)
473 static int HostBitsTestParse01(
void)
475 char cmd[16] =
"", name[256] =
"", dir[16] =
"";
478 FAIL_IF(!DetectHostbitParse(
"isset,name", cmd,
sizeof(cmd), name,
479 sizeof(name), dir,
sizeof(dir)));
480 FAIL_IF(strcmp(cmd,
"isset") != 0);
481 FAIL_IF(strcmp(name,
"name") != 0);
488 FAIL_IF(!DetectHostbitParse(
"isset, name", cmd,
sizeof(cmd), name,
489 sizeof(name), dir,
sizeof(dir)));
490 FAIL_IF(strcmp(cmd,
"isset") != 0);
491 FAIL_IF(strcmp(name,
"name") != 0);
497 FAIL_IF(!DetectHostbitParse(
"isset,name ", cmd,
sizeof(cmd), name,
498 sizeof(name), dir,
sizeof(dir)));
499 FAIL_IF(strcmp(cmd,
"isset") != 0);
500 FAIL_IF(strcmp(name,
"name") != 0);
506 FAIL_IF(!DetectHostbitParse(
"isset, name ", cmd,
sizeof(cmd), name,
507 sizeof(name), dir,
sizeof(dir)));
508 FAIL_IF(strcmp(cmd,
"isset") != 0);
509 FAIL_IF(strcmp(name,
"name") != 0);
515 FAIL_IF(!DetectHostbitParse(
"isset,name,src", cmd,
sizeof(cmd), name,
516 sizeof(name), dir,
sizeof(dir)));
517 FAIL_IF(strcmp(cmd,
"isset") != 0);
518 FAIL_IF(strcmp(name,
"name") != 0);
519 FAIL_IF(strcmp(dir,
"src") != 0);
525 FAIL_IF(!DetectHostbitParse(
"isset, name ,src", cmd,
sizeof(cmd), name,
526 sizeof(name), dir,
sizeof(dir)));
527 FAIL_IF(strcmp(cmd,
"isset") != 0);
528 FAIL_IF(strcmp(name,
"name") != 0);
529 FAIL_IF(strcmp(dir,
"src") != 0);
535 FAIL_IF(!DetectHostbitParse(
"isset, name , src ", cmd,
sizeof(cmd), name,
536 sizeof(name), dir,
sizeof(dir)));
537 FAIL_IF(strcmp(cmd,
"isset") != 0);
538 FAIL_IF(strcmp(name,
"name") != 0);
539 FAIL_IF(strcmp(dir,
"src") != 0);
545 FAIL_IF(DetectHostbitParse(
"isset, name withspace ", cmd,
sizeof(cmd), name,
546 sizeof(name), dir,
sizeof(dir)));
558 static int HostBitsTestSig01(
void)
560 uint8_t *buf = (uint8_t *)
561 "GET /one/ HTTP/1.1\r\n"
562 "Host: one.example.org\r\n"
564 uint16_t buflen = strlen((
char *)buf);
572 memset(&th_v, 0,
sizeof(th_v));
577 p->
proto = IPPROTO_TCP;
597 HostBitsTestShutdown();
608 static int HostBitsTestSig02(
void)
614 memset(&th_v, 0,
sizeof(th_v));
622 "alert ip any any -> any any (hostbits:isset,abc,src; content:\"GET \"; sid:1;)");
626 "alert ip any any -> any any (hostbits:isnotset,abc,dst; content:\"GET \"; sid:2;)");
630 "alert ip any any -> any any (hostbits:!isset,abc,dst; content:\"GET \"; sid:3;)");
639 "alert ip any any -> any any (hostbits:unset,abc,src; content:\"GET \"; sid:4;)");
643 "alert ip any any -> any any (hostbits:toggle,abc,dst; content:\"GET \"; sid:5;)");
657 static int HostBitsTestSig03(
void)
659 uint8_t *buf = (uint8_t *)
660 "GET /one/ HTTP/1.1\r\n"
661 "Host: one.example.org\r\n"
663 uint16_t buflen = strlen((
char *)buf);
673 memset(&th_v, 0,
sizeof(th_v));
678 p->
proto = IPPROTO_TCP;
687 s =
de_ctx->
sig_list =
SigInit(
de_ctx,
"alert ip any any -> any any (msg:\"isset option\"; hostbits:isset,fbt; content:\"GET \"; sid:1;)");
700 HostBitsTestShutdown();
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
SigTableElmt * sigmatch_table
void HostBitToggle(Host *h, uint32_t idx, uint32_t expire)
void(* Free)(DetectEngineCtx *, void *)
void HostBitsRegisterTests(void)
this function registers unit tests for HostBits
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
main detection engine ctx
Host * HostGetHostFromHash(Address *a)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
void StorageCleanup(void)
#define DETECT_XBITS_TRACK_IPDST
void DetectHostbitsRegister(void)
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
uint32_t VarNameStoreRegister(const char *name, const enum VarTypes type)
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
void HostBitInitCtx(void)
int HostBitIsset(Host *h, uint32_t idx, uint32_t ts)
@ DETECT_SM_LIST_POSTMATCH
int DetectXbitMatchHost(Packet *p, const DetectXbitsData *xd)
#define DETECT_XBITS_CMD_ISNOTSET
int HostBitIsnotset(Host *h, uint32_t idx, uint32_t ts)
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
#define PASS
Pass the test.
void HostBitUnset(Host *h, uint32_t idx)
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
void HostCleanup(void)
Cleanup the host engine.
Per thread variable structure.
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
void VarNameStoreUnregister(const uint32_t id, const enum VarTypes type)
void PacketFree(Packet *p)
Return a malloced packet.
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
void HostBitSet(Host *h, uint32_t idx, uint32_t expire)
int StorageFinalize(void)
Data structures and function prototypes for keeping state for the detection engine.
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
#define SCLogError(...)
Macro used to log ERROR messages.
#define DETECT_XBITS_TRACK_IPSRC
#define DETECT_XBITS_CMD_NOALERT
#define DETECT_XBITS_CMD_ISSET
DetectEngineCtx * DetectEngineCtxInit(void)
Host * HostLookupHostFromHash(Address *a)
look up a host in the hash
void HostInitConfig(bool quiet)
initialize the configuration
#define DETECT_XBITS_CMD_SET
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
void DetectHostbitFree(DetectEngineCtx *, void *)
#define DETECT_XBITS_CMD_TOGGLE
#define DETECT_XBITS_CMD_UNSET
#define SIGMATCH_IPONLY_COMPAT
void(* RegisterTests)(void)