Skip to content

Instantly share code, notes, and snippets.

@yehgdotnet
Forked from joswr1ght/catchredir.m
Created October 15, 2017 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yehgdotnet/5e1f396bd440601bba358e4752be5089 to your computer and use it in GitHub Desktop.
Save yehgdotnet/5e1f396bd440601bba358e4752be5089 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