Skip to content

At login

Login Hooks: Root Scripts at macOS Login (LoginHook)

How the deprecated loginwindow LoginHook and LogoutHook run a script as root at each login, what is known on current macOS, and how to find and remove one.

MITRE ATT&CK
T1037.002
Tactics
Persistence, Execution
Runs
At login
Set up by
root
Locations
  • /private/var/root/Library/Preferences/com.apple.loginwindow.plist
  • /Library/Preferences/com.apple.loginwindow.plist
  • /Library/Managed Preferences/com.apple.loginwindow.plist

How it works

loginwindow runs as root and reads its settings from the com.apple.loginwindow preferences domain. Two keys in that domain name scripts:

  • LoginHook: run as root when a user logs in at the login window, with the user's short name as $1.
  • LogoutHook: run as root when the user logs out.

Apple's "Customizing Login and Logout" documentation describes them: one script of each kind at most, run as root, and other login actions wait until the hook finishes, so a hook must return quickly. It also gives the way to install one:

sudo defaults write com.apple.loginwindow LoginHook /path/to/script

Because of sudo, that command writes root's own preferences, /private/var/root/Library/Preferences/com.apple.loginwindow.plist, which Apple names as the file to check. Preferences are also read from the system-wide /Library/Preferences/com.apple.loginwindow.plist and from managed preferences installed by a configuration profile (/Library/Managed Preferences/). Hooks run only for logins through the login window: an SSH session or a su does not trigger them.

Status on current macOS. Apple has described login and logout scripts as deprecated since launchd arrived in Mac OS X 10.4, and recommends launchd jobs instead, but has not documented their removal. On macOS 27.2, the loginwindow binary still contains the code paths and log strings for a login hook ("login hook launch", "running managed LoginHook"), and /System/Library/CoreServices/sessionlogoutd contains the logout hook code. That shows the feature is still built in; it does not prove that an unmanaged defaults key runs on every release, and the managed (profile) path may behave differently. Treat any LoginHook you find as live until you have tested the same configuration on a disposable VM of the same release.

Where it lives

PathWhatWho can write it
/System/Library/CoreServices/loginwindow.appThe program that runs the hookNobody: sealed system volume
/private/var/root/Library/Preferences/com.apple.loginwindow.plistRoot's loginwindow preferences: where sudo defaults write puts the hookroot
/Library/Preferences/com.apple.loginwindow.plistSystem-wide loginwindow settingsroot
/Library/Managed Preferences/com.apple.loginwindow.plistSettings from a configuration profile (MDM)The profile system
~/Library/Preferences/com.apple.loginwindow.plistA user's own loginwindow domain: not where hooks are documentedThe user
The script itself (anywhere, often /Library/Scripts or /usr/local)The code that runs as rootIts owner: check it

How attackers use it

Setting a hook needs root, and it then gives root code execution at every login without any launchd plist. That keeps it out of the usual /Library/LaunchDaemons review and, as far as we know, out of the Background Task Management list and its notifications. It is old tradecraft listed by MITRE (T1037.002); public macOS malware reports rarely show it, so treat it as a pattern rather than something tied to a family.

A hook that starts a payload in the background, so login is not delayed:

sudo defaults write com.apple.loginwindow LoginHook /Library/Scripts/.lh.sh
#!/bin/sh
# /Library/Scripts/.lh.sh, run as root with the user's short name in $1
nohup /Library/Application\ Support/.cache/agent "$1" >/dev/null 2>&1 &
exit 0

The same key can hold an interpreter one-liner instead of a script path, or point at a script in a folder a non-admin can write, so anyone who edits it gets root at the next login. A LogoutHook can run clean-up or exfiltration as root each time a user logs out. Because only one value exists, an attacker may also append a line to a legitimate hook script an administrator installed years ago: the setting looks expected, the change is in the script.

How to check a host

On a live Mac, read-only:

sudo defaults read com.apple.loginwindow LoginHook        # root's domain
sudo defaults read com.apple.loginwindow LogoutHook
defaults read /Library/Preferences/com.apple.loginwindow LoginHook
sudo plutil -p /private/var/root/Library/Preferences/com.apple.loginwindow.plist
plutil -p "/Library/Managed Preferences/com.apple.loginwindow.plist"
sudo profiles show -type configuration                    # profiles carrying loginwindow settings
ls -le@ /path/to/hook.sh; ls -ld /path/to /path
shasum -a 256 /path/to/hook.sh
log show --last 7d --predicate 'process == "loginwindow" AND eventMessage CONTAINS[c] "hook"'

When defaults read answers that the domain/default pair does not exist, that file holds no hook. For a hook you find: read the script, check who owns it and every folder above it, compare its times with the plist's, and look in the unified log for loginwindow hook messages around the logins you care about (what is logged varies by release, and older entries may have rotated away). If the script calls a binary, run codesign -dv --verbose=4 and pkgutil --file-info on it.

From a collection: take all three plists and the script. Note that root's preferences are under /private/var/root/Library/Preferences; the collection command on the home page takes that file too. macOS Persistence reads any of these plists offline.

What macOS Persistence flags

parseLoginwindow reads every com.apple.loginwindow.plist in the collection (system-wide, root's, and users') and makes one entry per LoginHook or LogoutHook value, run as root with the hook as its command:

  • Login or logout hook: always, on every hook found; legitimate uses are rare on current macOS.
  • Runs from a temporary or shared folder and Runs from a hidden folder: the hook path is in /tmp, /private/var/folders, /Users/Shared, or under a dot-folder.
  • The command rules, which matter when the value is a one-liner: Downloads and runs code, Reverse or bind shell, Decodes and runs a payload, Interpreter with inline code, Removes Gatekeeper's quarantine, Turns shell history off, Makes a file immutable, Detaches from the session, Embedded encoded blob.
  • Changed date: the plist was modified in the 7 days before the newest collected file. loginwindow rewrites its preferences for unrelated settings, so this date says when the file changed, not when the hook was set.
  • program also runs from mechanisms: the hook script is also started by a launch agent, daemon or another mechanism.

Limits: the tool reads the preference file, not the script the hook points to, so a malicious line inside a legitimately named script is invisible to it. It does not read /Library/Managed Preferences or configuration profiles. It also reports a hook found in a user's own ~/Library/Preferences, a file Apple does not document as a hook location.

Cleaning up

  1. Preserve: copy the plist(s) and the script with their times (sudo tar -czf hook.tgz …), hash the script, and save the defaults read output and any profile that sets loginwindow keys.
  2. Remove the setting where you found it: sudo defaults delete com.apple.loginwindow LoginHook (root's domain), sudo defaults delete /Library/Preferences/com.apple.loginwindow LoginHook, and the same for LogoutHook. A hook from a configuration profile goes away only when the profile is removed or changed in the MDM.
  3. Delete the script and whatever it launched, then look for the same payload in LaunchDaemons, LaunchAgents and login items.
  4. The attacker had root: review /etc/sudoers.d, local admin accounts and SSH keys, and rotate the passwords of every user who logged in while the hook was in place, since a root script at login could read anything they unlocked.

References

See also

On Mac Forensics

The artifacts behind this technique, in the Mac Forensics reference: