攻防笔记一

2019-08-08  本文已影响0人  浪淘沙008

hook没有任何防护的代码

屏幕快照 2019-08-08 上午11.20.01.png 屏幕快照 2019-08-08 上午11.21.42.png

图一是正常创建的工程项目,图二是通过MonkeyDev创建的Logos Tweak工程,目的是通过图二工程hook图一的touch方法,正常将两个工程运行到手机上点击即可发现hook效果

hook通过设置Other Linker Flags防护的代码

-Wl,-sectcreate,__RESTRICT,__restrict,/dev/null

#import <mach-o/loader.h>
#import <mach-o/dyld.h>


#if __LP64__
#define macho_header               mach_header_64
#define LC_SEGMENT_COMMAND        LC_SEGMENT_64
#define LC_SEGMENT_COMMAND_WRONG LC_SEGMENT
#define LC_ENCRYPT_COMMAND        LC_ENCRYPTION_INFO
#define macho_segment_command    segment_command_64
#define macho_section            section_64
#else
#define macho_header               mach_header
#define LC_SEGMENT_COMMAND        LC_SEGMENT
#define LC_SEGMENT_COMMAND_WRONG LC_SEGMENT_64
#define LC_ENCRYPT_COMMAND        LC_ENCRYPTION_INFO_64
#define macho_segment_command    segment_command
#define macho_section            section
#endif

+ (void)load
{
    const struct mach_header_64 * header = _dyld_get_image_header(0);//获取可执行文件的头部
    if (hasRestrictedSegment(header)) {
        NSLog(@"防止Tweak注入状态!!");
    }else{
        NSLog(@"被修改了,进行下一步操作");
    }
}

static bool hasRestrictedSegment(const struct macho_header* mh)
{
    const uint32_t cmd_count = mh->ncmds;
    const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(struct 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;
                
                printf("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 = &sectionsStart[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;
}

白名单防护

RESTRICT防护的方式在iOS 10.0之后失效,所以需要通过其它方式来检测第三方插件的插入

#import <mach-o/dyld.h>
#import <mach-o/loader.h>

void whiteList() {
    int count = _dyld_image_count();
    for (int i = 0; i < count; i++) {
        const char * imageName = _dyld_get_image_name(i);
        printf("%s \n", imageName);
    }
}
const char * libsName = "";     // 白名单,

bool CheckWhiteList() {
    int count = _dyld_image_count(); //加载库的数量
    for (int i = 0; i < count; i++) {
        const char * imageName = _dyld_get_image_name(i);
        if (!strstr(libsName, imageName) && !strstr(imageName, "/var/containers/Bundle/")) {  //  /var/containers/Bundle 由于应用本身所在位置会不同,所以要排除应用本身的检测
            printf("该库不在白名单%s \n", imageName);
            return NO;
        }
    }
    return YES;
}
上一篇下一篇

猜你喜欢

热点阅读