// Incremental correlation against the full pass (the oracle). A failure // prints the seed; re-run with STRAWBERRY_PROPERTY_SEED=. // // The oracle is what the daemon stored before: the full pass after every // batch, each finding upserted by id. // // I1 add-only: the stored findings are identical (same ids, same latest // version of each), and equal the final full pass: one finding per // group, whose id stays the same as the group grows // I2 the same with in-place updates (an indicator's content or groups change) // I3 a batch touching one process rebuilds only its groups, not everything #include "strawberry/correlation_engine.hpp" #include #include #include #include #include #include #include #include #include using namespace strawberry; namespace { uint64_t seed_from_env() { if (const char* text = std::getenv("STRAWBERRY_PROPERTY_SEED")) return std::strtoull(text, nullptr, 10); return std::random_device{}(); } const uint64_t kSeed = seed_from_env(); #define PROPERTY(cond) \ do { \ if (!(cond)) { \ std::cerr << "PROPERTY FAILED: " #cond " (seed " << kSeed << ", " << __FILE__ << ":" \ << __LINE__ << ")" << std::endl; \ std::abort(); \ } \ } while (0) bool same(const FindingRecord& a, const FindingRecord& b) { return a.id == b.id && a.title == b.title && a.severity == b.severity && a.score == b.score && a.summary == b.summary && a.related_node_ids == b.related_node_ids && a.related_indicator_ids == b.related_indicator_ids && a.metadata == b.metadata && a.first_seen == b.first_seen && a.last_seen == b.last_seen; } IndicatorEntry random_indicator(std::mt19937_64& rng, const std::string& id) { static const std::vector sources{"yara", "ioc", "behavior", "heuristic", "memory", "prevention", "demo", "other"}; static const std::vector severities{"low", "medium", "high", "critical", ""}; IndicatorEntry e; e.id = id; const int pid = 100 + static_cast(rng() % 12); const int ppid = 1 + static_cast(rng() % 4); e.hash = rng() % 5 == 0 ? std::string{} : std::string(64, static_cast('a' + rng() % 4)); e.rule_name = "Rule" + std::to_string(rng() % 6); e.severity = severities[rng() % severities.size()]; e.description = rng() % 3 == 0 ? "" : "did thing " + std::to_string(rng() % 5); e.timestamp = static_cast(1'000 + rng() % 50); // ties on purpose e.metadata["source"] = sources[rng() % sources.size()]; if (rng() % 3 != 0) e.metadata["namespace"] = rng() % 2 ? "default" : "feed-A"; if (rng() % 10 == 0) e.metadata["scan_kind"] = rng() % 2 ? "process_executable" : "file"; if (rng() % 8 != 0) e.metadata["pid"] = std::to_string(pid); if (rng() % 3 != 0) e.metadata["parent_pid"] = std::to_string(ppid); if (rng() % 2) e.metadata["process_node_id"] = "process_" + std::to_string(pid) + "_b_" + std::to_string(pid * 7); if (rng() % 2) e.metadata["parent_process_node_id"] = "process_" + std::to_string(ppid) + "_b_" + std::to_string(ppid * 7); if (rng() % 4 != 0) e.metadata["process_path"] = "/usr/bin/p" + std::to_string(pid % 5); if (rng() % 9 == 0) e.metadata["evidence_only"] = "true"; if (rng() % 9 == 0) e.metadata["verdict"] = rng() % 2 ? "block" : "would_block"; return e; } void run(bool with_updates, int iterations) { std::mt19937_64 rng(kSeed ^ (with_updates ? 0x9e3779b97f4a7c15ull : 0)); for (int iter = 0; iter < iterations; ++iter) { IncrementalCorrelator inc; std::map current; std::map latest; // incremental, upserted by id std::map stored_full; // full pass after every batch const int batches = 1 + static_cast(rng() % 8); int next_id = 0; for (int b = 0; b < batches; ++b) { std::vector batch; const int n = static_cast(rng() % 12); for (int i = 0; i < n; ++i) { std::string id; if (with_updates && !current.empty() && rng() % 3 == 0) { auto it = current.begin(); std::advance(it, static_cast(rng() % current.size())); id = it->first; } else { id = "ind-" + std::to_string(next_id++); } auto e = random_indicator(rng, id); current[id] = e; batch.push_back(std::move(e)); } const auto ids = inc.update(batch); std::vector members; for (const auto& id : ids) members.push_back(current.at(id)); for (auto& f : inc.build(members)) latest[f.id] = std::move(f); PROPERTY(inc.pending_groups() == 0); std::vector all; for (const auto& [id, e] : current) all.push_back(e); for (auto& f : CorrelationEngine::build_findings(all)) stored_full[f.id] = std::move(f); } PROPERTY(latest.size() == stored_full.size()); if (!with_updates) { // Stable ids: nothing superseded is left behind. std::vector all; for (const auto& [id, e] : current) all.push_back(e); PROPERTY(CorrelationEngine::build_findings(all).size() == latest.size()); } for (const auto& [id, f] : stored_full) { const auto it = latest.find(id); PROPERTY(it != latest.end()); if (!same(it->second, f)) { std::cerr << "differs " << id << " rule=" << f.metadata.at("rule") << "\n inc: " << it->second.summary << "\n full: " << f.summary << std::endl; } PROPERTY(same(it->second, f)); } } } void test_touches_only_affected_groups() { IncrementalCorrelator inc; std::vector base; for (int pid = 1; pid <= 50; ++pid) { IndicatorEntry e; e.id = "i" + std::to_string(pid); e.hash = std::string(63, 'a') + static_cast('a' + pid % 6); e.rule_name = "R" + std::to_string(pid); e.severity = "high"; e.timestamp = pid; e.metadata = {{"source", "yara"}, {"pid", std::to_string(pid)}, {"process_path", "/bin/x" + std::to_string(pid)}}; base.push_back(e); } const auto ids = inc.update(base); PROPERTY(ids.size() == 50); (void) inc.build(base); IndicatorEntry more = base[0]; more.id = "i-new"; more.timestamp = 99; const auto touched = inc.update({more}); // process group of pid 1, hash group shared with 8 others, rule R1. PROPERTY(touched.size() < 15); PROPERTY(inc.pending_groups() == 3); std::cout << "I3 touches only affected groups: PASSED (" << touched.size() << " of 51 refetched)" << std::endl; } // The store's change log feeds the correlator: first read is a reset, then // exactly the written ids; a rolled-back new indicator shows up as removed. void test_database_change_log() { namespace fs = std::filesystem; const auto dir = fs::temp_directory_path() / ("strawberry_corr_inc_" + std::to_string(::getpid())); std::error_code ec; fs::remove_all(dir, ec); Database db; PROPERTY(db.open(dir.string())); auto first = db.take_indicator_changes(); PROPERTY(first.reset && first.changed.empty()); IndicatorEntry e; e.hash = std::string(64, 'a'); e.rule_name = "R"; e.severity = "high"; e.metadata = {{"source", "yara"}, {"pid", "7"}}; for (int i = 0; i < 3; ++i) { e.id = "c" + std::to_string(i); e.timestamp = i; PROPERTY(db.store_indicator(e)); } auto changes = db.take_indicator_changes(); PROPERTY(!changes.reset && changes.changed.size() == 3 && changes.removed.empty()); PROPERTY(db.take_indicator_changes().changed.empty()); // drained PROPERTY(db.begin_transaction()); e.id = "rolled-back"; PROPERTY(db.store_indicator(e)); PROPERTY(db.rollback_transaction()); e.id = "c1"; e.severity = "critical"; PROPERTY(db.store_indicator(e)); changes = db.take_indicator_changes(); PROPERTY(!changes.reset && changes.changed.size() == 1 && changes.changed[0].severity == "critical"); PROPERTY(changes.removed == std::vector{"rolled-back"}); IncrementalCorrelator inc; (void) inc.update(db.list_indicators()); const auto ids = inc.update(changes.changed, changes.removed); const auto findings = inc.build(db.find_indicators(ids)); PROPERTY(!findings.empty() && findings.front().severity == "critical"); db.close(); fs::remove_all(dir, ec); std::cout << "change log: PASSED" << std::endl; } void test_policy_change_does_not_reuse_cleared_members() { IndicatorEntry a; a.id = "cleared"; a.hash = std::string(64, 'a'); a.rule_name = "Shared"; a.severity = "high"; a.timestamp = 100; a.metadata = {{"source", "yara"}, {"pid", "101"}, {"process_node_id", "process_101_boot_1"}, {"process_path", "/synthetic/a"}}; auto b = a; b.id = "survives"; b.hash = std::string(64, 'b'); b.metadata["pid"] = "102"; b.metadata["process_node_id"] = "process_102_boot_1"; bool cleared = false; AllowlistFn allow = [&](const std::string& hash, const std::string&, const std::string&) { return cleared && hash == a.hash; }; IncrementalCorrelator inc; inc.set_allowlist(allow); (void) inc.update({a}); (void) inc.build({a}); cleared = true; const auto ids = inc.update({b}); std::vector members; for (const auto& id : ids) members.push_back(id == a.id ? a : b); const auto actual = inc.build(members); const auto expected = CorrelationEngine::build_findings({a, b}, allow); PROPERTY(actual.size() == expected.size()); for (std::size_t i = 0; i < actual.size(); ++i) PROPERTY(same(actual[i], expected[i])); // Restoring policy requires a reset + current snapshot, including previously excluded evidence. cleared = false; inc.reset(); (void) inc.update({a, b}); const auto restored = inc.build({a, b}); const auto full = CorrelationEngine::build_findings({a, b}, allow); PROPERTY(restored.size() == full.size()); for (std::size_t i = 0; i < restored.size(); ++i) PROPERTY(same(restored[i], full[i])); std::cout << "I4 policy changes do not reuse cleared members: PASSED" << std::endl; } } // namespace int main() { test_policy_change_does_not_reuse_cleared_members(); std::cout << "incremental correlation properties, seed " << kSeed << std::endl; run(false, 400); std::cout << "I1 add-only equals full: PASSED" << std::endl; run(true, 400); std::cout << "I2 with updates equals full: PASSED" << std::endl; test_touches_only_affected_groups(); test_database_change_log(); return 0; }