飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 5645|回复: 7

[iOS] Anti-Anti dylib(反 反-dylib钩子(Anti-tweak))

[复制链接]
  • TA的每日心情
    开心
    2016-6-16 14:07
  • 签到天数: 10 天

    [LV.3]偶尔看看II

    发表于 2015-1-20 18:28:27 | 显示全部楼层 |阅读模式
    警告:本文涉及的patch有风险,请三思而后行!!

    版主提供了 anti dylib 的文章,https://www.chinapyg.com/thread-76158-1-1.html
    原理很简单,看下面源代码即可~

    用IDA 载入 /usr/lib/dyld 分析 -- 我的版本是iOS7.1.2
    结合源代码观看 http://www.opensource.apple.com/source/dyld/dyld-353.2.1/src/dyld.cpp

    下面是我拎出来的相关片段:
    1. //
    2. // Look for a special segment in the mach header.
    3. // Its presences means that the binary wants to have DYLD ignore
    4. // DYLD_ environment variables.
    5. //

    6. // 检测目标bin中是否存在 __RESTRICT 或 __restrict 节
    7. static bool hasRestrictedSegment(const macho_header* mh)
    8. {
    9.         const uint32_t cmd_count = mh->ncmds;
    10.         const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
    11.         const struct load_command* cmd = cmds;
    12.         for (uint32_t i = 0; i < cmd_count; ++i) {
    13.                 switch (cmd->cmd) {
    14.                         case LC_SEGMENT_COMMAND:
    15.                         {
    16.                                 const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
    17.                                 
    18.                                 //dyld::log("seg name: %s\n", seg->segname);
    19.                                 if (strcmp(seg->segname, "__RESTRICT") == 0) {
    20.                                         const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
    21.                                         const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
    22.                                         for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
    23.                                                 if (strcmp(sect->sectname, "__restrict") == 0)
    24.                                                         return true;
    25.                                         }
    26.                                 }
    27.                         }
    28.                         break;
    29.                 }
    30.                 cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
    31.         }
    32.                
    33.         return false;
    34. }


    35. static bool processRestricted(const macho_header* mainExecutableMH)
    36. {        
    37. #if __MAC_OS_X_VERSION_MIN_REQUIRED
    38.     // ask kernel if code signature of program makes it restricted
    39.     uint32_t flags;
    40.         if ( csops(0, CS_OPS_STATUS, &flags, sizeof(flags)) != -1 ) {
    41.                 if ( flags & CS_ENFORCEMENT ) {
    42.                         gLinkContext.codeSigningEnforced = true;
    43.                 }
    44.         }
    45.         if (flags & CS_RESTRICT) {
    46.                 sRestrictedReason = restrictedByEntitlements;
    47.                 return true;
    48.         }
    49. #else
    50.         gLinkContext.codeSigningEnforced = true;
    51. #endif
    52.         
    53.         // all processes with setuid or setgid bit set are restricted
    54.     if ( issetugid() ) {
    55.                 sRestrictedReason = restrictedBySetGUid;
    56.                 return true;
    57.         }
    58.                
    59.         // <rdar://problem/13158444&13245742> Respect __RESTRICT,__restrict section for root processes
    60.         if ( hasRestrictedSegment(mainExecutableMH) ) {
    61.                 // existence of __RESTRICT/__restrict section make process restricted
    62.                 sRestrictedReason = restrictedBySegment;
    63.                 return true;
    64.         }
    65.     return false;
    66. }
    复制代码

    IDA逆向 :
    dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long *)
    Anti-Anti-dylib.png

    patch方案:
    1.不要改变原有规则,即对__RESTRICT 区段的检测还是保留,我们可以在 macho 文件里面插入特殊标记,比如(P.Y.G),然后进行检测,如果找到 特殊标记,则进行patch,否则走原始流程,这样在开发tweak的时候,按照我们预先定义的特殊标记即可成功挂载!
    2.使用KMP定位到patch点即可!
    3.game over!!




    评分

    参与人数 1威望 +4 收起 理由
    sndncel + 4 这么好的帖子,怎么能不给分呢?哈哈。

    查看全部评分

    PYG19周年生日快乐!
  • TA的每日心情
    开心
    2015-8-23 23:49
  • 签到天数: 27 天

    [LV.4]偶尔看看III

    发表于 2015-1-20 18:39:17 来自手机 | 显示全部楼层
    牛犇
    前排学习下
    PYG19周年生日快乐!
  • TA的每日心情
    开心
    2019-3-17 22:44
  • 签到天数: 132 天

    [LV.7]常住居民III

    发表于 2015-1-20 20:58:39 | 显示全部楼层
    牛BEN,好思维决定一切,学习。。。
    PYG19周年生日快乐!
  • TA的每日心情
    开心
    2018-11-20 21:29
  • 签到天数: 44 天

    [LV.5]常住居民I

    发表于 2015-1-20 21:09:04 | 显示全部楼层
    {:soso_e154:}啥时候才能跟上飘大的步伐……不敢想{:soso_e127:}
    PYG19周年生日快乐!
  • TA的每日心情
    擦汗
    2024-2-27 14:57
  • 签到天数: 2043 天

    [LV.Master]伴坛终老

    发表于 2015-1-21 08:34:48 | 显示全部楼层
    虽然看不太懂,不过还是要顶顶呀。哈哈。
    PYG19周年生日快乐!

    该用户从未签到

    发表于 2015-1-21 16:15:34 | 显示全部楼层
    学习 搜藏了  
    PYG19周年生日快乐!
  • TA的每日心情
    开心
    2024-3-20 17:12
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2015-1-23 22:47:30 | 显示全部楼层
    期待出一个完整的解决方案~
    PYG19周年生日快乐!

    该用户从未签到

    发表于 2015-1-29 10:42:49 | 显示全部楼层
    收藏了收藏了收藏了
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表