#include "strawberry/database.hpp" #include "strawberry/detection_pipeline.hpp" #include "strawberry/resource_governor.hpp" #include "strawberry/hash_utils.hpp" #include "strawberry/policy.hpp" #include "strawberry/process_monitor.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(_WIN32) #include #endif #ifndef STRAWBERRY_HAVE_YARA #define STRAWBERRY_HAVE_YARA 0 #endif namespace fs = std::filesystem; namespace { #if STRAWBERRY_HAVE_YARA fs::path make_test_root() { const auto root = fs::temp_directory_path() / "strawberry_runtime_pipeline_tests"; std::error_code ec; fs::remove_all(root, ec); fs::create_directories(root, ec); assert(!ec); return root; } void write_binary_file(const fs::path& path, const std::string& content) { if (path.has_parent_path()) { std::error_code ec; fs::create_directories(path.parent_path(), ec); assert(!ec); } std::ofstream output(path, std::ios::binary); assert(output.good()); output.write(content.data(), static_cast(content.size())); output.close(); assert(output.good()); } void write_cmdline_file(const fs::path& path, const std::vector& args) { if (path.has_parent_path()) { std::error_code ec; fs::create_directories(path.parent_path(), ec); assert(!ec); } std::ofstream output(path, std::ios::binary); assert(output.good()); for (const auto& arg : args) { output.write(arg.data(), static_cast(arg.size())); output.put('\0'); } output.close(); assert(output.good()); } void write_status_file(const fs::path& path, int parent_pid) { if (path.has_parent_path()) { std::error_code ec; fs::create_directories(path.parent_path(), ec); assert(!ec); } std::ofstream output(path); assert(output.good()); output << "Name:\tsynthetic-runtime\n"; output << "State:\tR (running)\n"; output << "PPid:\t" << parent_pid << "\n"; output.close(); assert(output.good()); } void write_rule_file(const fs::path& path) { if (path.has_parent_path()) { std::error_code ec; fs::create_directories(path.parent_path(), ec); assert(!ec); } std::ofstream output(path); assert(output.good()); output << "rule SyntheticRuntimeRule {\n" " meta:\n" " severity = \"High\"\n" " strings:\n" " $marker = \"synthetic-payload-marker\"\n" " condition:\n" " $marker\n" "}\n"; output.close(); assert(output.good()); } void set_env_var(const std::string& key, const std::string& value) { #if defined(_WIN32) _putenv_s(key.c_str(), value.c_str()); #else ::setenv(key.c_str(), value.c_str(), 1); #endif } void unset_env_var(const std::string& key) { #if defined(_WIN32) _putenv_s(key.c_str(), ""); #else ::unsetenv(key.c_str()); #endif } void test_procfs_event_scans_and_persists_match() { const auto root = make_test_root(); const auto proc_root = root / "proc"; const auto rules_dir = root / "rules"; const auto executable = root / "bin" / "synthetic_exec"; const auto database_path = (root / "runtime_pipeline.db").string(); std::error_code ec; fs::create_directories(proc_root, ec); assert(!ec); fs::create_directories(rules_dir, ec); assert(!ec); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker"); write_rule_file(rules_dir / "synthetic_runtime.yar"); const auto expected_hash = strawberry::HashUtils::sha256_file(executable.string()); assert(!expected_hash.empty()); set_env_var("STRAWBERRY_PROCFS_ROOT", proc_root.string()); set_env_var("STRAWBERRY_PROCESS_MONITOR_POLL_MS", "25"); strawberry::Config config; config.db_path = database_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); strawberry::ProcessMonitor monitor; assert(monitor.initialize()); std::mutex mutex; std::condition_variable cv; std::vector observed_events; std::cout << "Test procfs_event_scans_and_persists_match:" << std::endl; assert(monitor.start([&](const strawberry::ProcessInfo& info) { pipeline.handle_process(info); { std::lock_guard lock(mutex); observed_events.push_back(info); } cv.notify_all(); })); const auto staged_proc_dir = proc_root / "pending-321"; fs::create_directories(staged_proc_dir, ec); assert(!ec); fs::create_symlink(executable, staged_proc_dir / "exe", ec); assert(!ec); write_cmdline_file(staged_proc_dir / "cmdline", {"synthetic_exec", "--runtime-test"}); write_status_file(staged_proc_dir / "status", 77); const auto live_proc_dir = proc_root / "321"; fs::rename(staged_proc_dir, live_proc_dir, ec); assert(!ec); { std::unique_lock lock(mutex); const bool observed = cv.wait_for(lock, std::chrono::seconds(2), [&]() { return !observed_events.empty(); }); assert(observed); } monitor.stop(); pipeline.shutdown(); strawberry::Database database; assert(database.open(database_path)); auto hits = database.find_indicators_by_hash(expected_hash); assert(hits.size() == 1); assert(hits[0].rule_name == "SyntheticRuntimeRule"); assert(hits[0].severity == "high"); assert(hits[0].metadata.at("source") == "yara"); assert(hits[0].metadata.at("scan_kind") == "process_executable"); assert(hits[0].metadata.at("pid") == "321"); assert(hits[0].metadata.at("parent_pid") == "77"); assert(hits[0].metadata.at("process_path") == executable.string()); assert(hits[0].metadata.at("cmdline") == "synthetic_exec --runtime-test"); assert(!hits[0].metadata.at("event_timestamp_ns").empty()); auto findings = database.list_findings(); assert(findings.size() == 1); assert(findings[0].title == "YARA match on executed process image"); assert(findings[0].severity == "high"); assert(findings[0].metadata.at("rule") == "process_executable_yara_match"); assert(findings[0].metadata.at("pid") == "321"); assert(findings[0].summary.find("SyntheticRuntimeRule") != std::string::npos); const std::string file_node_id = "file_" + expected_hash; auto related = database.find_related_nodes("process_321", 1); assert(related.size() == 2); bool saw_file = false; bool saw_indicator = false; for (const auto& node : related) { if (node.id == file_node_id) { saw_file = true; assert(node.type == "file"); assert(node.properties.at("path") == executable.string()); assert(node.properties.at("hash") == expected_hash); assert(node.properties.at("kind") == "executable_image"); } else if (node.type == "indicator") { saw_indicator = true; assert(node.properties.at("rule_name") == "SyntheticRuntimeRule"); assert(node.properties.at("cmdline") == "synthetic_exec --runtime-test"); assert(node.properties.at("parent_pid") == "77"); } } assert(saw_file); assert(saw_indicator); auto edges = database.get_node_edges("process_321"); assert(edges.size() == 3); bool saw_spawned = false; bool saw_executed = false; bool saw_matched = false; for (const auto& edge : edges) { if (edge.relationship_type == "spawned") { saw_spawned = true; } else if (edge.relationship_type == "executed") { saw_executed = true; assert(edge.to_id == file_node_id); } else if (edge.relationship_type == "matched") { saw_matched = true; } } assert(saw_spawned); assert(saw_executed); assert(saw_matched); auto parent_related = database.find_related_nodes("process_77", 1); assert(parent_related.size() == 1); assert(parent_related[0].id == "process_321"); auto file_related = database.find_related_nodes(file_node_id, 1); assert(file_related.size() == 1); assert(file_related[0].type == "indicator"); assert(file_related[0].properties.at("rule_name") == "SyntheticRuntimeRule"); auto file_edges = database.get_node_edges(file_node_id); assert(file_edges.size() == 2); bool saw_content_matched = false; for (const auto& edge : file_edges) { if (edge.relationship_type == "content_matched") { saw_content_matched = true; } } assert(saw_content_matched); database.close(); std::cout << " PASSED" << std::endl; unset_env_var("STRAWBERRY_PROCFS_ROOT"); unset_env_var("STRAWBERRY_PROCESS_MONITOR_POLL_MS"); fs::remove_all(root, ec); } void write_text_file(const fs::path& path, const std::string& content) { write_binary_file(path, content); } void test_fails_closed_without_rules() { const auto root = make_test_root(); const auto empty_rules = root / "empty_rules"; std::error_code ec; fs::create_directories(empty_rules, ec); std::cout << "Test fails_closed_without_rules:" << std::endl; strawberry::Config config; config.db_path = (root / "no_rules.db").string(); config.yara_rules_path = empty_rules.string(); strawberry::DetectionPipeline strict; assert(!strict.initialize(config)); config.require_rules = false; strawberry::DetectionPipeline permissive; assert(permissive.initialize(config)); permissive.shutdown(); std::cout << " PASSED" << std::endl; fs::remove_all(root, ec); } void test_single_writer_lock_and_read_only_queries() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; write_rule_file(rules_dir / "synthetic_runtime.yar"); const auto db_path = (root / "locked.db").string(); std::cout << "Test single_writer_lock_and_read_only_queries:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); // A second writer must be refused while the pipeline owns the store. strawberry::Database second_writer; assert(!second_writer.open(db_path)); strawberry::DetectionPipeline second_pipeline; assert(!second_pipeline.initialize(config)); // Readers are allowed and never write back. strawberry::Database reader; assert(reader.open_read_only(db_path)); assert(reader.is_read_only()); reader.close(); pipeline.shutdown(); assert(second_writer.open(db_path)); second_writer.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_stable_ids_severity_default_and_heuristics() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto executable = root / "bin" / "reused_pid_exec"; const auto db_path = (root / "identity.db").string(); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "no-severity-marker"); write_text_file(rules_dir / "no_severity.yar", "rule NoSeverityRule {\n" " strings:\n" " $m = \"no-severity-marker\"\n" " condition:\n" " $m\n" "}\n"); const auto hash = strawberry::HashUtils::sha256_file(executable.string()); std::cout << "Test stable_ids_severity_default_and_heuristics:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); // Two different process instances that happen to share PID 500. strawberry::ProcessInfo first; first.pid = 500; first.parent_pid = 1; first.executable_path = executable.string(); first.image_path = executable.string(); first.hash = hash; first.cmdline = "reused_pid_exec --first"; first.start_time_ticks = 1000; first.parent_start_time_ticks = 1; first.boot_id = "feedfacecafebeef"; first.event_timestamp_ns = 1'000; auto second = first; second.cmdline = "reused_pid_exec --second"; second.start_time_ticks = 9000; second.event_timestamp_ns = 2'000; second.executable_deleted = true; pipeline.handle_process(first); pipeline.handle_process(second); assert(pipeline.run_pending_correlation()); assert(!pipeline.run_pending_correlation()); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(db_path)); const auto first_id = strawberry::process_node_id(first); const auto second_id = strawberry::process_node_id(second); assert(first_id == "process_500_feedface_1000"); assert(second_id == "process_500_feedface_9000"); auto first_node = database.find_graph_node(first_id); auto second_node = database.find_graph_node(second_id); assert(first_node && second_node); assert(first_node->properties.at("cmdline") == "reused_pid_exec --first"); assert(second_node->properties.at("cmdline") == "reused_pid_exec --second"); assert(second_node->properties.at("exe_deleted") == "true"); assert(second_node->properties.at("parent_node_id") == "process_1_feedface_1"); bool saw_yara = false; bool saw_deleted = false; for (const auto& hit : database.find_indicators_by_hash(hash)) { if (hit.rule_name == "NoSeverityRule") { saw_yara = true; assert(hit.severity == "medium"); } else if (hit.rule_name == "DeletedExecutable") { saw_deleted = true; assert(hit.severity == "low"); assert(hit.metadata.at("source") == "heuristic"); assert(hit.metadata.at("process_node_id") == second_id); } } assert(saw_yara); assert(saw_deleted); // Same hash in two process instances (same PID) is still two processes. bool saw_same_hash = false; std::size_t per_process_findings = 0; for (const auto& finding : database.list_findings()) { if (finding.metadata.at("rule") == "same_hash_multiple_process_matches") { saw_same_hash = true; } if (finding.metadata.at("rule") == "process_executable_yara_match") { ++per_process_findings; } } assert(saw_same_hash); assert(per_process_findings == 2); database.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } std::string identity_of(const fs::path& path) { struct stat st{}; assert(::stat(path.c_str(), &st) == 0); return std::to_string(st.st_dev) + ":" + std::to_string(st.st_ino); } void test_scans_only_the_verified_image() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto malicious = root / "bin" / "malicious"; const auto clean = root / "bin" / "clean"; const auto db_path = (root / "verified.db").string(); write_rule_file(rules_dir / "synthetic_runtime.yar"); write_binary_file(malicious, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker"); write_binary_file(clean, std::string("\x7f\x45\x4c\x46") + "nothing to see"); std::cout << "Test scans_only_the_verified_image:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); // Event resolved to the clean image, but by scan time the pinned path // points at a different inode (process re-exec'd): must not be scanned. strawberry::ProcessInfo swapped; swapped.pid = 700; swapped.executable_path = clean.string(); swapped.image_path = malicious.string(); swapped.image_identity = identity_of(clean); pipeline.handle_process(swapped); // Process already exited (pinned link gone): fall back to the on-disk // path because it is still the same inode. strawberry::ProcessInfo exited; exited.pid = 701; exited.executable_path = malicious.string(); exited.image_path = (root / "proc" / "701" / "exe").string(); exited.image_identity = identity_of(malicious); pipeline.handle_process(exited); // Duplicate event for the same process instance and image is ignored. pipeline.handle_process(exited); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(db_path)); const auto indicators = database.list_indicators(); assert(indicators.size() == 1); assert(indicators[0].metadata.at("pid") == "701"); assert(indicators[0].rule_name == "SyntheticRuntimeRule"); assert(indicators[0].hash == strawberry::HashUtils::sha256_file(malicious.string())); database.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_stats_and_rescan_after_rule_reload() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto executable = root / "bin" / "late_detected"; const auto db_path = (root / "stats.db").string(); // Start with a rule that does not match the binary. write_text_file(rules_dir / "active.yar", "rule Unrelated { strings: $a = \"nothing-matches-this\" condition: $a }\n"); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker"); std::cout << "Test stats_and_rescan_after_rule_reload:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); auto stats = pipeline.stats(); assert(stats.rules_loaded && stats.rules_generation == 1 && stats.rule_files == 1); assert(pipeline.database_path() == db_path); strawberry::ProcessInfo info; info.pid = 800; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); info.start_time_ticks = 42; pipeline.handle_process(info); pipeline.handle_process(info); // rescan with same rules: deduped stats = pipeline.stats(); assert(stats.events_handled == 2); assert(stats.images_scanned == 1); assert(stats.detections == 0); // A broken rule file: reload fails, previous rules stay active, error is reported. write_text_file(rules_dir / "active.yar", "rule Broken { condition: }\n"); assert(!pipeline.reload_rules()); stats = pipeline.stats(); assert(stats.rules_loaded && stats.rules_generation == 1); assert(!stats.last_rules_error.empty()); // Fixed rule that matches: rescan of the same process now records it once. write_rule_file(rules_dir / "active.yar"); assert(pipeline.reload_rules()); stats = pipeline.stats(); assert(stats.rules_generation == 2 && stats.last_rules_error.empty()); pipeline.handle_process(info); pipeline.handle_process(info); stats = pipeline.stats(); assert(stats.detections == 1); assert(stats.last_detection_ns > 0); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(db_path)); assert(database.list_indicators().size() == 1); database.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_scan_on_demand_is_read_only_and_bounded() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto sample = root / "sample.bin"; const auto db_path = (root / "ondemand.db").string(); write_rule_file(rules_dir / "synthetic_runtime.yar"); write_binary_file(sample, std::string("prefix synthetic-payload-marker suffix")); std::cout << "Test scan_on_demand_is_read_only_and_bounded:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config)); int fd = ::open(sample.c_str(), O_RDONLY | O_CLOEXEC); assert(fd >= 0); auto result = pipeline.scan_on_demand(fd, 1024); assert(result.ok); assert(result.size == fs::file_size(sample)); assert(result.sha256 == strawberry::HashUtils::sha256_file(sample.string())); assert(result.matches.size() == 1); assert(result.matches[0].rule == "SyntheticRuntimeRule"); assert(result.matches[0].severity == "high"); // Size cap. result = pipeline.scan_on_demand(fd, 8); assert(!result.ok && result.error.find("too large") != std::string::npos); ::close(fd); // Non-regular file. fd = ::open(root.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC); assert(fd >= 0); result = pipeline.scan_on_demand(fd, 1024); assert(!result.ok && result.error == "not a regular file"); ::close(fd); assert(pipeline.stats().detections == 0); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(db_path)); assert(database.list_indicators().empty()); database.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_feed_sources_allowlist_and_size_cap() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto feeds_dir = root / "feeds"; const auto executable = root / "bin" / "dual_match"; const auto db_path = (root / "policy_pipeline.db").string(); write_rule_file(rules_dir / "synthetic_runtime.yar"); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker feed-marker"); // One installed feed (feeds/f1/current -> v1) with its own rule. fs::create_directories(feeds_dir / "f1" / "v1"); write_text_file(feeds_dir / "f1" / "v1" / "feed.yar", "rule FeedRule { meta: severity = \"medium\" strings: $f = \"feed-marker\" condition: $f }\n"); fs::create_directory_symlink("v1", feeds_dir / "f1" / "current"); std::cout << "Test feed_sources_allowlist_and_size_cap:" << std::endl; strawberry::Config config; config.db_path = db_path; config.yara_rules_path = rules_dir.string(); config.policy_path = (root / "policy.json").string(); config.feeds_dir = feeds_dir.string(); strawberry::PolicyStore policy(config.policy_path); assert(policy.load()); std::string error; assert(policy.apply("feed-remove", R"({"id":"yara-forge-core"})", 0, &error)); assert(policy.apply("feed-add", R"({"id":"f1","type":"url","url":"https://example.com/feed.yar"})", 0, &error)); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config, &policy)); assert(pipeline.stats().rule_count == 2); // local + feed, separate namespaces const auto hash = strawberry::HashUtils::sha256_file(executable.string()); uint64_t start = 100; const auto event = [&]() { strawberry::ProcessInfo info; info.pid = 900; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); info.start_time_ticks = start++; return info; }; pipeline.handle_process(event()); assert(pipeline.stats().detections == 2); // Suppress the feed rule everywhere: only the local rule is stored. assert(policy.apply("allowlist-add", R"({"kind":"rule","value":"FeedRule"})", 1000, &error)); pipeline.handle_process(event()); assert(pipeline.stats().detections == 3); assert(pipeline.stats().suppressed == 1); // Allowlist the exact binary: nothing stored. assert(policy.apply("allowlist-add", R"({"kind":"sha256","value":")" + hash + R"("})", 1000, &error)); pipeline.handle_process(event()); assert(pipeline.stats().detections == 3); assert(pipeline.stats().suppressed == 3); // Disabling the feed drops its rules on reload. assert(policy.apply("feed-set-enabled", R"({"id":"f1","enabled":false})", 1000, &error)); assert(pipeline.reload_rules()); assert(pipeline.stats().rule_count == 1); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(db_path)); std::size_t feed_hits = 0; for (const auto& indicator : database.list_indicators()) { feed_hits += indicator.rule_name == "FeedRule" ? 1 : 0; if (indicator.rule_name == "FeedRule") { assert(indicator.metadata.at("namespace") == "f1"); } } assert(feed_hits == 1); database.close(); // Size cap: images over process_scan_max_bytes are counted, not scanned. config.process_scan_max_bytes = 8; config.db_path = (root / "size_cap.db").string(); strawberry::DetectionPipeline capped; assert(capped.initialize(config)); capped.handle_process(event()); assert(capped.stats().images_too_large == 1); assert(capped.stats().images_scanned == 0); assert(capped.stats().detections == 0); capped.shutdown(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_ioc_hash_detection_and_score_severity() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto feeds_dir = root / "feeds"; const auto known_bad = root / "bin" / "known_bad"; const auto scored = root / "bin" / "scored"; write_text_file(rules_dir / "scored.yar", "rule Scored_Rule { meta: score = 75 strings: $s = \"scored-marker\" condition: $s }\n"); write_binary_file(known_bad, std::string("\x7f\x45\x4c\x46") + "no rule matches this"); write_binary_file(scored, std::string("\x7f\x45\x4c\x46") + "scored-marker"); const auto bad_hash = strawberry::HashUtils::sha256_file(known_bad.string()); fs::create_directories(feeds_dir / "hash-feed" / "v1"); write_text_file(feeds_dir / "hash-feed" / "v1" / "list.txt", std::string(64, '0') + "\n" + bad_hash + "\n"); fs::create_directory_symlink("v1", feeds_dir / "hash-feed" / "current"); std::cout << "Test ioc_hash_detection_and_score_severity:" << std::endl; strawberry::Config config; config.db_path = (root / "ioc.db").string(); config.yara_rules_path = rules_dir.string(); config.policy_path = (root / "policy.json").string(); config.feeds_dir = feeds_dir.string(); strawberry::PolicyStore policy(config.policy_path); assert(policy.load()); std::string error; assert(policy.apply("feed-add", R"({"id":"hash-feed","kind":"hashes","type":"url","url":"https://example.com/h.txt"})", 0, &error)); strawberry::DetectionPipeline pipeline; assert(pipeline.initialize(config, &policy)); assert(pipeline.stats().ioc_hashes == 2); const auto event = [&](const fs::path& exe, uint64_t start) { strawberry::ProcessInfo info; info.pid = 950; info.executable_path = exe.string(); info.image_path = exe.string(); info.image_identity = identity_of(exe); info.start_time_ticks = start; return info; }; pipeline.handle_process(event(known_bad, 1)); pipeline.handle_process(event(scored, 2)); // On-demand scans report IOC hits too. const int fd = ::open(known_bad.c_str(), O_RDONLY | O_CLOEXEC); const auto on_demand = pipeline.scan_on_demand(fd, 1 << 20); ::close(fd); assert(on_demand.ok && on_demand.matches.size() == 1); assert(on_demand.matches[0].rule == "IOC_KnownMalwareHash" && on_demand.matches[0].severity == "critical"); assert(on_demand.matches[0].namespace_name == "hash-feed"); pipeline.shutdown(); strawberry::Database database; assert(database.open_read_only(config.db_path)); bool saw_ioc = false; bool saw_scored = false; for (const auto& indicator : database.list_indicators()) { if (indicator.rule_name == "IOC_KnownMalwareHash") { saw_ioc = true; assert(indicator.severity == "critical"); assert(indicator.metadata.at("source") == "ioc"); assert(indicator.hash == bad_hash); } else if (indicator.rule_name == "Scored_Rule") { saw_scored = true; assert(indicator.severity == "high"); // score 75 -> high } } assert(saw_ioc && saw_scored); bool saw_title = false; for (const auto& finding : database.list_findings()) { saw_title |= finding.title.find("Known malware executed") != std::string::npos && finding.severity == "critical"; } assert(saw_title); database.close(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } void test_governed_shared_cache_degrade_and_caps() { const auto root = make_test_root(); const auto rules_dir = root / "rules"; const auto executable = root / "bin" / "guarded"; const auto other = root / "bin" / "guarded_later"; write_rule_file(rules_dir / "synthetic_runtime.yar"); write_binary_file(executable, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker"); write_binary_file(other, std::string("\x7f\x45\x4c\x46") + "synthetic-payload-marker-2"); std::cout << "Test governed_shared_cache_degrade_and_caps:" << std::endl; strawberry::ResourceSample sample; sample.host_source = "psi"; sample.cpu_some_pct = 1; sample.own_cpu_pct = 1; sample.rss_bytes = 10ull << 20; strawberry::GovernorOptions options; options.profile = *strawberry::scan_profile_named("balanced"); options.sampler = [&sample]() { return sample; }; options.sleeper = [](std::chrono::milliseconds) {}; strawberry::ResourceGovernor governor(options); governor.tick(); strawberry::Config config; config.db_path = (root / "governed.db").string(); config.yara_rules_path = rules_dir.string(); strawberry::DetectionPipeline pipeline; pipeline.set_governor(&governor); assert(pipeline.initialize(config)); // The exec guard scans first; its result serves the pipeline too. int fd = ::open(executable.c_str(), O_RDONLY | O_CLOEXEC); assert(fd >= 0); auto assessment = pipeline.assess_exec(fd, executable.string()); ::close(fd); assert(assessment.scanned && !assessment.degraded); assert(assessment.top_rule == "SyntheticRuntimeRule"); assert(pipeline.perf().yara_scan_us.summary().count == 1); strawberry::ProcessInfo info; info.pid = 900; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); info.start_time_ticks = 7; // Small, already cached image: cheap, handled on the live path. assert(pipeline.slow_scan_key(info, 0).empty()); pipeline.handle_process(info); auto stats = pipeline.stats(); assert(stats.detections == 1); assert(stats.scan_cache_hits == 1 && stats.images_scanned == 0); assert(pipeline.perf().yara_scan_us.summary().count == 1); // no second YARA pass // Severe host pressure: the guard decides on hash/IOC/quarantine only, and // a degraded result is not cached (the pipeline still scans it). sample.cpu_some_pct = 95; governor.tick(); fd = ::open(other.c_str(), O_RDONLY | O_CLOEXEC); assert(fd >= 0); assessment = pipeline.assess_exec(fd, other.string()); ::close(fd); assert(assessment.scanned && assessment.degraded && assessment.top_rule.empty()); assert(!assessment.sha256.empty() && !assessment.md5.empty()); assert(pipeline.perf().yara_scan_us.summary().count == 1); info.pid = 901; info.executable_path = other.string(); info.image_path = other.string(); info.image_identity = identity_of(other); // Uncached: needs a full scan (key returned) unless below the size floor. assert(!pipeline.slow_scan_key(info, 0).empty()); assert(pipeline.slow_scan_key(info, 1 << 20).empty()); pipeline.handle_process(info); assert(pipeline.slow_scan_key(info, 0).empty()); // cached now stats = pipeline.stats(); assert(stats.images_scanned == 1 && stats.detections == 2); pipeline.shutdown(); // The profile's image cap applies even when the config allows more. auto small = *strawberry::scan_profile_named("balanced"); small.pipeline_max_image_bytes = 8; options.profile = small; strawberry::ResourceGovernor capped(options); capped.tick(); strawberry::DetectionPipeline limited; limited.set_governor(&capped); config.db_path = (root / "capped.db").string(); assert(limited.initialize(config)); info.pid = 902; info.executable_path = executable.string(); info.image_path = executable.string(); info.image_identity = identity_of(executable); limited.handle_process(info); assert(limited.stats().images_too_large == 1 && limited.stats().images_scanned == 0); // Repeated execs of a too-large image are cache hits, not re-reads. info.pid = 903; limited.handle_process(info); assert(limited.stats().images_too_large == 1 && limited.stats().scan_cache_hits == 1); limited.shutdown(); std::cout << " PASSED" << std::endl; std::error_code ec; fs::remove_all(root, ec); } #endif } // namespace int main() { std::cout << "Running DetectionPipeline tests..." << std::endl << std::endl; #if !STRAWBERRY_HAVE_YARA std::cout << "SKIP: runtime pipeline end-to-end test requires libyara" << std::endl; return 0; #else test_procfs_event_scans_and_persists_match(); test_fails_closed_without_rules(); test_single_writer_lock_and_read_only_queries(); test_stable_ids_severity_default_and_heuristics(); test_scans_only_the_verified_image(); test_stats_and_rescan_after_rule_reload(); test_scan_on_demand_is_read_only_and_bounded(); test_feed_sources_allowlist_and_size_cap(); test_ioc_hash_detection_and_score_severity(); test_governed_shared_cache_degrade_and_caps(); std::cout << std::endl << "All tests passed!" << std::endl; return 0; #endif }