iosobjective-chooktheos

How to use theos %hookf to hook an objective-c function in an iOS app?


I am trying to hook a function called addition. The implementation looks something like this:

NSNumber *addition(NSNumber *num1, NSNumber *num2) {
    double result = [num1 doubleValue] + [num2 doubleValue];
    return @(result);
}

Reading the theos documentation here, I have written a theos tweak as follows:

#import <UIKit/UIKit.h>

NSInteger addition(NSInteger num1, NSInteger num2);

%hookf(NSInteger, addition, NSInteger num1, NSInteger  num2) {
    return 99;
}

However, it results in this error:

==> Linking tweak objchooker (armv7)…
ld: warning: -multiply_defined is obsolete
Undefined symbols for architecture armv7:
  "_addition", referenced from:
      __logosLocalInit in Tweak.x.f54e192c.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [/Users/hnin.sin/Desktop/objchooker/.theos/obj/debug/armv7/objchooker.dylib] Error 1
make[2]: *** [/Users/hnin.sin/Desktop/objchooker/.theos/obj/debug/armv7/objchooker.dylib] Error 2
make[1]: *** [internal-library-all_] Error 2
make: *** [objchooker.all.tweak.variables] Error 2

%hook and %hookf both works on my swift test app but only %hook works on this objective-c app. Am I doing something wrongly for %hookf or does %hookf not work in objective-c apps?


Solution

  • After much trial and error, I found out that it is actually possible to hook onto Objective-C functions.

    The thing that I was missing here that was causing this error:

    Undefined symbols for architecture armv7:"_addition", 
    referenced from:
      __logosLocalInit in Tweak.x.f54e192c.o
    ld: symbol(s) not found for architecture armv7
    

    was that I did not add this to the end of the tweak

    %ctor {
        %init(addition= MSFindSymbol(NULL, "_addition"));
    }
    

    and that is important because this function was created by me and THEOS doesn't really know where it is unless it searches for it during runtime with the help of the symbol I have provided.

    the whole tweak looks something like this

    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    %hookf(NSNumber *, addition, NSNumber *num1, NSNumber *num2) {
        return @99;
    }
    %ctor {
        %init(addition= MSFindSymbol(NULL, "_addition"));
    }