這個系列,是很早聽 MJ 課程時的整理,如今分享出來。 其中一些參考資料有些有引用,有些可能忘記添加了,若是有引用部分資料,能夠聯繫我。html
iOS 逆向(一)環境搭建
iOS 逆向(二)Cycript
iOS 逆向(三)逆向工具
iOS 逆向(四)脫殼
審覈中 iOS 逆向(五)Theos工具 iOS 逆向(六)動態調試
iOS 逆向(七)重簽名git
對Mach-O文件的靜態分析,有如下工具:github
對運行中的APP進行代碼調試,工具包括:緩存
注入代碼到APP中,必要時還可能須要從新簽名、打包ipa。sass
具體實現,參考逆向(五)Theos工具、逆向(六)動態調試。bash
Cycript在上篇文章中已經介紹。因此此處着重介紹Reveal。markdown
Mac版本架構
官網:revealapp.com 經過郵箱可得到14天試用期.app
手機版編輯器
Reveal Loader
必定要安裝該源的loader。
安裝完Reveal Loader後,打開【設置】,找到Reveal
,選擇須要調試的APP
找到Mac的Reveal中的RevealServer
文件,覆蓋iPhone的/Library/RHRevealLoader/RevealServer
文件
以後,最好重啓桌面,能夠在iPhone上輸入終端命令
打開Mac版本Reveal,在手機上開啓容許Reveal調試的APP,Reveal mac就會出現調試APP。
如圖按照上述步驟沒有連上app,問題解決 1)安裝 Reveal2Loader 2) 打開Reveal選擇頂部菜單Help->Show Reveal Library in Finder->iOS Library,把RevealServer.frameworkr拷貝到手機Device->Library->Frameworks文件夾下,拷貝方式能夠經過iFunBox手動操做 3)從新啓動手機以後能夠看到app顯示出來
顧名思義,它的做用就是把Mach-O文件的class信息給dump出來(把類信息給導出來),生成對應的.h頭文件
下載地址見文末,下載完成後,將class-dump文件複製到Mac的/usr/local/bin
目錄,這樣在終端就能識別class-dump
命令了
經常使用格式
//直接在控制檯輸出
$ class-dump ~/Desktop/jike
//-H 表示要生成頭文件
//-o 用於制定頭文件的存放目錄
$ class-dump -H Mach-O文件路徑 -o 頭文件存放目錄
複製代碼
Hopper Disassmbler可以將Mach-O文件的機器語言代碼反編譯成彙編代碼、OC僞代碼或者Swift僞代碼
另外,IDA一樣是反彙編工具,做用同Hopper相似。
不一樣的OC代碼,編譯出來的彙編代碼多是同樣的
可是在同一種架構平臺下,每一條彙編指令都有與之對應的惟一的機器指令
經常使用快捷鍵
Shift + Option + X 找出哪裏引用了這個方法
MachOView是查看Mach-O文件的工具,Mach-O是蘋果平臺的可執行文件格式,其具體能夠參考Mach-O(一)結構、Mach-O(二)內存分佈。
該工具是開源工具,可從Github下載。
MachOExploer和MachOView功能同樣,也是開源工具。Github地址
在iOS中,系統庫,如UIKit、Foundation等,蘋果爲了提升效率,將這些庫都打包成一個動態庫,並在系統啓動的時候加載,這個集合庫,就叫作共享庫緩存。
咱們在逆向時,爲了分析代碼,須要將該共享庫提取出來。
在macOS/iOS中,是使用了/usr/lib/dyld
程序來加載動態庫。
dyld,dynamic link editor(動態連接編輯器),也叫dynamic loader(動態加載器)。
因此第一種方式,直接以dyld的方式提取。dyld源碼地址參考文末。
下載最新代碼,本文更新時,最新版本是dyld-635.2
可使用dyld源碼中的launch-cache/dsc_extractor.cpp
將#if 0前面的代碼刪除(包括#if 0),把最後面的#endif也刪掉
最後只剩下:
#include <stdio.h>
#include <stddef.h>
#include <dlfcn.h>
typedef int (*extractor_proc)(const char* shared_cache_file_path, const char* extraction_root_path,
void (^progress)(unsigned current, unsigned total));
int main(int argc, const char* argv[])
{
if ( argc != 3 ) {
fprintf(stderr, "usage: dsc_extractor <path-to-cache-file> <path-to-device-dir>\n");
return 1;
}
//void* handle = dlopen("/Volumes/my/src/dyld/build/Debug/dsc_extractor.bundle", RTLD_LAZY);
void* handle = dlopen("/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/lib/dsc_extractor.bundle", RTLD_LAZY);
if ( handle == NULL ) {
fprintf(stderr, "dsc_extractor.bundle could not be loaded\n");
return 1;
}
extractor_proc proc = (extractor_proc)dlsym(handle, "dyld_shared_cache_extract_dylibs_progress");
if ( proc == NULL ) {
fprintf(stderr, "dsc_extractor.bundle did not have dyld_shared_cache_extract_dylibs_progress symbol\n");
return 1;
}
int result = (*proc)(argv[1], argv[2], ^(unsigned c, unsigned total) { printf("%d/%d\n", c, total); } );
fprintf(stderr, "dyld_shared_cache_extract_dylibs_progress() => %d\n", result);
return 0;
}
複製代碼
編譯dsc_extractor.cpp,得到dsc_extractor
$ clang++ -o dsc_extractor dsc_extractor.cpp
複製代碼
或者
$ clang++ -o dsc_extractor ./dsc_extractor.cpp dsc_iterator.cpp
複製代碼
動態庫共享緩存在iPhone的目錄爲:/System/Library/Caches/com.apple.dyld
,將其拷貝到電腦上:
而後解析出動態庫:
// dsc_extractor 緩存 輸出文件夾
$ dsc_extractor dyld_shared_cache_armv7s armv7s
複製代碼
工具地址:JTool
用法
//提取UIKit動態庫
$ jtool -extract UIKit path/to/dyld_shared_cache
//提取所有動態庫,注意,該提取動做會產生超大的文件,10G+
$ jtool -lv cache_armv7 | cut -c 24- | tail +5 | while read line ; do jtool -extract $line cache_armv7 ; done
複製代碼
dyld_cache_extract 是一個GUI工具,蠻方便的工具。
【越獄-逆向】處理iOS APP信息的命令行工具。MJAppTools有詳細說明。
其它經常使用命令,好比lldb、otool、nm、codesign。
連接