Skip to content

Instantly share code, notes, and snippets.

@eni9889
Forked from joswr1ght/catchredir.m
Created February 26, 2017 05:18
Show Gist options
  • Save eni9889/149336b2d24c56f43111b48f07751804 to your computer and use it in GitHub Desktop.
Save eni9889/149336b2d24c56f43111b48f07751804 to your computer and use it in GitHub Desktop.
Demonstration code to detect runtime method swizzling with Cydia Substrate/Cycript.
// Compile with:
// clang catchredir.m -o catchredir -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/ -miphoneos-version-min=7 -framework Foundation
#import <Foundation/Foundation.h>
#import <stdio.h>
#import <objc/runtime.h>
@interface UrlConnection : NSObject
@property (strong) NSString *url;
- (void)connect;
@end
@implementation UrlConnection
- (void)connect {
// Connect to a server, or other behavior the attacker wants to change
}
@end
int main() {
Class ucclass = objc_getClass("UrlConnection");
SEL sel = sel_getUid("connect");
IMP runtimeimp, ucconnectimp = class_getMethodImplementation(ucclass, sel);
while(1) {
[NSThread sleepForTimeInterval:10.0f];
ucclass = objc_getClass("UrlConnection");
sel = sel_getUid("connect");
runtimeimp = class_getMethodImplementation(ucclass, sel);
printf("pointer %p and %p\n", ucconnectimp, runtimeimp);
if (runtimeimp != ucconnectimp) printf("Modification Detected\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment