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

3,620 阅读2分钟

0、 啥玩意

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

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

1、 安装框架

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

当前环境 Mac Pro 10.14.4,所以可以直接通过homebrew安装

$ brew install unicorn

这样的话,其实unicorn 的核心框架就安装好了,这时候可以直接编写c代码调用。但是如果想要用其他语言需要用其他语言对应的包装器。

官网文档说通过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

2、运行官方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