7. 逆向工具集和安裝和使用

iOS逆向工程的工具

大致可分為四類:檢測工具、反編譯工具、調試工具、開發工具

  • 檢測工具
    如:Reveal、tcpdump等
  • 反編譯工具(反匯編工具 - 分析二進制文件并得到一些信息)
    如:IDA、Hopper Disassembler、classdump等
  • 調試工具
    如:lldb、Cycript等
  • 開發工具
    如:Xcode、theos等

classdump

class-dump只能導出未經加密的App的頭文件。classdump是對"otool -ov" 信息的翻譯,以一種我們熟悉的易讀的方式呈現。官網http://stevenygard.com/projects/class-dump/

  • class-dump的安裝
    點擊下載后解壓后會有class-dump和源碼文件。將class-dump 復制到/usr/bin/class-dump。如果是OS X 10.11,因為沒有/usr/bin文件夾的寫權限,所以將class-dump復制到/usr/local/bin/class-dump即可。
    同時打開Terminal,執行命令賦予其執行權限:
    sudo chmod 777 /usr/bin/class-dump
  • otool工具簡介
    otool(object file displaying tool) :目標文件的展示工具。可以用來發現應用中使用到了哪些系統庫,調用了其中哪些方法,使用了庫中哪些對象及屬性,它是Xcode自帶的常用工具。

otool命令介紹

