strawberry/tests/test_yara_scanner.cpp
trvon ad1676a594
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
Integrate features into the daemon, build and docs
Wire the policy, feeds, containment, exec guard, behavior, governor,
crash safety and event store into the daemon, detection pipeline, CLI
and meson build (zstd now required); Dockerfile, conan, systemd unit
(LimitCORE=0, resource limits) and installer updates; existing tests
adapted (store paths are directories now). Detections are counted and
notified even when evidence cannot be persisted (persist_failures in
status). prevention_timeout_ms is clamped and behavior dedupe handles
out-of-order events. Docs: API contract, developer guide, plan, UX,
agent rules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:30 -06:00

329 lines
10 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
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();
#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;
}