- UID
- 2
注册时间2004-12-1
阅读权限255
最后登录1970-1-1
总坛主
TA的每日心情 | 开心 2024-12-1 11:04 |
---|
签到天数: 12 天 [LV.3]偶尔看看II
|
警告:本文涉及的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
下面是我拎出来的相关片段:
- //
- // Look for a special segment in the mach header.
- // Its presences means that the binary wants to have DYLD ignore
- // DYLD_ environment variables.
- //
- // 检测目标bin中是否存在 __RESTRICT 或 __restrict 节
- static bool hasRestrictedSegment(const macho_header* mh)
- {
- const uint32_t cmd_count = mh->ncmds;
- const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
- const struct load_command* cmd = cmds;
- for (uint32_t i = 0; i < cmd_count; ++i) {
- switch (cmd->cmd) {
- case LC_SEGMENT_COMMAND:
- {
- const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
-
- //dyld::log("seg name: %s\n", seg->segname);
- if (strcmp(seg->segname, "__RESTRICT") == 0) {
- const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
- const struct macho_section* const sectionsEnd = §ionsStart[seg->nsects];
- for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
- if (strcmp(sect->sectname, "__restrict") == 0)
- return true;
- }
- }
- }
- break;
- }
- cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
- }
-
- return false;
- }
- static bool processRestricted(const macho_header* mainExecutableMH)
- {
- #if __MAC_OS_X_VERSION_MIN_REQUIRED
- // ask kernel if code signature of program makes it restricted
- uint32_t flags;
- if ( csops(0, CS_OPS_STATUS, &flags, sizeof(flags)) != -1 ) {
- if ( flags & CS_ENFORCEMENT ) {
- gLinkContext.codeSigningEnforced = true;
- }
- }
- if (flags & CS_RESTRICT) {
- sRestrictedReason = restrictedByEntitlements;
- return true;
- }
- #else
- gLinkContext.codeSigningEnforced = true;
- #endif
-
- // all processes with setuid or setgid bit set are restricted
- if ( issetugid() ) {
- sRestrictedReason = restrictedBySetGUid;
- return true;
- }
-
- // <rdar://problem/13158444&13245742> Respect __RESTRICT,__restrict section for root processes
- if ( hasRestrictedSegment(mainExecutableMH) ) {
- // existence of __RESTRICT/__restrict section make process restricted
- sRestrictedReason = restrictedBySegment;
- return true;
- }
- return false;
- }
复制代码
IDA逆向 :
dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long *)
patch方案:
1.不要改变原有规则,即对__RESTRICT 区段的检测还是保留,我们可以在 macho 文件里面插入特殊标记,比如(P.Y.G),然后进行检测,如果找到 特殊标记,则进行patch,否则走原始流程,这样在开发tweak的时候,按照我们预先定义的特殊标记即可成功挂载!
2.使用KMP定位到patch点即可!
3.game over!!
|
评分
-
查看全部评分
|