strawberry/tests/test_yara_scanner.cpp
trvon 8411c70ba8
macOS: build and test fixes found re-validating on the Mac (57/57 green)
- fleet.cpp: HOST_NAME_MAX is Linux-only; use a 256-byte buffer (the
  POSIX/RFC 1035 limit gethostname() truncates to).
- test_replay: two dangling pointers - victim(run(...)) returned a pointer
  into a temporary report destroyed at the end of the statement. Passed on
  Linux by allocator luck; failed deterministically on macOS for every seed.
- test_yara_scanner: a brace initializer with a comma inside assert() (the
  macro splits it on macOS), and ::close needs <unistd.h> outside the
  Linux-only block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:35:34 -07:00

747 lines
30 KiB
C++

#include "strawberry/yara_scanner.hpp"
#include <algorithm>
#include <thread>
#include <atomic>
#include <cassert>
#include <fcntl.h>
#include <unistd.h>
#include "strawberry/rule_partition.hpp"
#include <chrono>
#include <iterator>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <system_error>
#include <vector>
#if defined(__linux__)
#include <csignal>
#include <sys/wait.h>
#include <unistd.h>
#endif
#ifndef STRAWBERRY_HAVE_YARA
#define STRAWBERRY_HAVE_YARA 0
#endif
namespace fs = std::filesystem;
namespace {
fs::path make_test_root() {
const auto root = fs::temp_directory_path() / "strawberry_yara_tests";
std::error_code ec;
fs::remove_all(root, ec);
fs::create_directories(root);
return root;
}
void write_text_file(const fs::path& path, const std::string& content) {
if (path.has_parent_path()) {
fs::create_directories(path.parent_path());
}
std::ofstream output(path);
assert(output.good());
output << content;
output.close();
assert(output.good());
}
#if !STRAWBERRY_HAVE_YARA
void test_without_libyara_reports_graceful_failure() {
const auto root = make_test_root();
const auto rules = root / "rules" / "sample.yar";
const auto sample = root / "sample.bin";
write_text_file(
rules,
"rule FallbackRule {\n"
" strings:\n"
" $a = \"suspicious_payload\"\n"
" condition:\n"
" $a\n"
"}\n");
write_text_file(sample, "suspicious_payload\n");
strawberry::YaraScanner scanner;
std::vector<strawberry::YaraMatch> matches;
std::cout << "Test without_libyara_reports_graceful_failure:" << std::endl;
assert(!scanner.load_rules(rules.string()));
assert(!scanner.has_rules());
assert(!scanner.scan_file(sample.string(), matches));
assert(matches.empty());
std::cout << " PASSED" << std::endl;
std::error_code ec;
fs::remove_all(root, ec);
}
#endif
#if STRAWBERRY_HAVE_YARA
bool has_tag(const std::vector<std::string>& tags, const std::string& target) {
return std::find(tags.begin(), tags.end(), target) != tags.end();
}
void test_load_directory_and_scan_file() {
const auto root = make_test_root();
const auto rules_dir = root / "rules";
const auto rule_file = rules_dir / "detect_payload.yar";
const auto ignored_file = rules_dir / "notes.txt";
const auto sample = root / "payload.bin";
const auto benign = root / "benign.bin";
write_text_file(
rule_file,
"rule DetectPayload : unit_test sample {\n"
" meta:\n"
" description = \"Detect sample payload\"\n"
" severity = 5\n"
" enabled = true\n"
" strings:\n"
" $payload = \"suspicious_payload\"\n"
" condition:\n"
" $payload\n"
"}\n");
write_text_file(ignored_file, "this should not be compiled as a rule\n");
write_text_file(sample, "header\nsuspicious_payload\nfooter\n");
write_text_file(benign, "totally benign content\n");
strawberry::YaraScanner scanner;
std::vector<strawberry::YaraMatch> matches;
std::cout << "Test load_directory_and_scan_file:" << std::endl;
assert(scanner.load_rules(rules_dir.string()));
assert(scanner.has_rules());
assert(!scanner.are_rules_stale(std::chrono::hours(1)));
assert(scanner.scan_file(sample.string(), matches));
assert(matches.size() == 1);
assert(matches[0].rule_name == "DetectPayload");
assert(has_tag(matches[0].tags, "unit_test"));
assert(has_tag(matches[0].tags, "sample"));
assert(matches[0].meta_info.find("description=Detect sample payload") != std::string::npos);
assert(matches[0].meta_info.find("severity=5") != std::string::npos);
assert(matches[0].meta_info.find("enabled=true") != std::string::npos);
matches.clear();
assert(scanner.scan_file(benign.string(), matches));
assert(matches.empty());
std::cout << " PASSED" << std::endl;
std::error_code ec;
fs::remove_all(root, ec);
}
void test_failed_reload_keeps_previous_rules() {
const auto root = make_test_root();
const auto good_rules = root / "good.yar";
const auto bad_rules = root / "bad.yar";
const auto sample = root / "payload.bin";
write_text_file(
good_rules,
"rule GoodRule {\n"
" strings:\n"
" $a = \"suspicious_payload\"\n"
" condition:\n"
" $a\n"
"}\n");
write_text_file(
bad_rules,
"rule BrokenRule {\n"
" strings:\n"
" $a = \"unterminated\"\n"
" condition:\n"
"}\n");
write_text_file(sample, "suspicious_payload\n");
strawberry::YaraScanner scanner;
std::vector<strawberry::YaraMatch> matches;
std::cout << "Test failed_reload_keeps_previous_rules:" << std::endl;
assert(scanner.load_rules(good_rules.string()));
assert(scanner.has_rules());
assert(!scanner.load_rules(bad_rules.string()));
assert(scanner.has_rules());
assert(scanner.scan_file(sample.string(), matches));
assert(matches.size() == 1);
assert(matches[0].rule_name == "GoodRule");
std::cout << " PASSED" << std::endl;
std::error_code ec;
fs::remove_all(root, ec);
}
#if defined(__linux__)
void test_scan_process_via_proc_exe() {
const auto root = make_test_root();
const auto rule_file = root / "elf_process.yar";
write_text_file(
rule_file,
"rule DetectElfExecutable : linux process_scan {\n"
" strings:\n"
" $elf = { 7F 45 4C 46 }\n"
" condition:\n"
" $elf at 0\n"
"}\n");
strawberry::YaraScanner scanner;
std::vector<strawberry::YaraMatch> matches;
std::cout << "Test scan_process_via_proc_exe:" << std::endl;
assert(scanner.load_rules(rule_file.string()));
assert(scanner.has_rules());
assert(scanner.scan_process(static_cast<int>(::getpid()), matches));
assert(matches.size() == 1);
assert(matches[0].rule_name == "DetectElfExecutable");
assert(has_tag(matches[0].tags, "linux"));
assert(has_tag(matches[0].tags, "process_scan"));
std::cout << " PASSED" << std::endl;
std::error_code ec;
fs::remove_all(root, ec);
}
#endif
#endif
} // namespace
#if STRAWBERRY_HAVE_YARA && defined(__linux__)
// A child holds a marker that exists only in its heap (built at runtime from a
// reversed literal); the memory scan must find it. A parent may read its own
// child's memory even with yama ptrace_scope=1.
void test_scan_process_memory_finds_heap_marker() {
std::cout << "Test scan_process_memory_finds_heap_marker:" << std::endl;
const auto make_marker = []() {
std::string reversed = "24_rekram_yromem_yrrebwarts";
return std::string(reversed.rbegin(), reversed.rend());
};
int ready[2];
assert(::pipe(ready) == 0);
const pid_t child = ::fork();
if (child == 0) {
::close(ready[0]);
auto* volatile held = new std::string(make_marker() + make_marker());
(void)held;
const char ok = 1;
(void)!::write(ready[1], &ok, 1);
::pause();
::_exit(0);
}
::close(ready[1]);
char ok = 0;
assert(::read(ready[0], &ok, 1) == 1);
::close(ready[0]);
const auto root = fs::temp_directory_path() / "strawberry_memory_scan_test";
std::error_code ec;
fs::remove_all(root, ec);
fs::create_directories(root, ec);
{
std::ofstream rule(root / "memory.yar");
rule << "rule Memory_Marker { meta: severity = \"high\" strings: $m = \"" << make_marker()
<< "\" condition: $m }\n";
}
strawberry::YaraScanner scanner;
assert(scanner.load_rules(root.string()));
std::vector<strawberry::YaraMatch> matches;
std::string error;
const bool scanned = scanner.scan_process_memory(child, matches, 20, &error);
::kill(child, SIGKILL);
::waitpid(child, nullptr, 0);
if (!scanned) {
std::cout << " SKIP (cannot read child memory: " << error << ")" << std::endl;
fs::remove_all(root, ec);
return;
}
assert(std::any_of(matches.begin(), matches.end(), [](const auto& m) { return m.rule_name == "Memory_Marker"; }));
// Exited process: clean failure.
matches.clear();
assert(!scanner.scan_process_memory(child, matches, 5, &error));
fs::remove_all(root, ec);
std::cout << " PASSED" << std::endl;
}
#endif
#if STRAWBERRY_HAVE_YARA
// Exec checks, on-demand and memory scans share one scanner without the
// pipeline lock, so scans must be safe against each other and a reload.
void test_concurrent_scans_during_reload() {
std::cout << "Test concurrent_scans_during_reload:" << std::endl;
const auto root = fs::temp_directory_path() / "strawberry_concurrent_scan_test";
std::error_code ec;
fs::remove_all(root, ec);
fs::create_directories(root, ec);
{
std::ofstream rule(root / "c.yar");
rule << "rule Concurrent_Marker { strings: $m = \"concurrent-marker\" condition: $m }\n";
}
strawberry::YaraScanner scanner;
assert(scanner.load_rules(root.string()));
const std::string payload = std::string(4096, 'x') + "concurrent-marker";
std::atomic<bool> stop{false};
std::atomic<int> failures{0};
std::atomic<int> scans{0};
std::vector<std::thread> threads;
for (int t = 0; t < 4; ++t) {
threads.emplace_back([&]() {
while (!stop) {
std::vector<strawberry::YaraMatch> matches;
if (!scanner.scan_memory(reinterpret_cast<const unsigned char*>(payload.data()), payload.size(), matches)
|| matches.size() != 1) {
++failures;
}
++scans;
}
});
}
for (int i = 0; i < 5; ++i) {
assert(scanner.load_rules(root.string()));
}
stop = true;
for (auto& thread : threads) {
thread.join();
}
assert(failures == 0 && scans > 0);
fs::remove_all(root, ec);
std::cout << " PASSED (" << scans << " scans across 5 reloads)" << std::endl;
}
#endif
#if STRAWBERRY_HAVE_YARA
// String hits: offsets and digests are right, and the per-string, per-rule
// and per-scan caps hold. Five identical rules over 20 strings x 6 hits each:
// each rule keeps 16 strings x 4 hits = 64, so the scan cap (256) leaves the
// fifth rule with none.
void test_string_hits_capped() {
std::cout << "Test string_hits_capped:" << std::endl;
std::string strings, payload;
for (int i = 0; i < 20; ++i) {
const std::string token = "tok" + std::string(1, static_cast<char>('a' + i)) + "_marker";
strings += " $s" + std::to_string(i) + " = \"" + token + "\"\n";
for (int k = 0; k < 6; ++k) payload += token + "|";
}
std::string rules;
for (int r = 0; r < 5; ++r) {
rules += "rule Hits" + std::to_string(r) + " {\n strings:\n" + strings + " condition:\n any of them\n}\n";
}
const auto root = make_test_root();
write_text_file(root / "rules" / "hits.yar", rules);
strawberry::YaraScanner scanner;
assert(scanner.load_rules((root / "rules").string()));
std::vector<strawberry::YaraMatch> matches;
const auto* bytes = reinterpret_cast<const unsigned char*>(payload.data());
assert(scanner.scan_memory(bytes, payload.size(), matches));
assert(matches.size() == 5);
std::size_t total = 0;
for (const auto& m : matches) {
total += m.strings.size();
assert(m.strings.size() <= strawberry::kMaxHitStringsPerRule * strawberry::kMaxHitsPerString);
for (const auto& h : m.strings) {
assert(h.offset + h.length <= payload.size());
assert(h.digest == strawberry::match_digest(bytes + h.offset, h.length));
assert(payload.compare(h.offset, 3, "tok") == 0 && h.length == 11); // "tok?_marker"
}
}
assert(total == strawberry::kMaxHitsPerScan);
assert(matches[0].strings.size() == 64);
assert(matches[0].strings.front().identifier == "$s0" && matches[0].strings.front().offset == 0);
std::cout << " PASSED" << std::endl;
std::error_code ec;
fs::remove_all(root, ec);
}
#endif
#if STRAWBERRY_HAVE_YARA
// ---- compiled-rules cache (K1-K4) ----
const char* kRuleA =
"rule CacheA {\n strings:\n $a = \"cache_marker_alpha\"\n condition:\n $a\n}\n";
const char* kRuleB =
"rule CacheB {\n strings:\n $b = \"cache_marker_beta\"\n condition:\n $b\n}\n";
std::string slurp(const fs::path& path) {
std::string out;
FILE* f = std::fopen(path.string().c_str(), "rb");
if (f == nullptr) return out;
char buf[4096];
std::size_t n;
while ((n = std::fread(buf, 1, sizeof(buf), f)) > 0) out.append(buf, n);
std::fclose(f);
return out;
}
std::vector<std::string> scan_names(strawberry::YaraScanner& scanner, const std::string& data) {
std::vector<strawberry::YaraMatch> matches;
scanner.scan_memory(reinterpret_cast<const unsigned char*>(data.data()), data.size(), matches);
std::vector<std::string> names;
for (const auto& m : matches) names.push_back(m.rule_name);
std::sort(names.begin(), names.end());
return names;
}
void test_cache_key_determinism() {
std::cout << "Test cache key determinism:" << std::endl;
const auto root = make_test_root();
write_text_file(root / "a.yar", kRuleA);
write_text_file(root / "b.yar", kRuleB);
using strawberry::RuleFile;
const std::vector<RuleFile> files{{root / "a.yar", ""}, {root / "b.yar", "feed"}};
const auto key = strawberry::compiled_rules_key(files, "4.5.0");
assert(key.size() == 64);
assert(key == strawberry::compiled_rules_key(files, "4.5.0"));
assert(key != strawberry::compiled_rules_key(files, "4.5.1")); // version
assert(key != strawberry::compiled_rules_key({files[1], files[0]}, "4.5.0")); // order
assert(key != strawberry::compiled_rules_key({{root / "a.yar", "x"}, files[1]}, "4.5.0")); // namespace
write_text_file(root / "a.yar", std::string(kRuleA) + " "); // one byte
assert(key != strawberry::compiled_rules_key(files, "4.5.0"));
assert(strawberry::compiled_rules_key({{root / "missing.yar", ""}}, "4.5.0").empty());
std::cout << " PASSED" << std::endl;
}
void test_cache_round_trip_and_disabled() {
std::cout << "Test cache round trip / disabled:" << std::endl;
const auto root = make_test_root();
write_text_file(root / "rules" / "a.yar", kRuleA);
write_text_file(root / "rules" / "b.yar", kRuleB);
const auto cache = root / "cache";
const std::string data = "xx cache_marker_alpha yy cache_marker_beta";
strawberry::YaraScanner first;
first.set_rules_cache_dir(cache.string());
assert(first.load_rules((root / "rules").string()));
assert(first.rules_cache_status() == "miss");
assert(fs::exists(cache / "rules.yrc") && fs::exists(cache / "rules.yrc.key"));
for (const char* name : {"rules.yrc", "rules.yrc.key"}) {
assert((fs::status(cache / name).permissions() & (fs::perms::group_all | fs::perms::others_all))
== fs::perms::none);
}
assert((fs::status(cache).permissions() & (fs::perms::group_all | fs::perms::others_all)) == fs::perms::none);
assert(!fs::exists(cache / "rules.yrc.tmp"));
strawberry::YaraScanner second;
second.set_rules_cache_dir(cache.string());
assert(second.load_rules((root / "rules").string()));
assert(second.rules_cache_status() == "hit");
assert(second.rule_count() == first.rule_count() && second.rule_count() == 2);
const auto names = scan_names(first, data);
assert(names.size() == 2);
assert(names == scan_names(second, data));
// K4: disabled writes nothing and behaves the same.
const auto off_cache = root / "off";
strawberry::YaraScanner off;
off.set_rules_cache_dir("");
assert(off.load_rules((root / "rules").string()));
assert(off.rules_cache_status() == "disabled");
assert(!fs::exists(off_cache));
assert(scan_names(off, data) == names);
std::cout << " PASSED" << std::endl;
}
void test_cache_stale_and_corrupt() {
std::cout << "Test cache stale / corrupt:" << std::endl;
const auto root = make_test_root();
write_text_file(root / "rules" / "a.yar", kRuleA);
const auto cache = root / "cache";
{
strawberry::YaraScanner s;
s.set_rules_cache_dir(cache.string());
assert(s.load_rules((root / "rules").string()) && s.rules_cache_status() == "miss");
}
const auto key1 = slurp(cache / "rules.yrc.key");
write_text_file(root / "rules" / "a.yar", std::string(kRuleA) + kRuleB); // source changed
{
strawberry::YaraScanner s;
s.set_rules_cache_dir(cache.string());
assert(s.load_rules((root / "rules").string()));
assert(s.rules_cache_status() == "miss");
assert(s.rule_count() == 2);
}
const auto key2 = slurp(cache / "rules.yrc.key");
assert(!key2.empty() && key2 != key1);
// Corrupt rules.yrc under a matching key: handled, recompiled, rewritten.
write_text_file(cache / "rules.yrc", "this is not a compiled yara rules file at all");
{
strawberry::YaraScanner s;
s.set_rules_cache_dir(cache.string());
assert(s.load_rules((root / "rules").string()));
assert(s.rules_cache_status() == "error");
assert(s.rule_count() == 2);
}
assert(slurp(cache / "rules.yrc.key") == key2);
{
strawberry::YaraScanner s;
s.set_rules_cache_dir(cache.string());
assert(s.load_rules((root / "rules").string()));
assert(s.rules_cache_status() == "hit");
}
std::cout << " PASSED" << std::endl;
}
#endif
// A minimal little-endian ELF64 image with one PT_LOAD covering [0, load_end); bytes after it are
// "debug" data the loader never maps.
std::vector<unsigned char> make_elf64(std::size_t total, std::size_t load_end) {
std::vector<unsigned char> img(total, 0);
const auto put = [&](std::size_t off, uint64_t v, int n) {
for (int i = 0; i < n; ++i) img[off + i] = static_cast<unsigned char>(v >> (8 * i));
};
img[0] = 0x7f; img[1] = 'E'; img[2] = 'L'; img[3] = 'F'; img[4] = 2; img[5] = 1; img[6] = 1;
put(32, 64, 8); // e_phoff
put(54, 56, 2); // e_phentsize
put(56, 1, 2); // e_phnum
put(64, 1, 4); // p_type = PT_LOAD
put(64 + 8, 0, 8); // p_offset
put(64 + 32, load_end, 8); // p_filesz
return img;
}
void test_elf_load_ranges() {
std::cout << "Test elf_load_ranges:" << std::endl;
auto img = make_elf64(1 << 20, 256 << 10);
auto r = strawberry::elf_load_ranges(img.data(), img.size());
assert(r.size() == 1 && r[0].offset == 0 && r[0].size == (256u << 10));
// Not ELF, big-endian, truncated: empty (caller scans everything).
std::vector<unsigned char> text(4096, 'x');
assert(strawberry::elf_load_ranges(text.data(), text.size()).empty());
auto be = img;
be[5] = 2;
assert(strawberry::elf_load_ranges(be.data(), be.size()).empty());
assert(strawberry::elf_load_ranges(img.data(), 40).empty());
// Hostile headers: absurd phnum / phoff past the end / filesz past the end are bounded.
auto hostile = img;
hostile[56] = 0xff; hostile[57] = 0xff; // phnum 65535
assert(strawberry::elf_load_ranges(hostile.data(), hostile.size()).empty());
auto past = img;
for (int i = 0; i < 8; ++i) past[32 + i] = 0xff; // phoff = UINT64_MAX
assert(strawberry::elf_load_ranges(past.data(), past.size()).empty());
auto big = make_elf64(8192, 1ull << 40); // filesz beyond the buffer: clamped
r = strawberry::elf_load_ranges(big.data(), big.size());
assert(r.size() == 1 && r[0].offset == 0 && r[0].size == 8192);
// A real system binary: ranges are sorted, non-overlapping, start at 0, inside the file.
std::ifstream in("/bin/true", std::ios::binary);
std::vector<unsigned char> real((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
if (!real.empty()) {
r = strawberry::elf_load_ranges(real.data(), real.size());
assert(!r.empty() && r[0].offset == 0);
for (std::size_t i = 0; i < r.size(); ++i) {
assert(r[i].offset + r[i].size <= real.size());
if (i > 0) assert(r[i].offset > r[i - 1].offset + r[i - 1].size);
}
}
std::cout << " PASSED" << std::endl;
}
void test_scan_memory_ranges() {
std::cout << "Test scan_memory_ranges (blocks, fast mode, file offsets):" << std::endl;
const auto root = fs::temp_directory_path() / "strawberry_range_scan_test";
std::error_code ec;
fs::remove_all(root, ec);
fs::create_directories(root, ec);
{
std::ofstream rule(root / "ranges.yar");
rule << "rule In_Loaded { strings: $a = \"LOADED-MARKER-7c1\" condition: $a }\n"
"rule In_Debug { strings: $d = \"DEBUG-MARKER-9e4\" condition: $d }\n"
"rule Elf_At_Zero { condition: uint32(0) == 0x464c457f and filesize == 1048576 }\n"
"rule Offset_Exact { strings: $a = \"LOADED-MARKER-7c1\" condition: $a at 4096 }\n";
}
strawberry::YaraScanner scanner;
assert(scanner.load_rules(root.string()));
auto img = make_elf64(1 << 20, 256 << 10);
const std::string loaded = "LOADED-MARKER-7c1", debug = "DEBUG-MARKER-9e4";
std::copy(loaded.begin(), loaded.end(), img.begin() + 4096);
std::copy(debug.begin(), debug.end(), img.begin() + (512 << 10)); // outside PT_LOAD
const auto names = [](const std::vector<strawberry::YaraMatch>& m) {
std::vector<std::string> n;
for (const auto& x : m) n.push_back(x.rule_name);
std::sort(n.begin(), n.end());
return n;
};
strawberry::YaraScanner::EngineOptions full;
std::vector<strawberry::YaraMatch> m;
assert(scanner.scan_memory_ranges(img.data(), img.size(), {}, full, m));
assert((names(m) == std::vector<std::string>{"Elf_At_Zero", "In_Debug", "In_Loaded", "Offset_Exact"}));
// Loaded ranges only: the debug-only string is not seen; offsets and filesize keep file meaning.
const auto ranges = strawberry::elf_load_ranges(img.data(), img.size());
assert(scanner.scan_memory_ranges(img.data(), img.size(), ranges, full, m));
assert((names(m) == std::vector<std::string>{"Elf_At_Zero", "In_Loaded", "Offset_Exact"}));
// Fast mode: same rule set on these inputs.
strawberry::YaraScanner::EngineOptions fast;
fast.fast_mode = true;
assert(scanner.scan_memory_ranges(img.data(), img.size(), {}, fast, m));
assert(names(m).size() == 4);
// Two disjoint ranges: a string in the second block is found at its file offset.
std::vector<strawberry::ScanRange> two{{0, 8192}, {512u << 10, 4096}};
assert(scanner.scan_memory_ranges(img.data(), img.size(), two, full, m));
assert((names(m) == std::vector<std::string>{"Elf_At_Zero", "In_Debug", "In_Loaded", "Offset_Exact"}));
fs::remove_all(root, ec);
std::cout << " PASSED" << std::endl;
}
bool wait_partitions(strawberry::YaraScanner& sc, const std::string& want) {
for (int i = 0; i < 300; ++i) {
if (sc.partition_status().state == want) return true;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
return false;
}
std::vector<std::string> match_names(const std::vector<strawberry::YaraMatch>& m) {
std::vector<std::string> n;
for (const auto& x : m) n.push_back(x.namespace_name + ":" + x.rule_name);
std::sort(n.begin(), n.end());
return n;
}
void test_partitions_equivalent_to_full() {
std::cout << "Test rule partitions give identical matches to the full rule set:" << std::endl;
const auto root = fs::temp_directory_path() / "strawberry_partition_test";
std::error_code ec;
fs::remove_all(root, ec);
fs::create_directories(root / "a", ec);
fs::create_directories(root / "g", ec);
{
std::ofstream r(root / "a" / "main.yar");
r << "import \"pe\"\n"
"rule Pe_Only { strings: $a = \"PART-MARK-A\" condition: uint16(0) == 0x5a4d and $a }\n"
"rule Pe_Module { strings: $a = \"PART-MARK-A\" condition: pe.is_pe and $a }\n"
"rule Elf_Only { strings: $a = \"PART-MARK-A\" condition: uint32(0) == 0x464c457f and $a }\n"
"rule Macho_Only { strings: $a = \"PART-MARK-A\" condition: uint32(0) == 0xfeedfacf and $a }\n"
"rule Small_Lt { strings: $a = \"PART-MARK-A\" condition: filesize < 1MB and $a }\n"
"rule Small_Le { strings: $a = \"PART-MARK-A\" condition: filesize <= 1MB and $a }\n"
"rule Small_Pe { strings: $b = \"PART-MARK-B\" condition: ( uint16(0) == 0x5a4d and filesize < 2MB ) and $b }\n"
"rule Any_Type { strings: $a = \"PART-MARK-A\" condition: $a }\n"
"rule Or_Cond { strings: $a = \"PART-MARK-A\" condition: uint16(0) == 0x5a4d or $a }\n"
"private rule Pe_Helper { condition: uint16(0) == 0x5a4d }\n"
"rule Uses_Helper { strings: $b = \"PART-MARK-B\" condition: Pe_Helper or $b }\n"
"rule Not_Pe { strings: $a = \"PART-MARK-A\" condition: not (uint16(0) == 0x5a4d) and $a }\n";
}
{
// A global rule gates its whole namespace: dropping it from a partition would wrongly let
// G_Gated match non-PE files. Partitions must keep it.
std::ofstream r(root / "g" / "gated.yar");
r << "global rule G_Pe { condition: uint16(0) == 0x5a4d }\n"
"rule G_Gated { strings: $a = \"PART-MARK-A\" condition: $a }\n";
}
const std::vector<strawberry::YaraScanner::RuleSource> sources{{(root / "a").string(), "a"}, {(root / "g").string(), "g"}};
strawberry::YaraScanner full, part;
full.set_rule_partitions(false);
part.set_rule_partitions(true, 1000); // build every distinct subset so all are checked
assert(full.load_rules(sources) && part.load_rules(sources));
assert(full.partition_status().state == "off");
assert(wait_partitions(part, "ready"));
const auto st = part.partition_status();
assert(st.subsets >= 4 && st.total_rules == 14);
// ELF >= 1 MiB keeps neither PE rule nor the small-file rules (but keeps globals + references).
assert((st.kept[strawberry::rules::PartitionKey{strawberry::rules::ClassGroup::nonpe, 2}.index()] < st.total_rules));
const std::vector<std::string> heads{std::string("MZ\x90\x00", 4), std::string("\x7f" "ELF", 4),
std::string("\xcf\xfa\xed\xfe", 4), std::string("text", 4)};
const std::vector<std::size_t> sizes{200, (1u << 20) - 1, 1u << 20, (1u << 20) + 1, (2u << 20) - 1, 5u << 20};
int cases = 0, nonempty = 0;
for (const auto& head : heads) {
for (const auto size : sizes) {
std::vector<unsigned char> img(size, 0);
std::copy(head.begin(), head.end(), img.begin());
const std::string a = "PART-MARK-A", b = "PART-MARK-B";
std::copy(a.begin(), a.end(), img.begin() + 64);
std::copy(b.begin(), b.end(), img.begin() + 128);
std::vector<strawberry::YaraMatch> mf, mp;
assert(full.scan_memory(img.data(), img.size(), mf));
assert(part.scan_memory(img.data(), img.size(), mp));
if (match_names(mf) != match_names(mp)) {
std::cerr << "MISMATCH head=" << int(static_cast<unsigned char>(head[0])) << " size=" << size << std::endl;
for (auto& n : match_names(mf)) std::cerr << " full " << n << std::endl;
for (auto& n : match_names(mp)) std::cerr << " part " << n << std::endl;
std::abort();
}
// Block scans and fd scans choose partitions the same way.
std::vector<strawberry::YaraMatch> rf, rp;
const std::vector<strawberry::ScanRange> ranges{{0, 4096}, {size / 2, 64}};
assert(full.scan_memory_ranges(img.data(), img.size(), ranges, {}, rf));
assert(part.scan_memory_ranges(img.data(), img.size(), ranges, {}, rp));
assert(match_names(rf) == match_names(rp));
const auto file = root / ("img-" + std::to_string(cases) + ".bin");
{
std::ofstream o(file, std::ios::binary);
o.write(reinterpret_cast<const char*>(img.data()), static_cast<std::streamsize>(img.size()));
}
const int fd = ::open(file.c_str(), O_RDONLY | O_CLOEXEC);
std::vector<strawberry::YaraMatch> ff, fp;
assert(full.scan_fd(fd, ff) && part.scan_fd(fd, fp));
::close(fd);
assert(match_names(ff) == match_names(fp));
fs::remove(file, ec);
++cases;
if (!mf.empty()) ++nonempty;
}
}
assert(cases == 24 && nonempty >= 20);
const auto after = part.partition_status();
uint64_t served = 0;
for (auto n : after.scans) served += n;
assert(served > 0); // the partitioned scanner really used subsets
fs::remove_all(root, ec);
std::cout << " PASSED (" << cases << " file shapes x 3 scan paths, " << st.subsets << " subsets)" << std::endl;
}
void test_partitions_fail_closed() {
std::cout << "Test rule partitions fail closed on sources they cannot split:" << std::endl;
const auto root = fs::temp_directory_path() / "strawberry_partition_failclosed";
std::error_code ec;
fs::remove_all(root, ec);
fs::create_directories(root, ec);
{
std::ofstream inc(root / "inc.yara.txt");
inc << "rule Included { strings: $a = \"INC-MARK\" condition: $a }\n";
std::ofstream r(root / "main.yar");
r << "include \"inc.yara.txt\"\nrule Main { condition: uint16(0) == 0x5a4d }\n";
}
strawberry::YaraScanner sc;
assert(sc.load_rules(root.string()));
assert(wait_partitions(sc, "disabled"));
assert(sc.partition_status().reason.find("include") != std::string::npos);
const std::string data = "xxINC-MARKxx";
std::vector<strawberry::YaraMatch> m;
assert(sc.scan_memory(reinterpret_cast<const unsigned char*>(data.data()), data.size(), m));
assert(match_names(m) == std::vector<std::string>{":Included"} || (m.size() == 1 && m[0].rule_name == "Included"));
fs::remove_all(root, ec);
std::cout << " PASSED" << std::endl;
}
int main() {
std::cout << "Running YaraScanner tests..." << std::endl << std::endl;
#if STRAWBERRY_HAVE_YARA
test_load_directory_and_scan_file();
test_cache_key_determinism();
test_cache_round_trip_and_disabled();
test_cache_stale_and_corrupt();
test_failed_reload_keeps_previous_rules();
test_concurrent_scans_during_reload();
test_string_hits_capped();
test_elf_load_ranges();
test_scan_memory_ranges();
test_partitions_equivalent_to_full();
test_partitions_fail_closed();
#if defined(__linux__)
test_scan_process_via_proc_exe();
test_scan_process_memory_finds_heap_marker();
#endif
#else
test_without_libyara_reports_graceful_failure();
#endif
std::cout << std::endl << "All tests passed!" << std::endl;
return 0;
}