Overview
This guide provides a comprehensive walkthrough of analyzingObjectiveSwizzling.ipa, a sample application that demonstrates Objective-C method swizzling. You’ll learn how to detect swizzling, understand its purpose, and use the SwizzlingDetector script to automate the analysis.
What is Method Swizzling?
Method swizzling is a technique in Objective-C that allows you to swap the implementations of two methods at runtime. It leverages the dynamic nature of the Objective-C runtime to change method behavior without modifying the original source code.Common Use Cases
Legitimate:- Analytics and tracking (e.g., tracking view controller appearances)
- Debugging and development tools
- Aspect-oriented programming
- Hot-patching bugs in production
- Bypassing security checks
- Intercepting sensitive data
- Anti-debugging and anti-tampering
- Keylogging and credential theft
How Swizzling Works
originalMethod will execute the implementation of swizzled_originalMethod, and vice versa.
Getting Started
Detection Methods
Method 1: Using SwizzlingDetector Script
The fastest way to detect swizzling is using the automated script:Load the Script
- Open Script Manager in Ghidra (Window → Script Manager)
- Search for “SwizzlingDetector”
- Ensure the script is in your
ghidra_scriptsdirectory
Run the Script
Double-click the script to execute it. The script will search for common swizzling functions.
Method 2: Manual Detection
You can also manually search for swizzling indicators:Check Imports
Look at the Symbol Tree (Window → Symbol Tree):
- Expand “Imports”
- Search for:
method_exchangeImplementationsclass_getInstanceMethodclass_getClassMethodmethod_setImplementation
Search Strings
Use Ghidra’s string search (Search → For Strings):
- Look for selector names that might indicate swizzling
- Search for common swizzled methods like
viewDidAppear:,viewWillAppear:
Analysis Workflow
Step 1: Locate Swizzling Code
Once the SwizzlingDetector script identifies swizzling references, navigate to those addresses:Step 2: Identify What’s Being Swizzled
Analyze the swizzling code to determine which methods are affected:- Target Class:
UIViewController - Original Method:
viewDidAppear: - Replacement Method:
swizzled_viewDidAppear: - When: In the
+loadmethod (executed when the class loads)
Step 3: Analyze the Replacement Method
Find the Replacement
Search for the swizzled method implementation:
- Look for the selector name (e.g.,
swizzled_viewDidAppear:) - Navigate to its implementation
Step 4: Document Findings
Add comprehensive comments in Ghidra:Common Swizzling Patterns
Pattern 1: View Controller Tracking
Purpose: Analytics and user behavior tracking- Swizzles view controller lifecycle methods
- Calls analytics or tracking functions
- Usually in
+loadmethod
Pattern 2: Anti-Debugging
Purpose: Detect and prevent debugging- Swizzles system security checks
- Modifies debugger detection
- May call
sysctlorptrace
Pattern 3: Data Interception
Purpose: Intercept sensitive data (potentially malicious)- Swizzles data I/O methods
- Intercepts network requests
- Accesses sensitive APIs (keychain, clipboard)
Using the SwizzlingDetector Script
Script Workflow
Automatic Detection
The script searches for these runtime functions:
method_exchangeImplementationsclass_getInstanceMethodclass_getClassMethodmethod_setImplementation
Interpreting Results
No Swizzling Found:- The runtime function is imported at
00100004a2c - It’s called from two locations:
001000045b8and0010000467c - Navigate to these addresses to analyze the swizzling code
Practice Exercises
Run Detection
Use the SwizzlingDetector script on ObjectiveSwizzling.ipa. How many swizzling references did you find?
Identify Methods
For each reference, determine:
- Which class is being swizzled?
- Which method is being swizzled?
- What is the replacement method?
Analyze Intent
Read the replacement method implementations and classify each as:
- Legitimate (analytics, debugging)
- Security (anti-tampering)
- Suspicious (data interception)
Advanced Topics
Fishhook and Other Hooking Techniques
Method swizzling only works for Objective-C methods. For C functions and system calls, attackers use other techniques:- Fishhook: Hooks C functions by modifying the Mach-O symbol table
- Frida: Dynamic instrumentation framework
- Substrate: iOS hooking framework (jailbreak)
Defensive Swizzling Detection
Some applications detect if they’ve been swizzled:Troubleshooting
Script Returns No Results
- Verify the binary is properly loaded and analyzed
- Check if it’s a Swift-only app (no Objective-C runtime)
- The app might use alternative hooking methods
Too Many False Positives
- Common in apps using analytics SDKs (Crashlytics, Firebase)
- Focus on swizzling of security-sensitive methods
- Check if the swizzling is from a known framework
Can’t Find Replacement Method
- The replacement might be in a different module/framework
- Search for the selector name in strings
- Look in external libraries loaded by the app
Key Takeaways
Method swizzling is a powerful technique that can be used for both legitimate and malicious purposes. Always analyze the context and behavior of swizzled methods to determine intent.
- Swizzling is detectable through runtime function imports
- The SwizzlingDetector script automates initial detection
- Manual analysis is required to understand the purpose
- Compare behavior with unmodified applications
- Document all findings with clear comments
See Also
- Swizzling Detector Script - Full script documentation
- Sample IPA Files - Overview of all examples
- Control Flow Flattening Example - Another obfuscation technique