Dylib Hijacking on macOS: @rpath and Weak Dylibs
How dyld resolves @rpath, why a missing weak dylib or a writable search path lets a foreign library load, and how to spot it and its defences in the Mach-O.
TL;DR. A Mach-O program names its libraries in load commands, and dyld looks for them in a fixed order. If an attacker can put a file where dyld looks first, the attacker's code runs inside the program. Two conditions create that opening:
- a weak library that may legitimately be missing;
- an
@rpathlibrary searched through a list of folders, one of which the attacker can write to.
Library validation, part of the hardened runtime, closes most of it. Every one of these conditions is visible in the file itself: the Libraries tab and a few findings of Mach-O Parser read them out.
How dyld finds a library
Each LC_LOAD_DYLIB command carries an install name. It can be an absolute path (/usr/lib/libSystem.B.dylib) or start with a placeholder:
| Placeholder | Means |
|---|---|
@executable_path/ | The folder of the main program |
@loader_path/ | The folder of the binary containing this load command (a framework loading its own helper) |
@rpath/ | Try each LC_RPATH search path in order, and take the first folder that has the file |
The search paths come from LC_RPATH commands in the main program and in the libraries along the way, and can themselves use @executable_path and @loader_path. The first match wins, and nothing checks that it is the file the developer meant.
Three other load commands change the picture:
LC_LOAD_WEAK_DYLIB: the library may be missing. The program still starts, and the symbols it would have provided are left null.LC_REEXPORT_DYLIB: this library passes through another library's symbols, as if they were its own.LC_DYLD_ENVIRONMENT:DYLD_*_PATHvariables baked into the main program, for example extra search folders.
Two ways to hijack
Patrick Wardle described both in 2015, and they still apply to programs that lack library validation:
- A missing weak library. The program weakly links
@rpath/Foo.dylib, and no search path has it. Place aFoo.dylibin any search path and it loads on the next launch. - The
@rpathsearch order. The program links@rpath/Foo.dylib, with search pathsAthenB, and the real file is inB. Place aFoo.dylibinAand it wins.
A planted library must look right to the program. The usual trick is dylib proxying: the attacker's library runs its own code at load time, from an initialiser in __mod_init_func or __init_offsets. It then re-exports the real library with LC_REEXPORT_DYLIB, so every symbol the program expects is still there and nothing seems broken. ATT&CK files this under T1574.004, Dylib Hijacking.
A third case needs no search order at all: an absolute install name or search path inside a folder ordinary users can write to (/Users/Shared, /tmp, a user's home). Whoever controls that folder controls the library.
The defences, and how to read them in the file
- Library validation. With the hardened runtime (the
runtimeflag) or thelibrary-validationflag in the CodeDirectory,dyldonly loads libraries signed by Apple or by the program's own Team ID. A planted library from anyone else is refused. The entitlementcom.apple.security.cs.disable-library-validationswitches this off, as plug-in hosts often must. - DYLD variables ignored. Hardened and restricted programs ignore
DYLD_INSERT_LIBRARIESand theDYLD_*_PATHvariables, unless they holdcom.apple.security.cs.allow-dyld-environment-variables. - System Integrity Protection. SIP protects Apple's own locations, so a system binary's absolute paths are not plantable. Third-party apps get no such protection.
- Where the program lives. An app in
/Applicationsinstalled by an administrator is less exposed than one a user dragged into~/Applications. A search path inside/Users/Sharedis exposed to everyone.
So the reading order is: which libraries are weak or @rpath, what are the search paths and in which order, are any of them writable by ordinary users, and is library validation on? See entitlements and the hardened runtime for the entitlements involved.
What Mach-O Parser reports
The Libraries tab lists every linked library with its kind (load, weak, re-export, upward), its version and its ordinal, followed by the LC_RPATH search paths in order. The findings say what matters:
| Finding | Level | Condition |
|---|---|---|
| Loads a library from a writable location | high | A library's path starts in /tmp, /private/tmp, /var/tmp, /Users/Shared or a user folder |
| Weak @rpath libraries (dylib hijack candidate) | medium | A weak library resolved through @rpath, with search paths to plant it in |
| Unusual run-path search location | low | An absolute LC_RPATH outside the usual system and application folders |
| Baked-in dyld environment | medium | LC_DYLD_ENVIRONMENT sets variables for the program |
| Non-standard dynamic linker | high | LC_LOAD_DYLINKER names something other than /usr/lib/dyld |
| Library validation disabled | medium | The disable-library-validation entitlement |
The updater sample behind Try a sample has the classic setup. It weakly links @rpath/libhelper.dylib, and its search paths are, in order, /Users/Shared/.updater and then @executable_path. Any user who can write to /Users/Shared/.updater decides what the updater loads. It has no hardened runtime, and it asks for disable-library-validation anyway. In this sample the attacker built the program, so the "vulnerability" is a planned way to swap modules. The same reading applies unchanged to a legitimate app with a careless search path.
Looking at the planted library itself
On a compromised machine you usually find the library, not the conditions. Signs that a dylib is a hijacker or a proxy:
- An install name copied from someone else: its
LC_ID_DYLIBmatches a library a legitimate app expects (@rpath/Sparkle.framework/…), but it sits in the wrong folder. - An
LC_REEXPORT_DYLIBpointing at the real library, often under a renamed path. - Initialisers. A
__mod_init_funcor__init_offsetssection in a library whose real counterpart has none. - A signature that doesn't fit: ad hoc or unsigned, or a different Team ID from the app it loads into.
- A recent birth time in an application folder that otherwise dates from the app's installation.
Then compare it with the genuine library. Mach-O Parser's Compare view shows the exports, imports and signature side by side. A proxy exports little of its own and re-exports the rest.
A short checklist
- List weak and
@rpathlibraries, and the search paths in order. - Flag search paths and absolute paths in writable places.
- Check library validation: hardened runtime and no
disable-library-validation. - Check for
LC_DYLD_ENVIRONMENTandallow-dyld-environment-variables. - On the machine, check who can write to each folder in the search order.
- For any library found in such a folder, check its install name, re-exports, initialisers and signature.
Frequently asked questions
What is the difference between dylib hijacking and DYLD_INSERT_LIBRARIES?
Dylib hijacking plants a library where a program will look for one of its own libraries, so it loads on every launch without any environment variable (ATT&CK T1574.004). DYLD_INSERT_LIBRARIES asks dyld to load an extra library through an environment variable at launch (T1574.006). dyld ignores that variable for hardened and restricted programs unless they hold the allow-dyld-environment-variables entitlement.
Does the hardened runtime prevent dylib hijacking?
Mostly. The hardened runtime turns on library validation, which only lets the process load libraries signed by Apple or by the same Team ID. A planted library signed by someone else, or not signed at all, is refused. The protection disappears when the program holds com.apple.security.cs.disable-library-validation.
Can static analysis tell whether a hijack is exploitable?
It shows the conditions inside the file: weak and @rpath libraries, the search order, search paths in writable places and whether library validation is on. Whether a given folder is writable by an attacker is a property of the machine, so check the permissions there.