otool 一些用途

otool

iOS應用所依賴的系統(tǒng)庫檢查

otool(object file displaying tool) : 針對目標文件的展示工具,用來發(fā)現(xiàn)應用中使用到了哪些系統(tǒng)庫,調用了其中哪些方法,使用了庫中哪些對象及屬性,它是Xcode自帶的常用工具。下面是一些常用的命令:

-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

1、依賴的庫查詢:

在iOS開發(fā)中,通常我們Xcode工程的一個最終的Product是app格式,而如果你在Finder中點擊“顯示包內容”看看app里都有什么,你會看到里面的好多東西,有nib、plist、mobileprovision和各種圖片資源等,而其中最重要的一個文件就是被標為“Unix可執(zhí)行文件”(用file查看是Mach-O executable arm)類型的文件。我們的代碼邏輯都在這里面。

比如我們需要分析 釘釘 這個應用所用到的一些系統(tǒng)庫、支持的架構信息及版本號:(部分)

otool -L DingTalk
DingTalk (architecture armv7):
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.4.0)
    /usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
    /usr/lib/libicucore.A.dylib (compatibility version 1.0.0, current version 57.1.0)
    /usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 104.2.0)
    /usr/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.9.0)
    /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.8)
    /System/Library/Frameworks/AVFoundation.framework/AVFoundation (compatibility version 1.0.0, current version 2.0.0)
    /System/Library/Frameworks/Accelerate.framework/Accelerate (compatibility version 1.0.0, current version 4.0.0)
    ...
    ...
    ...
    
DingTalk (architecture arm64):
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.4.0)
    /usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
    /usr/lib/libicucore.A.dylib (compatibility version 1.0.0, current version 57.1.0)
    /usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 104.2.0)
    /usr/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.9.0)
    /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.8)
    /System/Library/Frameworks/AVFoundation.framework/AVFoundation (compatibility version 1.0.0, current version 2.0.0)
    /System/Library/Frameworks/Accelerate.framework/Accelerate (compatibility version 1.0.0, current version 4.0.0)
    /System/Library/Frameworks/AddressBook.framework/AddressBook (compatibility version 1.0.0, current version 30.0.0)
    ...
    ...
    ...

2、匯編碼

其實,查看依賴系統(tǒng)庫僅僅是其中的一小功能。otool命令配上不同的參數可以發(fā)揮很強大的功用。如果使用 otool -tV ex 則整個ARM的匯編碼就都顯示出來了。能看到ARM的匯編碼,那接下來怎么用就看大家的了,想象空間無限啊:

otool -tV DingTalk
DingTalk (architecture armv7):
(__TEXT,__text) section
__ZN12mobilesearch12SearcherImpl13sqlite_hookerEPviPKcS3_x:
011a85cc    f0 b5   push    {r4, r5, r6, r7, lr}
011a85ce    03 af   add r7, sp, #0xc
011a85d0    2d e9 00 0d     push.w  {r8, r10, r11}
011a85d4    ad f1 40 04     sub.w   r4, sp, #0x40
011a85d8    6f f3 03 04     bfc r4, #0, #4
011a85dc    a5 46   mov sp, r4
011a85de    04 f9 ed 82     vst1.64 {d8, d9, d10, d11}, [r4:128]!
011a85e2    04 f9 ef c2     vst1.64 {d12, d13, d14, d15}, [r4:128]
011a85e6    ad f5 24 5d     sub.w   sp, sp, #0x2900
011a85ea    88 b0   sub sp, #0x20
011a85ec    0a 93   str r3, [sp, #0x28]
011a85ee    04 46   mov r4, r0
011a85f0    4f f6 06 50     movw    r0, #0xfd06
011a85f4    0b aa   add r2, sp, #0x2c
011a85f6    c0 f2 4b 10     movt    r0, #0x14b
011a85fa    4f f6 00 23     movw    r3, #0xfa00
011a85fe    c0 f2 4b 13     movt    r3, #0x14b
011a8602    78 44   add r0, pc
011a8604    7b 44   add r3, pc
011a8606    0e 46   mov r6, r1
011a8608    00 68   ldr r0, [r0]
011a860a    1b 68   ldr r3, [r3]
011a860c    df f8 30 15     ldr.w   r1, [pc, #0x530]
011a8610    1b 68   ldr r3, [r3]
011a8612    79 44   add r1, pc
011a8614    13 60   str r3, [r2]
...
...
...

3、查看該應用是否砸殼

otool -l DingTalk | grep -B 2 crypt
          cmd LC_ENCRYPTION_INFO
      cmdsize 20
     cryptoff 16384
    cryptsize 40239104
      cryptid 0
--
          cmd LC_ENCRYPTION_INFO_64
      cmdsize 24
     cryptoff 16384
    cryptsize 45400064
      cryptid 0

cryptid 0(砸殼) 1(未砸殼)

4、 Mach-O頭結構等

其實otool文檔本身的介紹就是:otool是對目標文件或者庫文件的特定部分進行展示。這里舉個例子,看一下其頭部的內容是怎樣的:

otool -h DingTalk
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
 0xfeedface      12          9  0x00           2    71       7148 0x00218085
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
 0xfeedfacf 16777228          0  0x00           2    71       7832 0x00218085

至于各字段的含義,可參看 loader.h

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容