iOS越狱(10)动态调试

什么叫动态调试?

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

Xcode的动态调试原理

Xcode动态调试
  • 关于GCC、LLVM、GDB、LLDB
    Xcode的编译器发展历程:GCC → LLVM
    Xcode的调试器发展历程:GDB → LLDB
  • debugserver⼀开始存放在Mac的Xcode⾥面
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/De
    viceSupport/9.1/DeveloperDiskImage.dmg/usr/bin/debugserver
  • 当Xcode识别到手机设备时,Xcode会自动将debugserver安装到iPhone上 /Developer/usr/bin/debugserver
  • Xcode调试的局限性
    一般情况下,只能调试通过Xcode安装的APP

动态调试任意APP

终端调试

1、debugserver的权限问题
默认情况下,/Developer/usr/bin/debugserver缺少一定的权限,只能调试通过Xcode安装的APP,无法调试其他APP(⽐如来自App Store的APP)
如果希望调试其他APP,需要对debugserver重新签名,签上2个调试相关的权限
get-task-allow
task_for_pid-allow

2、如何给debugserver签上权限

  • iPhone上的/Developer⽬录是只读的,无法直接对/Developer/usr/bin/debugserver⽂件签名,需要先把debugserver复制到Mac
  • 通过ldid命令导出⽂文件以前的签名权限
$ ldid -e debugserver > debugserver.entitlements
  • 给debugserver.plist⽂文件加上get-task-allow和task_for_pid-allow权限


    添加权限
  • 通过ldid命令重新签名

$ ldid -Sdebugserver.entitlements debugserver
  • 将已经签好权限的debugserver放到/usr/bin目录,便于找到debugserver指令
  • 关于权限的签名,也可以使⽤用codesign
 
# 查看权限信息
$ codesign -d --entitlements - debugserver
# 签名权限
$ codesign -f -s - --entitlements debugserver.entitlements debugserver # 或者简写为
$ codesign -fs- --entitlements debugserver.entitlements debugserver

3、让debugserver附加到某个APP进程

$ debugserver *:端⼝口号 -a 进程
  • *:端⼝号 使⽤用iPhone的某个端口启动debugserver服务(只要不是保留端口号就行)
  • -a 进程 输入APP的进程信息(进程ID或者进程名称)

4、在Mac上启动LLDB,远程连接iPhone上的debugserver服务

  • 启动LLDB
 $ lldb
(lldb)
  • 连接debugserver服务
(lldb) process connect connect://手机IP地址:debugserver服务端口号
  • 使⽤用LLDB的c命令让程序先继续运⾏
(lldb) c
  • 接下来就可以使用LLDB命令调试APP

5、通过debugserver启动APP

$ debugserver -x auto *:端⼝号 APP的可执行⽂件路径

常⽤LLDB指令

1、 指令的格式

 [ [...]]  [-options [option-
value]] [argument [argument...]]
  • 命令
  • :子命令
  • :命令操作
  • :命令选项
  • :命令参数

比如给test函数设置断点

breakpoint set -n test

2、help

  • 查看指令的用法
  • ⽐如help breakpoint、help breakpoint set

3、 expression --

  • 执行一个表达式
    :命令选项
    -- :命令选项结束符,表示所有的命令选项已经设置完毕,如果没有命令选项,--可以 省略
    :需要执行的表达式
 expression self.view.backgroundColor = [UIColor redColor] 
  • expression、expression --和指令print、p、call的效果⼀一样
  • expression -O --和指令po的效果⼀一样

4、thread backtrace

  • 打印线程的堆栈信息
  • 和指令bt的效果一样

5、thread return [<返回值>]

  • 让函数直接返回某个值,不会执行断点后面的代码

6、frame variable [<变量名>]

  • 打印当前栈帧的变量

7、thread continue、continue、c :程序继续运⾏
8、thread step-over、next、n :单步运行,把子函数当做整体一步执⾏
9、thread step-in、step、s :单步运⾏,遇到子函数会进⼊子函数
10、thread step-out、finish :直接执⾏完当前函数的所有代码,返回到上一个函数
11、thread step-inst-over、nexti、ni
12、thread step-inst、stepi、si

  • si、ni和s、n类似
  • s、n是源码级别
  • si、ni是汇编指令级别

13、breakpoint set

  • 设置断点
  • breakpoint set -a 函数地址
  • breakpoint set -n 函数名
    breakpoint set -n test
    breakpoint set -n touchesBegan:withEvent:
    breakpoint set -n "-[ViewController touchesBegan:withEvent:]"
  • breakpoint set -r 正则表达式
  • breakpoint set -s 动态库 -n 函数名

14、breakpoint list

  • 列出所有的断点(每个断点都有⾃己的编号)

15、breakpoint disable 断点编号 :禁⽤断点
16、breakpoint enable 断点编号 :启⽤断点
17、breakpoint delete 断点编号 :删除断点

18、breakpoint command add 断点编号
给断点预先设置需要执⾏的命令,到触发断点时,就会按顺序执⾏
19、breakpoint command list 断点编号 查看某个断点设置的命令
20、breakpoint command delete 断点编号 删除某个断点设置的命令
21、内存断点—— 在内存数据发⽣改变的时候触发

  • watchpoint set variable 变量
  • watchpoint set variable self->age
  • watchpoint set expression 地址
  • watchpoint set expression &(self->_age)
  • watchpoint list
  • watchpoint disable 断点编号
  • watchpoint enable 断点编号
  • watchpoint delete 断点编号
  • watchpoint command add 断点编号
  • watchpoint command list 断点编号
  • watchpoint command delete 断点编号

22、image lookup

  • image lookup -t 类型 :查找某个类型的信息
  • image lookup -a 地址 :根据内存地址查找在模块中的位置
  • image lookup -n 符号或者函数名 :查找某个符号或者函数的位置

23、image list

  • 列出所加载的模块信息
  • image list -o -f
    打印出模块的偏移地址、全路径

小技巧
敲Enter,会⾃动执行上次的命令 绝⼤部分指令都可以使用缩写

破解iOS程序

1、编写tweak代码
2、直接在Hopper Disassembly里面修改汇编代码后重新导出Mach-O文件

你可能感兴趣的:(iOS越狱(10)动态调试)