iOS逆向之动态调试任意APP,终端LLDB、手机debugserver

一、动态调试

将程序运行起来,通过下断点、打印等方式,查看参数、返回值、函数调用流程等

二、Xcode的动态调试

Xcode调试器使用的是LLDB 
1. 在Xcode的上进行打断、打印等行为,通过调试器LLDB将指令传输给debugserver(即debugserver监听LLDB传输的指令)
2. debugserver 接收到指令,将指令执行到到APP上
3. APP 执行调试指令,将执行后的反馈,返还给debugserver
4. debugserver再将信息传给LLDB,LLDB将信息展示到Xcode的输出栏中

关于GCC、LLVM、GDB、LLDB
    Xcode的编译器发展历程 GCC -> LLVM
    Xcode的调试器发展历程 GDB -> LLDB

debugserver一般是开始存放在Mac的Xcode里面
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/9.1/DeveloperDiskImage.dmg/usr/bin/debugserver

通过Xcode给手机安装APP时,Xcode会将debugserver安装到iPhone上
     /Developer/usr/bin/debugserver

Xcode调试的局限性
    一般情况下,只能调试通过Xcode安装的APP

三、动态调试任意APP

即通过终端,调试手机上的任意APP

1. debugserver的权限问题
    A. 默认情况下,/Developer/usr/bin/debugserver 缺少一定的权限,只能调试通过Xcode安装的APP,无法调试其他来源的APP。
    B. 如果希望调试其他APP,需要对debugserver重新签名,签上2个测试相关的权限
        a. get-task-allow
        b. task-for-pid-allow       

2.如何给debugserver签上权限    
    A. iphone上 的/Developer 目录是只读权限,无法直接对 /Developer/usr/bin/debugserver  文件签名。需要把debugserver 复制到Mac上
    B. 通过 ldid 命令将文件以前签名权限文件

    ldid -e debugserver > debugserver.entitlements

    C.打开debugserver.entitlements 文件加上权限

        D. 通过 ldid 命令重新给 debugserver 签名权限

ldid -Sdebugserver.entitlements debugserver

        E. 将已将签好权限的debugserver放到/usr/bin目录中,通过chmod +x  /usr/bin/debugserver 赋予更高权限启动

    3. 让debugserver附加到某个APP进程(此处已微信为例)        
        debugserver *:端口号 -a 进程
        A. *:端口号   使用iPhone的某个端口启动 debugserver服务(只要不是保留端口号都可以)
        B. -a 进程     输入APP的进程信息(进程ID或者进程名称)

debugserver 127.0.0.1:10011 -a WeChat


此处不建议使用 debugserver *:10011 -a WeChat  如果使用* 会在后续报错

    此处已端口号 为10011 APP为微信为例。且微信要出于唤醒状态。

        如果出现Failed to get connection from a remote gdb process.连接失败,则可以试着重启手机(reboot)或者killall SpringBoard

如果权限列表中出现,下面的权限也会导致 Failed to get connection from a remote gdb process.连接失败

    com.apple.security.network.server
   
    com.apple.security.network.client
    
    seatbelt-profiles
    
    debugserver
    

建议权写成 





com.apple.backboardd.debugapplications

com.apple.backboardd.launchapplications

com.apple.frontboard.debugapplications

com.apple.frontboard.launchapplications

com.apple.springboard.debugapplications

com.apple.system-task-ports

get-task-allow

platform-application

run-unsigned-code

task_for_pid-allow



   4.在Mac上启动 LLDB ,并远程连接上iPhone上的debugserver服务 
        A. 启动LLDB电脑终端输入

lldb

        B. 链接debugserver服务 
            process connect connect://手机IP地址:debugserver服务端口号

     process connect connect://localhost:10011

此处使用 /localhost:10011 是因为做了映射,把本地的10011映射到了手机端口的10011上了
需要用到 tcperlay.py工具

出现下图表示链接成功,并且自动进入断点模式,对手机上的APP的任何操作都无效

        C.使用LLDB的指令 让程序运行 c

c

你可能感兴趣的:(iOS逆向之动态调试任意APP,终端LLDB、手机debugserver)