#include "strawberry/file_watch.hpp" #include "strawberry/parse_num.hpp" #include "strawberry/thread_entry.hpp" #include #include #include #include #include #include #include #include #include #include #include #if defined(__linux__) #include #endif #include #if defined(__APPLE__) #include #endif #if defined(__APPLE__) && STRAWBERRY_HAVE_ENDPOINT_SECURITY #include "platform/es_support.hpp" #include "strawberry/swallowed.hpp" #endif namespace strawberry { namespace fs = std::filesystem; std::vector default_watch_specs(const std::vector& home_dirs) { std::vector specs{ {"/etc/systemd/system", "persistence", "systemd unit", {}, true}, {"/etc/systemd/user", "persistence", "systemd user unit", {}, true}, {"/usr/lib/systemd/system", "persistence", "systemd unit", {}, true}, {"/etc/cron.d", "persistence", "cron job", {}, true}, {"/etc/cron.hourly", "persistence", "cron job", {}, true}, {"/etc/cron.daily", "persistence", "cron job", {}, true}, {"/etc/cron.weekly", "persistence", "cron job", {}, true}, {"/etc/cron.monthly", "persistence", "cron job", {}, true}, {"/var/spool/cron", "persistence", "user crontab", {}, true}, {"/var/spool/cron/crontabs", "persistence", "user crontab", {}, true}, {"/etc/profile.d", "persistence", "login script", {}, true}, {"/etc/sudoers.d", "persistence", "sudoers", {}, true}, {"/etc/xdg/autostart", "persistence", "desktop autostart", {}, true}, {"/etc/udev/rules.d", "persistence", "udev rule", {}, true}, {"/etc", "persistence", "system startup file", {"ld.so.preload", "crontab", "sudoers", "rc.local", "profile", "bash.bashrc", "zshrc", "environment"}, true}, {"/tmp", "drop", "temporary directory", {}, true}, {"/var/tmp", "drop", "temporary directory", {}, true}, {"/dev/shm", "drop", "shared memory", {}, true}, }; for (const auto& home : home_dirs) { specs.push_back({home + "/.config/systemd/user", "persistence", "systemd user unit", {}, false}); specs.push_back({home + "/.config/autostart", "persistence", "desktop autostart", {}, false}); specs.push_back({home + "/.ssh", "persistence", "ssh authorized_keys", {"authorized_keys", "authorized_keys2", "rc"}, false}); specs.push_back({home, "persistence", "shell rc file", {".bashrc", ".bash_profile", ".bash_login", ".profile", ".zshrc", ".zprofile", ".zshenv", ".xprofile", ".xinitrc"}, false}); specs.push_back({home + "/Downloads", "drop", "downloads", {}, false}); } return specs; } std::vector macos_watch_specs(const std::vector& home_dirs) { std::vector specs{ {"/Library/LaunchDaemons", "persistence", "launch daemon", {}, true}, {"/Library/LaunchAgents", "persistence", "launch agent", {}, true}, {"/Library/StartupItems", "persistence", "startup item", {}, true}, {"/Library/Security/SecurityAgentPlugins", "persistence", "authorization plugin", {}, true}, {"/private/etc/periodic/daily", "persistence", "periodic job", {}, true}, {"/private/etc/periodic/weekly", "persistence", "periodic job", {}, true}, {"/private/etc/periodic/monthly", "persistence", "periodic job", {}, true}, {"/private/var/at/tabs", "persistence", "user crontab", {}, true}, {"/usr/lib/cron/tabs", "persistence", "user crontab", {}, true}, {"/private/etc/sudoers.d", "persistence", "sudoers", {}, true}, {"/private/etc", "persistence", "system startup file", {"sudoers", "profile", "zshrc", "zprofile", "bashrc", "rc.common", "launchd.conf", "crontab", "periodic.conf"}, true}, {"/private/tmp", "drop", "temporary directory", {}, true}, {"/private/var/tmp", "drop", "temporary directory", {}, true}, {"/Users/Shared", "drop", "shared folder", {}, true}, }; for (const auto& home : home_dirs) { specs.push_back({home + "/Library/LaunchAgents", "persistence", "launch agent", {}, false}); specs.push_back({home + "/.ssh", "persistence", "ssh authorized_keys", {"authorized_keys", "authorized_keys2", "rc"}, false}); specs.push_back({home, "persistence", "shell rc file", {".bashrc", ".bash_profile", ".bash_login", ".profile", ".zshrc", ".zprofile", ".zshenv", ".zlogin"}, false}); specs.push_back({home + "/Downloads", "drop", "downloads", {}, false}); } return specs; } std::vector platform_watch_specs(const std::vector& home_dirs) { #if defined(__APPLE__) return macos_watch_specs(home_dirs); #else return default_watch_specs(home_dirs); #endif } const WatchSpec* match_watch_spec(const std::vector& specs, const std::string& path) { const auto slash = path.rfind('/'); if (slash == std::string::npos || slash + 1 >= path.size()) { return nullptr; } const std::string dir = slash == 0 ? std::string("/") : path.substr(0, slash); const std::string name = path.substr(slash + 1); for (const auto& spec : specs) { if (spec.dir != dir) { continue; } if (spec.names.empty() || std::find(spec.names.begin(), spec.names.end(), name) != spec.names.end()) { return &spec; } } return nullptr; } #if defined(__APPLE__) std::vector local_home_dirs() { std::vector homes{"/var/root"}; ::setpwent(); while (const passwd* entry = ::getpwent()) { if (entry->pw_uid >= 501 && entry->pw_uid < 60000 && entry->pw_dir != nullptr && std::string_view(entry->pw_dir).starts_with("/Users/")) { homes.emplace_back(entry->pw_dir); } } ::endpwent(); std::sort(homes.begin() + 1, homes.end()); homes.erase(std::unique(homes.begin(), homes.end()), homes.end()); return homes; } #endif std::vector home_dirs_from_passwd(const std::string& passwd_text) { std::vector homes{"/root"}; std::istringstream input(passwd_text); std::string line; while (std::getline(input, line)) { std::vector fields; std::stringstream ss(line); std::string field; while (std::getline(ss, field, ':')) { fields.push_back(field); } if (fields.size() < 7) { continue; } { const int uid = parse_number(fields[2]).value_or(-1); if (uid >= 1000 && uid < 60000 && fields[5].starts_with("/home/")) { homes.push_back(fields[5]); } } } return homes; } #if defined(__linux__) class FileWatcher::Impl { public: ~Impl() { stop(); } bool start(std::vector specs, std::function on_event) { stop(); on_event_ = std::move(on_event); fan_fd_ = ::fanotify_init(FAN_CLASS_NOTIF | FAN_CLOEXEC | FAN_NONBLOCK | FAN_REPORT_DFID_NAME, O_RDONLY | O_CLOEXEC | O_LARGEFILE); if (fan_fd_ < 0) { error_ = std::string("fanotify_init (DFID_NAME) failed: ") + std::strerror(errno); return false; } for (auto& spec : specs) { std::error_code ec; if (!fs::is_directory(spec.dir, ec)) { continue; } const auto key = handle_key(spec.dir); if (key.empty()) { continue; } const uint64_t mask = FAN_CREATE | FAN_MOVED_TO | FAN_CLOSE_WRITE | FAN_EVENT_ON_CHILD; if (::fanotify_mark(fan_fd_, FAN_MARK_ADD, mask, AT_FDCWD, spec.dir.c_str()) != 0) { continue; } specs_by_handle_.emplace(key, std::move(spec)); } if (specs_by_handle_.empty()) { error_ = "no watch directories could be marked"; ::close(fan_fd_); fan_fd_ = -1; return false; } running_ = true; thread_ = std::thread(run_guarded("file-watch", [this]() { loop(); })); std::cout << "[FileWatcher] Watching " << specs_by_handle_.size() << " persistence/drop location(s)" << std::endl; return true; } void stop() { if (running_.exchange(false) && thread_.joinable()) { thread_.join(); } if (fan_fd_ >= 0) { ::close(fan_fd_); fan_fd_ = -1; } specs_by_handle_.clear(); } bool active() const { return running_; } std::size_t watched() const { return specs_by_handle_.size(); } const std::string& error() const { return error_; } private: // Directory identity: handle type + opaque handle bytes. static std::string handle_key(const std::string& dir) { struct { file_handle handle; unsigned char bytes[MAX_HANDLE_SZ]; } buffer{}; buffer.handle.handle_bytes = MAX_HANDLE_SZ; int mount_id = 0; if (::name_to_handle_at(AT_FDCWD, dir.c_str(), &buffer.handle, &mount_id, 0) != 0) { return {}; } return std::to_string(buffer.handle.handle_type) + ":" + std::string(reinterpret_cast(buffer.handle.f_handle), buffer.handle.handle_bytes); } void loop() { alignas(fanotify_event_metadata) char buffer[16384]; while (running_) { pollfd pfd{fan_fd_, POLLIN, 0}; if (::poll(&pfd, 1, 200) <= 0 || (pfd.revents & POLLIN) == 0) { continue; } const ssize_t length = ::read(fan_fd_, buffer, sizeof(buffer)); if (length <= 0) { continue; } auto* metadata = reinterpret_cast(buffer); ssize_t remaining = length; while (FAN_EVENT_OK(metadata, remaining)) { handle(metadata); metadata = FAN_EVENT_NEXT(metadata, remaining); } } } void handle(const fanotify_event_metadata* metadata) { if (metadata->vers != FANOTIFY_METADATA_VERSION || (metadata->mask & FAN_ONDIR) != 0) { return; } auto* info = reinterpret_cast(metadata + 1); if (info->hdr.info_type != FAN_EVENT_INFO_TYPE_DFID_NAME) { return; } const auto* handle = reinterpret_cast(info->handle); const std::string key = std::to_string(handle->handle_type) + ":" + std::string(reinterpret_cast(handle->f_handle), handle->handle_bytes); const char* name = reinterpret_cast(handle->f_handle) + handle->handle_bytes; const auto it = specs_by_handle_.find(key); if (it == specs_by_handle_.end() || name == nullptr || name[0] == '\0') { return; } const auto& spec = it->second; const std::string entry(name); if (!spec.names.empty() && std::find(spec.names.begin(), spec.names.end(), entry) == spec.names.end()) { return; } // Drop dirs: only completed writes matter (executables written there). if (spec.category == "drop" && (metadata->mask & (FAN_CLOSE_WRITE | FAN_MOVED_TO)) == 0) { return; } FileWriteEvent event; event.pid = metadata->pid; event.path = spec.dir + "/" + entry; event.category = spec.category; event.label = spec.label; event.system_scope = spec.system_scope; event.timestamp_ns = static_cast(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count()); if (on_event_) { try { on_event_(event); } catch (const std::exception& e) { std::cerr << "[FileWatcher] handler failed: " << e.what() << std::endl; } } } int fan_fd_ = -1; std::atomic running_{false}; std::thread thread_; std::map specs_by_handle_; std::function on_event_; std::string error_; }; #elif defined(__APPLE__) && STRAWBERRY_HAVE_ENDPOINT_SECURITY // macOS: Endpoint Security notify events. Target-path muting is inverted, so // only events under the watched locations reach the handler: named entries // are selected literally, whole directories by prefix. class FileWatcher::Impl { public: ~Impl() { stop(); } bool start(std::vector specs, std::function on_event) { stop(); on_event_ = std::move(on_event); error_.clear(); specs_.clear(); for (auto& spec : specs) { // ES reports real paths (/etc is /private/etc). std::error_code ec; const auto real = fs::weakly_canonical(spec.dir, ec); if (!ec && !real.empty()) { spec.dir = real.string(); } while (spec.dir.size() > 1 && spec.dir.back() == '/') { spec.dir.pop_back(); } specs_.push_back(std::move(spec)); } const auto result = client_.create(^(es_client_t*, const es_message_t* message) { handle(message); }); if (result != ES_NEW_CLIENT_RESULT_SUCCESS) { error_ = std::string("Endpoint Security client failed: ") + es::new_client_error(result); return false; } es_client_t* client = client_.get(); (void) ::es_unmute_all_target_paths(client); if (::es_invert_muting(client, ES_MUTE_INVERSION_TYPE_TARGET_PATH) != ES_RETURN_SUCCESS) { error_ = "es_invert_muting(TARGET_PATH) failed"; client_.reset(); return false; } std::size_t selected = 0; for (const auto& spec : specs_) { const std::string base = spec.dir == "/" ? std::string("/") : spec.dir + "/"; if (spec.names.empty()) { selected += ::es_mute_path(client, base.c_str(), ES_MUTE_PATH_TYPE_TARGET_PREFIX) == ES_RETURN_SUCCESS; continue; } bool any = false; for (const auto& name : spec.names) { any |= ::es_mute_path(client, (base + name).c_str(), ES_MUTE_PATH_TYPE_TARGET_LITERAL) == ES_RETURN_SUCCESS; } selected += any; } const es_event_type_t events[] = {ES_EVENT_TYPE_NOTIFY_CREATE, ES_EVENT_TYPE_NOTIFY_RENAME, ES_EVENT_TYPE_NOTIFY_CLOSE}; if (::es_subscribe(client, events, 3) != ES_RETURN_SUCCESS) { error_ = "es_subscribe(CREATE/RENAME/CLOSE) failed"; client_.reset(); return false; } watched_ = selected; active_ = true; std::cout << "[FileWatcher] Watching " << selected << " location(s) (Endpoint Security)" << std::endl; return true; } void stop() { active_ = false; client_.reset(); // waits for in-flight handlers } bool active() const { return active_; } std::size_t watched() const { return watched_; } const std::string& error() const { return error_; } private: static std::string destination_path(es_destination_type_t type, const es_file_t* existing, const es_file_t* dir, const es_string_token_t& filename) { if (type == ES_DESTINATION_TYPE_EXISTING_FILE) { return es::file_path(existing); } return es::file_path(dir) + "/" + es::to_string(filename); } // Runs on the ES queue: never throws. void handle(const es_message_t* message) noexcept { try { std::string path; bool completed = false; // content is in place (closed after writing, or renamed in) switch (message->event_type) { case ES_EVENT_TYPE_NOTIFY_CREATE: { const auto& e = message->event.create; path = destination_path(e.destination_type, e.destination.existing_file, e.destination.new_path.dir, e.destination.new_path.filename); break; } case ES_EVENT_TYPE_NOTIFY_RENAME: { const auto& e = message->event.rename; path = destination_path(e.destination_type, e.destination.existing_file, e.destination.new_path.dir, e.destination.new_path.filename); completed = true; break; } case ES_EVENT_TYPE_NOTIFY_CLOSE: if (!message->event.close.modified) { return; } path = es::file_path(message->event.close.target); completed = true; break; default: return; } const WatchSpec* spec = match_watch_spec(specs_, path); if (spec == nullptr) { return; } // Drop dirs: only completed writes matter (executables written there). if (spec->category == "drop" && !completed) { return; } FileWriteEvent event; event.pid = es::pid_of(message->process); event.path = path; event.category = spec->category; event.label = spec->label; event.system_scope = spec->system_scope; event.timestamp_ns = es::message_wall_ns(message); if (on_event_) { on_event_(event); } } catch (const std::exception& e) { std::cerr << "[FileWatcher] handler failed: " << e.what() << std::endl; } catch (...) { STRAWBERRY_NOTE_SWALLOWED(); } } es::Client client_; std::vector specs_; std::function on_event_; std::atomic active_{false}; std::size_t watched_ = 0; std::string error_; }; #else // fanotify is Linux-only: report the watcher unavailable. class FileWatcher::Impl { public: bool start(std::vector /*specs*/, std::function /*on_event*/) { error_ = "file watching is not available on this platform"; return false; } void stop() {} bool active() const { return false; } std::size_t watched() const { return 0; } const std::string& error() const { return error_; } private: std::string error_; }; #endif FileWatcher::FileWatcher() : impl_(std::make_unique()) {} FileWatcher::~FileWatcher() = default; bool FileWatcher::start(std::vector specs, std::function on_event) { return impl_->start(std::move(specs), std::move(on_event)); } void FileWatcher::stop() { impl_->stop(); } bool FileWatcher::active() const { return impl_->active(); } std::size_t FileWatcher::watched() const { return impl_->watched(); } const std::string& FileWatcher::error() const { return impl_->error(); } } // namespace strawberry