radare2 通过 r2pipe 脚本,利用 retdec.com 的 REST API 提供了反编译的功能,所以你首先要到网站上注册,拿到免费的 API key。
安装上该模块,当然你可能需要先安装上 npm,它是 JavaScript 的包管理器:
$ git clone https://github.com/jpenalbae/r2-scripts.git
$ cd r2-scripts/decompiler/
$ npm install
将 API key 写入到 ~/.config/radare2/retdec.key 中,然后就可以开心地反编译了。
还是 helloworld 的例子,用 r2 打开,反编译 main 函数。
[0x000003e0]> #!pipe node /home/firmy/r2-scripts/decompiler/decompile.js @ mainStart: 0x51dEnd: 0x558Uploading binary to retdec.comPlease wait for decompilation to finish....//// This file was generated by the Retargetable Decompiler// Website: https://retdec.com// Copyright (c) 2017 Retargetable Decompiler <info@retdec.com>//#include<stdint.h>#include<stdio.h>// ------------------------ Functions -------------------------// Address range: 0x51d - 0x558intmain() {int32_t v1;int32_t v2 =__x86_get_pc_thunk_ax((int32_t)&v1,0);puts((char*)(v2 +175));return0;}// --------------- Dynamically Linked Functions ---------------// int puts(const char * s);// --------------------- Meta-Information ---------------------// Detected compiler/packer: gcc (7.2.0)// Detected functions: 1// Decompiler release: v2.2.1 (2016-09-07)// Decompilation date: 2017-12-15 07:48:04
每次输入反编译器路径是不是有点烦,在文件 ~/.config/radare2/radare2rc 里配置一下 alias 就好了,用 $decompile 替代:
# Alias
$decompile=#!pipe node /home/user/r2-scripts/decompiler/decompile.js
[0x000003e0]> $decompile -h
Usage: $decompile [-acChps] [-n naming] @ addr
-a: disable selective decompilation (decompile the hole file)
-c: clear comments
-C: save decompilation results in r2 as a comment
-p: produce python code instead of C
-s: silent. Do not display messages
-h: displays this help menu
-n naming: select variable naming
Where valid variable namings are:
readable: Tries to produce as meaningful variable names as possible
address: Variables are named by their addresses in the binary file
hungarian: Prefix variables with their type
simple: Name variables simply by assigning fruit names
unified: Globals, locals and parameters are named just gX, vX and aX
**********************************************************************
This will upload the binary being analyzed to retdec.com !!!
You have been warned...
**********************************************************************