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
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>
427 lines
16 KiB
C++
427 lines
16 KiB
C++
#include "strawberry/correlation_engine.hpp"
|
|
#include "strawberry/database.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);
|
|
|
|
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
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
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;
|
|
}
|