首页
社区
课程
招聘
[保存] Hooks MGCopyAnswer(arm64)
发表于: 2017-3-21 14:48 12033

[保存] Hooks MGCopyAnswer(arm64)

2017-3-21 14:48
12033

When CydiaSubstrate hooks a C function, it sorts of overwrites an assembly version of goto your_function; at the beginning of the original function. This "goto" in ARM64 is 16 bytes in size, which means if the original function is too short (< 16 bytes), CydiaSubstrate can spill over and corrupt the neighboring functions.

This is exactly why the problem of MGCopyAnswer. The implementation of MGCopyAnswer is basically (on 9.3.2 arm64):

01 00 80 d2        movz x1, #0
01 00 00 14        b    MGCopyAnswer_internal

which is just 8 bytes (< 16 bytes) in size. So CydiaSubstrate will corrupt the 8 bytes after the end of MGCopyAnswer.

Unfortunately, MGCopyAnswer_internal is right after MGCopyAnswer, and even worse this function and is called by MGGetBoolAnswer as well. Since MGCopyAnswer_internal is corrupt, you get an EXC_BAD_INSTRUCTION crash inside libMobileGestalt.

A good news for MGCopyAnswer is that, you could just hook MGCopyAnswer_internal! This has an additional benefit that many related functions like MGGetBoolAnswerMGCopyAnswerWithErrorMGCopyMultipleAnswers etc. can respond to your change as well. The bad thing is that MGCopyAnswer_internal is completely internal, and there is no symbols pointing to it. We could rely on the fact that MGCopyAnswer_internal is exactly 8 bytes after MGCopyAnswer on ARM64, and develop this ugly hack:

static CFPropertyListRef (*orig_MGCopyAnswer_internal)(CFStringRef prop, uint32_t* outTypeCode);
CFPropertyListRef new_MGCopyAnswer_internal(CFStringRef prop, uint32_t* outTypeCode) {
    return orig_MGCopyAnswer_internal(prop, outTypeCode);
}

extern "C" MGCopyAnswer(CFStringRef prop);

static CFPropertyListRef (*orig_MGCopyAnswer)(CFStringRef prop);
CFPropertyListRef new_MGCopyAnswer(CFStringRef prop) {
    return orig_MGCopyAnswer(prop);
}

%ctor {
    uint8_t MGCopyAnswer_arm64_impl[8] = {0x01, 0x00, 0x80, 0xd2, 0x01, 0x00, 0x00, 0x14};
    const uint8_t* MGCopyAnswer_ptr = (const uint8_t*) MGCopyAnswer;
    if (memcmp(MGCopyAnswer_ptr, MGCopyAnswer_arm64_impl, 8) == 0) {
        MSHookFunction(MGCopyAnswer_ptr + 8, (void*)new_MGCopyAnswer_internal, (void**)&orig_MGCopyAnswer_internal);
    } else {
        MSHookFunction(MGCopyAnswer_ptr, (void*)new_MGCopyAnswer, (void**)&orig_MGCopyAnswer);
    }
}

(This only checks for arm64 on 9.3.2. Other platforms may crash in different ways, and have different assembly code, so you may need to add additional conditions into enter the hook-MGCopyAnswer_internal branch. YMMV!)

出处:libmobilegestalt-dylib-crashed-when-hooking-mgcopyanswer-for-arm64 



[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (17)
雪    币: 226
活跃值: (15)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
good job
2017-3-21 14:51
0
雪    币: 95
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
IOS  10  hook不了了
2017-4-13 11:20
0
雪    币: 2
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
iOS9  hook蹦B
2017-4-16 11:55
0
雪    币: 3020
活跃值: (3065)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
5
我爱胭脂浓 iOS9 hook蹦B
我各种机器上一切正常
2017-5-18 18:07
0
雪    币: 3020
活跃值: (3065)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
6
昨日重现SIM IOS 10 hook不了了
一切正常
2017-5-18 18:08
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
HOOK  后可以修改不?
2017-5-22 15:37
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
楼主能留个联系方式不.........
2017-5-22 15:46
0
雪    币: 208
活跃值: (469)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
9
学习了...
2017-5-22 20:40
0
雪    币: 208
活跃值: (469)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
10
  MSHookFunction(MGCopyAnswer_ptr  +  8...)    是因为MGCopyAnswer_internal正好在MGCopyAnswer后面8bytes处吗?
2017-5-22 21:08
0
雪    币: 3020
活跃值: (3065)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
11
永恒梦魇 MSHookFunction(MGCopyAnswer_ptr + 8...) 是因为MGCopyAnswer_internal正好在MGCopyAnswer后面8bytes处吗?

2017-5-25 11:27
0
雪    币: 18
活跃值: (1009)
能力值: ( LV7,RANK:110 )
在线值:
发帖
回帖
粉丝
12
2017-5-26 10:41
0
雪    币: 3020
活跃值: (3065)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
13




猪会被杀掉

调调大婶子给作者加个版权好吗?https://stackoverflow.com/questions/37903788/libmobilegestalt-dylib-crashe ...

可以, 但是出处好像不确定是不是这

2017-5-26 17:01
0
雪    币: 4
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
14
extern  &quot;C&quot;  MGCopyAnswer(CFStringRef  prop);    报错。  改:extern  &quot;C&quot;  CFPropertyListRef  MGCopyAnswer(CFStringRef  prop);
MSHookFunction报错。    把MSHookFunction的第一个参数加“&amp;”
然后编译,8.4-5C  崩溃
小白求解
2017-6-2 15:44
0
雪    币: 192
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
15
作者给你点个赞
2017-6-29 17:34
0
雪    币: 30
活跃值: (41)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
16
作者再给你点个赞。。。
2017-7-17 10:23
0
雪    币: 34
活跃值: (12)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
17
小白请问  outTypeCode  这个参数需要传什么值呢?
2017-10-19 12:39
0
雪    币: 244
活跃值: (169)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
18
mark下进来学习
2017-10-23 17:45
0
游客
登录 | 注册 方可回帖
返回
//