Overview
The Swizzling Detector is a Ghidra script that identifies method swizzling in iOS applications. Method swizzling is a technique used to change the behavior of existing methods at runtime, commonly used for both legitimate purposes (debugging, analytics) and malicious ones (tampering, hooking). This script searches for common Objective-C runtime functions associated with swizzling and reports all references to these functions.What is Method Swizzling?
Method swizzling is an Objective-C runtime feature that allows you to swap the implementations of two methods. It’s commonly implemented using these runtime functions:method_exchangeImplementations- Swaps implementations of two methodsclass_getInstanceMethod- Retrieves an instance methodclass_getClassMethod- Retrieves a class methodmethod_setImplementation- Directly sets a method’s implementation
While swizzling can be used legitimately (e.g., in debugging frameworks or analytics SDKs), it’s also a common technique in malware and anti-analysis tools.
Installation
Download the Script
Download
SwizzlingDetector.py to your Ghidra scripts directory:- macOS/Linux:
~/ghidra_scripts/ - Windows:
%USERPROFILE%\ghidra_scripts\
Usage
Run the Script
Open the Script Manager, search for “SwizzlingDetector” or navigate to the iOS category, and double-click to run.
Review Results
The script will output its findings to the console:
- If no swizzling is found, you’ll see “No swizzling found”
- If swizzling is detected, you’ll see each swizzling method and all references to it
Example Output
No Swizzling Detected
Swizzling Detected
method_exchangeImplementations and class_getInstanceMethod, with multiple call sites for each.
Script Code
How It Works
- Symbol Search: The script searches the symbol table for functions matching known swizzling method names
- Reference Analysis: For each swizzling method found, it locates all cross-references (calls to that method)
- Report Generation: Outputs the location of each swizzling method and all addresses that call it
Analysis Workflow
When swizzling is detected, follow these steps to understand its purpose:Analyze Context
Look at the surrounding code:
- What class is performing the swizzling?
- What methods are being swizzled?
- When is the swizzling performed (app launch, specific trigger)?
Identify Purpose
Determine if the swizzling is:
- Legitimate: Part of a known framework (analytics, crash reporting)
- Anti-debugging: Swizzling system APIs to detect debugging
- Malicious: Hooking sensitive methods for data exfiltration
Common Swizzling Patterns
Analytics SDK
Anti-Debugging
Limitations
- False Negatives: This script only detects Objective-C runtime swizzling. Swift method replacement or Fishhook-based hooking won’t be detected
- Imported Methods: Only detects swizzling if the runtime functions are actually imported by the binary
- Dynamic Loading: Swizzling performed by dynamically loaded libraries may not be visible in the main binary
See Also
- Method Swizzling Example - Detailed walkthrough using ObjectiveSwizzling.ipa
- Objective-C Runtime Documentation