iOS防护----关键函数地址校验

作者: 捡书 | 来源:发表于2020-07-15 16:26 被阅读0次

使用dladdr方法可以获得一个函数所在的模块.从而判断该函数是否被替换掉。

#include <dlfcn.h>
#include <objc/objc.h>
#include <objc/runtime.h>
#include <stdio.h>
#include <string.h>

+ (void)test {
    Dl_info info;
    IMP imp;
    Method orginalMethod = class_getClassMethod([NSObject class], @selector(load));
    imp = method_getImplementation(orginalMethod);
    if (dladdr(imp, &info)) {
        printf("dli_fname: %s\n", info.dli_fname);
        printf("dli_sname: %s\n", info.dli_sname);
        printf("dli_fbase: %p\n", info.dli_fbase);
        printf("dli_saddr: %p\n", info.dli_saddr);
    } else {
        printf("error: can't find that symbol.\n");
    }
}
====================================================
dli_fname: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework/Foundation
dli_sname: +[NSObject(NSObject) load]
dli_fbase: 0x7fff25888000 //模块地址
dli_saddr: 0x7fff2591bb43 //函数地址

通过该方法验证指定类的方法是否都来自指定模块(可以根据实际情况自定义修改),建议使用inline方式编译,像这样以内联函数的形式编译,攻击者必须修改每一处调用该函数的的地方:

#include <dlfcn.h>
#include <objc/objc.h>
#include <objc/runtime.h>
#include <stdio.h>
#include <string.h>

static inline BOOL validate_methods(const char *cls,const char *fnamePre) __attribute__ ((always_inline));

BOOL validate_methods(const char *cls,const char *fnamePre){
    Class aClass = objc_getClass(cls);
    Method *methods;
    unsigned int nMethods;
    Dl_info info;
    IMP imp;
    Method m;
    if(!aClass)
        return NO;
    methods = class_copyMethodList(aClass, &nMethods);
    while (nMethods--) {
        m = methods[nMethods];
        imp = method_getImplementation(m);
        if(!imp){
            free(methods);
            return NO;
        }
        if(!dladdr(imp, &info)){
            free(methods);
            return NO;
        }
        /*Validate image path*/
        if(!strstr(info.dli_fname, fnamePre)){
            printf("%s \n", info.dli_fname);
            printf("%s \n", info.dli_sname);
            goto FAIL;
        }
    }
    return YES;
FAIL:
    printf("method %s failed integrity test:\n",sel_getName(method_getName(m)));
    printf("    dli_fname:%s\n",info.dli_fname);
    printf("    dli_sname:%s\n",info.dli_sname);
    printf("    dli_fbase:%p\n",info.dli_fbase);
    printf("    dli_saddr:%p\n",info.dli_saddr);
    free(methods);
    return NO;
}

当然,我们可以查看几个常见的关键函数地址:

Dl_info info;
if (dladdr(exit, &info)) {
    printf("dli_fname: %s\n", info.dli_fname);
    printf("dli_sname: %s\n", info.dli_sname);
    printf("dli_fbase: %p\n", info.dli_fbase);
    printf("dli_saddr: %p\n", info.dli_saddr);
} else {
    printf("error: can't find that symbol.\n");
}

if (dladdr(syscall, &info)) {
    printf("dli_fname: %s\n", info.dli_fname);
    printf("dli_sname: %s\n", info.dli_sname);
    printf("dli_fbase: %p\n", info.dli_fbase);
    printf("dli_saddr: %p\n", info.dli_saddr);
} else {
    printf("error: can't find that symbol.\n");
}

if (dladdr(sysctl, &info)) {
    printf("dli_fname: %s\n", info.dli_fname);
    printf("dli_sname: %s\n", info.dli_sname);
    printf("dli_fbase: %p\n", info.dli_fbase);
    printf("dli_saddr: %p\n", info.dli_saddr);
} else {
    printf("error: can't find that symbol.\n");
}

================================================
dli_fname: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_c.dylib
dli_sname: exit
dli_fbase: 0x7fff51a76000
dli_saddr: 0x7fff51ad0046
dli_fname: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_kernel.dylib
dli_sname: __syscall
dli_fbase: 0x7fff51b5a000
dli_saddr: 0x7fff51b5c9f0
dli_fname: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_c.dylib
dli_sname: sysctl
dli_fbase: 0x7fff51a76000
dli_saddr: 0x7fff51aa1304

相关文章

  • iOS防护----关键函数地址校验

    使用dladdr方法可以获得一个函数所在的模块.从而判断该函数是否被替换掉。 通过该方法验证指定类的方法是否都来自...

  • iOS 防止Ipa重签名

    内容来源于「iOS安全防护之重签名防护和sysctl反调试」。 校验描述文件信息 可以在启动时校验描述文件信息与打...

  • 2020-12-27

    --- #### **1. php如何校验IP地址** filter_var() 函数 通过指定的过滤器过滤一...

  • iOS应用代码注入防护

    iOS应用代码注入防护 iOS应用代码注入防护

  • 基于Python的接口自动化测试(3)

    封装接口返回值校验(关键参数校验和参数完整性校验)

  • ios身份证精确校验

    ios身份证精确校验

  • 内联函数(inline修饰的函数)

    内联函数 iOS开发同学经常会在有些代码中看到inline关键词修饰的函数,这里就和大家说下这个关键词。 ...

  • iOS逆向之文件系统结构

    上一篇文章地址:iOS逆向之介绍 上一篇文章中,介绍了iOS逆向做了些什么,需要怎样的防护,逆向的流程,提到的工具...

  • 破解游戏的一些记录

    我个人喜欢分为三个阶段进行破解 找关键函数并通过这些关键函数去确定金币所属地址 尝试修改并调用ui更新相关函数 找...

  • 比特币转错地址怎么办?

    这个问题分如下几种情况: 一、无效地址 提币转账的时候,默认网络是有地址校验的,如果是无效地址,不满足地址校验标准...

网友评论

    本文标题:iOS防护----关键函数地址校验

    本文链接:https://www.haomeiwen.com/subject/umkjhktx.html