Preserve independent support, typed rule namespaces and stable process lifetimes. Share authoritative managed-set reconciliation across daemon and offline correlation, load offline policy without seeding/migration, and release resources after shutdown-flush exceptions. Keep retirement history, transaction ownership and fault/allocation regressions; no whole-set crash-atomicity or live acceptance claim.
693 lines
31 KiB
C++
693 lines
31 KiB
C++
#include "strawberry/correlation_engine.hpp"
|
|
#include "strawberry/database.hpp"
|
|
#include "strawberry/hash_utils.hpp"
|
|
|
|
#include <cassert>
|
|
#include <filesystem>
|
|
#include <iostream>
|
|
#include <set>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace {
|
|
|
|
void test_process_yara_hits_emit_finding() {
|
|
const auto db_path = (fs::temp_directory_path() / "strawberry_correlation_engine.db").string();
|
|
std::error_code ec;
|
|
fs::remove_all(db_path, ec); // the store is a directory
|
|
|
|
strawberry::Database database;
|
|
assert(database.open(db_path));
|
|
|
|
strawberry::GraphNode process{
|
|
.id = "process_1234",
|
|
.type = "process",
|
|
.properties = {
|
|
{"pid", "1234"},
|
|
{"path", "/tmp/suspicious"},
|
|
{"hash", "hash-1234"},
|
|
{"cmdline", "suspicious --run"},
|
|
},
|
|
};
|
|
strawberry::GraphNode file{
|
|
.id = "file_hash-1234",
|
|
.type = "file",
|
|
.properties = {
|
|
{"path", "/tmp/suspicious"},
|
|
{"hash", "hash-1234"},
|
|
{"kind", "executable_image"},
|
|
},
|
|
};
|
|
strawberry::GraphNode indicator_a{
|
|
.id = "indicator_hit-a",
|
|
.type = "indicator",
|
|
.properties = {
|
|
{"rule_name", "RuleA"},
|
|
{"severity", "high"},
|
|
{"hash", "hash-1234"},
|
|
{"pid", "1234"},
|
|
},
|
|
};
|
|
strawberry::GraphNode indicator_b{
|
|
.id = "indicator_hit-b",
|
|
.type = "indicator",
|
|
.properties = {
|
|
{"rule_name", "RuleB"},
|
|
{"severity", "medium"},
|
|
{"hash", "hash-1234"},
|
|
{"pid", "1234"},
|
|
},
|
|
};
|
|
|
|
assert(database.add_graph_node(process));
|
|
assert(database.add_graph_node(file));
|
|
assert(database.add_graph_node(indicator_a));
|
|
assert(database.add_graph_node(indicator_b));
|
|
assert(database.add_graph_edge({"process_1234", "file_hash-1234", "executed", 100}));
|
|
assert(database.add_graph_edge({"process_1234", "indicator_hit-a", "matched", 110}));
|
|
assert(database.add_graph_edge({"process_1234", "indicator_hit-b", "matched", 120}));
|
|
assert(database.add_graph_edge({"file_hash-1234", "indicator_hit-a", "content_matched", 111}));
|
|
assert(database.add_graph_edge({"file_hash-1234", "indicator_hit-b", "content_matched", 121}));
|
|
|
|
strawberry::IndicatorEntry hit_a{
|
|
.id = "hit-a",
|
|
.hash = "hash-1234",
|
|
.rule_name = "RuleA",
|
|
.severity = "high",
|
|
.description = "YARA hit A",
|
|
.metadata = {
|
|
{"source", "yara"},
|
|
{"scan_kind", "process_executable"},
|
|
{"pid", "1234"},
|
|
{"process_path", "/tmp/suspicious"},
|
|
{"cmdline", "suspicious --run"},
|
|
},
|
|
.timestamp = 110,
|
|
};
|
|
strawberry::IndicatorEntry hit_b{
|
|
.id = "hit-b",
|
|
.hash = "hash-1234",
|
|
.rule_name = "RuleB",
|
|
.severity = "medium",
|
|
.description = "YARA hit B",
|
|
.metadata = {
|
|
{"source", "yara"},
|
|
{"scan_kind", "process_executable"},
|
|
{"pid", "1234"},
|
|
{"process_path", "/tmp/suspicious"},
|
|
{"cmdline", "suspicious --run"},
|
|
},
|
|
.timestamp = 120,
|
|
};
|
|
|
|
assert(database.store_indicator(hit_a));
|
|
assert(database.store_indicator(hit_b));
|
|
|
|
std::cout << "Test process_yara_hits_emit_finding:" << std::endl;
|
|
auto emitted = strawberry::CorrelationEngine::correlate(database);
|
|
assert(emitted.size() == 1);
|
|
|
|
const auto& finding = emitted[0];
|
|
assert(finding.title == "YARA match on executed process image");
|
|
assert(finding.severity == "high");
|
|
assert(finding.score == 85);
|
|
assert(finding.summary.find("Process 1234") != std::string::npos);
|
|
assert(finding.summary.find("RuleA") != std::string::npos);
|
|
assert(finding.summary.find("RuleB") != std::string::npos);
|
|
assert(finding.metadata.at("indicator_count") == "2");
|
|
assert(finding.first_seen == 110);
|
|
assert(finding.last_seen == 120);
|
|
|
|
// An allowlisted hash produces no finding, even from already-stored evidence
|
|
// (so a cleared false positive does not regenerate on the next pass).
|
|
const auto allowlisted = strawberry::CorrelationEngine::build_findings(
|
|
{hit_a, hit_b},
|
|
[](const std::string& hash, const std::string&, const std::string&) { return hash == "hash-1234"; });
|
|
assert(allowlisted.empty());
|
|
const auto unrelated_allowlist = strawberry::CorrelationEngine::build_findings(
|
|
{hit_a, hit_b}, [](const std::string& hash, const std::string&, const std::string&) { return hash == "nope"; });
|
|
assert(!unrelated_allowlist.empty());
|
|
|
|
std::set<std::string> related_nodes(finding.related_node_ids.begin(), finding.related_node_ids.end());
|
|
assert(related_nodes.contains("process_1234"));
|
|
assert(related_nodes.contains("file_hash-1234"));
|
|
assert(related_nodes.contains("indicator_hit-a"));
|
|
assert(related_nodes.contains("indicator_hit-b"));
|
|
|
|
std::set<std::string> related_indicators(
|
|
finding.related_indicator_ids.begin(), finding.related_indicator_ids.end());
|
|
assert(related_indicators.contains("hit-a"));
|
|
assert(related_indicators.contains("hit-b"));
|
|
|
|
auto stored = database.find_finding(finding.id);
|
|
assert(stored.has_value());
|
|
assert(stored->summary == finding.summary);
|
|
assert(stored->metadata.at("rule") == "process_executable_yara_match");
|
|
|
|
auto listed = database.list_findings();
|
|
assert(listed.size() == 1);
|
|
assert(listed[0].id == finding.id);
|
|
|
|
// Re-running should refresh the same deterministic finding, not create a duplicate.
|
|
auto emitted_again = strawberry::CorrelationEngine::correlate(database);
|
|
assert(emitted_again.size() == 1);
|
|
auto listed_again = database.list_findings();
|
|
assert(listed_again.size() == 1);
|
|
assert(listed_again[0].id == finding.id);
|
|
|
|
std::cout << " PASSED" << std::endl;
|
|
|
|
database.close();
|
|
fs::remove_all(db_path, ec); // the store is a directory
|
|
}
|
|
|
|
void test_lineage_multiple_yara_hits_emit_finding() {
|
|
const auto db_path = (fs::temp_directory_path() / "strawberry_correlation_lineage.db").string();
|
|
std::error_code ec;
|
|
fs::remove_all(db_path, ec); // the store is a directory
|
|
|
|
strawberry::Database database;
|
|
assert(database.open(db_path));
|
|
|
|
assert(database.add_graph_node({"process_77", "process", {{"pid", "77"}}}));
|
|
assert(database.add_graph_node({"process_100", "process", {{"pid", "100"}, {"parent_pid", "77"}}}));
|
|
assert(database.add_graph_node({"process_101", "process", {{"pid", "101"}, {"parent_pid", "77"}}}));
|
|
assert(database.add_graph_node({"file_hash-100", "file", {{"hash", "hash-100"}, {"path", "/tmp/a"}}}));
|
|
assert(database.add_graph_node({"file_hash-101", "file", {{"hash", "hash-101"}, {"path", "/tmp/b"}}}));
|
|
assert(database.add_graph_node({"indicator_hit-100", "indicator", {{"rule_name", "RuleA"}}}));
|
|
assert(database.add_graph_node({"indicator_hit-101", "indicator", {{"rule_name", "RuleB"}}}));
|
|
assert(database.add_graph_edge({"process_77", "process_100", "spawned", 90}));
|
|
assert(database.add_graph_edge({"process_77", "process_101", "spawned", 91}));
|
|
assert(database.add_graph_edge({"process_100", "indicator_hit-100", "matched", 100}));
|
|
assert(database.add_graph_edge({"process_101", "indicator_hit-101", "matched", 110}));
|
|
|
|
assert(database.store_indicator({
|
|
.id = "hit-100",
|
|
.hash = "hash-100",
|
|
.rule_name = "RuleA",
|
|
.severity = "medium",
|
|
.description = "YARA hit A",
|
|
.metadata = {
|
|
{"source", "yara"},
|
|
{"scan_kind", "process_executable"},
|
|
{"pid", "100"},
|
|
{"parent_pid", "77"},
|
|
{"process_path", "/tmp/a"},
|
|
},
|
|
.timestamp = 100,
|
|
}));
|
|
assert(database.store_indicator({
|
|
.id = "hit-101",
|
|
.hash = "hash-101",
|
|
.rule_name = "RuleB",
|
|
.severity = "high",
|
|
.description = "YARA hit B",
|
|
.metadata = {
|
|
{"source", "yara"},
|
|
{"scan_kind", "process_executable"},
|
|
{"pid", "101"},
|
|
{"parent_pid", "77"},
|
|
{"process_path", "/tmp/b"},
|
|
},
|
|
.timestamp = 110,
|
|
}));
|
|
|
|
std::cout << "Test lineage_multiple_yara_hits_emit_finding:" << std::endl;
|
|
auto emitted = strawberry::CorrelationEngine::correlate(database);
|
|
assert(emitted.size() == 3);
|
|
|
|
auto findings = database.list_findings();
|
|
assert(findings.size() == 3);
|
|
|
|
const strawberry::FindingRecord* lineage = nullptr;
|
|
for (const auto& finding : findings) {
|
|
if (finding.metadata.at("rule") == "lineage_multiple_yara_matches") {
|
|
lineage = &finding;
|
|
break;
|
|
}
|
|
}
|
|
|
|
assert(lineage != nullptr);
|
|
assert(lineage->title == "Multiple matched descendants in process lineage");
|
|
assert(lineage->severity == "high");
|
|
assert(lineage->score == 90);
|
|
assert(lineage->metadata.at("parent_pid") == "77");
|
|
assert(lineage->metadata.at("child_pids") == "100,101");
|
|
assert(lineage->summary.find("Parent process 77") != std::string::npos);
|
|
|
|
std::set<std::string> related_nodes(lineage->related_node_ids.begin(), lineage->related_node_ids.end());
|
|
assert(related_nodes.contains("process_77"));
|
|
assert(related_nodes.contains("process_100"));
|
|
assert(related_nodes.contains("process_101"));
|
|
assert(related_nodes.contains("indicator_hit-100"));
|
|
assert(related_nodes.contains("indicator_hit-101"));
|
|
|
|
std::cout << " PASSED" << std::endl;
|
|
|
|
database.close();
|
|
fs::remove_all(db_path, ec); // the store is a directory
|
|
}
|
|
|
|
void test_same_hash_multiple_processes_emit_finding() {
|
|
const auto db_path = (fs::temp_directory_path() / "strawberry_correlation_same_hash.db").string();
|
|
std::error_code ec;
|
|
fs::remove_all(db_path, ec); // the store is a directory
|
|
|
|
strawberry::Database database;
|
|
assert(database.open(db_path));
|
|
|
|
assert(database.add_graph_node({"process_200", "process", {{"pid", "200"}, {"path", "/tmp/a"}}}));
|
|
assert(database.add_graph_node({"process_201", "process", {{"pid", "201"}, {"path", "/tmp/b"}}}));
|
|
assert(database.add_graph_node({"file_shared-hash", "file", {{"hash", "shared-hash"}, {"path", "/tmp/a"}}}));
|
|
assert(database.add_graph_node({"indicator_hit-200", "indicator", {{"rule_name", "SharedRule"}}}));
|
|
assert(database.add_graph_node({"indicator_hit-201", "indicator", {{"rule_name", "SharedRule"}}}));
|
|
assert(database.add_graph_edge({"process_200", "file_shared-hash", "executed", 200}));
|
|
assert(database.add_graph_edge({"process_201", "file_shared-hash", "executed", 210}));
|
|
assert(database.add_graph_edge({"process_200", "indicator_hit-200", "matched", 205}));
|
|
assert(database.add_graph_edge({"process_201", "indicator_hit-201", "matched", 215}));
|
|
|
|
assert(database.store_indicator({
|
|
.id = "hit-200",
|
|
.hash = "shared-hash",
|
|
.rule_name = "SharedRule",
|
|
.severity = "medium",
|
|
.description = "YARA hit A",
|
|
.metadata = {
|
|
{"source", "yara"},
|
|
{"scan_kind", "process_executable"},
|
|
{"pid", "200"},
|
|
{"process_path", "/tmp/a"},
|
|
},
|
|
.timestamp = 205,
|
|
}));
|
|
assert(database.store_indicator({
|
|
.id = "hit-201",
|
|
.hash = "shared-hash",
|
|
.rule_name = "SharedRule",
|
|
.severity = "high",
|
|
.description = "YARA hit B",
|
|
.metadata = {
|
|
{"source", "yara"},
|
|
{"scan_kind", "process_executable"},
|
|
{"pid", "201"},
|
|
{"process_path", "/tmp/b"},
|
|
},
|
|
.timestamp = 215,
|
|
}));
|
|
|
|
std::cout << "Test same_hash_multiple_processes_emit_finding:" << std::endl;
|
|
auto emitted = strawberry::CorrelationEngine::correlate(database);
|
|
assert(emitted.size() == 4);
|
|
|
|
auto findings = database.list_findings();
|
|
assert(findings.size() == 4);
|
|
|
|
const strawberry::FindingRecord* same_hash = nullptr;
|
|
for (const auto& finding : findings) {
|
|
if (finding.metadata.at("rule") == "same_hash_multiple_process_matches") {
|
|
same_hash = &finding;
|
|
break;
|
|
}
|
|
}
|
|
|
|
assert(same_hash != nullptr);
|
|
assert(same_hash->title == "Same matched executable hash across processes");
|
|
assert(same_hash->severity == "high");
|
|
assert(same_hash->score == 95);
|
|
assert(same_hash->metadata.at("hash") == "shared-hash");
|
|
assert(same_hash->metadata.at("pids") == "200,201");
|
|
assert(same_hash->metadata.at("indicator_count") == "2");
|
|
assert(same_hash->first_seen == 205);
|
|
assert(same_hash->last_seen == 215);
|
|
assert(same_hash->summary.find("across 2 processes") != std::string::npos);
|
|
|
|
std::set<std::string> related_nodes(same_hash->related_node_ids.begin(), same_hash->related_node_ids.end());
|
|
assert(related_nodes.contains("process_200"));
|
|
assert(related_nodes.contains("process_201"));
|
|
assert(related_nodes.contains("file_shared-hash"));
|
|
assert(related_nodes.contains("indicator_hit-200"));
|
|
assert(related_nodes.contains("indicator_hit-201"));
|
|
|
|
std::cout << " PASSED" << std::endl;
|
|
|
|
database.close();
|
|
fs::remove_all(db_path, ec); // the store is a directory
|
|
}
|
|
|
|
void test_same_rule_multiple_processes_emit_finding() {
|
|
const auto db_path = (fs::temp_directory_path() / "strawberry_correlation_same_rule.db").string();
|
|
std::error_code ec;
|
|
fs::remove_all(db_path, ec); // the store is a directory
|
|
|
|
strawberry::Database database;
|
|
assert(database.open(db_path));
|
|
|
|
assert(database.add_graph_node({"process_300", "process", {{"pid", "300"}, {"path", "/tmp/a"}}}));
|
|
assert(database.add_graph_node({"process_301", "process", {{"pid", "301"}, {"path", "/tmp/b"}}}));
|
|
assert(database.add_graph_node({"file_hash-300", "file", {{"hash", "hash-300"}, {"path", "/tmp/a"}}}));
|
|
assert(database.add_graph_node({"file_hash-301", "file", {{"hash", "hash-301"}, {"path", "/tmp/b"}}}));
|
|
assert(database.add_graph_node({"indicator_hit-300", "indicator", {{"rule_name", "CampaignRule"}}}));
|
|
assert(database.add_graph_node({"indicator_hit-301", "indicator", {{"rule_name", "CampaignRule"}}}));
|
|
assert(database.add_graph_edge({"process_300", "file_hash-300", "executed", 300}));
|
|
assert(database.add_graph_edge({"process_301", "file_hash-301", "executed", 310}));
|
|
assert(database.add_graph_edge({"process_300", "indicator_hit-300", "matched", 305}));
|
|
assert(database.add_graph_edge({"process_301", "indicator_hit-301", "matched", 315}));
|
|
|
|
assert(database.store_indicator({
|
|
.id = "hit-300",
|
|
.hash = "hash-300",
|
|
.rule_name = "CampaignRule",
|
|
.severity = "medium",
|
|
.description = "YARA hit A",
|
|
.metadata = {
|
|
{"source", "yara"},
|
|
{"scan_kind", "process_executable"},
|
|
{"pid", "300"},
|
|
{"process_path", "/tmp/a"},
|
|
},
|
|
.timestamp = 305,
|
|
}));
|
|
assert(database.store_indicator({
|
|
.id = "hit-301",
|
|
.hash = "hash-301",
|
|
.rule_name = "CampaignRule",
|
|
.severity = "high",
|
|
.description = "YARA hit B",
|
|
.metadata = {
|
|
{"source", "yara"},
|
|
{"scan_kind", "process_executable"},
|
|
{"pid", "301"},
|
|
{"process_path", "/tmp/b"},
|
|
},
|
|
.timestamp = 315,
|
|
}));
|
|
|
|
std::cout << "Test same_rule_multiple_processes_emit_finding:" << std::endl;
|
|
auto emitted = strawberry::CorrelationEngine::correlate(database);
|
|
assert(emitted.size() == 3);
|
|
|
|
auto findings = database.list_findings();
|
|
assert(findings.size() == 3);
|
|
|
|
const strawberry::FindingRecord* same_rule = nullptr;
|
|
for (const auto& finding : findings) {
|
|
if (finding.metadata.at("rule") == "same_yara_rule_multiple_processes") {
|
|
same_rule = &finding;
|
|
break;
|
|
}
|
|
}
|
|
|
|
assert(same_rule != nullptr);
|
|
assert(same_rule->title == "Same YARA rule across multiple processes");
|
|
assert(same_rule->severity == "high");
|
|
assert(same_rule->score == 92);
|
|
assert(same_rule->metadata.at("matched_rule") == "CampaignRule");
|
|
assert(same_rule->metadata.at("pids") == "300,301");
|
|
assert(same_rule->metadata.at("hashes") == "hash-300,hash-301");
|
|
assert(same_rule->metadata.at("indicator_count") == "2");
|
|
assert(same_rule->first_seen == 305);
|
|
assert(same_rule->last_seen == 315);
|
|
assert(same_rule->summary.find("across 2 processes") != std::string::npos);
|
|
|
|
std::set<std::string> related_nodes(same_rule->related_node_ids.begin(), same_rule->related_node_ids.end());
|
|
assert(related_nodes.contains("process_300"));
|
|
assert(related_nodes.contains("process_301"));
|
|
assert(related_nodes.contains("file_hash-300"));
|
|
assert(related_nodes.contains("file_hash-301"));
|
|
assert(related_nodes.contains("indicator_hit-300"));
|
|
assert(related_nodes.contains("indicator_hit-301"));
|
|
|
|
std::cout << " PASSED" << std::endl;
|
|
|
|
database.close();
|
|
fs::remove_all(db_path, ec); // the store is a directory
|
|
}
|
|
|
|
// Evidence-only indicators (binary replaced by an update, Strawberry's own
|
|
// unit files) do not make a finding alone, but count with another signal.
|
|
void test_evidence_only_needs_another_signal() {
|
|
const auto db_path = (fs::temp_directory_path() / "strawberry_correlation_evidence_only.db").string();
|
|
std::error_code ec;
|
|
fs::remove_all(db_path, ec); // the store is a directory
|
|
strawberry::Database database;
|
|
assert(database.open(db_path));
|
|
|
|
strawberry::IndicatorEntry replaced;
|
|
replaced.id = "process-hit:77:1:0";
|
|
replaced.hash = "hash-77";
|
|
replaced.rule_name = "DeletedExecutable";
|
|
replaced.severity = "low";
|
|
replaced.description = "Process is running a binary that was replaced on disk after it started (likely an update)";
|
|
replaced.timestamp = 100;
|
|
replaced.metadata = {{"source", "heuristic"}, {"scan_kind", "process_executable"}, {"pid", "77"},
|
|
{"process_node_id", "process_77"}, {"process_path", "/usr/bin/app"}, {"evidence_only", "true"}};
|
|
assert(database.store_indicator(replaced));
|
|
assert(strawberry::CorrelationEngine::correlate(database).empty());
|
|
|
|
strawberry::IndicatorEntry yara = replaced;
|
|
yara.id = "process-hit:77:2:0";
|
|
yara.rule_name = "RuleX";
|
|
yara.severity = "high";
|
|
yara.description = "Process matched YARA rule";
|
|
yara.timestamp = 200;
|
|
yara.metadata["source"] = "yara";
|
|
yara.metadata.erase("evidence_only");
|
|
assert(database.store_indicator(yara));
|
|
const auto findings = strawberry::CorrelationEngine::correlate(database);
|
|
bool found = false;
|
|
for (const auto& f : findings) {
|
|
if (f.metadata.at("pid") == "77") {
|
|
found = true;
|
|
assert(f.related_indicator_ids.size() == 2); // the evidence still counts
|
|
assert(f.metadata.at("rules").find("DeletedExecutable") != std::string::npos);
|
|
}
|
|
}
|
|
assert(found);
|
|
database.close();
|
|
fs::remove_all(db_path, ec);
|
|
std::cout << "Test evidence_only_needs_another_signal: PASSED" << std::endl;
|
|
}
|
|
|
|
// One rule across hundreds of processes: the finding is stored with its
|
|
// related lists capped (it used to exceed the store's record limit).
|
|
void test_wide_finding_is_capped() {
|
|
const auto db_path = (fs::temp_directory_path() / "strawberry_correlation_wide.db").string();
|
|
std::error_code ec;
|
|
fs::remove_all(db_path, ec); // the store is a directory
|
|
strawberry::Database database;
|
|
assert(database.open(db_path));
|
|
assert(database.begin_transaction());
|
|
for (int pid = 1000; pid < 1300; ++pid) {
|
|
strawberry::IndicatorEntry e;
|
|
e.id = "process-hit:" + std::to_string(pid) + ":1:0";
|
|
e.hash = "hash-" + std::to_string(pid);
|
|
e.rule_name = "WideRule";
|
|
e.severity = "medium";
|
|
e.description = "Process matched YARA rule";
|
|
e.timestamp = pid;
|
|
e.metadata = {{"source", "yara"}, {"scan_kind", "process_executable"}, {"pid", std::to_string(pid)},
|
|
{"process_node_id", "process_" + std::to_string(pid)}, {"process_path", "/tmp/w"}};
|
|
assert(database.store_indicator(e));
|
|
}
|
|
assert(database.commit_transaction());
|
|
const auto findings = strawberry::CorrelationEngine::correlate(database);
|
|
bool wide = false;
|
|
for (const auto& f : database.list_findings()) {
|
|
if (f.related_indicator_ids.size() == 200) {
|
|
wide = true;
|
|
assert(f.metadata.at("related_indicator_count") == "300");
|
|
assert(f.related_indicator_ids.back() == "process-hit:1299:1:0"); // most recent kept
|
|
}
|
|
assert(f.related_node_ids.size() <= 200 && f.related_indicator_ids.size() <= 200);
|
|
}
|
|
assert(wide && !findings.empty());
|
|
database.close();
|
|
fs::remove_all(db_path, ec);
|
|
std::cout << "Test wide_finding_is_capped: PASSED" << std::endl;
|
|
}
|
|
|
|
void test_namespaced_rule_identity() {
|
|
const auto indicator = [](const std::string& id, const std::string& pid, const std::string& ns,
|
|
const std::string& source) {
|
|
strawberry::IndicatorEntry e;
|
|
e.id = id;
|
|
e.hash = std::string(64, id.front());
|
|
e.rule_name = "SharedRule";
|
|
e.severity = "high";
|
|
e.timestamp = 100;
|
|
e.metadata = {{"source", source}, {"namespace", ns}, {"pid", pid},
|
|
{"process_node_id", "process_" + pid + "_boot_1"}, {"process_path", "/synthetic/bin/x"}};
|
|
return e;
|
|
};
|
|
// A shared bare name is not a shared rule across namespaces or source domains.
|
|
for (const auto& source : {std::string("yara"), std::string("memory"), std::string("ioc")}) {
|
|
for (const auto& ns : {std::string("feed-B"), std::string("feed-A"), std::string{}}) {
|
|
const auto a = indicator("a", "101", "feed-A", "yara");
|
|
const auto b = indicator("b", "102", ns, source);
|
|
const auto full = strawberry::CorrelationEngine::build_findings({a, b});
|
|
bool shared = false;
|
|
for (const auto& f : full) {
|
|
if (f.metadata.at("rule") != "same_yara_rule_multiple_processes") continue;
|
|
shared = true;
|
|
assert(source == "yara" && ns == "feed-A");
|
|
assert(f.metadata.at("rule_namespace") == "feed-A");
|
|
assert(f.metadata.at("rule_source") == "yara");
|
|
}
|
|
assert(shared == (source == "yara" && ns == "feed-A"));
|
|
strawberry::IncrementalCorrelator incremental;
|
|
(void) incremental.update({a, b});
|
|
const auto changed = incremental.build({a, b});
|
|
assert(changed.size() == full.size());
|
|
for (std::size_t i = 0; i < full.size(); ++i) assert(changed[i].id == full[i].id);
|
|
}
|
|
}
|
|
const auto a = indicator("a", "101", "", "yara");
|
|
const auto b = indicator("b", "102", "", "demo"); // demo shares the YARA domain
|
|
const std::string old_key = "rule\nSharedRule";
|
|
const auto old_id = "finding_rule_yara_" + strawberry::HashUtils::sha256_data(
|
|
reinterpret_cast<const unsigned char*>(old_key.data()), old_key.size()).substr(0, 16);
|
|
const auto legacy = strawberry::CorrelationEngine::build_findings({a, b});
|
|
bool preserved = false;
|
|
for (const auto& f : legacy) if (f.metadata.at("rule") == "same_yara_rule_multiple_processes") {
|
|
assert(f.id == old_id && f.metadata.at("rule_namespace").empty());
|
|
preserved = true;
|
|
}
|
|
assert(preserved);
|
|
const auto c = indicator("c", "103", "feed-A", "yara");
|
|
const auto d = indicator("d", "104", "feed-A", "yara");
|
|
const auto both = strawberry::CorrelationEngine::build_findings({a, b, c, d});
|
|
assert(strawberry::CorrelationEngine::superseded_findings(both).empty());
|
|
// Adversarial metadata cannot move separators between typed key fields.
|
|
auto x = a; x.metadata["namespace"] = std::string("feed-A") + '\x1f' + "R"; x.rule_name = "S";
|
|
auto y = b; y.metadata["source"] = "yara"; y.metadata["namespace"] = "feed-A";
|
|
y.rule_name = std::string("R") + '\x1f' + "S";
|
|
strawberry::IncrementalCorrelator inc;
|
|
(void) inc.update({x, y});
|
|
const auto actual = inc.build({x, y});
|
|
const auto expected = strawberry::CorrelationEngine::build_findings({x, y});
|
|
assert(actual.size() == expected.size());
|
|
for (std::size_t i = 0; i < actual.size(); ++i) assert(actual[i].id == expected[i].id);
|
|
std::cout << "Test namespaced_rule_identity: PASSED" << std::endl;
|
|
}
|
|
|
|
void test_instance_counts_not_pid_counts() {
|
|
strawberry::IndicatorEntry a;
|
|
a.id = "first"; a.hash = std::string(64, 'a'); a.rule_name = "R"; a.severity = "high"; a.timestamp = 100;
|
|
a.metadata = {{"source", "yara"}, {"pid", "101"}, {"process_node_id", "process_101_boot_1"},
|
|
{"parent_pid", "77"}, {"parent_process_node_id", "process_77_boot_1"},
|
|
{"process_path", "/synthetic/x"}};
|
|
auto b = a; b.id = "second"; b.timestamp = 200; b.metadata["process_node_id"] = "process_101_boot_2";
|
|
int aggregates = 0;
|
|
for (const auto& f : strawberry::CorrelationEngine::build_findings({a, b})) {
|
|
if (f.metadata.at("rule") == "process_executable_yara_match") continue;
|
|
++aggregates;
|
|
assert(f.summary.find("2 process") != std::string::npos || f.summary.find("2 descendant") != std::string::npos);
|
|
assert(f.metadata.at("process_instance_count") == "2");
|
|
assert(f.metadata.at("unique_pid_count") == "1");
|
|
}
|
|
assert(aggregates == 3);
|
|
std::cout << "Test instance_counts_not_pid_counts: PASSED" << std::endl;
|
|
}
|
|
|
|
void test_full_api_rule_migration(bool split_namespaces) {
|
|
const auto root = fs::temp_directory_path() / (split_namespaces ? "sb-corr-api-split" : "sb-corr-api-migration");
|
|
std::error_code ec; fs::remove_all(root, ec);
|
|
strawberry::Database database;
|
|
assert(database.open(root.string()));
|
|
strawberry::IndicatorEntry a;
|
|
a.id = "api-a"; a.hash = std::string(64, 'a'); a.rule_name = "FullApiShared"; a.timestamp = 100; a.severity = "high";
|
|
a.metadata = {{"source", "yara"}, {"namespace", "feed-A"}, {"pid", "101"},
|
|
{"process_node_id", "process_101_boot_1"}, {"process_path", "/synthetic/a"}};
|
|
auto b = a; b.id = "api-b"; b.hash = std::string(64, 'b'); b.metadata["pid"] = "102";
|
|
b.metadata["process_node_id"] = "process_102_boot_1"; b.metadata["process_path"] = "/synthetic/b";
|
|
if (split_namespaces) b.metadata["namespace"] = "feed-B";
|
|
assert(database.store_indicator(a) && database.store_indicator(b));
|
|
const std::string old_key = "rule\nFullApiShared";
|
|
strawberry::FindingRecord old;
|
|
old.id = "finding_rule_yara_" + strawberry::HashUtils::sha256_data(
|
|
reinterpret_cast<const unsigned char*>(old_key.data()), old_key.size()).substr(0, 16);
|
|
old.title = "Old bare-rule aggregate";
|
|
old.metadata = {{"engine", "strawberry-rule-correlation-v1"}, {"rule", "same_yara_rule_multiple_processes"},
|
|
{"matched_rule", "FullApiShared"}, {"indicator_count", "2"}};
|
|
old.related_indicator_ids = {a.id, b.id};
|
|
assert(database.store_finding(old));
|
|
auto foreign = old; foreign.id = "foreign"; foreign.metadata["engine"] = "other-engine";
|
|
assert(database.store_finding(foreign));
|
|
strawberry::FindingRecord manual; manual.id = "manual"; manual.title = "Operator note";
|
|
assert(database.store_finding(manual));
|
|
const auto emitted = strawberry::CorrelationEngine::correlate(database);
|
|
assert(emitted.size() == (split_namespaces ? 2u : 3u));
|
|
assert(database.list_findings().size() == emitted.size() + 2);
|
|
assert(!database.find_finding(old.id).has_value());
|
|
assert(database.find_finding("foreign").has_value() && database.find_finding("manual").has_value());
|
|
assert(database.list_indicators().size() == 2);
|
|
database.close();
|
|
assert(database.open_read_only(root.string()));
|
|
assert(!database.find_finding(old.id).has_value());
|
|
assert(database.list_findings().size() == emitted.size() + 2);
|
|
database.close(); fs::remove_all(root, ec);
|
|
std::cout << "Test full_api_rule_migration split=" << split_namespaces << ": PASSED" << std::endl;
|
|
}
|
|
|
|
void test_full_api_policy_withdrawal_and_outer_transaction() {
|
|
const auto root = fs::temp_directory_path() / "sb-corr-api-policy";
|
|
std::error_code ec; fs::remove_all(root, ec);
|
|
strawberry::Database database;
|
|
assert(database.open(root.string()));
|
|
strawberry::IndicatorEntry a;
|
|
a.id = "api-a"; a.hash = std::string(64, 'a'); a.rule_name = "FullApiShared"; a.timestamp = 100; a.severity = "high";
|
|
a.metadata = {{"source", "yara"}, {"namespace", "feed-A"}, {"pid", "101"},
|
|
{"process_node_id", "process_101_boot_1"}, {"process_path", "/synthetic/a"}};
|
|
auto b = a; b.id = "api-b"; b.hash = std::string(64, 'b'); b.metadata["pid"] = "102";
|
|
b.metadata["process_node_id"] = "process_102_boot_1"; b.metadata["process_path"] = "/synthetic/b";
|
|
assert(database.begin_transaction());
|
|
assert(database.store_indicator(a) && database.store_indicator(b));
|
|
assert(strawberry::CorrelationEngine::correlate(database).size() == 3); // join, never commit caller's transaction
|
|
database.rollback_transaction();
|
|
assert(database.list_indicators().empty() && database.list_findings().empty());
|
|
assert(database.store_indicator(a) && database.store_indicator(b));
|
|
assert(strawberry::CorrelationEngine::correlate(database).size() == 3);
|
|
const auto selected = strawberry::CorrelationEngine::correlate(database,
|
|
[](const std::string& hash, const std::string& path, const std::string& rule) {
|
|
return hash == std::string(64, 'a') && path == "/synthetic/a" && rule == "FullApiShared";
|
|
});
|
|
assert(selected.size() == 1);
|
|
assert(database.list_findings().size() == 1); // stale A and shared-rule finding withdrawn
|
|
assert(database.list_indicators().size() == 2);
|
|
assert(strawberry::CorrelationEngine::correlate(database).size() == 3);
|
|
assert(database.list_findings().size() == 3); // raw evidence restored after filter removal
|
|
database.close();
|
|
assert(database.open_read_only(root.string()));
|
|
assert(database.list_indicators().size() == 2 && database.list_findings().size() == 3);
|
|
database.close(); fs::remove_all(root, ec);
|
|
std::cout << "Test full_api_policy_withdrawal_and_outer_transaction: PASSED" << std::endl;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc == 2) {
|
|
if (std::string(argv[1]) == "namespace") { test_namespaced_rule_identity(); return 0; }
|
|
if (std::string(argv[1]) == "instances") { test_instance_counts_not_pid_counts(); return 0; }
|
|
if (std::string(argv[1]) == "api-migration") { test_full_api_rule_migration(false); return 0; }
|
|
if (std::string(argv[1]) == "api-split") { test_full_api_rule_migration(true); return 0; }
|
|
if (std::string(argv[1]) == "api-policy") { test_full_api_policy_withdrawal_and_outer_transaction(); return 0; }
|
|
return 2;
|
|
}
|
|
test_namespaced_rule_identity();
|
|
test_instance_counts_not_pid_counts();
|
|
test_full_api_rule_migration(false);
|
|
test_full_api_rule_migration(true);
|
|
test_full_api_policy_withdrawal_and_outer_transaction();
|
|
test_wide_finding_is_capped();
|
|
test_evidence_only_needs_another_signal();
|
|
std::cout << "Running CorrelationEngine tests..." << std::endl << std::endl;
|
|
|
|
test_process_yara_hits_emit_finding();
|
|
test_lineage_multiple_yara_hits_emit_finding();
|
|
test_same_hash_multiple_processes_emit_finding();
|
|
test_same_rule_multiple_processes_emit_finding();
|
|
|
|
std::cout << std::endl << "All tests passed!" << std::endl;
|
|
return 0;
|
|
}
|