strawberry/tests/test_yara_scanner.cpp
trvon b46e367e6d
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Record YARA string hits as rule_match telemetry
Each rule match from a fresh scan now writes a rule_match record: the
rule, namespace, severity, rules generation and its matched strings
(identifier, offset, length, 64-bit FNV-1a digest of the captured bytes).
The matched bytes themselves are never stored. Cache hits write no
rule_match, since their scan_result already names the rules.

Hits are collected in the libyara callback (libyara 4+) and capped at
16 strings per rule, 4 hits per string and 256 per scan. The callback
also tolerates a null user_data now.

Tests: caps, offsets and digests against a real scan; record round-trip
with the encode cap; the telemetry record fuzzer covers rule_match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 11:08:58 -06:00

374 lines
12 KiB
C++

#include "strawberry/yara_scanner.hpp"
#include <algorithm>
#include <thread>
#include <atomic>
#include <cassert>
#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
int main() {
std::cout << "Running YaraScanner tests..." << std::endl << std::endl;
#if STRAWBERRY_HAVE_YARA
test_load_directory_and_scan_file();
test_failed_reload_keeps_previous_rules();
test_concurrent_scans_during_reload();
test_string_hits_capped();
#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;
}