Go to the documentation of this file.
59 #define PARSE_REGEX "^([a-z]+)" "(?:,\\s*([^,]+))?" "(?:,\\s*(?:track\\s+([^,]+)))" "(?:,\\s*(?:expire\\s+([^,]+)))?"
65 static void XBitsRegisterTests(
void);
148 return DetectIPPairbitMatchIsset(p,xd);
150 return DetectIPPairbitMatchIsnotset(p,xd);
152 return DetectIPPairbitMatchSet(p,xd);
154 return DetectIPPairbitMatchUnset(p,xd);
156 return DetectIPPairbitMatchToggle(p,xd);
199 char fb_cmd_str[16] =
"", fb_name[256] =
"";
200 char hb_dir_str[16] =
"";
204 pcre2_match_data *match = NULL;
206 if (ret != 2 && ret != 3 && ret != 4 && ret != 5) {
207 SCLogError(
"\"%s\" is not a valid setting for xbits.", rawstr);
209 pcre2_match_data_free(match);
214 pcre2len =
sizeof(fb_cmd_str);
215 int res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)fb_cmd_str, &pcre2len);
217 SCLogError(
"pcre2_substring_copy_bynumber failed");
218 pcre2_match_data_free(match);
223 pcre2len =
sizeof(fb_name);
224 res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)fb_name, &pcre2len);
226 SCLogError(
"pcre2_substring_copy_bynumber failed");
227 pcre2_match_data_free(match);
231 pcre2len =
sizeof(hb_dir_str);
232 res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)hb_dir_str, &pcre2len);
234 SCLogError(
"pcre2_substring_copy_bynumber failed");
235 pcre2_match_data_free(match);
239 if (strlen(hb_dir_str) > 0) {
240 if (strcmp(hb_dir_str,
"ip_src") == 0) {
243 }
else if (strcmp(hb_dir_str,
"ip_dst") == 0) {
246 }
else if (strcmp(hb_dir_str,
"ip_pair") == 0) {
251 pcre2_match_data_free(match);
257 char expire_str[16] =
"";
258 pcre2len =
sizeof(expire_str);
259 res = pcre2_substring_copy_bynumber(
260 match, 4, (PCRE2_UCHAR8 *)expire_str, &pcre2len);
262 SCLogError(
"pcre2_substring_copy_bynumber failed");
263 pcre2_match_data_free(match);
271 pcre2_match_data_free(match);
276 pcre2_match_data_free(match);
284 pcre2_match_data_free(match);
285 if (strcmp(fb_cmd_str,
"noalert") == 0) {
287 }
else if (strcmp(fb_cmd_str,
"isset") == 0) {
289 }
else if (strcmp(fb_cmd_str,
"isnotset") == 0) {
291 }
else if (strcmp(fb_cmd_str,
"set") == 0) {
293 }
else if (strcmp(fb_cmd_str,
"unset") == 0) {
295 }
else if (strcmp(fb_cmd_str,
"toggle") == 0) {
298 SCLogError(
"xbits action \"%s\" is not supported.", fb_cmd_str);
304 if (strlen(fb_name) != 0)
315 if (strlen(fb_name) == 0)
330 SCLogDebug(
"idx %" PRIu32
", cmd %s, name %s",
331 cd->
idx, fb_cmd_str, strlen(fb_name) ? fb_name :
"(none)");
341 int result = DetectXbitParse(
de_ctx, rawstr, &cd);
344 }
else if (cd == NULL) {
392 static void XBitsTestSetup(
void)
402 static void XBitsTestShutdown(
void)
410 static int XBitsTestParse01(
void)
418 #define BAD_INPUT(str) \
419 FAIL_IF_NOT(DetectXbitParse(de_ctx, (str), &cd) == -1);
424 BAD_INPUT(
"set,abc,track nonsense, expire 3600");
425 BAD_INPUT(
"set,abc,track ip_source, expire 3600");
426 BAD_INPUT(
"set,abc,track ip_src, expire -1");
427 BAD_INPUT(
"set,abc,track ip_src, expire 0");
431 #define GOOD_INPUT(str, command, trk, typ, exp) \
432 FAIL_IF_NOT(DetectXbitParse(de_ctx, (str), &cd) == 0); \
434 FAIL_IF_NOT(cd->cmd == (command)); \
435 FAIL_IF_NOT(cd->tracker == (trk)); \
436 FAIL_IF_NOT(cd->type == (typ)); \
437 FAIL_IF_NOT(cd->expire == (exp)); \
438 DetectXbitFree(NULL, cd); \
445 GOOD_INPUT(
"set,abc,track ip_pair, expire 3600",
449 GOOD_INPUT(
"set,abc,track ip_src, expire 1234",
464 static int XBitsTestSig01(
void)
466 uint8_t *buf = (uint8_t *)
467 "GET /one/ HTTP/1.1\r\n"
468 "Host: one.example.org\r\n"
470 uint16_t buflen = strlen((
char *)buf);
478 memset(&th_v, 0,
sizeof(th_v));
483 p->
proto = IPPROTO_TCP;
492 "alert ip any any -> any any (xbits:set,abc,track ip_pair; content:\"GET \"; sid:1;)");
514 static int XBitsTestSig02(
void)
523 "alert ip any any -> any any (xbits:isset,abc,track ip_src; content:\"GET \"; sid:1;)");
527 "alert ip any any -> any any (xbits:isnotset,abc,track ip_dst; content:\"GET \"; sid:2;)");
531 "alert ip any any -> any any (xbits:set,abc,track ip_pair; content:\"GET \"; sid:3;)");
535 "alert ip any any -> any any (xbits:unset,abc,track ip_src; content:\"GET \"; sid:4;)");
539 "alert ip any any -> any any (xbits:toggle,abc,track ip_dst; content:\"GET \"; sid:5;)");
543 "alert ip any any -> any any (xbits:!set,abc,track ip_dst; content:\"GET \"; sid:6;)");
553 static void XBitsRegisterTests(
void)
void IPPairBitUnset(IPPair *h, uint32_t idx)
void StatsReleaseResources(void)
Releases the resources allotted by the Stats API.
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void DetectXbitsRegister(void)
void IPPairInitConfig(bool quiet)
initialize the configuration
SigTableElmt * sigmatch_table
void(* Free)(DetectEngineCtx *, void *)
void IPPairRelease(IPPair *h)
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
main detection engine ctx
void IPPairBitSet(IPPair *h, uint32_t idx, uint32_t expire)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
void StorageCleanup(void)
#define DETECT_XBITS_TRACK_IPDST
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)
#define GOOD_INPUT(str, command, trk, typ, exp)
@ DETECT_SM_LIST_POSTMATCH
int DetectXbitMatchHost(Packet *p, const DetectXbitsData *xd)
#define DETECT_XBITS_CMD_ISNOTSET
#define DETECT_XBITS_EXPIRE_DEFAULT
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
#define PASS
Pass the test.
void IPPairBitToggle(IPPair *h, uint32_t idx, uint32_t expire)
void IPPairCleanup(void)
Cleanup the ippair engine.
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)
#define DETECT_XBITS_TRACK_IPPAIR
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
IPPair * IPPairGetIPPairFromHash(Address *a, Address *b)
int IPPairBitIsnotset(IPPair *h, uint32_t idx, uint32_t ts)
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...
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
void IPPairBitInitCtx(void)
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)
IPPair * IPPairLookupIPPairFromHash(Address *a, Address *b)
look up a ippair in the hash
void HostInitConfig(bool quiet)
initialize the configuration
int IPPairBitIsset(IPPair *h, uint32_t idx, uint32_t ts)
#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.
#define DETECT_XBITS_CMD_TOGGLE
#define DETECT_XBITS_CMD_UNSET
void StatsThreadCleanup(ThreadVars *tv)
#define SIGMATCH_IPONLY_COMPAT
void(* RegisterTests)(void)