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

Clam 2001 regex slash colon 0.105.2 #695

Conversation

micahsnyder
Copy link
Contributor

Cherry-pick of #653 for 0.105.2.

Do not merge until 0.105.2 version bump PR is merged.

There is an issue parsing PCRE patterns if the pattern contains a '/' in
the middle, followed by a ':'.  When splitting the subsignature (or yara
regex string) by ':' delimiters to identify the offset, it will
inadvertently think that the '/' in the middle of the sig is the end of
the PCRE string and will therefore consider the ':' in the string as
valid delimiter instead of ignoring it for being inside of the regex
string.

The solution I came up with is to ignore all content after a '/' when
tokenizing rather than ignoring content between a matching pair of /'s.
This works for LDB signatures because PCRE subsignatures are always
the last subsignature and because a ':' never comes *after* the PCRE
string.
It works for YARA rules because the `cli_tokenize()` function is only
ever used on the regex strings, never on the whole rule.

Fixes: Cisco-Talos#594
@micahsnyder micahsnyder changed the base branch from main to dev/0.105.2 September 8, 2022 04:39
@micahsnyder micahsnyder marked this pull request as draft September 9, 2022 23:24
@micahsnyder
Copy link
Contributor Author

Marked this as a draft until the fix-PR is reviewed, merged, and we can revise this to do whatever that does.

That is, this PR is bad and must not be merged until it can be fixed.

@micahsnyder
Copy link
Contributor Author

#696 is done. I'll fix up this backport PR next to include and squash those changes into this one.

My recent fix for the issue where a '\' followed by ':' in a Yara regex
string would fail to parse introduced a new issue that broke loading a
signature in the current daily.ldb database.

Unbeknownst to me at the time, you can have multiple PCRE subsignatures
in a logical signature, so long as they're the last subsignatures.
The previous fix made it so the signature parser muddled more than one
PCRE subsignature into one messed up regex string.

This commit essentially reverts the previous fix, while keeping some of
the code readability improvements in that function.
Instead, it addresses the problem a different way. To resolve the
original problem, I'm simply checking if the signame starts with "YARA".
If it does, we don't tokenize it by ':' delimiters.
I was unaware that while Yara rule regex strings may-or-may-not
escape '/' characters in the regex string, Clam logical sigs MUST escape
them. The Yara rule parser automatically removes the unnecessary '/':
https://github.com/Cisco-Talos/clamav/blob/clamav-0.105.1/libclamav/yara_lexer.l#L509-L514

That's a good feature, we don't want to remove that. But the Clam
logical sigs don't have an equivalent feature. So I changed the LDB
version of the regex '/' + ':' test to include the escape '\/'
character.

This commit also adds some new tests to make sure we don't break support
for LDB sigs with multiple PCRE subsignatures in the future, and to test
that the offset feature and the case-insensitive feature work for PCRE
subsignatures.
@micahsnyder
Copy link
Contributor Author

Okay this is updated. Leaving as a draft until version # is bumped in dev/0.105.2

@micahsnyder micahsnyder added this to the 0.105.2 milestone Nov 18, 2022
@micahsnyder micahsnyder marked this pull request as ready for review January 27, 2023 21:53
@micahsnyder micahsnyder merged commit 235f16c into Cisco-Talos:dev/0.105.2 Jan 27, 2023
23 of 24 checks passed
@micahsnyder micahsnyder deleted the CLAM-2001-regex-slash-colon-0.105.2 branch January 27, 2023 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant