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>
75 lines
2.5 KiB
C++
75 lines
2.5 KiB
C++
// Live eBPF behavior telemetry check (connect + ptrace). Needs root and the
|
|
// BPF object; run in a privileged container.
|
|
#include "strawberry/process_monitor.hpp"
|
|
|
|
#include <arpa/inet.h>
|
|
#include <cassert>
|
|
#include <chrono>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <mutex>
|
|
#include <netinet/in.h>
|
|
#include <signal.h>
|
|
#include <sys/ptrace.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/wait.h>
|
|
#include <thread>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
|
|
using namespace strawberry;
|
|
|
|
int main() {
|
|
if (::geteuid() != 0) {
|
|
std::cout << "SKIP: behavior_live needs root" << std::endl;
|
|
return 0;
|
|
}
|
|
::setenv("STRAWBERRY_PROCESS_MONITOR_BACKEND", "bpf", 1);
|
|
std::mutex mutex;
|
|
std::vector<BehaviorEvent> events;
|
|
ProcessMonitor monitor;
|
|
if (!monitor.initialize()) {
|
|
std::cerr << "FAIL: bpf backend unavailable" << std::endl;
|
|
return 1;
|
|
}
|
|
monitor.set_behavior_callback([&](const BehaviorEvent& e) { std::lock_guard l(mutex); events.push_back(e); });
|
|
assert(monitor.start([](const ProcessInfo&) {}));
|
|
assert(monitor.behavior_tracepoints_attached());
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
|
|
|
// connect() to 127.0.0.1:9 (discard; refused is fine, the attempt is traced).
|
|
const int fd = ::socket(AF_INET, SOCK_STREAM, 0);
|
|
sockaddr_in addr{};
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons(9);
|
|
::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
|
::connect(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
|
|
::close(fd);
|
|
|
|
// ptrace attach to a child.
|
|
const pid_t child = ::fork();
|
|
if (child == 0) {
|
|
::pause();
|
|
::_exit(0);
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
::ptrace(PTRACE_ATTACH, child, nullptr, nullptr);
|
|
::waitpid(child, nullptr, 0);
|
|
::ptrace(PTRACE_DETACH, child, nullptr, nullptr);
|
|
::kill(child, SIGKILL);
|
|
::waitpid(child, nullptr, 0);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
monitor.stop();
|
|
bool saw_connect = false;
|
|
bool saw_ptrace = false;
|
|
std::lock_guard l(mutex);
|
|
for (const auto& e : events) {
|
|
saw_connect |= e.type == BehaviorEvent::Type::connect && e.address == "127.0.0.1" && e.port == 9;
|
|
saw_ptrace |= e.type == BehaviorEvent::Type::ptrace && e.request == PTRACE_ATTACH;
|
|
}
|
|
std::cout << "events=" << events.size() << " connect=" << saw_connect << " ptrace=" << saw_ptrace << std::endl;
|
|
assert(saw_connect && saw_ptrace);
|
|
std::cout << "behavior_live: ALL PASSED" << std::endl;
|
|
return 0;
|
|
}
|