GhidraDec 编译测试
摘要:本文对GhidraDec进行编译和测试,以了解GhidraDec,基本手段是解决编译过程可能遇到的问题和测试对比该插件与原生Ghidra反编译效果,无甚特色。
关键词:Ghidra,IDA,RetDec,反编译
1、介绍
GhidraDec是一款用于IDA Pro的插件,主要目的是在IDA中使用Ghidra的反编译器(decompiler.exe),即类似IDA的F5伪码反编译功能,并非反汇编功能。由于Ghidra 9.x和10.x版本间的协议差异,GhidraDec只支持Ghidra 10.x版本的反编译器,兼容IDA 6/7x版本。支持Ghidra和IDA所支持的处理器。更多信息参考官方说明:
(1) 官仓: https://github.com/GregoryMorse/GhidraDec
(2) Ghidra支持的处理器列表: https://github.com/NationalSecurityAgency/ghidra/tree/master/Ghidra/Processors
(3) IDA支持的处理器列表: https://hex-rays.com/products/ida/processors/
2、编译
2.1 编译要求
这里主要为Windows 10 x64的IDA Pro 7.5编译GhidraDec插件
(1)由于是IDA Pro 7.5的插件,别无二选是IDASDK75;
(2)参考IDASDK75\readme.txt,这里我们选择Visual Studio 2017;
(3)这里我们使用win_flex_bison,版本尽量2.x以上(参考附件);
(4)这里我们使用VC编译环境,搭配cmake进行编译;
(5)git是搬运github代码必备。
图2-1 官方说明的编译要求
2.2 编译
我们主要通过cmd.exe命令行完成编译。
2.2.1 编译环境设置
(1)启动cmd.exe,初始化Visual Studio 2017 x64编译环境;
%comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars64.bat"
(2)添加cmake、win_flex_bison、git路径到path环境变量;
set path=D:\ProgramData\CMake\bin;D:\win_flex_bison;D:\ProgramData\Git\cmd;%path%
(3)搬运GhidraDec和Ghidra代码;
说明,搬运Ghidra主要是使用 Ghidra\Features\Decompiler\src\decompile目录的源码,本次编译测试中需要修正decompiler.cpp源码,附件提供了修正版,如果使用附件的decompile目录代码,也可以忽略下载ghidra代码;虽然GhidraDec有decompile9源码,考虑到官方提到的只支持Ghidra10.x,这里直接使用目前最新的10.x版本源码,至于直接修改decompile9为decompile行不行,这里未作测试。
git clone https://github.com/GregoryMorse/GhidraDec.git
git clone https://github.com/NationalSecurityAgency/ghidra.git
cd ghidra
git checkout
#rem 然后将Ghidra\Features\Decompiler\src\decompile目录复制到GhidraDec目录下,或直接使用附件decompile目录源码
注意:如果环境除了win_flex_bison外,也有其他bison,为避免其他版本可能较低或是其他环境专属带来的干扰,
需要修改GhidraDec\CMakeLists.txt的下述行内容,挑战win_bison的为优先查找顺序。
图2-2 设置win_bison优先于其他版本bison
2.2.2 编译安装
(1)cmake配置和编译
应当注意到,下述-DIDA_SDK_DIR应设置为实际的IDASDK75路径。
cd GhidraDec
mkdir build
cd build
cmake .. -DIDA_SDK_DIR="D:\Program Files\IDA 7.5 SP3\plugins\idasdk75" -G "Visual Studio 15 2017 Win64"
cmake --build . --config Release
(2)可能出现的错误与解决方案
A、"unrecognized: %destructor"错误。这一般是由于bison版本过低造成,换较高版本bison解决。
B、error C2398: 元素“2”: 从“const color_t”转换到“_Elem”。这是编译器太高端所致,这里需要修改decompiler.cpp的源码进行排错。
#问题在于 D:\Program Files\IDA 7.5 SP3\plugins\idasdk75\include\lines.hpp 定义的 color_t 为uchar,而 COLOR_ON 默认为char。
std::string({ COLOR_ON, COLOR_AUTOCMT })
#这里通过static_cast<char> 将所有引起C2398错误的地方修正
std::string({ COLOR_ON, static_cast<char>(COLOR_AUTOCMT) })
图2-3 混合char与uchar引起的C2398错误
(3)将GhidraDec\build\Release目录下的GhidraDec插件ghidradec.dll、ghidradec64.dll复制到IDA的plugins目录即可完成安装。
A、设置decompiler.exe的目录。选择“Options|GhidraDec plugin opitons"进行配置,然后选定ghidra的安装目录,
图2-4 GhidraDec配置选项菜单
图2-5 GhidraDec配置中选定ghidra安装目录
图2-6 通过"Edit|Plugins|Ghidra Decompiler Plugin"或快捷键"Ctrl+G"进行反编译调用
3、测试
这里我们对ghidradec.dll插件本身进行反编译测试。
3.1 Ghidra与GhidraDec对比
如图3-1所示,其左侧是Ghidra的反编译展示,右侧是IDA Pro的GhidraDec插件对同一函数反编译展示。总体而言,函数体反编译的结构基本一致,但GhidraDec很多时候会无法反编译且导致IDA奔溃,且反编译结果没法直接重命名修改,这就非常鸡肋。
图3-1 Ghidra与IDA的GhidraDec反编译比对
3.2 GhidraDec原理
如下是上述行数在GhidraDec反编译时输出的日志信息,可见:
A、GhidraDec先对IDA的idb数据库ghidradec.dll.i64存副本为ghidradec.dll.dec-backup.i64,
B、然后根据IDA反汇编结果转换为IR(llvmIr)存为ghidradec.dll.i64.json,
C、最后通过ghidra的反编译器decompile.exe转换为高级伪码。
[GhidraDec info] : Found decompile.exe at D:\ghidra_10.0_PUBLIC_20210621/Ghidra/Features/Decompiler/os/win64/ -> plugin is properly configured.
[GhidraDec info] : Working on input file "D:\GhidraDec\build\Release\ghidradec.dll".
[GhidraDec info] : Running Ghidra decompiler plugin:
[GhidraDec info] : Saving IDA database ...
[GhidraDec info] : IDA database saved into : D:\GhidraDec\build\Release\ghidradec.dll.dec-backup.i64
[GhidraDec info] : Generating retargetable decompilation DB ...
[GhidraDec info] : Retargetable decompilation DB saved into: D:\GhidraDec\build\Release\ghidradec.dll.i64.json
[GhidraDec info] : Decompile input ...
[GhidraDec info] : Decompilation command: D:\ghidra_10.0_PUBLIC_20210621/Ghidra/Features/Decompiler/os/win64/decompile.exe
[GhidraDec info] : Running the decompilation command ...
[GhidraDec info] : Detected Processor spec: D:\ghidra_10.0_PUBLIC_20210621/Ghidra/Processors\x86/data/languages/x86-64.pspec Compiler spec: D:\ghidra_10.0_PUBLIC_20210621/Ghidra/Processors\x86/data/languages/x86-64-win.cspec Sleigh file: D:\FTP\CTF2021\newtools\ghidra_10.0_PUBLIC_20210621/Ghidra/Processors\x86/data/languages/x86-64.sla
[GhidraDec info] : Local decompilation ...
[GhidraDec info] : Decompiling function: sub_18009B070 @ 0x18009b070
[GhidraDec info] : Decompilation completed: sub_18009B070 in 1 seconds
3.3 Ghidra、GhidraDec、IDA's F5差异
A、Ghidra采用自身的反汇编器,生成自己的IR(llvmIr),然后再用decompile.exe反编译器反编译为高级伪码。
bin --> Ghidra-CPU-disassembler --> Ghidra-disasm --> llvmIr --> decompiler.exe --> pesudo-code
B、IDA's F5采用自身的反汇编器,生成自己的IR(微码),然后再用自己的反编译器对IR反编译为高级伪码。
bin --> IDA-CPU-disassembler --> IDA-disasm --> IDA-mIR --> IDA-decompiler --> pesudo-code
C、GhidraDec是利用IDA反汇编器的输出结果,生成ghidra的IR(llmvlr),再用decompile.exe反编译器反编译为高级伪码。
bin --> IDA-CPU-disassembler --> IDA-disasm --> llvmIr --> decompiler.exe --> pesudo-code
4、总结
目前看来,GhidraDec只是作为打通IDA和ghidra之间通道的一种尝试,可以作为一种思路参考。在IDA-diasm到llvmIR转换的环节缺陷,导致偶发性的无法反编译和奔溃,GhidraDec返回给IDA的反编译结果无法如IDA或ghidra原生环境中都可以进一步修改。纵然有许多缺陷,实用性上也比较鸡肋,但不妨作为融合IDA和ghidra两者优势的试验典型。
附件说明:
[1] win_flex_bions-latest.zip 为找到Windows的2.7版;
[2] decompile.zip为ghidra对应目录的源码,这里主要修正了decompiler.cpp可能导致的C2398编译错误;
[3] GhidraDec_x64Release.zip为编译GhidraDec插件,要正常运行,还需要安装ghidra 10.x。
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课
最后于 2021-7-5 17:42
被tritium编辑
,原因: