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.
395 lines
18 KiB
C++
395 lines
18 KiB
C++
#include "strawberry/behavior_engine.hpp"
|
|
#include "strawberry/file_watch.hpp"
|
|
#include "strawberry/ioc_set.hpp"
|
|
|
|
#include <cassert>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <set>
|
|
#include <vector>
|
|
|
|
#include <unistd.h>
|
|
|
|
namespace fs = std::filesystem;
|
|
using namespace strawberry;
|
|
|
|
namespace {
|
|
|
|
constexpr uint64_t kSecond = 1'000'000'000ULL;
|
|
|
|
struct Harness {
|
|
std::map<int, ProcessRef> processes;
|
|
std::map<int, bool> flagged;
|
|
std::map<std::string, std::string> ioc;
|
|
std::vector<BehaviorDetection> out;
|
|
std::size_t resolve_calls = 0;
|
|
std::set<std::string> expected; // installer-written files (expected_write)
|
|
std::unique_ptr<BehaviorEngine> engine;
|
|
|
|
Harness() {
|
|
BehaviorEngineOptions o;
|
|
o.resolve = [this](int pid) -> std::optional<ProcessRef> {
|
|
++resolve_calls;
|
|
const auto it = processes.find(pid);
|
|
return it == processes.end() ? std::nullopt : std::optional<ProcessRef>(it->second);
|
|
};
|
|
o.is_flagged = [this](int pid) { return flagged[pid]; };
|
|
o.ioc_ip = [this](const std::string& ip) -> std::optional<std::string> {
|
|
const auto it = ioc.find(ip);
|
|
return it == ioc.end() ? std::nullopt : std::optional<std::string>(it->second);
|
|
};
|
|
o.emit = [this](const BehaviorDetection& d) { out.push_back(d); };
|
|
o.expected_write = [this](const std::string& path) { return expected.count(path) > 0; };
|
|
engine = std::make_unique<BehaviorEngine>(o);
|
|
}
|
|
|
|
void add(int pid, const std::string& exe, int ppid = -1) {
|
|
ProcessRef ref{pid, "process_" + std::to_string(pid) + "_x", exe};
|
|
ref.ppid = ppid;
|
|
processes[pid] = ref;
|
|
}
|
|
|
|
void observe_exec(int pid, uint64_t start, uint64_t at) {
|
|
ProcessInfo info;
|
|
info.pid = pid;
|
|
info.executable_path = processes.at(pid).exe;
|
|
info.start_time_ticks = start;
|
|
info.boot_id = "boot";
|
|
info.event_timestamp_ns = at;
|
|
processes.at(pid).node_id = process_node_id(info);
|
|
engine->on_exec(info);
|
|
}
|
|
};
|
|
|
|
BehaviorEvent connect(int pid, const std::string& ip, uint64_t at, uint16_t port = 443) {
|
|
BehaviorEvent e;
|
|
e.type = BehaviorEvent::Type::connect;
|
|
e.pid = pid;
|
|
e.address = ip;
|
|
e.port = port;
|
|
e.timestamp_ns = at;
|
|
return e;
|
|
}
|
|
|
|
void test_network_rules() {
|
|
std::cout << "Test network rules:" << std::endl;
|
|
Harness h;
|
|
h.add(10, "/usr/bin/curl");
|
|
h.add(11, "/tmp/implant");
|
|
h.ioc["203.0.113.9"] = "feodo-c2";
|
|
|
|
h.engine->on_behavior(connect(10, "93.184.216.34", 1 * kSecond));
|
|
assert(h.out.empty()); // ordinary connection
|
|
assert(h.resolve_calls == 0); // identity repair adds no per-connect procfs work
|
|
|
|
h.engine->on_behavior(connect(10, "203.0.113.9", 2 * kSecond));
|
|
assert(h.out.size() == 1 && h.out[0].rule == "IOC_Connection" && h.out[0].severity == "high");
|
|
assert(h.out[0].metadata.at("feed") == "feodo-c2" && h.out[0].related_id == "network_203.0.113.9:443");
|
|
h.engine->on_behavior(connect(10, "203.0.113.9", 3 * kSecond));
|
|
assert(h.out.size() == 1); // suppressed repeat
|
|
// Out-of-order delivery (eBPF ring buffer, events from other CPUs): an
|
|
// older timestamp is still a repeat. Unsigned subtraction used to wrap here
|
|
// and re-emit the alert (found by fold int-overflow).
|
|
h.ioc["203.0.113.77"] = "feodo-c2";
|
|
h.engine->on_behavior(connect(10, "203.0.113.77", 10 * kSecond));
|
|
assert(h.out.size() == 2);
|
|
h.engine->on_behavior(connect(10, "203.0.113.77", 9 * kSecond + kSecond / 2));
|
|
assert(h.out.size() == 2);
|
|
h.out.pop_back();
|
|
|
|
h.flagged[11] = true;
|
|
h.engine->on_behavior(connect(11, "198.51.100.7", 4 * kSecond, 8080));
|
|
assert(h.out.size() == 2 && h.out[1].rule == "FlaggedProcessNetwork");
|
|
assert(h.out[1].metadata.at("remote_port") == "8080");
|
|
|
|
h.engine->on_behavior(connect(99, "203.0.113.9", 5 * kSecond)); // process already gone
|
|
assert(h.out.size() == 2);
|
|
assert(h.engine->stats().connects == 7);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_ptrace_and_modules() {
|
|
std::cout << "Test ptrace and module rules:" << std::endl;
|
|
Harness h;
|
|
h.add(20, "/usr/bin/gdb");
|
|
h.add(21, "/home/u/.local/bin/injector");
|
|
h.add(22, "/usr/bin/sshd");
|
|
h.add(23, "/usr/bin/modprobe");
|
|
h.add(24, "/tmp/rootkit-loader");
|
|
BehaviorEvent p;
|
|
p.type = BehaviorEvent::Type::ptrace;
|
|
p.target_pid = 22;
|
|
p.timestamp_ns = kSecond;
|
|
p.pid = 20;
|
|
h.engine->on_behavior(p);
|
|
assert(h.out.empty()); // debugger
|
|
p.pid = 21;
|
|
h.engine->on_behavior(p);
|
|
assert(h.out.size() == 1 && h.out[0].rule == "PtraceAttach" && h.out[0].severity == "medium");
|
|
assert(h.out[0].related_id == "process_22_x" && h.out[0].metadata.at("target_path") == "/usr/bin/sshd");
|
|
|
|
// Same binary, parent/child (LeakSanitizer's tracer attaching to its
|
|
// parent): low. Same binary without the family link, or family with a
|
|
// different binary, stays medium.
|
|
h.add(30, "/home/u/build/fuzz_target", 1);
|
|
h.add(31, "/home/u/build/fuzz_target", 30);
|
|
h.add(32, "/home/u/build/fuzz_target", 1);
|
|
h.add(33, "/home/u/.local/bin/injector", 30);
|
|
p.pid = 31;
|
|
p.target_pid = 30;
|
|
h.engine->on_behavior(p);
|
|
assert(h.out.size() == 2 && h.out[1].severity == "low");
|
|
p.pid = 32;
|
|
h.engine->on_behavior(p);
|
|
assert(h.out.size() == 3 && h.out[2].severity == "medium");
|
|
p.pid = 33;
|
|
h.engine->on_behavior(p);
|
|
assert(h.out.size() == 4 && h.out[3].severity == "medium");
|
|
h.out.resize(1);
|
|
|
|
BehaviorEvent m;
|
|
m.type = BehaviorEvent::Type::module_load;
|
|
m.timestamp_ns = 2 * kSecond;
|
|
m.pid = 23;
|
|
h.engine->on_behavior(m);
|
|
assert(h.out.size() == 1); // modprobe is normal
|
|
m.pid = 24;
|
|
h.engine->on_behavior(m);
|
|
assert(h.out.size() == 2 && h.out[1].rule == "KernelModuleLoad" && h.out[1].severity == "high"); // runs from /tmp
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_persistence() {
|
|
std::cout << "Test persistence writes:" << std::endl;
|
|
Harness h;
|
|
h.add(30, "/usr/bin/pacman");
|
|
h.add(31, "/usr/bin/python3.12");
|
|
h.add(32, "/dev/shm/x");
|
|
h.add(33, "/usr/bin/rustup");
|
|
FileWriteEvent w{30, "/etc/systemd/system/foo.service", "persistence", "systemd unit", true, kSecond};
|
|
h.engine->on_file_write(w);
|
|
assert(h.out.empty()); // package manager
|
|
w.pid = 31;
|
|
h.engine->on_file_write(w);
|
|
assert(h.out.size() == 1 && h.out[0].rule == "PersistenceWrite" && h.out[0].severity == "medium");
|
|
assert(h.out[0].metadata.at("location") == "systemd unit" && h.out[0].relationship == "wrote_persistence");
|
|
FileWriteEvent rc{33, "/home/u/.bashrc", "persistence", "shell rc file", false, 2 * kSecond};
|
|
h.engine->on_file_write(rc);
|
|
assert(h.out.size() == 2 && h.out[1].severity == "low");
|
|
rc.pid = 32;
|
|
h.engine->on_file_write(rc);
|
|
assert(h.out.size() == 3 && h.out[2].severity == "high"); // writer runs from /dev/shm
|
|
|
|
// Strawberry's installer writing a unit with the shipped content is
|
|
// evidence only; the same writer from a suspicious image is not.
|
|
h.add(34, "/usr/bin/install");
|
|
h.expected.insert("/etc/systemd/system/strawberry.service");
|
|
FileWriteEvent own{34, "/etc/systemd/system/strawberry.service", "persistence", "systemd unit", true, 3 * kSecond};
|
|
h.engine->on_file_write(own);
|
|
assert(h.out.size() == 4 && h.out[3].metadata.at("evidence_only") == "true" && h.out[3].severity == "low");
|
|
FileWriteEvent other{34, "/etc/systemd/system/evil.service", "persistence", "systemd unit", true, 4 * kSecond};
|
|
h.engine->on_file_write(other);
|
|
assert(h.out.size() == 5 && !h.out[4].metadata.contains("evidence_only") && h.out[4].severity == "medium");
|
|
own.pid = 32; // /dev/shm writer: never evidence-only
|
|
own.timestamp_ns = 5 * kSecond;
|
|
h.engine->on_file_write(own);
|
|
assert(h.out.size() == 6 && !h.out[5].metadata.contains("evidence_only") && h.out[5].severity == "high");
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_download_then_execute() {
|
|
std::cout << "Test download-then-execute:" << std::endl;
|
|
Harness h;
|
|
h.add(40, "/usr/bin/curl");
|
|
h.add(41, "/usr/lib/firefox/firefox");
|
|
h.add(42, "/usr/bin/cp");
|
|
h.observe_exec(40, 1, 90 * kSecond);
|
|
h.observe_exec(41, 1, 190 * kSecond);
|
|
h.observe_exec(42, 1, 290 * kSecond);
|
|
|
|
h.engine->on_behavior(connect(40, "198.51.100.1", 100 * kSecond));
|
|
h.engine->on_file_write({40, "/tmp/payload", "drop", "temporary directory", true, 101 * kSecond});
|
|
ProcessInfo exec;
|
|
exec.pid = 50;
|
|
exec.executable_path = "/tmp/payload";
|
|
exec.event_timestamp_ns = 130 * kSecond;
|
|
h.engine->on_exec(exec);
|
|
assert(h.out.size() == 1 && h.out[0].rule == "DownloadThenExecute" && h.out[0].severity == "high");
|
|
assert(h.out[0].metadata.at("writer_path") == "/usr/bin/curl" && h.out[0].metadata.at("remote_address") == "198.51.100.1");
|
|
|
|
// Browser download: medium.
|
|
h.engine->on_behavior(connect(41, "203.0.113.2", 200 * kSecond));
|
|
h.engine->on_file_write({41, "/home/u/Downloads/app.AppImage", "drop", "downloads", false, 210 * kSecond});
|
|
exec.pid = 51;
|
|
exec.executable_path = "/home/u/Downloads/app.AppImage";
|
|
exec.event_timestamp_ns = 220 * kSecond;
|
|
h.engine->on_exec(exec);
|
|
assert(h.out.size() == 2 && h.out[1].severity == "medium");
|
|
|
|
// Writer with no network activity: not a download.
|
|
h.engine->on_file_write({42, "/tmp/local-copy", "drop", "temporary directory", true, 300 * kSecond});
|
|
exec.pid = 52;
|
|
exec.executable_path = "/tmp/local-copy";
|
|
exec.event_timestamp_ns = 301 * kSecond;
|
|
h.engine->on_exec(exec);
|
|
assert(h.out.size() == 2);
|
|
|
|
// Too late (outside the window).
|
|
h.engine->on_behavior(connect(40, "198.51.100.1", 400 * kSecond));
|
|
h.engine->on_file_write({40, "/tmp/old", "drop", "temporary directory", true, 401 * kSecond});
|
|
exec.pid = 53;
|
|
exec.executable_path = "/tmp/old";
|
|
exec.event_timestamp_ns = 401 * kSecond + 3600 * kSecond;
|
|
h.engine->on_exec(exec);
|
|
assert(h.out.size() == 2);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_ioc_net_set_and_watch_specs() {
|
|
std::cout << "Test IOC network set and watch specs:" << std::endl;
|
|
const auto dir = fs::temp_directory_path() / ("sb-behavior-" + std::to_string(::getpid()));
|
|
fs::create_directories(dir);
|
|
std::ofstream(dir / "ips.txt") << "203.0.113.9\n2001:db8::5\n";
|
|
// Malformed or catch-all prefixes must be dropped: a /0 (or "/x", which
|
|
// atoi reads as 0) would match every address and flag every connection.
|
|
std::ofstream(dir / "nets.txt") << "198.51.100.0/24\n2001:db8:1::/48\n"
|
|
<< "192.0.2.1/x\n192.0.2.2/0\n192.0.2.3/33\n192.0.2.4/-8\n2001:db8:2::/129\n10.0.0.0/\n"
|
|
<< "128.0.0.0/1\n2000::/3\n";
|
|
IocNetSet set;
|
|
set.load({{"feodo", (dir / "ips.txt").string()}}, {{"drop", (dir / "nets.txt").string()}});
|
|
assert(set.size() == 4);
|
|
assert(set.find("203.0.113.9") == std::optional<std::string>("feodo"));
|
|
assert(set.find("198.51.100.200") == std::optional<std::string>("drop"));
|
|
assert(set.find("2001:db8:1:ff::1") == std::optional<std::string>("drop"));
|
|
assert(set.find("2001:db8::5") == std::optional<std::string>("feodo"));
|
|
assert(!set.find("198.51.101.1"));
|
|
assert(!set.find("not-an-ip"));
|
|
fs::remove_all(dir);
|
|
|
|
const auto homes = home_dirs_from_passwd("root:x:0:0::/root:/bin/bash\nu:x:1000:1000::/home/u:/bin/zsh\n"
|
|
"svc:x:999:999::/var/lib/svc:/usr/bin/nologin\nbad\n");
|
|
assert((homes == std::vector<std::string>{"/root", "/home/u"}));
|
|
const auto specs = default_watch_specs({"/home/u"});
|
|
bool saw_authorized_keys = false;
|
|
bool saw_downloads = false;
|
|
for (const auto& spec : specs) {
|
|
saw_authorized_keys |= spec.dir == "/home/u/.ssh" && spec.category == "persistence";
|
|
saw_downloads |= spec.dir == "/home/u/Downloads" && spec.category == "drop";
|
|
}
|
|
assert(saw_authorized_keys && saw_downloads);
|
|
assert(BehaviorEngine::is_drop_location("/dev/shm/x") && BehaviorEngine::is_drop_location("/home/u/Downloads/a"));
|
|
assert(!BehaviorEngine::is_drop_location("/usr/bin/x"));
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_watch_matching_and_macos_specs() {
|
|
std::cout << "Test watch spec matching and the macOS watch list:" << std::endl;
|
|
const auto specs = macos_watch_specs({"/Users/u"});
|
|
// Whole directory: any direct child.
|
|
const auto* agent = match_watch_spec(specs, "/Users/u/Library/LaunchAgents/com.evil.plist");
|
|
assert(agent && agent->category == "persistence" && !agent->system_scope);
|
|
const auto* daemon = match_watch_spec(specs, "/Library/LaunchDaemons/x.plist");
|
|
assert(daemon && daemon->label == "launch daemon" && daemon->system_scope);
|
|
// Named entries only.
|
|
assert(match_watch_spec(specs, "/Users/u/.zshrc"));
|
|
assert(!match_watch_spec(specs, "/Users/u/notes.txt"));
|
|
assert(match_watch_spec(specs, "/Users/u/.ssh/authorized_keys"));
|
|
assert(!match_watch_spec(specs, "/Users/u/.ssh/known_hosts"));
|
|
// Direct children only, like fanotify directory marks.
|
|
assert(!match_watch_spec(specs, "/Library/LaunchDaemons/sub/x.plist"));
|
|
const auto* download = match_watch_spec(specs, "/Users/u/Downloads/a.dmg");
|
|
assert(download && download->category == "drop");
|
|
// Malformed paths.
|
|
assert(!match_watch_spec(specs, ""));
|
|
assert(!match_watch_spec(specs, "/Users/u/Downloads/"));
|
|
assert(!match_watch_spec(specs, "no-slash"));
|
|
// The Linux list still matches Linux paths.
|
|
const auto linux_specs = default_watch_specs({"/home/u"});
|
|
assert(match_watch_spec(linux_specs, "/home/u/Downloads/x"));
|
|
assert(!match_watch_spec(linux_specs, "/Users/u/Downloads/x"));
|
|
#if defined(__APPLE__)
|
|
assert(BehaviorEngine::is_drop_location("/private/tmp/x") && BehaviorEngine::is_drop_location("/Users/u/Downloads/a"));
|
|
assert(!BehaviorEngine::is_drop_location("/Applications/x"));
|
|
const auto homes = local_home_dirs();
|
|
assert(!homes.empty() && homes.front() == "/var/root");
|
|
for (std::size_t i = 1; i < homes.size(); ++i) {
|
|
assert(homes[i].starts_with("/Users/"));
|
|
}
|
|
#endif
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_network_history_is_instance_bound() {
|
|
Harness h;
|
|
h.add(70, "/usr/bin/network-client");
|
|
h.observe_exec(70, 1, 90 * kSecond);
|
|
h.engine->on_behavior(connect(70, "198.51.100.1", 100 * kSecond));
|
|
// PID reused before its new exec has reached the behavior worker.
|
|
h.add(70, "/usr/bin/local-copy");
|
|
h.processes.at(70).node_id = "process_70_boot_2";
|
|
h.engine->on_file_write({70, "/tmp/new-instance", "drop", "temporary directory", true, 110 * kSecond});
|
|
ProcessInfo payload;
|
|
payload.pid = 80; payload.executable_path = "/tmp/new-instance"; payload.event_timestamp_ns = 111 * kSecond;
|
|
h.engine->on_exec(payload);
|
|
assert(h.out.empty());
|
|
// Even after the new instance is known, a delayed OLD connect must not join it.
|
|
h.observe_exec(70, 2, 109 * kSecond);
|
|
h.engine->on_behavior(connect(70, "198.51.100.1", 100 * kSecond));
|
|
h.engine->on_file_write({70, "/tmp/delayed-event", "drop", "temporary directory", true, 115 * kSecond});
|
|
payload.executable_path = "/tmp/delayed-event"; payload.event_timestamp_ns = 116 * kSecond;
|
|
h.engine->on_exec(payload);
|
|
assert(h.out.empty());
|
|
ProcessInfo delayed_exec;
|
|
delayed_exec.pid = 70; delayed_exec.executable_path = "/usr/bin/network-client";
|
|
delayed_exec.start_time_ticks = 1; delayed_exec.boot_id = "boot"; delayed_exec.event_timestamp_ns = 90 * kSecond;
|
|
h.engine->on_exec(delayed_exec); // old scan worker cannot rewind the new observed lifetime
|
|
// New-instance connection is usable, so the rule is not disabled wholesale.
|
|
h.engine->on_behavior(connect(70, "198.51.100.2", 120 * kSecond));
|
|
h.engine->on_file_write({70, "/tmp/genuine-download", "drop", "temporary directory", true, 121 * kSecond});
|
|
payload.executable_path = "/tmp/genuine-download"; payload.event_timestamp_ns = 122 * kSecond;
|
|
h.engine->on_exec(payload);
|
|
assert(h.out.size() == 1 && h.out.front().rule == "DownloadThenExecute");
|
|
assert(h.out.front().metadata.at("writer_node_id") == "process_70_boot_2");
|
|
// No observed stable exec: ordinary connections cannot establish a lifetime.
|
|
h.add(71, "/usr/bin/unknown-instance");
|
|
h.engine->on_behavior(connect(71, "198.51.100.3", 130 * kSecond));
|
|
h.engine->on_file_write({71, "/tmp/unknown-instance", "drop", "temporary directory", true, 131 * kSecond});
|
|
payload.executable_path = "/tmp/unknown-instance"; payload.event_timestamp_ns = 132 * kSecond;
|
|
h.engine->on_exec(payload);
|
|
assert(h.out.size() == 1);
|
|
const auto calls_before_churn = h.resolve_calls;
|
|
// Observe more instances than the retained-window cap. No resolutions or
|
|
// clear-all behavior is needed; the newest valid join remains usable.
|
|
for (int pid = 1000; pid < 10000; ++pid) {
|
|
ProcessInfo observed;
|
|
observed.pid = pid; observed.start_time_ticks = 1; observed.boot_id = "boot";
|
|
observed.executable_path = "/synthetic/writer"; observed.event_timestamp_ns = 190 * kSecond;
|
|
h.engine->observe_process(observed);
|
|
h.engine->on_behavior(connect(pid, "198.51.100.4", 200 * kSecond));
|
|
}
|
|
assert(h.resolve_calls == calls_before_churn);
|
|
h.processes[9999] = {9999, "process_9999_boot_1", "/synthetic/writer"};
|
|
h.engine->on_file_write({9999, "/tmp/latest-known", "drop", "temporary directory", true, 201 * kSecond});
|
|
payload.pid = 81; payload.boot_id = "boot"; payload.start_time_ticks = 1;
|
|
payload.executable_path = "/tmp/latest-known"; payload.event_timestamp_ns = 202 * kSecond;
|
|
h.engine->on_exec(payload); // distinct executed lifetime: do not hit the earlier positive's DTE suppression
|
|
assert(h.out.size() == 2 && h.out.back().metadata.at("writer_node_id") == "process_9999_boot_1");
|
|
std::cout << "Test network_history_is_instance_bound: PASSED" << std::endl;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
test_network_history_is_instance_bound();
|
|
std::cout << "Running behavior engine tests..." << std::endl << std::endl;
|
|
test_network_rules();
|
|
test_ptrace_and_modules();
|
|
test_persistence();
|
|
test_download_then_execute();
|
|
test_ioc_net_set_and_watch_specs();
|
|
test_watch_matching_and_macos_specs();
|
|
std::cout << std::endl << "All tests passed!" << std::endl;
|
|
return 0;
|
|
}
|