Skip to content

Use the include directive to centralize common private rules#937

Merged
egibs merged 4 commits into
chainguard-dev:mainfrom
egibs:global-rules
May 21, 2025
Merged

Use the include directive to centralize common private rules#937
egibs merged 4 commits into
chainguard-dev:mainfrom
egibs:global-rules

Conversation

@egibs
Copy link
Copy Markdown
Member

@egibs egibs commented May 20, 2025

yara-x 0.15.0 introduced the include directive in parity with Yara. This PR leverages the directive by storing commonly-used private rules in a central globals.yara file which can then be included in rule files as necessary.

This directive inserts the included file's contents in each file but since we namespace by path we can have any number of duplicate rule names without encountering compilation errors.

Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
@tstromberg
Copy link
Copy Markdown

tstromberg commented May 20, 2025

For clarity when reviewing rules, can we prefix the global variables in some way? like global_macho instead of just macho?

Does this break the ability to run yara on a given rule file?

@egibs
Copy link
Copy Markdown
Member Author

egibs commented May 20, 2025

For clarity when reviewing rules, can we prefix the global variables in some way? like global_macho instead of just macho?

Does this break the ability to run yara on a given rule file?

We can, yea. I don't know the answer to the second question yet, but it appears that there are some quirks with the path when running tests even though the rules compile from the root of the repository just fine.

Edit: fixed the rule compilation in 059616d (#937).

Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
@egibs
Copy link
Copy Markdown
Member Author

egibs commented May 20, 2025

Does this break the ability to run yara on a given rule file?

Looks good using a rule with include ...:

$ yr scan --path-as-namespace --profiling -w -r rules/anti-static/macho/footer.yara out/chainguard-dev/malcontent-samples/
high_entropy_trailer out/chainguard-dev/malcontent-samples/macOS/2024.SpectralBlur.DPRK/SpectralBlur-macshare
high_entropy_trailer out/chainguard-dev/malcontent-samples/macOS/2024.Ezuri/libdpt1.so
high_entropy_trailer out/chainguard-dev/malcontent-samples/macOS/2024.Rustdoor/localfile
high_entropy_trailer out/chainguard-dev/malcontent-samples/macOS/2024.BeaverTail/Jami
high_entropy_trailer out/chainguard-dev/malcontent-samples/macOS/2024.LightSpy/dropper
high_entropy_trailer out/chainguard-dev/malcontent-samples/macOS/2024.Previewers/Previewers
high_entropy_trailer out/chainguard-dev/malcontent-samples/macOS/2024.cobaltstrike/EDnFsVAEbP
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 716 file(s) scanned in 4.8s. 7 file(s) matched.

«««««««««««« PROFILING INFORMATION »»»»»»»»»»»»

No profiling information gathered, all rules were very fast.

Comment thread pkg/compile/compile.go
@egibs egibs changed the title Use the include directive to centralize common private rules Use the include directive to centralize common private rules May 20, 2025
@egibs egibs requested a review from eslerm May 20, 2025 17:16
Comment thread pkg/compile/compile.go
Comment thread .github/workflows/style.yaml
Comment thread rules/anti-static/elf/entropy.yara
Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
@egibs egibs requested a review from eslerm May 20, 2025 21:36
@egibs egibs merged commit e9da72f into chainguard-dev:main May 21, 2025
12 checks passed
@egibs egibs deleted the global-rules branch May 28, 2025 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants