iOS 逆向工程總結

越獄環境

arm64架構php

  • iPhone 5s 及之後機型
  • iPad Air,iPad mini2 及之後機型
  • iOS 8 ~ iOS 10 完美越獄

檢查手機及對應版本是否能夠越獄:jailbreak.25pp.com/iospython

Cydia 安裝

軟件源

  • apt.25pp.com
  • apt.saurik.com
  • Bigboss

插件

  • Apple File Conduit
  • AppSync Unified
  • iFile
  • pp助手
  • openssh
  • Cycript
  • adv-cmds
  • reveal loader
  • Vi IMproved

Mac 上軟件安裝

逆向環境

$ 爲 Mac 終端命令,# 爲 iPhone 終端命令ios

Wi-Fi 鏈接

默認密碼 alpinegit

$ ssh root@<iPhone-IP-Address>
複製代碼

服務器身份信息變動

刪除公鑰信息(~/.ssh/known_hostsgithub

$ ssh-keygen -R <iPhone-IP-Address>
複製代碼

免密 SSH 登錄 iPhone

$ ssh-keygen
$ ssh-copy-id root@<iPhone-IP-Address>
$ scp ~/.ssh/id_rsa.pub root@<iPhone-IP-Address>:~
$ ssh root@<iPhone-IP-Address>
# mkdir .ssh
# cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
# rm ~/id_rsa.pub
# chmod 755 ~
# chmod 755 ~/.ssh
# chmod 644 ~/.ssh/authorized_keys
複製代碼

USB 調試

下載 usbmuxd~/Documents,把 iPhone 的 22 端口映射到本機 localhost 的 10010 端口objective-c

$ cd ~/Documents/usbmuxd-1.0.8/python-client
$ python tcprelay.py -t 22:10010 10011:10011
複製代碼

新建一個命令行窗口,經過鏈接 localhost 的 10010 端口即鏈接 iPhone 的 22 端口express

$ ssh root@localhost -p 10010
複製代碼

iOS 終端的中文亂碼問題

~/.inputrc 裏添加如下內容:sass

set convert-meta off 
set output-meta on
set meta-flag on 
set input-meta on
複製代碼

Cycript

經常使用工具:mjcriptbash

列出全部進程服務器

# ps -A
複製代碼

進入 App 的 Cycript 環境

# cycript -p <pid or app_exec_name>
複製代碼

經常使用語法

[UIApplication sharedApplication]
UIApp
var app = UIApp.keyWindow
#<address_value>
ObjectiveC.classes
*UIApp
UIApp.keyWindow.recursiveDescription().toString()
choose(UIViewController)
複製代碼

Mach-O 文件

#define MH_OBJECT 0x1 /* relocatable object file */
#define MH_EXECUTE 0x2 /* demand paged executable file */
#define MH_FVMLIB 0x3 /* fixed VM shared library file */
#define MH_CORE 0x4 /* core file */
#define MH_PRELOAD 0x5 /* preloaded executable file */
#define MH_DYLIB 0x6 /* dynamically bound shared library */
#define MH_DYLINKER 0x7 /* dynamic link editor */
#define MH_BUNDLE 0x8 /* dynamically bound bundle file */
#define MH_DYLIB_STUB 0x9 /* shared library stub for static */
                           /* linking only, no section contents */
#define MH_DSYM 0xa /* companion file with only debug */
                           /* sections */
#define MH_KEXT_BUNDLE 0xb /* x86_64 kexts */
複製代碼
常見 Mach-O 描述
MH_OBJECT 目標文件(.o)靜態庫文件(.a)即多個 .o 合併在一塊兒
MH_DYLIB 動態庫 dylibframework
MH_DYLINKER 動態連接編輯器 /usr/lib/dyld
MH_DSYM 存儲二進制文件符號信息的文件.dSYM/Contents/Resources/DWARF/xxx(經常使用於分析APP的崩潰信息)

查看Mach-O的文件類型

$ file <file_dir>
複製代碼

導出某種特定架構

$ lipo <mach_o_file_dir> -thin arm64 -output <output_dir>
複製代碼

合併多種架構

$ lipo <mach_o_file_1> <mach_o_file_2> -output <output_dir>
複製代碼

脫殼

工具

查看可執行文件是否已脫殼

$ otool -l <exec_dir> | grep crypt
複製代碼

crypt0 則已脫殼

make 編譯後獲得的 dumpdecrypted.dylib 放到 /var/root/ 下,使用 ps -A 獲取 App 可執行文件

# DYLD_INSERT_LIBRARIES=dumpdecrypted.dylib <exec_dir>
複製代碼

導出頭文件

使用工具 class-dump 導出 Objective-C 編寫的 App 的頭文件

$ class-dump -H <app_exec_dir> -o <headers_folder_output_dir>
複製代碼

Theos

安裝簽名工具 ldid

$ brew install ldid
複製代碼

配置環境變量,在 .bash_profile 里加入:

$ export THEOS=~/theos
$ export PATH=$THEOS/bin:$PATH
$ export THEOS_DEVICE_IP=127.0.0.1
$ export THEOS_DEVICE_PORT=10010
複製代碼

下載 theos

$ cd ~ && git clone --recursive https://github.com/theos/theos.git $THEOS
複製代碼

建立 tweak 項目

$ nic.pl
複製代碼

選擇 [iphone/tweak]

MobileSubstrate Bundle filter: 破解 App 的 Bundle ID

打開 Tweak.xm 文件編寫代碼

%hook XXXView
- (id)initWithFrame:(struct CGRect)arg1 {
  return nil; 
}
%end
複製代碼

編譯打包安裝

$ make clean && make && make package && make install
複製代碼

其餘資料:

目錄結構:github.com/theos/theos… 環境變量:iphonedevwiki.net/index.php/T… Logos語法:iphonedevwiki.net/index.php/L…

動態調試

複製 iPhone 上 /Developer 裏的 debugserver,導出 entitlements 權限文件

$ ldid -e debugserver > debugserver.entitlements
複製代碼

打開 entitlements 文件,增長如下兩項:

  • get-task-allow
  • task_for_pid-allow

Boolen 值,設爲 YES

而後把 entitlements 權限從新籤給 debugserver

$ ldid -Sdebugserver.entitlements debugserver
複製代碼

再把 debugserver 移動到 iPhone 的 /usr/bin

把 debugserver 附加到 App 上

# debugserver *:10011 -a <pid or app_exec_name>
複製代碼

啓動 lldb 並鏈接 debugserver

$ lldb
(lldb) process connect connect://<iPhone-IP-Address>:10011
複製代碼

經過 debugserver 啓動 App

# debugserver -x auto *:10011 <app_exec_dir>
複製代碼

LLDB

執行一個表達式

(lldb) expression self.view.backgroundColor = [UIColor redColor]
複製代碼

--爲命令選項結束符

打印線程堆棧信息

(lldb) thread backtrace
(lldb) bt
複製代碼

讓函數不執行斷電後的內容並直接返回

(lldb) thread return
複製代碼

打印當前棧幀變量

(lldb) frame variable
複製代碼

繼續運行

(lldb) thread continue
(lldb) continue
(lldb) c
複製代碼

單步運行(子函數爲一步)

(lldb) thread step-over
(lldb) next
(lldb) n
(lldb) ni  # instruction
複製代碼

單步運行(遇到子函數則進入)

(lldb) thread step-in
(lldb) step
(lldb) s
(lldb) si  # instructios
複製代碼

執行完當前函數全部代碼,返回上一個函數

(lldb) thread step-out
(lldb) finish
複製代碼

斷點

(lldb) breakpoint set -n test
(lldb) breakpoint set -n touchesBegan:withEvent:
(lldb) breakpoint set -n "-[ViewController touchesBegan:withEvent:]"
(lldb) breakpoint set -r <regex_expression>

(lldb) breakpoint list
(lldb) breakpoint enable <bpt_no>
(lldb) breakpoint disable <bpt_no>
(lldb) breakpoint delete <bpt_no>
複製代碼

內存斷點(在內存數據發生改變時觸發)

(lldb) watchpoint set variable self->age
(lldb) watchpoint set expression &(self->_age)
複製代碼

查找某個類型的信息

(lldb) image lookup -t <type>
複製代碼

根據內存地址查找在模塊中的位置

(lldb) image lookup -a <address>
複製代碼

查找某個符號或者函數的位置

(lldb) image lookup -n <symbol_name or func_name>
複製代碼

列出所加載模塊的信息

(lldb) image list
(lldb) image list -o -f  # 打印模塊全路徑和偏移地址
複製代碼

重簽名

獲取使用付費證書在 Xcode 編譯後的 App 包中的 embedded.mobileprovision 文件

embedded.mobileprovision 提取 entitlements.plist 權限文件

$ security cms -D -i embedded.mobileprovision > tmp.plist
$ /usr/libexec/PlistBuddy -x -c 'Print :Entitlements' tmp.plist > entitlements.plist
複製代碼

查看可用證書

$ security find-identity -v -p codesigning
複製代碼

.app 包內的動態庫,AppExtension 等進行重簽名

$ codesign -fs <cer_id or cer_str> <dylib_dir>
複製代碼

.app 包進行重簽名

$ codesign -fs <cer_id or cer_str> --entitlements entitlements.plist <app_file>
複製代碼

GUI工具:

提取 embedded.mobileprovision 拷貝到 .app 內部後,再使用 iOS App Signer 工具進行重簽名

動態庫注入

下載工具包 insert_dylib,編譯後把可執行文件放入 /usr/local/bin

$ cd <app_file_exec_dir>
複製代碼

在 iPhone 的 /Library/MobileSubstrate/DynamicLibararies 目錄下找到把編寫的 tweak 項目生成的動態庫,把它拷貝出來,注入到 App 可執行文件中

$ insert_dylib @executable_path/<tweak_dylib_name> <app_exec_name> --all-yes --waek <app_exec_name>
複製代碼

更改動態庫加載地址

拷貝 iPhone 的 /Library/Frameworks/CydiaSubstrate.framework/CydiaSubstratetweak 項目生成的動態庫同級目錄下

更改 tweak 項目生成的動態庫依賴項目錄

$ install_name_tool -change /Library/Frameworks/CydiaSubstrate.framework/CydiaSubstrate @loader_path/CydiaSubstrate <tweak_dylib_name>
複製代碼

重簽名後安裝 ipa 文件到非越獄手機上

關於更多內容能夠查看個人我的博客

以上大部分代碼和工具包能夠在個人 GitHub 裏找到

相關文章
相關標籤/搜索