0

我想挂钩一个png_handle_IHDR在 ImageIO 框架中本地声明的函数。我使用 MobileSafari 作为过滤器。但是在调用原始函数时,mobilesafari 崩溃了。在检查 NSLog 后,我得到了这个:

Jun  5 17:21:08 unknown MobileSafari[553] <Warning>: dlopen ImageIO success!
Jun  5 17:21:08 unknown MobileSafari[553] <Warning>: Zeroing of nlist success!
Jun  5 17:21:08 unknown MobileSafari[553] <Warning>: method name successfully assigned!
Jun  5 17:21:08 unknown MobileSafari[553] <Warning>: nlist SUCCESS! nlsetting..
Jun  5 17:21:08 unknown MobileSafari[553] <Warning>: nlset success! Hooking..
Jun  5 17:21:09 unknown MobileSafari[553] <Warning>: Png IHDR handle hooking!
Jun  5 17:21:09 unknown UIKitApplication:com.apple.mobilesafari[0x819][553] <Notice>: libpng error: Invalid IHDR chunk
Jun  5 17:21:09 unknown ReportCrash[554] <Notice>: Formulating crash report for process MobileSafari[553]
Jun  5 17:21:09 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:com.apple.mobilesafari[0x819]) Job appears to have crashed: Abort trap: 6
Jun  5 17:21:09 unknown SpringBoard[530] <Warning>: Application 'Safari' exited abnormally with signal 6: Abort trap: 6

我立即意识到很可能我得到了png_handle_IHDR错误的函数原型。以下是我在调整中的代码:

#import <CoreFoundation/CoreFoundation.h>
#include <substrate.h>

#define ImageIO "/System/Library/Frameworks/ImageIO.framework/ImageIO"

void (*png_handle_IHDR)();

MSHook(void, png_handle_IHDR){
    NSLog(@"Png IHDR handle hooking!\n");
    _png_handle_IHDR(); //crashed here!!
    NSLog(@"Png IHDR handle hooking finished!\n");

}

template <typename Type_>
static void nlset(Type_ &function, struct nlist *nl, size_t index) {
    struct nlist &name(nl[index]);
    uintptr_t value(name.n_value);
    if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
        value |= 0x00000001;
    function = reinterpret_cast<Type_>(value);
}

MSInitialize {

        if (dlopen(ImageIO, RTLD_LAZY | RTLD_NOLOAD)!=NULL)
        {       
                NSLog(@"dlopen ImageIO success!\n");
                struct nlist nl[2];
                bzero(&nl, sizeof(nl));
                NSLog(@"Zeroing of nlist success!\n");
                nl[0].n_un.n_name = (char*) "_png_handle_IHDR"; 
                NSLog(@"method name successfully assigned!\n");
                nlist(ImageIO,nl);
                NSLog(@"nlist SUCCESS! nlsetting..\n");
                nlset(png_handle_IHDR, nl, 0);  
                NSLog(@"nlset success! Hooking..\n");
                MSHookFunction(png_handle_IHDR,MSHake(png_handle_IHDR));
        }        
}

我的makefile是这样的:

include theos/makefiles/common.mk

TWEAK_NAME = privateFunctionTest
privateFunctionTest_FILES = Tweak.xm

include $(THEOS_MAKE_PATH)/tweak.mk
privateFunctionTest_FRAMEWORKS = UIKit ImageIO CoreGraphics Foundation CoreFoundation

编辑:问题是,知道成功钩子所必需的原始函数参数吗?如果是,从反汇编中获取函数原型是唯一的方法吗?在任何 SDK 标头中都没有对此的定义。谢谢。

4

1 回答 1

2

好的,我反编译了函数,得到了反编译器猜到的函数原型。只要参数和返回类型大致匹配,例如bool : int, unsigned int : int,它仍然可以工作而不会终止执行。所以这有效:

int *png_handle_IHDR(int a1, int a2, int a3);

MSHook(int, png_handle_IHDR, int a1, int a2, int a3){

    NSLog(@"png_handle_IHDR(%d,%d,%d)", a1,a2,a3);
    int val = _png_handle_IHDR(a1,a2,a3);
    NSLog(@"Png IHDR handle hooking finished, returning %d as result!", val);
    return val;

}
于 2012-06-07T03:18:00.520 回答