LLDB+debugserver调试第三方应用

2019.11.18更新

entitlements.plist内容变更为以下,主要解决iOS12.x上的问题

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.backboardd.debugapplications</key>
    <true/>
    <key>com.apple.backboardd.launchapplications</key>
    <true/>
    <key>com.apple.frontboard.debugapplications</key>
    <true/>
    <key>com.apple.frontboard.launchapplications</key>
    <true/>
    <key>com.apple.springboard.debugapplications</key>
    <true/>
    <key>com.apple.system-task-ports</key>
    <true/>
    <key>get-task-allow</key>
    <true/>
    <key>platform-application</key>
    <true/>
    <key>run-unsigned-code</key>
    <true/>
    <key>task_for_pid-allow</key>
    <true/>
</dict>
</plist>

前言

本文主要介绍越狱手机通过LLDB、debugserver调试第三方应用,关于LLDB、debugserver的内容,前面在学习中也有提到过:LLDB调试命令ptrace反调试

一、简介

debugserver就像是远程服务的控制台应用,主要给gdb或者lldb调试,手机设备开启debugserver服务,本地Mac通过LLDB发送指令给debugserver,debugserver在真机调试的时候被安装到手机上,可以在手机上/Developer/usr/bin/debugserver找到。日常正向开发就是Xcode内部的LLDB调起debugserver进程来调试我们自己的App。

二、debugserver命令选项

iOS端调起debugserver

debugserver host:port [program-name program-arg1 program-arg2 ...]

选项 含义
-a 进程 将debugserver依附到指定进程,通过PID或可执行文件名称
-d integer 指定等待时间
-i integer 指定等待时间间隔
-l filename 日志文件。将文件名设置为stdout以记录标准输出
-t 使用task ID代替PID
-v 日志模式

三、配置debugserver

debugserver默认只能调试自己开发的应用,调试其他应用会抛异常unable to start the exception thread。默认的debugserver缺少task_for_pid()权限,因此需要给debugserver赋予task_for_pid权限。

unable to start the exception thread异常

(一)、方法一 使用ldid赋予权限

(1)Mac端找到debugserver文件
debugserver可以在/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/你越狱手机版本/DeveloperDiskImage.dmg里的usr/bin路径找到,拷贝出来。由于ldid不支持胖二进制文件,因此需要先瘦身:

lipo -thin armv7 debugserver -output ~/debugserver (注意,本文使用iPhone5测试,因此对应armv7,请自行对应架构)

image.png

(2)新建一个xml文件

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>com.apple.springboard.debugapplications</key>
        <true/>
        <key>get-task-allow</key>
        <true/>
        <key>task_for_pid-allow</key>
        <true/>
        <key>run-unsigned-code</key>
        <true/>
    </dict>
</plist>
xml

(3)赋予权限

ldid -Sxml全路径 debugserver全路径
例如:ldid -S/Users/kinken_yuen/Desktop/ent.xml /Users/kinken_yuen/Desktop/debugserver

赋予权限

(4)拷贝配置后的debugserver到手机

注意: 手机上的/Developer目录实际上是只读的,你不能直接将debugserver复制回去,放到别的地方使用,可以是:scp -P 2222 ./debugserver root@127.0.0.1:/usr/bin/debugserver
下面的方法二最后也需要如此操作

(二)、方法二 使用codesign赋予权限

(1)拷贝出debugserver
先将debugserver从手机复制到Mac

scp -P 2222 root@localhost:/Developer/usr/bin/debugserver ./

因为是从当前越狱手机拷贝,因此不需要再瘦身

debugserver文件

(1)使用entitlements权限文件签名
新建entitlements.plist,写入内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/ PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.springboard.debugapplications</key>
    <true/>
    <key>run-unsigned-code</key>
    <true/>
    <key>get-task-allow</key>
    <true/>
    <key>task_for_pid-allow</key>
    <true/>
</dict> 
</plist>

(3)赋予权限

codesign -s - --entitlements entitlements.plist -f debugserver

签名权限

(4)拷贝配置后的debugserver到手机
参考方法一

四、附加到进程

(一)、通过WIFI连接

(1)、手机端开启debugserver


(2)、Mac端LLDB连接debugserver


进入lldb

process connect connect://设备IP地址:1234(对应于手机开启的端口号)

连接debugserver

(二)、通过USB连接

(1)手机端开启debugserver(同上)
(2)Mac端

  • 先做端口转发

iproxy 1234 1234

接着

process connect connect://127.0.0.1:1234
process connect connect://localhost:1234

USB连接

五、小结

第三方应用动态调试的方式不止一种,如重签名后用Xcode调试非越狱Cycript调试越狱Cycript调试,以及上面的lldb + debugserver调试。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容