iOS防护----获取Mach-O文件的UUID

Posted 普通网友

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS防护----获取Mach-O文件的UUID相关的知识,希望对你有一定的参考价值。

代码不复杂,原理是动态解析自身内存里面的macho文件,根据macho文件格式找到LC_UUID然后就能得到uuid的值:

#include <mach-o/dyld.h>

static const char* uuidBytesToString(const uint8_t* uuidBytes) 
    CFUUIDRef uuidRef = CFUUIDCreateFromUUIDBytes(NULL, *((CFUUIDBytes*)uuidBytes));
    NSString* str = (__bridge_transfer NSString*)CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    
    return str == NULL ? NULL : strdup(str.UTF8String);


void getUUID(int index) 
    int offset = 0;
    struct mach_header_64 * header = (struct mach_header_64 *)_dyld_get_image_header(index);
    if(header->magic != MH_MAGIC_64) 
        return ;
    
    offset = sizeof(struct mach_header_64);
    int ncmds = header->ncmds;
    while(ncmds--) 
        struct load_command * lcp = (struct load_command *)((uint8_t*)header + offset);
        offset += lcp->cmdsize;
        if (lcp->cmd == LC_UUID) 
            struct uuid_command * uuid = (struct uuid_command *)lcp;
            printf("%s %s \\n", uuidBytesToString(uuid->uuid), _dyld_get_image_name(index));
            break;
        
    


int main(int argc, char * argv[]) 
    NSString * appDelegateClassName;
    @autoreleasepool 
        // Setup code that might create autoreleased objects goes here.
        appDelegateClassName = NSStringFromClass([AppDelegate class]);
        getUUID(0);
    
    return UIApplicationMain(argc, argv, nil, appDelegateClassName);

如果想获取所有动态库的UUID,其实也很简单,就是获取到 APP 中所有的 image count,然后一个个遍历获取header、Load Command,进而找到所有 Mach-O 的 UUID:

void getAllUUID() 
    int imageCount = (int)_dyld_image_count();
    for (int i = 0; i < imageCount; ++i) 
        struct mach_header_64* header = (struct mach_header_64*)_dyld_get_image_header((unsigned)i);
        if(header->magic != MH_MAGIC_64) 
            return ;
        
        getUUID(i);
    

以上是关于iOS防护----获取Mach-O文件的UUID的主要内容,如果未能解决你的问题,请参考以下文章

无法在 iOS 可执行文件中获取 Mach-O 文本段大小

iOS 静态混淆,使用宏进行替换字符串,或者解析mach-o中对应的section进行类名和方法名的替换

iOS逆向之Mach-O文件(上)

iOS系统分析Mach-O二进制文件解析

通过Mach-O文件结构看iOS堆栈信息

通过Mach-O文件结构看iOS堆栈信息