-f print the fat headers
-a print the archive header
-h print the mach header
-l print the load commands
-L print shared libraries used
-D print shared library id name
-t print the text section (disassemble with -v)
-p <routine name>  start dissassemble from routine name
-s <segname> <sectname> print contents of section
-d print the data section
-o print the Objective-C segment
-r print the relocation entries
-S print the table of contents of a library
-T print the table of contents of a dynamic shared library
-M print the module table of a dynamic shared library
-R print the reference table of a dynamic shared library
-I print the indirect symbol table
-H print the two-level hints table
-G print the data in code table
-v print verbosely (symbolically) when possible
-V print disassembled operands symbolically
-c print argument strings of a core file
-X print no leading addresses or headers
-m don't use archive(member) syntax
-B force Thumb disassembly (ARM objects only)
-q use llvm's disassembler (the default)
-Q use otool(1)'s disassembler
-mcpu=arg use `arg' as the cpu for disassembly
-j print opcode bytes
-P print the info plist section as strings
-C print linker optimization hints
--version print the version of /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool

otool命令使用

- 依賴庫的查詢
如: otool -L WeChart

- 是否加殼
如:otool -l WeChart | grep -B 2 crypt

classdump的使用

 class-dump -s -S -H /Applications/Memenet/Memenet.app/Contents/MacOS/memenet -o ./MyHeaders

查看文件夾個數

查看某文件夾下文件的個數,包括子文件夾里的。
ls -lR|grep "^-"|wc -l

查看某文件夾下文件夾的個數,包括子文件夾里的。
ls -lR|grep "^d"|wc -l

dumpdecrypted 砸殼工具

下載最新源碼: git clone https://github.com/stefanesser/dumpdecrypted.git
編譯動態庫文件(dumpdecrypted.dylib): make

  1. 將dumpdecrypted.dylib從電腦上復制到手機tmp目錄
scp dumpdecrypted.dylib root@192.168.0.6:/tmp
  1. 定位要砸殼的StoreApp的執行文件名字TargetApp (ps -e 可以得到全路徑)
ps -e | grep WeChat 

得到WeChat的路徑,下邊會用到

/var/mobile/Containers/Bundle/Application/749DC69A-3A8D-4B5C-9926-1220E69FC85F/WeChat.app/WeChat
  1. 定位要砸殼的StoreApp的Document目錄
cycript -p TargetApp [[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0]

得到路徑

#"file:///var/mobile/Containers/Data/Application/986376B5-EF08-4CAF-81FB-CAE48D1FE4AE/Documents/"
  1. 進入Document目錄
 cd  /var/mobile/Containers/Data/Application/986376B5-EF08-4CAF-81FB-CAE48D1FE4AE/Documents/
  1. 拷貝dumpdecrypted.dylib到本路徑下
cp  /tmp/dumpdecrypted.dylib .
  1. 設置DYLD_INSERT_LIBRARIES
    DYLD_INSERT_LIBRARIES=dumpdecrypted.dylib /var/mobile/Containers/Bundle/Application/749DC69A-3A8D-4B5C-9926-1220E69FC85F/WeChat.app/WeChat
  1. 將砸殼后的WeChat.decrypted文件拷貝到電腦上tmp文件夾下
 scp WeChat.decrypted gaoshang@192.168.0.11:/tmp
  1. 在電腦端執行class-dump 命令,就可以得到工程中的header文件
cd /tmp
class-dump -s -S -H WeChat.decrypted -o ./WeChatHeaders

theos

作者:@DHowett,越獄開發工具包

1、 xcode工具集的路徑需要設置正確

查看命令: xcode-select --print-path   
設置命令: xcode-select --switch /Applications/Xcode-beta.app/Contents/Developer 

2、 通過github安裝theos

https://github.com/theos/theos/wiki/Installation

3、 修改所有者

sudo chown -R $(id -u):$(id -g) theos 

theos維基百科:

配置環境變量

export THEOS=/opt/theos

可以寫入~/.bash_profile
source ~/.bash_profile

echo $THEOS

ldid

作者:saurik
維基百科:http://iphonedevwiki.net/index.php/Ldid
越獄iPhone下的簽名工具(更改授權entitlements),可以為thos開發的程序進程簽名(支持在OS X和iOS上運行)。

安裝ldid

 brew install ldid fakeroot

加密算法

  • 對稱加密算法:RC4、DES、3DES、AES128、AES256等。加解密雙方密鑰相同。
  • 非對稱加密算法:RSA、Elgamal等。加解密雙方使用密鑰對。
  • 哈希算法:MD5(16Byte)、SHA1(20Byte)等。任意長度的信息轉換成到某一固定長度的信息摘要(具有唯一性,不可逆性),主要作用是對數據數據完整性校驗。

數字簽名 (蘋果官方的私鑰簽名,公鑰驗證)

數字簽名是非對稱密鑰加密技術與數字摘要技術的應用。對指定信息使用哈希算法,得到一個固定長度的信息摘要,然后再使用 私鑰 (注意必須是私鑰)對該摘要加密,就得到了數字簽名。

數字證書

數字證書是一個文件,由蘋果的 Apple Worldwide Developer Relations Certification Authority(WWDR)證書認證中心進行簽名,其主要作用是用來標示身份。證書文件主要包含兩部分內容:證書信息和證書簽名

  • 證書信息
    包含用戶的公鑰、用戶個人信息、證書頒發機構信息、證書有效期等信息。(這里的用戶主要指開發者)
  • 證書簽名
    WWDR將上述證書本身內容的使用哈希算法得到一個固定長度的信息摘要,然后使用自己的私鑰對該信息摘要加密生成數字簽名。

證書的驗證

iOS系統原本就持有WWDR的公鑰,系統首先會對證書內容通過指定的哈希算法計算得到一個信息摘要;然后使用WWDR的公鑰對證書中包含的數字簽名解密,從而得到經過WWDR的私鑰加密過的信息摘要;最后對比兩個信息摘要,如果內容相同就說明該證書可信。在驗證了證書是可信的以后,iOS系統就可以獲取到證書中包含的開發者的公鑰,并使用該公鑰來判斷代碼簽名的可用性了。

證書存在的意義

通過證書使用過程可以看出,證書本身只是一個容器,用來承載開發者的公鑰。iOS通過驗證證書的合法性來確保開發者公鑰的合法性。

  • 代碼簽名與驗證(開發者的私鑰簽名,公鑰驗證)打包過程中使用開發者私鑰對應用進行簽名。
  • 開發者的公鑰被包含在數字證書里,數字證書又被包含在描述文件(Provisioning File)中,描述文件在應用被安裝的時候會被拷貝到iOS設備中。iOS安全系統通過證書就能夠確定開發者身份,就能夠通過從證書中獲取到的公鑰來驗證開發者用該公鑰對應的私鑰簽名后的代碼、資源文件等有沒有被更改破壞,最終確定應用能否合法的在iOS設備上合法運行。

工具的使用

查看codesign load command
otool -l WeChat | grep -A 5 SIGNATURE

查看簽名信息
?  tmp codesign -dvvv WeChat
Executable=/private/tmp/WeChat
Identifier=com.tencent.xin
Format=Mach-O universal (armv7 arm64)
CodeDirectory v=20200 size=448783 flags=0x0(none) hashes=14017+5 location=embedded
Hash type=sha256 size=32
CandidateCDHash sha1=6e2f8a93dbe63c17ea3b3a3dc032826b9eddf2b7
CandidateCDHash sha256=d6f1afe23b598a76301711a4a62a5505a749a12a
Hash choices=sha1,sha256
CDHash=d6f1afe23b598a76301711a4a62a5505a749a12a
Signature size=3925
Authority=Apple iPhone OS Application Signing
Authority=Apple iPhone Certification Authority
Authority=Apple Root CA
Info.plist=not bound
TeamIdentifier=88L2Q4487U
Sealed Resources=none
Internal requirements count=1 size=96
查看entitlement內容
codesign -d --entitlements - WeChat
ldid -e WeChat

修改entitlement內容
ldid -Sentitlement.xml  WeChat

dpkg工具

- 安裝
$ brew install --from-bottle https://raw.githubusercontent.com/Homebrew/homebrew-core/7a4dabfc1a2acd9f01a1670fde4f0094c4fb6ffa/Formula/dpkg.rb
$ brew pin dpkg

-  使用
dpkg -i  xxxxx.deb   deb包安裝
dpkg -r  com.xxxxx.xxxx  deb包卸載
dpkg -s com.iosre.myiosreproject 查看安裝包信息
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容