Hook [NSString stringWithFormat] 如何在这hook的代码中打印出主线程的调用堆栈?

首先,对NSString的stringWithFormat方法进行hook的代码如下:


#import 
id stringWithFormat(id self, SEL op,id obj1, ...);
id stringWithFormat(id self, SEL op,id obj1, ...){
    va_list args;
    va_start(args, obj1);
    //等价实现方法
    NSString *result = [[NSMutableString alloc] initWithFormat:obj1 arguments:args ];
    // NSLog(@"NSString stringWithFormat ---%@",result?:@"");
	if (result.length == 32) {
		NSLog(@"params:%@",obj1);
        NSLog(@"shield found!%@",result);
		NSLog(@"%@",[NSThread callStackSymbols]);
    }
    va_end(args);
    return result;
}

static __attribute__((constructor)) void _logosLocalInit112() {
        Method originalMethod = class_getClassMethod(NSClassFromString( @"NSString" ), NSSelectorFromString( @"stringWithFormat:" ));
        method_setImplementation(originalMethod, (IMP)stringWithFormat );

}

打印的结果却不是我想要的那种主线程调用堆栈,如下:


(
	0   ReTest.dylib                        0x00000001086f7d48 stringWithFormat + 232
	1   discover                            0x00000001050d44fc _ZN6google8protobuf8internal21arena_destruct_objectIN2xy11fingerprint16TelephonyNetworkEEEvPv + 13399144
	2   discover                            0x00000001050d45d4 _ZN6google8protobuf8internal21arena_destruct_objectIN2xy11fingerprint16TelephonyNetworkEEEvPv + 13399360
	3   discover                            0x00000001050d7ae4 _ZN6google8protobuf8internal21arena_destruct_objectIN2xy11fingerprint16TelephonyNetworkEEEvPv + 13412944
	4   discover                            0x00000001050d7d48 _ZN6google8protobuf8internal21arena_destruct_objectIN2xy11fingerprint16TelephonyNetworkEEEvPv + 13413556
	5   discover                            0x00000001050d7c90 _ZN6google8protobuf8internal21arena_destruct_objectIN2xy11fingerprint16TelephonyNetworkEEEvPv + 13413372
	6   discover                            0x00000001050d70b4 _ZN6google8protobuf8internal21arena_destruct_

ReTest是我Tweak的name。

请问拿到这样的堆栈要如何解读呢?

看起来应该是没恢复符号,如果你调试的话,可以用 xia0LLDBsbt 命令来看恢复符号后的堆栈。

也可以用我适配后的 restore-symbol工具 patch 可执行文件后,重签名运行。

这样你的tweak输出的调用堆栈应该是有你想要的信息。

2 个赞

为啥我的restore-symbol会出现报错?我试过某红书的不同版本,都会报错:image

下载的地址对不? 是不是下载成原始的 restore-symbol 了?

我是按照github的步骤来的啊

git clone --recursive GitHub - HeiTanBc/restore-symbol: A reverse engineering tool to restore stripped symbol table for iOS app.

1 个赞