美文网首页
《iOS 逆向》010.10Hook原理探究

《iOS 逆向》010.10Hook原理探究

作者: 天雨流芳zhang | 来源:发表于2018-05-22 18:36 被阅读0次

    HOOK原理:

    1.MachO是被谁加载的?DYLD动态加载的。
    2.ASLR(地址空间布局随机化)技术MachO文件加载的时候是随机地址!
    3.PIC技术(位置代码独立)
    3.1 -如果MachO内部需要调用 系统的函数时
    -先在_DATA段中建立一个指针,指向外部函数!
    -DYLD会动态的进行绑定!将MachO中的DATA段中的指针,指向外部函数!

    //dis -s 是反汇编指令


    屏幕快照 2018-05-22 下午6.15.18.png

    验证原理

    源码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        NSLog(@"q");
        struct rebinding rebinding;
        rebinding.name = "NSLog";
        rebinding.replacement = myNSLog;
        rebinding.replaced = (void *)&old_nslog;
        
        //定义数组
        struct rebinding rebs[] = {rebinding};
        rebind_symbols(rebs, 1);
        
    }
        
    
    //函数指针保存原来的
    static void (* old_nslog)(NSString *format, ...);
    //新的nslog
    void myNSLog (NSString *format, ...){
        format = [NSString stringWithFormat:@"勾住了啊"];
        //调用原来的
        old_nslog(format);
        
    }
    
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        NSLog(@"asdadas");
    }
    

    0x000000010072c000+0x8018指针(就是_DATA段中建立的指针,指向外部函数NSLog)
    调试代码:

    (lldb) x 0x000000010072c000+0x8018
    0x100734018: 24 af cc 83 01 00 00 00 34 b9 cb 83 01 00 00 00  $.......4.......
    0x100734028: d8 94 9e 8c 01 00 00 00 38 2b 73 00 01 00 00 00  ........8+s.....
    (lldb) dis -s 0x0183ccaf24
    Foundation`NSLog:
        0x183ccaf24 <+0>:  sub    sp, sp, #0x20             ; =0x20 
        0x183ccaf28 <+4>:  stp    x29, x30, [sp, #0x10]
    (lldb) x 0x000000010072c000+0x8018
    0x100734018: b0 1c 73 00 01 00 00 00 34 b9 cb 83 01 00 00 00  ..s.....4.......
    0x100734028: d8 94 9e 8c 01 00 00 00 3c 2e e6 82 01 00 00 00  ........<.......
    (lldb) dis -s 0x0100731cb0
    004-FishhoookDemo`myNSLog:
        0x100731cb0 <+0>:  sub    sp, sp, #0x30             ; =0x30 
        0x100731cb4 <+4>:  stp    x29, x30, [sp, #0x20]
        0x100731cb8 <+8>:  add    x29, sp, #0x20            ; =0x20 
        0x100731cbc <+12>: mov    x8, #0x0
        0x100731cc0 <+16>: stur   x8, [x29, #-0x8]
        0x100731cc4 <+20>: sub    x9, x29, #0x8             ; =0x8 
        0x100731cc8 <+24>: str    x0, [sp, #0x10]
        0x100731ccc <+28>: mov    x0, x9
    (lldb) 
        0x183ccaf2c <+8>:  add    x29, sp, #0x10            ; =0x10 
        0x183ccaf30 <+12>: add    x8, x29, #0x10            ; =0x10 
        0x183ccaf34 <+16>: str    x8, [sp, #0x8]
        0x183ccaf38 <+20>: add    x1, x29, #0x10            ; =0x10 
        0x183ccaf3c <+24>: mov    x2, x30
        0x183ccaf40 <+28>: bl     0x183da6d38               ; _NSLogv
    (lldb) 
    

    rebinding.name = "NSLog";NSLog作为字符串是怎么被找到的呢?

    在Indirect Symbols中Data值是00000089,转换成十进制是137,


    Indirect Symbols表

    在Symbols Table中的Symbols表中索引137如图,137行Data为9B

    Symbols

    在String Table中0x0000D01C+0x9B=0xD087


    String Table
    fishhook

    相关文章

      网友评论

          本文标题:《iOS 逆向》010.10Hook原理探究

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