insert_dylib, 命令行 實用工具,用於將dylib載入命令插入到 Mach O 二進位文件中

分享于 

4分钟阅读

GitHub

  简体
Command line utility for inserting a dylib load command into a Mach-O binary
  • 源代码名称:insert_dylib
  • 源代码网址:http://www.github.com/Tyilo/insert_dylib
  • insert_dylib源代码文档
  • insert_dylib源代码下载
  • Git URL:
    git://www.github.com/Tyilo/insert_dylib.git
    Git Clone代码到本地:
    git clone http://www.github.com/Tyilo/insert_dylib
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/Tyilo/insert_dylib
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    insert_dylib

    命令行用于将dylib load命令插入mach o二进制文件的工具。

  • LC_LOAD_DYLIB load命令添加到load命令的末尾
  • 增加header的mach ncmds,并调整它sizeofcmds
  • (移除代码签名如果有)
  • 用法

    
    Usage: insert_dylib dylib_path binary_path [new_binary_path]
    
    
    Option flags: --inplace --weak --overwrite --strip-codesig --no-strip-codesig --all-yes
    
    
    
    

    insert_dylib插入load命令以在binary_path中加载dylib_path

    例子

    
    $ cat > test.c
    
    
    int main(void) {
    
    
     printf("Testingn");
    
    
     return 0;
    
    
    }
    
    
    ^D
    
    
    $ clang test.c -o test &> /dev/null
    
    
    $ insert_dylib /usr/lib/libfoo.dylib test
    
    
    The provided dylib path doesn't exist. Continue anyway? [y/n] y
    
    
    Added LC_LOAD_DYLIB to test_patched
    
    
    $ ./test
    
    
    Testing
    
    
    $ ./test_patched
    
    
    dyld: Library not loaded: /usr/lib/libfoo.dylib
    
    
     Referenced from: /Users/Tyilo/./test_patched
    
    
     Reason: image not found
    
    
    Trace/BPT trap: 5
    
    
    
    
    otooldiff在原始二进制文件和修补的二进制文件之间
    
    $ diff -u <(otool -hl test) <(otool -hl test_patched)
    
    
    --- /dev/fd/63 2014-07-30 04:08:40.000000000 +0200
    
    
    +++ /dev/fd/62 2014-07-30 04:08:40.000000000 +0200
    
    
    @@ -1,7 +1,7 @@
    
    
    -test:
    
    
    +test_patched:
    
    
     Mach header
    
    
     magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
    
    
    - 0xfeedfacf 16777223 3 0x80 2 16 1296 0x00200085
    
    
    + 0xfeedfacf 16777223 3 0x80 2 17 1344 0x00200085
    
    
     Load command 0
    
    
     cmd LC_SEGMENT_64
    
    
     cmdsize 72
    
    
    @@ -231,3 +231,10 @@
    
    
     cmdsize 16
    
    
     dataoff 8296
    
    
     datasize 64
    
    
    +Load command 16
    
    
    + cmd LC_LOAD_DYLIB
    
    
    + cmdsize 48
    
    
    + name /usr/lib/libfoo.dylib (offset 24)
    
    
    + time stamp 0 Thu Jan 1 01:00:00 1970
    
    
    + current version 0.0.0
    
    
    +compatibility version 0.0.0
    
    
    
    
    --weak选项
    
    $ insert_dylib --weak /usr/lib/libfoo.dylib test test_patched2
    
    
    The provided dylib path doesn't exist. Continue anyway? [y/n] y
    
    
    Added LC_LOAD_WEAK_DYLIB to test_patched2
    
    
    $ ./test_patched2
    
    
    Testing
    
    
    
    

    删除代码签名

    如果只执行此codesign_allocate (由codesignldid使用)将失败,并显示错误:

    
    .../codesign_allocate: file not in an order that can be processed (link edit information does not fill the __LINKEDIT segment):
    
    
    
    

    COM  COMM  mac  Comma  BIN  命令行  
    相关文章