New meson option enable_endpoint_security (auto, macOS only) links EndpointSecurity and bsm and builds three ES backends behind the existing interfaces. Helpers are in src/platform/es_support.hpp. - ProcessMonitor: NOTIFY_EXEC events (pid, parent, path, dev:ino, argv, start time) plus NOTIFY_TRACE and NOTIFY_KEXTLOAD for the behavior rules. Events go to a bounded queue (4096, overflow counted in events_dropped) drained on the monitor thread, so callbacks stay on one thread. Without an ES client the monitor polls libproc (backend "libproc"). backend_name() reports "endpoint_security". - ExecGuard: AUTH_EXEC with the fanotify guard's rules. It fails open at min(timeout, ES deadline - 500 ms) and on a full queue, and on macOS 27+ ES_DEADLINE_MISS_MODE_FAIL_OPEN makes the kernel allow a missed deadline too. Platform binaries and SIP paths are never denied, and our own execs are muted. ES does not cache verdicts, so rule changes apply on the next exec. Our own cache keys on dev:ino:size:mtime:generation. The image is opened by path and its dev:ino checked against the event; a swapped file counts as unscanned. - FileWatcher: NOTIFY_CREATE/RENAME/CLOSE(modified) with inverted target-path muting: named entries selected literally, whole directories by prefix. macos_watch_specs() covers launchd agents/daemons, periodic jobs, cron tabs, sudo drop-ins, shell rc files, authorized_keys, Downloads and the temp directories. match_watch_spec() (direct children, as with fanotify directory marks) is portable and tested. Home dirs come from the directory service (local_home_dirs). Every ES handler is noexcept and records swallowed exceptions. Client errors name the missing requirement (root, entitlement, Full Disk Access), and the daemon keeps running detection-only. macos_es_live (root, ES entitlement) checks deny/allow/cache/ platform-binary/fail-open/audit for the guard, exec telemetry and the file watcher, using small signed payload binaries. It has not run yet: the development Mac has SIP enabled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
278 lines
12 KiB
C++
278 lines
12 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 <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::unique_ptr<BehaviorEngine> engine;
|
|
|
|
Harness() {
|
|
BehaviorEngineOptions o;
|
|
o.resolve = [this](int pid) -> std::optional<ProcessRef> {
|
|
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); };
|
|
engine = std::make_unique<BehaviorEngine>(o);
|
|
}
|
|
|
|
void add(int pid, const std::string& exe) { processes[pid] = ProcessRef{pid, "process_" + std::to_string(pid) + "_x", exe}; }
|
|
};
|
|
|
|
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
|
|
|
|
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");
|
|
|
|
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
|
|
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.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;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
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;
|
|
}
|