At boot
LaunchDaemons: Root Persistence on macOS via launchd
How macOS LaunchDaemons run code as root at boot, how attackers plant and hijack them, and how to find, check and remove a malicious daemon.
- MITRE ATT&CK
- T1543.004
- Tactics
- Persistence, Privilege escalation, Execution
- Runs
- At boot
- Set up by
- root
- Locations
- /Library/LaunchDaemons/*.plist
- /Library/PrivilegedHelperTools/*
- /private/var/db/com.apple.xpc.launchd/disabled.plist
How it works
A daemon is a launchd job in the system domain. At boot, before anyone logs in, launchd loads every property list in /System/Library/LaunchDaemons and /Library/LaunchDaemons. Daemons run as root unless the plist sets UserName (and GroupName), which launchd honours only in the system domain. They have no GUI session: code that needs the user's desktop belongs in a LaunchAgent.
The plist format is the one agents use (man launchd.plist): Label, ProgramArguments or Program, RunAtLoad, KeepAlive, StartInterval, StartCalendarInterval, WatchPaths, EnvironmentVariables. Keys that matter more for daemons:
| Key | Effect |
|---|---|
UserName, GroupName | Run as this account instead of root |
MachServices | Advertise Mach/XPC service names; launchd starts the daemon when a client connects |
Sockets (+ inetdCompatibility) | launchd listens on a port or socket and starts the job on connection |
LaunchEvents | Start on IOKit, notification or other system events |
A daemon with only MachServices or Sockets does not run at boot: it starts on demand. launchd refuses a daemon plist that is not owned by root or that is group- or world-writable.
Manage them with launchctl bootstrap system /Library/LaunchDaemons/<file>, launchctl bootout system/<label>, launchctl print system/<label> and launchctl disable system/<label>. Modifying the system domain needs root; reading it does not. Disable overrides are kept in /private/var/db/com.apple.xpc.launchd/disabled.plist, not in the plist.
Privileged helpers are the common legitimate case. With SMJobBless (OS X 10.6, deprecated in Ventura 13) an app asks for an administrator's password, and the system copies its helper to /Library/PrivilegedHelperTools/<label> and writes /Library/LaunchDaemons/<label>.plist, usually with MachServices so the app talks to it over XPC. Since Ventura, SMAppService.daemon(plistName:) registers a daemon whose plist stays in the app bundle (Contents/Library/LaunchDaemons, with BundleProgram pointing inside the bundle); such a daemon waits for an administrator to allow it in System Settings > General > Login Items (see Login Items).
Big Sur 11 sealed /System. Ventura 13 added Background Task Management (BTM): each daemon it sees is recorded (type "legacy daemon" for plists in /Library/LaunchDaemons) with the developer name and team ID from its code signature, a "Background Items Added" notification appears, and an administrator can turn it off in Login Items. A plist dropped by root still loads; BTM records, it does not block.
Where it lives
| Path | What | Who can write it |
|---|---|---|
/System/Library/LaunchDaemons/*.plist | Apple's daemons | Nobody: sealed system volume |
/Library/LaunchDaemons/*.plist | Third-party daemons | root |
/Library/PrivilegedHelperTools/* | Helper programs installed by SMJobBless | root |
/Applications/*.app/Contents/Library/LaunchDaemons/*.plist | SMAppService daemons (13+) | Whoever can write the app bundle |
/private/var/db/com.apple.xpc.launchd/disabled.plist | System-domain enable/disable overrides | launchd |
/private/var/db/com.apple.backgroundtaskmanagement/BackgroundItems-v*.btm | BTM store (13+) | backgroundtaskmanagementd |
/private/var/db/receipts/*.bom, *.plist | Which package installed which file | The installer |
How attackers use it
Writing to /Library/LaunchDaemons needs root, so a daemon is what attackers install once they have it, often next to a user-level agent for the same payload. EvilQuest (2020, Objective-See) installed both, with the label com.apple.questd.
A root implant started at boot and restarted if killed, hidden in a dot-folder:
<key>Label</key><string>com.example.helperd</string>
<key>ProgramArguments</key>
<array><string>/Library/Application Support/.helper/helperd</string></array>
<key>RunAtLoad</key><true/>
<key>KeepAlive</key><true/>
A daemon that runs a program a normal user can write. Anyone who can replace that file gets code run as root at the next boot, so the plist itself is a privilege-escalation backdoor:
<key>Label</key><string>com.example.update</string>
<key>ProgramArguments</key>
<array><string>/bin/sh</string><string>/Users/Shared/update.sh</string></array>
<key>StartCalendarInterval</key><dict><key>Minute</key><integer>15</integer></dict>
A root shell on a port, with no binary of its own, using launchd's inetd mode (the application firewall may still prompt for incoming connections):
<key>Label</key><string>com.example.remote</string>
<key>Sockets</key>
<dict><key>Listener</key><dict><key>SockServiceName</key><string>4444</string></dict></dict>
<key>inetdCompatibility</key><dict><key>Wait</key><false/></dict>
<key>ProgramArguments</key><array><string>/bin/sh</string><string>-i</string></array>
Quieter variants trojanize what is already there: replace the helper in /Library/PrivilegedHelperTools or edit a vendor plist so its ProgramArguments start the payload first. Others hide behind MachServices alone: the daemon never runs until the attacker's own client connects. Vendor helpers that accept XPC messages without checking the caller's code signature are a recurring privilege-escalation class on macOS; an attacker can plant an old vulnerable helper on purpose.
How to check a host
On a live Mac, read-only:
ls -le@ /Library/LaunchDaemons /Library/PrivilegedHelperTools
plutil -p /Library/LaunchDaemons/com.example.helperd.plist
launchctl print system | grep -A3 'com.example' # loaded services
launchctl print system/com.example.helperd # path, program, state, last exit code
launchctl print-disabled system
sudo sfltool dumpbtm > btm.txt
codesign -dv --verbose=4 /Library/PrivilegedHelperTools/com.example.helper
spctl -a -vv /Applications/Example.app
pkgutil --file-info /Library/LaunchDaemons/com.example.helperd.plist
lsbom /private/var/db/receipts/com.example.pkg.bom
stat -f 'born %SB modified %Sm' /Library/LaunchDaemons/*.plist
For each daemon, check who owns the program and every folder above it (ls -ld): a root job that runs anything a non-admin user can write is a finding even if the vendor is legitimate. Check that the helper's team ID matches the app that installed it, and that pkgutil --file-info names a package you expect. A service that launchctl print system shows but whose plist is gone was loaded by hand.
From a collection: /Library/LaunchDaemons, /private/var/db/com.apple.xpc.launchd, the BTM folder and /private/var/db/receipts, plus /Library/PrivilegedHelperTools if you want to hash and sign-check the helpers. Compare plists to BTM records and to the package receipts, and their times to the timeline. The collection command takes the plists, overrides, BTM store and receipts; macOS Persistence joins them offline.
What macOS Persistence flags
The daemon is parsed by parseLaunchJob like an agent; the entry's user is UserName, or root. On the job:
- Daemon runs a program from a home folder: the program path starts with
/Users/. - Imitates an Apple label: a
com.apple.label in a plist outside/System. - Label differs from the file name.
- Job runs an interpreter directly: a shell,
osascript,python,python3,perl,rubyorcurlas the program. - Injects a library (DYLD_): a
DYLD_variable inEnvironmentVariablesor on the command line. - Runs from a temporary or shared folder and Runs from a hidden folder on the program path and the arguments (
/tmp,/private/var/folders,/Users/Shared, dot-folders). - The command-line rules on the joined arguments: Downloads and runs code, Reverse or bind shell, Decodes and runs a payload, Interpreter with inline code, Removes Gatekeeper's quarantine, Makes a file immutable, Detaches from the session, Embedded encoded blob.
Background Task Management: Unknown developer in BTM when the record for the plist has neither team ID nor developer name, and Not in Background Task Management when a store was collected but no record points at the plist. The system-domain overrides in disabled.plist, a disabled BTM disposition or a Disabled key mark the daemon disabled. BTM daemon records whose plist was not collected (such as SMAppService daemons) are listed with the note "plist not in the collection".
Packages and time: Not installed by any package for a plist in /Library/LaunchDaemons that no receipt lists (when receipts were collected), Differs from package package when the plist no longer matches the checksum its package recorded, and Changed date for a change in the 7 days before the newest collected file. program also runs from mechanisms links a daemon to an agent, login item or hook that starts the same program.
Limits: it reads plists, not the running system, and it does not read the helpers in /Library/PrivilegedHelperTools, verify signatures, or check the ownership and permissions of the programs. Only programs under /Users are flagged as user-writable; a program in another folder a user can write is not.
Cleaning up
- Preserve:
sudo tar -czf daemon.tgzthe plist, the program and its folder (times kept), hash them, and savelaunchctl print system/<label>andsudo sfltool dumpbtm. - Stop it:
sudo launchctl bootout system/com.example.helperd. For aSocketsjob, check the port is closed (lsof -nP -i :4444). - Remove the plist and the program. If the daemon is a vendor's, reinstall from a known-good package rather than editing it.
- Search for the same payload as a LaunchAgent in every home, as a login item, in cron and in shell startup files; check sudoers and local accounts, since the attacker had root.
- Rotate what root exposes: every local account password, keychains the attacker could reach, SSH host keys, and any credential stored on the Mac.
References
See also
Related techniques
On Mac Forensics
The artifacts behind this technique, in the Mac Forensics reference: