Hacktricks-skills macos-process-abuse
macOS process abuse and injection techniques for security research. Use this skill when investigating macOS privilege escalation, analyzing process injection vectors, researching library injection, function hooking, IPC abuse, or application-specific injection methods (Electron, Chromium, Java, .NET, Python, Ruby, Perl). Also use for understanding macOS process/thread internals, credentials/persona system, or when detecting process injection attacks. Make sure to use this skill for any macOS security research involving process manipulation, code injection, or privilege escalation techniques.
git clone https://github.com/abelrguezr/hacktricks-skills
skills/macos-hardening/macos-security-and-privilege-escalation/macos-proces-abuse/macos-proces-abuse/SKILL.MDmacOS Process Abuse Research
This skill provides comprehensive guidance on macOS process abuse techniques for authorized security research and penetration testing only. All techniques should only be used on systems you own or have explicit written permission to test.
Core Concepts
Process Fundamentals
Processes are containers for running threads, providing memory, descriptors, ports, and permissions. Understanding the process lifecycle is essential:
- fork(): Creates an exact copy of the current process
- execve(): Loads a new executable in the child process
- vfork(): Faster fork without memory copying
- posix_spawn(): Combines vfork and execve with flags
Key posix_spawn Flags
| Flag | Purpose |
|---|---|
| Reset effective IDs to real IDs |
| Set process group affiliation |
| Set signal default behavior |
| Set signal mask |
| Exec in same process |
| Start suspended |
| Start without ASLR |
| Allow rwx on data segments |
| Close all FDs on exec by default |
Process Identification
- PIDs: 64-bit, monotonically increasing, never wrap (XNU kernel)
- Process Groups: Group processes for collective signaling
- Sessions: Created via
, children inherit unless they create their ownsetsid(2) - Coalitions: Darwin-specific grouping for resource sharing (Leader, XPC service, Extension roles)
Credentials & Personae
Each process holds credentials identifying its privileges:
- Primary
anduid
(can belong to multiple groups)gid
bits can change user/group IDssetuid/setgid
syscall: Provides alternate credential setpersona
struct kpersona_info { uint32_t persona_info_version; uid_t persona_id; /* overlaps with UID */ int persona_type; gid_t persona_gid; uint32_t persona_ngroups; gid_t persona_groups[NGROUPS]; uid_t persona_gmuid; char persona_name[MAXLOGNAME + 1]; }
Thread Internals
Thread Creation & Management
- POSIX Threads (pthreads): Implemented in
/usr/lib/system/libsystem_pthread.dylib - pthread_create(): Calls
(XNU-specific syscall)bsdthread_create() - Default Stack Size: 512 KB (adjustable via thread attributes)
- Thread Initialization:
parses environment variables__pthread_init()
Thread Termination
- pthread_exit(): Clean exit with return value
- pthread_terminate(): Removes thread structures, deallocates Mach ports
- bsdthread_terminate: Syscall removing kernel-level structures
Synchronization Primitives
| Primitive | Signature | Size | Purpose |
|---|---|---|---|
| Regular Mutex | 0x4D555458 | 60 bytes | Standard mutex |
| Fast Mutex | 0x4d55545A | 60 bytes | Optimized mutex |
| Condition Variable | 0x434e4441 | 44 bytes | Wait for conditions |
| Once Variable | 0x4f4e4345 | 12 bytes | Execute once |
| Read-Write Lock | 0x52574c4b | 196 bytes | Multiple readers/single writer |
Note: Last 4 bytes of these objects detect overflows.
Thread Local Variables (TLV)
__thread int tlv_var; // Each thread has its own instance
Mach-O sections:
: Metadata about TLVs__DATA.__thread_vars
: Zero-initialized TLVs__DATA.__thread_bss
: Register destructors for thread cleanuptlv_atexit
Thread Priorities
Nice Values
- Range: -20 (highest) to 19 (lowest)
- Default: 0
- Use
to modify running processesrenice
Quality of Service (QoS) Classes
| Class | Priority | Use Case |
|---|---|---|
| User Interactive | Highest | UI responsiveness, animations |
| User Initiated | High | User-triggered computations |
| Utility | Medium | Long-running with progress indicator |
| Background | Lowest | Indexing, syncing, backups |
Use
thread_policy_[set/get] for scheduling policies.
Process Abuse Techniques
1. Library Injection
Forces a process to load a malicious library, running in the target's context with same permissions.
Key Environment Variables:
DYLD_INSERT_LIBRARIESCFNETWORK_LIBRARY_PATHRAWCAMERA_BUNDLE_PATH
Detection: Monitor these variables and
task_for_pid calls.
2. Function Hooking
Intercepts function calls to modify behavior, observe data, or control execution flow.
Common Targets:
- System calls
- Library functions
- Application entry points
3. Inter-Process Communication (IPC) Abuse
Misuses IPC mechanisms to:
- Subvert process isolation
- Leak sensitive information
- Perform unauthorized actions
4. Electron Application Injection
Vulnerable Parameters:
--inspect--inspect-brk--remote-debugging-port
environment variableELECTRON_RUN_AS_NODE
Attack Vector: Start Electron apps in debugging mode to inject code.
5. Chromium/Browser Injection
Flags for Man-in-the-Browser Attacks:
: Load malicious extensions--load-extension
: Bypass media permissions--use-fake-ui-for-media-stream
Capabilities:
- Steal keystrokes
- Intercept traffic
- Steal cookies
- Inject scripts into pages
6. Dirty NIB
NIB files define UI elements but can execute arbitrary commands.
Key Points:
- Gatekeeper doesn't stop modified NIB files in running apps
- Can make arbitrary programs execute arbitrary commands
- Target:
files in application bundles.nib
7. Java Application Injection
Environment Variable:
_JAVA_OPTS
Capabilities: Execute arbitrary code/commands through Java configuration.
8. .NET Application Injection
Method: Abuse .NET debugging functionality
Note: Not protected by macOS runtime hardening.
9. Perl Injection
Various Perl environment variables and configuration options can execute arbitrary code.
10. Ruby Injection
Ruby environment variables can be abused to execute arbitrary scripts.
11. Python Injection
Environment Variables:
: Drops to Python CLI on completionPYTHONINSPECT
: Executes script at interactive session startPYTHONSTARTUP
: Modify import pathPYTHONPATH
: Set Python home directoryPYTHONHOME
Important Notes:
won't execute whenPYTHONSTARTUP
creates interactive sessionPYTHONINSPECT- PyInstaller-compiled executables don't use these environment variables
Alternative Attack Vector:
# Hijack Homebrew Python (writable location) mv /opt/homebrew/bin/python3 /opt/homebrew/bin/python3.old cat > /opt/homebrew/bin/python3 <<EOF #!/bin/bash # Extra hijack code /opt/homebrew/bin/python3.old "$@" EOF chmod +x /opt/homebrew/bin/python3
Warning: Even root will run this code when running Python.
Detection Methods
Shield Framework
Shield detects and blocks process injection:
Monitored Indicators:
-
Environment Variables:
DYLD_INSERT_LIBRARIESCFNETWORK_LIBRARY_PATHRAWCAMERA_BUNDLE_PATHELECTRON_RUN_AS_NODE
-
Calls: Detects processes requesting task ports of otherstask_for_pid -
Electron Debug Parameters:
--inspect--inspect-brk--remote-debugging-port
-
Symlink/Hardlink Abuse: Alerts when link creator has different privilege level than target
Process Self-Detection
Use
task_name_for_pid to identify processes injecting code:
Requirements:
- Same UID as target process, OR
- Root privileges
Returns: Information about the injecting process (not injection capability)
Research Workflow
Before Testing
- Verify Authorization: Ensure you have explicit written permission
- Document Scope: Define what systems and techniques are in scope
- Backup Systems: Create snapshots before testing
- Set Up Isolation: Use VMs or isolated environments
During Testing
- Start with Reconnaissance: Understand the target system
- Test Detection First: Verify monitoring is in place
- Document Everything: Log all commands and observations
- Test One Technique at a Time: Isolate effects
After Testing
- Clean Up: Remove any injected code or modified files
- Report Findings: Document vulnerabilities and remediation
- Review Logs: Check for detection gaps
Safety Guidelines
⚠️ CRITICAL: Only use these techniques on systems you own or have explicit written authorization to test.
- Unauthorized use is illegal and unethical
- These techniques can cause system instability
- Always have a rollback plan
- Test in isolated environments first
- Document all activities for accountability