Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect analyze/v25 #6319

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 58 additions & 14 deletions src/detect-content.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,47 @@ void DetectContentPropagateLimits(Signature *s)
}
}

static inline bool NeedsAsHex(uint8_t c)
{
if (!isprint(c))
return true;

switch (c) {
case '/':
case ';':
case ':':
case '\\':
case ' ':
case '|':
case '"':
case '`':
case '\'':
return true;
}
return false;
}

void DetectContentPatternPrettyPrint(const DetectContentData *cd, char *str, size_t str_len)
{
bool hex = false;
for (uint16_t i = 0; i < cd->content_len; i++) {
if (NeedsAsHex(cd->content[i])) {
char hex_str[4];
snprintf(hex_str, sizeof(hex_str), "%s%02X", !hex ? "|" : " ", cd->content[i]);
strlcat(str, hex_str, str_len);
hex = true;
} else {
char p_str[3];
snprintf(p_str, sizeof(p_str), "%s%c", hex ? "|" : "", cd->content[i]);
strlcat(str, p_str, str_len);
hex = false;
}
}
if (hex) {
strlcat(str, "|", str_len);
}
}

#ifdef UNITTESTS /* UNITTESTS */

static bool TestLastContent(const Signature *s, uint16_t o, uint16_t d)
Expand All @@ -652,20 +693,21 @@ static bool TestLastContent(const Signature *s, uint16_t o, uint16_t d)
return true;
}

#define TEST_RUN(sig, o, d) \
{ \
SCLogDebug("TEST_RUN start: '%s'", (sig)); \
DetectEngineCtx *de_ctx = DetectEngineCtxInit(); \
FAIL_IF_NULL(de_ctx); \
char rule[2048]; \
snprintf(rule, sizeof(rule), "alert tcp any any -> any any (%s sid:1; rev:1;)", (sig)); \
Signature *s = DetectEngineAppendSig(de_ctx, rule); \
FAIL_IF_NULL(s); \
SigAddressPrepareStage1(de_ctx); \
bool res = TestLastContent(s, (o), (d)); \
FAIL_IF(res == false); \
DetectEngineCtxFree(de_ctx); \
}
#define TEST_RUN(sig, o, d) \
{ \
SCLogDebug("TEST_RUN start: '%s'", (sig)); \
DetectEngineCtx *de_ctx = DetectEngineCtxInit(); \
FAIL_IF_NULL(de_ctx); \
de_ctx->flags |= DE_QUIET; \
char rule[2048]; \
snprintf(rule, sizeof(rule), "alert tcp any any -> any any (%s sid:1; rev:1;)", (sig)); \
Signature *s = DetectEngineAppendSig(de_ctx, rule); \
FAIL_IF_NULL(s); \
SigAddressPrepareStage1(de_ctx); \
bool res = TestLastContent(s, (o), (d)); \
FAIL_IF(res == false); \
DetectEngineCtxFree(de_ctx); \
}

#define TEST_DONE \
PASS
Expand All @@ -677,6 +719,8 @@ static int DetectContentDepthTest01(void)
TEST_RUN("content:\"abc\"; offset:1; depth:3;", 1, 4);
// dsize applied as depth
TEST_RUN("dsize:10; content:\"abc\";", 0, 10);
TEST_RUN("dsize:<10; content:\"abc\";", 0, 10);
TEST_RUN("dsize:5<>10; content:\"abc\";", 0, 10);

// relative match, directly following anchored content
TEST_RUN("content:\"abc\"; depth:3; content:\"xyz\"; distance:0; within:3; ", 3, 6);
Expand Down
2 changes: 2 additions & 0 deletions src/detect-content.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,6 @@ void DetectContentFree(DetectEngineCtx *, void *);
bool DetectContentPMATCHValidateCallback(const Signature *s);
void DetectContentPropagateLimits(Signature *s);

void DetectContentPatternPrettyPrint(const DetectContentData *cd, char *str, size_t str_len);

#endif /* __DETECT_CONTENT_H__ */
1 change: 1 addition & 0 deletions src/detect-dsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ void SigParseApplyDsizeToContent(Signature *s)
}

if (cd->depth == 0 || cd->depth >= dsize) {
cd->flags |= DETECT_CONTENT_DEPTH;
cd->depth = (uint16_t)dsize;
SCLogDebug("updated %u, content %u to have depth %u "
"because of dsize.", s->id, cd->id, cd->depth);
Expand Down
Loading