Mac 上安装so黑盒测试工具 Unicorn

0、 啥玩意

Unicorn (The ultimate CPU emulator) ,能够简单理解为,能够在电脑上运行任意cpu 架构 指令 的一个框架。html

对我来讲,一个打包好的apk里面的动态库,也就是libxxx.so 是没办法直接在pc环境下调用的。可是借助这工具就能够了。java

一、 安装框架

官网提供好几种安装方式。源码编译安装,homebrew安装,等等。。。python

当前环境 Mac Pro 10.14.4,因此能够直接经过homebrew安装git

$ brew install unicorngithub

这样的话,其实unicorn 的核心框架就安装好了,这时候能够直接编写c代码调用。可是若是想要用其余语言须要用其余语言对应的包装器。bash

官网文档说经过homebrew安装的软件会安装在他本身的目录下面,若是想要被别的应用可以引用到的话须要导出:架构

$ export DYLD_LIBRARY_PATH=/usr/local/opt/unicorn/lib/:$DYLD_LIBRARY_PATH框架

个人理解是须要配置环境变量,可是如今我没有配置,依然能用,不知道为啥?函数

官网的文档里面说的若是是用Python的就很简单了,若是用Python的上面的步骤都省了,直接 pip install unicorn (可能mac和Linux须要root权限运行)。貌似这样安装的话 unicorn 的核心已经嵌入到了Python库里面,可是若是经过编译Python代码的方式安装的话须要提早安装 unicorn 的核心。也就是上面经过homebrew安装的。工具

至于java的话,须要在安装了核心库的基础上,再编译一个包装库(官方叫作bindings),而后咱们本身开发的话须要调用这个包装器(bindings)。看sample里面的代码吧。Source archive

二、运行官方c代码sample

官网地址

压缩文件地址

能够下载这个压缩包。而后进入到目录

$ make ## 命令行输入
##而后会看到打印以下信息
cc  test1.c -L/usr/local/Cellar/glib/2.44.1/lib -L/usr/local/opt/gettext/lib -lglib-2.0 -lintl  -lpthread -lm -lunicorn -o test1

$ ./test1  ## 命令行输入,调用编译好的可执行文件
Emulate i386 code
Emulation done. Below is the CPU context
>>> ECX = 0x1235
>>> EDX = 0x788f
复制代码
2.1 编译过程的一个错误

当我运行make的时候获得以下错误:

cc  test1.c -L/usr/local/Cellar/glib/2.58.3/lib -L/usr/local/opt/gettext/lib -lglib-2.0 -lintl -Wl,-framework -Wl,CoreFoundation -pthread -lm -lunicorn -o test1
ld: warning: directory not found for option '-L/usr/local/opt/gettext/lib'
ld: library not found for -lintl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test1] Error 1
复制代码

大概的意思是说找不到 /usr/local/opt/gettext/lib 这个路径,缘由是我电脑上没有安装 gettext 的这个工具,参考文章

解决办法,安装这个库。而后就能够了。

$ brew install gettext

PS:只谈安装没什么卵用,接下来经过一个列子分析一下,通常来讲Android apk 里面的动态库都是 arm架构的,这个动态库是不能直接在电脑上调用的,咱们经过这个框架来实现调用。调用一个没有源码的arm架构的so库里面的函数。想要调用so的里面的函数,须要先把so反汇编

3. bing python

安装官网提示,不管我用Python2.7 执行 pip install unicorn 仍是 Python 3.x 执行 pip3 install unicorn 都会不断的获得以下错误,大概意思是找不到,libunicorn.dylib 可是这个lib是经过homebrew安装的,在 /usr/local/opt/unicorn/lib/ 路径下面,我最后也没搞清楚为啥找不到??????

不断的提示错误:

ERROR: pthread check failed
       Make sure to have the pthread libs and headers installed.

make[1]: *** [qemu/config-host.h-timestamp] Error 1
error: [Errno 2] No such file or directory: 'libunicorn.dylib'
make: *** [install3] Error 1
复制代码

而后我下载了,他的Python库的源码,而后想经过源码的方式安装 wiki

For Python users (this must be done after installing the core as above) cd bindings/python; sudo make install

可是我依然获得这个错误,而后我去他的setup.py 查看源码逻辑。而后搜一下这个'libunicorn.dylib'在哪里用到了。

# check if a prebuilt library exists
    # if so, use it instead of building
    #看这段的逻辑,说要判断这两个文件是否存在,若是存在就copy到Python的编译环境里面,我去看了一下发现不存在,
    #LIBRARY_FILE=unicorn-1.0.1/bindings/python/prebuilt/libunicorn.dylib
    #STATIC_LIBRARY_FILE=unicorn-1.0.1/bindings/python/prebuilt/libunicorn.a
    #而后我手动把 /usr/local/opt/unicorn/lib/ 目录里面的文件copy过来而后就能安装了
    if os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE)) \
            and os.path.exists(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE)):
        shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', LIBRARY_FILE), LIBS_DIR)
        shutil.copy(os.path.join(ROOT_DIR, 'prebuilt', STATIC_LIBRARY_FILE), LIBS_DIR)
        return
        
复制代码

而后安装官网的教程运行:$ sudo make install3

之因此用install3,是由于我电脑上用的Python3

相关文章
相关标签/搜索