What is Method Swizzling?
Method swizzling is a powerful Objective-C runtime technique that allows you to change the implementation of existing methods at runtime. This is possible because Objective-C uses dynamic dispatch through message passing.How It Works
In Objective-C, method calls are actually messages sent to objects:Method swizzling is commonly used for debugging, analytics, aspect-oriented programming, and unfortunately, by malware to hook system methods.
Swizzling Example
Here’s how method swizzling is typically implemented:Example.m
Why Detect Swizzling?
Detecting method swizzling is important for:Security Analysis
Identify malicious hooks or tampering with security mechanisms
Jailbreak Detection
Many jailbreak detection bypasses use swizzling to hook security checks
Reverse Engineering
Understand how an app modifies its own or system behavior
Quality Assurance
Find unintended or undocumented method modifications
Detection Techniques
Runtime Method Signatures
Swizzling detection typically looks for calls to these Objective-C runtime functions:method_exchangeImplementations
method_exchangeImplementations
The primary function for swizzling - exchanges the implementations of two methods directly.
class_getInstanceMethod
class_getInstanceMethod
Retrieves an instance method from a class - often used to get methods before swizzling.
class_getClassMethod
class_getClassMethod
Retrieves a class method - used for swizzling class-level methods.
method_setImplementation
method_setImplementation
Directly sets a method’s implementation - an alternative to exchanging.
Static Binary Analysis
In Ghidra or other disassemblers, look for:- Imports of the runtime functions listed above
- Cross-references to these functions
- Code patterns in
+loador+initializemethods (common swizzling locations) - Categories on system classes (like
UIViewController+Tracking)
Using the SwizzlingDetector.py Ghidra Script
The SwizzlingDetector.py script automates the detection of method swizzling in iOS binaries analyzed with Ghidra.What It Does
Script Implementation
Here’s the complete SwizzlingDetector.py script:SwizzlingDetector.py
How to Use
Load Binary in Ghidra
Open your iOS application binary in Ghidra and complete the initial auto-analysis
Sample Output
ObjectiveSwizzling.ipa Example
The ObjectiveSwizzling.ipa example demonstrates practical method swizzling implementation. This sample app is designed for reverse engineering practice.What It Demonstrates
Basic Swizzling
Simple method exchange using method_exchangeImplementations
Security Bypass
Swizzling authentication or security check methods
Analytics Hooks
Intercepting view controller lifecycle methods
Runtime Inspection
Techniques for inspecting swizzled implementations
Analyzing ObjectiveSwizzling.ipa
Analyze Swizzled Methods
Navigate to each reference and examine the surrounding code to understand what’s being swizzled
Expected Findings
When analyzing ObjectiveSwizzling.ipa, you should find:- Swizzling in
+loadmethods of categories - Hooks on
UIViewControllerlifecycle methods - Possible authentication bypass through swizzled security checks
- Analytics tracking via swizzled UI methods
Advanced Detection
Beyond the basic script, consider these advanced detection methods:Runtime Inspection with Frida
Frida script
IMP Comparison
Compare method implementations to their expected values:Runtime check
Best Practices for Analysis
Document Findings
Keep notes on which methods are swizzled and their purpose
Cross-Reference
Check where swizzled methods are called in the app
Dynamic Testing
Combine static analysis with runtime instrumentation
Version Comparison
Compare swizzling across app versions to track changes
Mitigation and Protection
If you’re protecting your own app from swizzling attacks:Integrity Checks
Integrity Checks
Verify that critical methods haven’t been swizzled by checking their implementations at runtime.
Anti-Debugging
Anti-Debugging
Detect debugging and instrumentation tools that might be used to swizzle methods.
Code Obfuscation
Code Obfuscation
Make it harder to identify which methods to target for swizzling.
Swift Instead of Objective-C
Swift Instead of Objective-C
Swift’s static dispatch (for final classes/methods) is immune to swizzling.
Further Reading
Runtime Inspection
Deep dive into Objective-C runtime analysis
Swift Name Mangling
Understanding Swift symbol mangling