// Live Endpoint Security check (macOS): exec prevention (AUTH_EXEC), exec // telemetry (NOTIFY_EXEC) and the file watcher (NOTIFY_CREATE/CLOSE). // // Needs root, the ES entitlement and Full Disk Access: // make macos-sign BINARY=builddir/tests/macos_es_live && sudo make macos-live-tests // Without an Apple-granted entitlement this only runs with SIP disabled (a // development VM). Prints SKIP and exits 0 when ES is not available. #include "strawberry/exec_guard.hpp" #include "strawberry/file_watch.hpp" #include "strawberry/process_monitor.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace fs = std::filesystem; using namespace strawberry; namespace { std::string read_fd(int fd) { std::string out; char buffer[65536]; off_t offset = 0; ssize_t n = 0; while ((n = ::pread(fd, buffer, sizeof(buffer), offset)) > 0) { out.append(buffer, static_cast(n)); offset += n; } return out; } // Exec `path`; 0 on success, 100 + errno if execv failed. int try_exec(const std::string& path) { const pid_t child = ::fork(); if (child == 0) { char* argv[] = {const_cast(path.c_str()), nullptr}; ::execv(path.c_str(), argv); ::_exit(100 + errno); } int status = 0; ::waitpid(child, &status, 0); return WIFEXITED(status) ? WEXITSTATUS(status) : -1; } fs::path own_dir() { char path[4096]; uint32_t size = sizeof(path); if (::_NSGetExecutablePath(path, &size) != 0) { return fs::current_path(); } return fs::canonical(path).parent_path(); } bool es_unavailable(const std::string& error) { return error.find("Endpoint Security client failed") != std::string::npos; } template bool wait_for(Pred pred, std::chrono::milliseconds limit = std::chrono::milliseconds(5000)) { const auto until = std::chrono::steady_clock::now() + limit; while (std::chrono::steady_clock::now() < until) { if (pred()) { return true; } std::this_thread::sleep_for(std::chrono::milliseconds(20)); } return pred(); } void test_exec_guard(const fs::path& bin) { const auto evil = (bin / "es_live_evil").string(); const auto clean = (bin / "es_live_clean").string(); const auto slow = (bin / "es_live_slow").string(); std::mutex mutex; std::string mode = "enforce"; std::vector decisions; ExecGuardOptions options; options.timeout = std::chrono::milliseconds(400); options.mode = [&]() { std::lock_guard l(mutex); return mode; }; options.min_severity = []() { return std::string("critical"); }; options.assess = [](int fd, const std::string& path) { const auto content = read_fd(fd); ExecAssessment a; a.scanned = true; if (content.find("SLOW-MARKER") != std::string::npos) { std::this_thread::sleep_for(std::chrono::seconds(2)); } // /bin/echo stands in for a malicious platform binary. if (content.find("EVIL-MARKER") != std::string::npos || path == "/bin/echo") { a.top_rule = "Evil"; a.top_severity = "critical"; } return a; }; options.on_decision = [&](const ExecDecision& d) { std::lock_guard l(mutex); decisions.push_back(d); }; ExecGuard guard; if (!guard.start(options)) { const auto error = guard.stats().error; if (es_unavailable(error)) { std::cout << "SKIP: " << error << std::endl; std::exit(0); } std::cerr << "FAIL: " << error << std::endl; std::exit(1); } std::cout << "Enforce: malicious file is denied with EPERM" << std::endl; assert(try_exec(evil) == 100 + EPERM); std::cout << "Enforce: clean file runs" << std::endl; assert(try_exec(clean) == 0); std::cout << "Enforce: second attempt is served from cache" << std::endl; assert(try_exec(evil) == 100 + EPERM); assert(guard.stats().cache_hits >= 1); std::cout << "Enforce: platform binary is never blocked (alert only)" << std::endl; assert(try_exec("/bin/echo") == 0); std::cout << "Slow scan: allowed after the deadline (fail open)" << std::endl; const auto start = std::chrono::steady_clock::now(); assert(try_exec(slow) == 0); assert(std::chrono::steady_clock::now() - start < std::chrono::milliseconds(1500)); assert(guard.stats().timeouts >= 1); { std::lock_guard l(mutex); mode = "audit"; } std::cout << "Audit: malicious file runs, decision is would_block" << std::endl; assert(try_exec(evil) == 0); assert(wait_for([&]() { std::lock_guard l(mutex); for (const auto& d : decisions) { if (d.path == evil && d.verdict == ExecVerdict::would_block) { return true; } } return false; })); guard.stop(); std::cout << " exec guard: PASSED" << std::endl; } void test_process_monitor(const fs::path& bin) { const auto clean = (bin / "es_live_clean").string(); ProcessMonitor monitor; if (!monitor.initialize()) { std::cerr << "FAIL: process monitor did not initialize" << std::endl; std::exit(1); } if (monitor.backend_name() != "endpoint_security") { std::cerr << "FAIL: expected the endpoint_security backend, got " << monitor.backend_name() << std::endl; std::exit(1); } std::mutex mutex; std::vector seen; assert(monitor.start([&](const ProcessInfo& info) { std::lock_guard l(mutex); seen.push_back(info); })); assert(try_exec(clean) == 0); std::cout << "Exec telemetry: NOTIFY_EXEC reaches the callback with identity" << std::endl; assert(wait_for([&]() { std::lock_guard l(mutex); for (const auto& info : seen) { if (info.executable_path == clean && info.parent_pid == ::getpid() && info.start_time_ticks > 0 && !info.image_identity.empty() && !info.boot_id.empty()) { return true; } } return false; })); monitor.stop(); std::cout << " process monitor: PASSED" << std::endl; } void test_file_watch() { const auto dir = fs::path("/private/var/tmp") / ("sb-es-watch-" + std::to_string(::getpid())); fs::create_directories(dir); std::mutex mutex; std::vector events; FileWatcher watcher; std::vector specs{{dir.string(), "persistence", "test location", {}, true}}; if (!watcher.start(specs, [&](const FileWriteEvent& e) { std::lock_guard l(mutex); events.push_back(e); })) { std::cerr << "FAIL: " << watcher.error() << std::endl; std::exit(1); } const auto target = dir / "com.example.agent.plist"; std::ofstream(target) << ""; std::cout << "File watch: a write in a watched location is reported with the writer" << std::endl; assert(wait_for([&]() { std::lock_guard l(mutex); for (const auto& e : events) { if (e.path == target.string() && e.pid == ::getpid() && e.category == "persistence") { return true; } } return false; })); // Outside the watched directory: nothing. const auto before = [&]() { std::lock_guard l(mutex); return events.size(); }(); std::ofstream(fs::path("/private/var/tmp") / ("sb-es-unwatched-" + std::to_string(::getpid()))) << "x"; std::this_thread::sleep_for(std::chrono::milliseconds(300)); { std::lock_guard l(mutex); for (std::size_t i = before; i < events.size(); ++i) { assert(events[i].path.starts_with(dir.string() + "/")); } } watcher.stop(); std::error_code ec; fs::remove_all(dir, ec); fs::remove(fs::path("/private/var/tmp") / ("sb-es-unwatched-" + std::to_string(::getpid())), ec); std::cout << " file watch: PASSED" << std::endl; } } // namespace int main() { if (::geteuid() != 0) { std::cout << "SKIP: macos_es_live needs root" << std::endl; return 0; } const auto bin = own_dir(); test_exec_guard(bin); test_process_monitor(bin); test_file_watch(); std::cout << "macos_es_live: ALL PASSED" << std::endl; return 0; }