MacOS內核調試環境搭建
http://ddeville.me/2015/08/using-the-vmware-fusion-gdb-stub-for-kernel-debugging-with-lldb
http://ddeville.me/2015/08/kernel-debugging-with-lldb-and-vmware-fusion
https://blog.csdn.net/qq_32400847/article/details/79297177
https://media.defcon.org/DEF%20CON%2025/DEF%20CON%2025%20presentations/DEFCON-25-Min-Spark-Zheng-macOS-iOS-Kernel-Debugging.pdfhttps://theori.io/research/korean/osx-kernel-exploit-1
方法一:LLDB+VMware Fusion+KDK
準備工作
1.在Mac中安裝好LLDB(安裝適合自己mac版本的xcode即可,xcode中自帶lldb),下載地址:https://developer.apple.com/download/more/
2.在Mac中安裝好VMware Fusion,
3.準備好一個macos鏡像
4.下載對應版本內核調試工具包(KDK,下載地址;https://developer.apple.com/download/more/
對於虛擬機中的操作
1.安裝MacOS虛擬機
2.安裝對應版本內核調試工具包(KDK)
3.關閉SIP
關閉虛擬機的SIP,開機的時候按Command+R進入恢復模式,然後在終端輸入csrutil disable。重啟之後可以用csrutil status命令查看是否成功關閉。
4.內核替換
將位於KDK安裝目錄下的可執行文件kernel.development復制到/System/Library/Kernels
cp /Library/Developer/KDKs/KDK_10.12.1_16B2657.kdk/System/Library/Kernels/kernel.development /System/Library/Kernels
5.設置boot-args
為了將虛擬機設置成調試模式,需要使用nvram設置boot-args,命令如下
sudo nvram boot-args="debug=0x141 kext-dev-mode=1 kcsuffix=development pmuflags=1 -v"
debug=0x141含義如下圖所示
kext-dev-mode=1允許加載未簽名kext,kcsuffix=development指定加載上面拷貝的kernel.development,pmuflags=1關閉定時器,-v顯示內核加載信息
6.清除kext緩存
sudo kextcache -invalidate /
讓虛擬機系統的kext cache無效,使用新的內核調試
7.下載內核代碼
運行uname -v命令查看xnu源碼版本,並從蘋果開源代碼上下載,下載地址:https://opensource.apple.com/tarballs/xnu/
8.查看虛擬機網絡配置信息
為了將調試器連接到虛擬機,我們需要一些關於其網絡配置的信息
對於主機中的操作
1.安裝Xcode
Xcode中自帶了lldb,選擇適合自己mac版本的xcode版本進行安裝,我這裏選擇8.3.3
2.安裝內核調試工具包
安裝與虛擬機中相同版本的內核調試工具包
3.關閉SIP
開始調試
1.重啟虛擬機
虛擬機會等待調試器鏈接
2.啟動LLDB並將目標設置為位於KDK中的(本地)內核二進制文件
$lldb
(lldb)target create /Library/Developer/KDKs/KDK_10.12.1_16B2657.kdk/System/Library/Kernels/kernel.development
如果遇到以下問題
就執行以下命令
(lldb) command script import "/Library/Developer/KDKs/KDK_10.12.1_16B2657.kdk/System/Library/Kernels/kernel.dSYM/Contents/Resources/DWARF/../Python/kernel.py"
$ echo "settings set target.load-script-from-symbol-file true" > ~/.lldbinit
再重新啟動LLDB並將目標設置為位於KDK中的(本地)內核二進制文件
3.連接虛擬機
執行命令
kdp-remote 虛擬機IP地址
就可以開始調試
存在的問題
一旦內核啟動並且調試器繼續運行,內核就不能再從調試器中停止。
1.改進
在虛擬機中執行
$sudo nvram boot-args="debug=0x144 kext-dev-mode=1 kcsuffix=development pmuflags=1 -v"
之後如果想再運行時斷下,就在虛擬機中按組合鍵
Command-Option-Control-Shift-Escape
就可以了
方法二:VMware Fusion GDB stub + LLDB
修改虛擬機配置文件
每個虛擬機文件都包含一個可以編輯的.vmx配置文件.vmwarevm(確保虛擬機在編輯時沒有運行)
在文本編輯器中打開它並添加以下行:
# If you are debugging a 32-bit machine use `guest32`
debugStub.listen.guest64 = "TRUE"
用gdb進行調試
mac中gdb需要自己安裝,請大家自行解決
啟動虛擬機,在gdb中附加虛擬機進行調試
(gdb) file /Library/Developer/KDKs/KDK_10.10.5_14F27.kdk/System/Library/Kernels/kernel.development
Reading symbols from /Library/Developer/KDKs/KDK_10.10.5_14F27.kdk/System/Library/Kernels/kernel.development...Reading symbols from /Library/Developer/KDKs/KDK_10.10.5_14F27.kdk/System/Library/Kernels/kernel.development.dSYM/Contents/Resources/DWARF/kernel.development...
done.
(gdb) target remote localhost:8864
Remote debugging using localhost:8864
0xffffff800f9f1e52 in ?? ()
用lldb進行調試
LLDB實際上支持使用gdb-remote命令連接到GDB遠程機器
(lldb) file /Library/Developer/KDKs/KDK_10.10.5_14F27.kdk/System/Library/Kernels/kernel.development
Current executable set to '/Library/Developer/KDKs/KDK_10.10.5_14F27.kdk/System/Library/Kernels/kernel.development' (x86_64).
(lldb) gdb-remote 8864
Kernel UUID: C75BDFDD-9F27-3694-BB80-73CF991C13D8
Load Address: 0xffffff800f800000
Kernel slid 0xf600000 in memory.
Loaded kernel file /Library/Developer/KDKs/KDK_10.10.5_14F27.kdk/System/Library/Kernels/kernel.development
Loading 87 kext modules ....................................................................................... done.
Target arch: x86_64
Connected to live debugserver or arm core. Will associate on-core threads to registers reported by server.
Process 1 stopped
* thread #3: tid = 0x0066, name = '0xffffff801c91d9c0', queue = 'cpu-0', stop reason = signal SIGTRAP
frame #0: 0xffffffffffffffff
實時斷點
只需要Ctrl + C即可及時在調試器中斷下
MacOS內核調試環境搭建