我们经常发现部分很古老的生信软件是用python编写并且用pyinstaller打包而成的windows下可执行文件,但是很多时候我们在研究算法的时候去作者/官方主页上去查找当时的链接,发现已然被墙(很多以前的生信软件基本都放在谷歌code上) or 404(可能作者已经毕业且远离科研行业不在维护了),对于这种情况我们无需掌握复杂的反编译技巧,利用现有的工具可以完成exe到py文件的转换。

1)第一步解压exe,这里我们利用到一个开源软件pyinstxtractor:


# 解包你的exe文件(前提是此文件是pyinstaller打包而成的,非pyinstaller无法解包)
python pyinstxtractor 解包文件名称.exe

成功解包后你会发现在当前目录下面出现一个【程序名+_extracted】的文件夹。

2)反编译pyc文件,对于解包出来的文件是出于python编译的文件,此时利用 uncompyle6进行反编译pyc到py

安装 uncompyle6 :

pip install uncompyle6
Usage:
  uncompyle6 [OPTIONS]... [ FILE | DIR]...
  uncompyle6 [--help | -h | --V | --version]

Examples:
  uncompyle6      foo.pyc bar.pyc       # decompile foo.pyc, bar.pyc to stdout
  uncompyle6 -o . foo.pyc bar.pyc       # decompile to ./foo.pyc_dis and ./bar.pyc_dis
  uncompyle6 -o /tmp /usr/lib/python1.5 # decompile whole library

Options:
  -o <path>     output decompiled files to this path:
                if multiple input files are decompiled, the common prefix
                is stripped from these names and the remainder appended to
                <path>
                  uncompyle6 -o /tmp bla/fasel.pyc bla/foo.pyc
                    -> /tmp/fasel.pyc_dis, /tmp/foo.pyc_dis
                  uncompyle6 -o /tmp bla/fasel.pyc bar/foo.pyc
                    -> /tmp/bla/fasel.pyc_dis, /tmp/bar/foo.pyc_dis
                  uncompyle6 -o /tmp /usr/lib/python1.5
                    -> /tmp/smtplib.pyc_dis ... /tmp/lib-tk/FixTk.pyc_dis
  --compile | -c <python-file>
                attempts a decompilation after compiling <python-file>
  -d            print timestamps
  -p <integer>  use <integer> number of processes
  -r            recurse directories looking for .pyc and .pyo files
  --fragments   use fragments deparser
  --verify      compare generated source with input byte-code
  --verify-run  compile generated source, run it and check exit code
  --syntax-verify compile generated source
  --linemaps    generated line number correspondencies between byte-code
                and generated source output
  --encoding  <encoding>
                use <encoding> in generated source according to pep-0263
  --help        show this message

Debugging Options:
  --asm     | -a        include byte-code       (disables --verify)
  --grammar | -g        show matching grammar
  --tree={before|after}
  -t {before|after}     include syntax before (or after) tree transformation
                        (disables --verify)
  --tree++ | -T         add template rules to --tree=before when possible

Extensions of generated files:
  '.pyc_dis' '.pyo_dis'   successfully decompiled (and verified if --verify)
    + '_unverified'       successfully decompile but --verify failed
    + '_failed'           decompile failed (contact author for enhancement)

反编译pyc,这个时候你可以写一个脚本批量转换,也可以单独转换:


# 终端执行如下命令
uncompyle6 -o 一个例子.py 一个例子.pyc

如此,你会发现你熟悉的代码又呈现在你的眼前了。

参考文章:

1.http://www.pyinstaller.org