IDA Pro > IDA Pro教程 > 技术问题 > IDA Pro反编译器SDK

IDA Pro反编译器SDK

发布时间:2022-10-11 17: 28: 46

Below is the full source code of a sample plugin. It performs a quite useful transformation of the pseudocode: replaces zeroes in pointer contexts with NULLs. A NULL immediately conveys the idea that the current expression is pointer-related. This is especially useful for unknown function arguments.

 

The plugin is fully automatic. It hooks to the decompiler events and waits for the pseudocode to be ready. At that moment it takes control and modifies the ctree.

 

The conversion is performed by the convert_zeroes() function. It visits all expressions of the ctree and checks for pointer contexts. If a expression has a pointer type, then the make_null_if_zero() function is called for it. This function checks if the expression is a zero constant and converts it if necessary.

 

The plugin can be turned on or off by its menu item in the Plugins submenu.

 

The code is short and straightforward. Use it as a template for your plugins.

 

 

/*
*      Hex-Rays Decompiler project
*      Copyright (c) 2007-2019 by Hex-Rays, support@hex-rays.com
*      ALL RIGHTS RESERVED.
*
*      Sample plugin for Hex-Rays Decompiler.
*      It automatically replaces zeroes in pointer contexts with NULLs.
*      For example, expression like
*
*              funcptr = 0;
*
*      will be displayed as
*
*              funcptr = NULL;
*
*      Due to highly dynamic nature of the decompier output, we must
*      use the decompiler events to accomplish the task. The plugin will
*      wait for the ctree structure to be ready in the memory and will
*      replace zeroes in pointer contexts with NULLs.
*
*/

#include

// Hex-Rays API pointer
hexdsp_t *hexdsp = NULL;

static bool inited = false;

static const char nodename[] = “$ hexrays NULLs”;
static const char null_type[] = “MACRO_NULL”;
//————————————————————————–
// Is the plugin enabled?
// The user can disable it. The plugin will save the on/off switch in the
// current database.
static bool is_enabled(void)
{
 netnode n(nodename); // use a netnode to save the state
 return n.altval(0) == 0; // if the long value is positive, then disabled
}

//————————————————————————–
// If the expression is zero, convert it to NULL
static void make_null_if_zero(cexpr_t *e)
{
 if ( e->is_zero_const() && !e->type.is_ptr() )
 { // this is plain zero, convert it
   number_format_t &nf = e->n->nf;
   nf.flags = enum_flag();
   nf.serial = 0;
   nf.type_name = null_type;
   e->type = tinfo_t::get_stock(STI_PVOID);
 }
}

//————————————————————————–
// Convert zeroes of the ctree to NULLs
static void convert_zeroes(cfunc_t *cfunc)
{
 // To represent NULLs, we will use the MACRO_NULL enumeration
 // Normally it is present in the loaded tils but let’s verify it
 if ( !get_named_type(NULL, null_type, NTF_TYPE) )
 {
   msg(“%s type is missing, cannot convert zeroes to NULLs\n”, null_type);
   return;
 }

 // We derive a helper class from ctree_visitor_t
 // The ctree_visitor_t is a base class to derive
 // ctree walker classes.
 // You have to redefine some virtual functions
 // to do the real job. Here we redefine visit_expr() since we want
 // to examine and modify expressions.
 struct ida_local zero_converter_t : public ctree_visitor_t
 {
   zero_converter_t(void) : ctree_visitor_t(CV_FAST) {}
   int idaapi visit_expr(cexpr_t *e)
   {
     // verify if the current expression has pointer expressions
     // we handle the following patterns:
     //  A. ptr = 0;
     //  B. func(0); where argument is a pointer
     //  C. ptr op 0 where op is a comparison
     switch ( e->op )
     {
       case cot_asg:   // A
         if ( e->x->type.is_ptr() )
           make_null_if_zero(e->y);
         break;

       case cot_call:  // B
         {
           carglist_t &args = *e->a;
           for ( int i=0; i < args.size(); i++ ) // check all arguments
           {
             carg_t &a = args[i];
             if ( a.formal_type.is_ptr_or_array() )
               make_null_if_zero(&a);
           }
         }
         break;

       case cot_eq:    // C
       case cot_ne:
       case cot_sge:
       case cot_uge:
       case cot_sle:
       case cot_ule:
       case cot_sgt:
       case cot_ugt:
       case cot_slt:
       case cot_ult:
         // check both sides for zeroes
         if ( e->y->type.is_ptr() )
           make_null_if_zero(e->x);
         if ( e->x->type.is_ptr() )
           make_null_if_zero(e->y);
         break;

       default:
         break;

     }
     return 0; // continue walking the tree
   }
 };
 zero_converter_t zc;
 // walk the whole function body
 zc.apply_to(&cfunc->body, NULL);
}

//————————————————————————–
// This callback will detect when the ctree is ready to be displayed
// and call convert_zeroes() to create NULLs
static ssize_t idaapi callback(void *, hexrays_event_t event, va_list va)
{
 if ( event == hxe_maturity )
 {
   cfunc_t *cfunc = va_arg(va, cfunc_t*);
   ctree_maturity_t mat = va_argi(va, ctree_maturity_t);
   if ( mat == CMAT_FINAL ) // ctree is ready, time to convert zeroes to NULLs
     convert_zeroes(cfunc);
 }
 return 0;
}

//————————————————————————–
// Initialize the plugin.
int idaapi init(void)
{
 if ( !init_hexrays_plugin() )
   return PLUGIN_SKIP; // no decompiler
 if ( is_enabled() ) // null plugin is enabled?
 {
   install_hexrays_callback(callback, NULL);
   const char *hxver = get_hexrays_version();
   msg(“Hex-rays version %s has been detected, %s ready to use\n”, hxver, PLUGIN.wanted_name);
 }
 inited = true;
 return PLUGIN_KEEP;
}

//————————————————————————–
void idaapi term(void)
{
 if ( inited )
 {
   // clean up
   remove_hexrays_callback(callback, NULL);
   term_hexrays_plugin();
 }
}

//————————————————————————–
bool idaapi run(size_t)
{
 // since all real work is done in the callbacks, use the main plugin entry
 // to turn it on and off.
 // display a message explaining the purpose of the plugin:
 int code = askbuttons(
      “~E~nable”,
      “~D~isable”,
      “~C~lose”,
      –1,
      “AUTOHIDE NONE\n”
      “Sample plugin for Hex-Rays decompiler.\n”
      “\n”
      “This plugin is fully automatic.\n”
      “It detects zeroes in pointer contexts and converts them into NULLs.\n”
      “\n”
      “The current state of the plugin is: %s\n”,
      is_enabled() ? “ENABLED” : “DISABLED”);
 switch ( code )
 {
   case –1:    // close
     break;
   case 0:     // disable
   case 1:     // enable
     netnode n;
     n.create(nodename);
     n.altset(0, code == 0);
     if ( code )
       install_hexrays_callback(callback, NULL);
     else
       remove_hexrays_callback(callback, NULL);
     info(“The %s plugin has been %s.”, PLUGIN.wanted_name, code ? “ENABLED” : “DISABLED”);
     break;
 }
 return true;
}

//————————————————————————–
static char comment[] = “Sample2 plugin for Hex-Rays decompiler”;

//————————————————————————–
//
//      PLUGIN DESCRIPTION BLOCK
//
//————————————————————————–
plugin_t PLUGIN =
{
 IDP_INTERFACE_VERSION,
 0,                    // plugin flags
 init,                 // initialize
 term,                 // terminate. this pointer may be NULL.
 run,                  // invoke plugin
 comment,              // long comment about the plugin
                       // it could appear in the status line
                       // or as a hint
 “”,                   // multiline help about the plugin
 “Hex-Rays NULL converter”, // the preferred short name of the plugin
 “”                    // the preferred hotkey to run the plugin
};

 

中文翻译如下:

以下是一个示例插件的完整源代码。它执行一个非常有用的伪代码转换:将指针上下文中的零替换为NULL。NULL立即传达了当前表达式与指针相关的概念。这对于未知的函数参数尤其有用。

 

该插件是完全自动化的。它钩入反编译器事件并等待伪代码准备就绪。在那一刻,它接管并修改ctree。

 

转换是由convert_zeroes()函数执行的。它访问ctree的所有表达式并检查指针上下文。如果一个表达式具有指针类型,则为它调用make_null_if_zero()函数。此函数检查表达式是否为零常量并在必要时进行转换。

 

该插件可以通过插件子菜单中的菜单项打开或关闭。

 

代码短小简单。可以将其用作插件的模板。

/*

* Hex-Rays Decompiler project

* Copyright (c) 2007-2019 by Hex-Rays, support@hex-rays.com

* ALL RIGHTS RESERVED.

*

* Sample plugin for Hex-Rays Decompiler.

* It automatically replaces zeroes in pointer contexts with NULLs.

* For example, expression like

*

* funcptr = 0;

*

* will be displayed as

*

* funcptr = NULL;

*

* Due to highly dynamic nature of the decompier output, we must

* use the decompiler events to accomplish the task. The plugin will

* wait for the ctree structure to be ready in the memory and will

* replace zeroes in pointer contexts with NULLs.

*

*/

 

#include

 

// Hex-Rays API pointer

hexdsp_t *hexdsp = NULL;

 

static bool inited = false;

 

static const char nodename[] = “$ hexrays NULLs”;

static const char null_type[] = “MACRO_NULL”;

//————————————————————————–

// Is the plugin enabled?

// The user can disable it. The plugin will save the on/off switch in the

// current database.

static bool is_enabled(void)

{

netnode n(nodename); // use a netnode to save the state

return n.altval(0) == 0; // if the long value is positive, then disabled

}

 

//————————————————————————–

// If the expression is zero, convert it to NULL

static void make_null_if_zero(cexpr_t *e)

{

if ( e->is_zero_const() && !e->type.is_ptr() )

{ // this is plain zero, convert it

number_format_t &nf = e->n->nf;

nf.flags = enum_flag();

nf.serial = 0;

nf.type_name = null_type;

e->type = tinfo_t::get_stock(STI_PVOID);

}

}

 

//————————————————————————–

// Convert zeroes of the ctree to NULLs

static void convert_zeroes(cfunc_t *cfunc)

{

// To represent NULLs, we will use the MACRO_NULL enumeration

// Normally it is present in the loaded tils but let’s verify it

if ( !get_named_type(NULL, null_type, NTF_TYPE) )

{

msg(“%s type is missing, cannot convert zeroes to NULLs\n”, null_type);

return;

}

 

// We derive a helper class from ctree_visitor_t

// The ctree_visitor_t is a base class to derive

// ctree walker classes.

// You have to redefine some virtual functions

// to do the real job. Here we redefine visit_expr() since we want

// to examine and modify expressions.

struct ida_local zero_converter_t : public ctree_visitor_t

{

zero_converter_t(void) : ctree_visitor_t(CV_FAST) {}

int idaapi visit_expr(cexpr_t *e)

{

// verify if the current expression has pointer expressions

// we handle the following patterns:

// A. ptr = 0;

// B. func(0); where argument is a pointer

// C. ptr op 0 where op is a comparison

switch ( e->op )

{

case cot_asg: // A

if ( e->x->type.is_ptr() )

make_null_if_zero(e->y);

break;

 

case cot_call: // B

{

carglist_t &args = *e->a;

for ( int i=0; i < args.size(); i++ ) // check all arguments

{

carg_t &a = args[i];

if ( a.formal_type.is_ptr_or_array() )

make_null_if_zero(&a);

}

}

break;

 

case cot_eq: // C

case cot_ne:

case cot_sge:

case cot_uge:

case cot_sle:

case cot_ule:

case cot_sgt:

case cot_ugt:

case cot_slt:

case cot_ult:

// check both sides for zeroes

if ( e->y->type.is_ptr() )

make_null_if_zero(e->x);

if ( e->x->type.is_ptr() )

make_null_if_zero(e->y);

break;

 

default:

break;

 

}

return 0; // continue walking the tree

}

};

zero_converter_t zc;

// walk the whole function body

zc.apply_to(&cfunc->body, NULL);

}

 

//————————————————————————–

// This callback will detect when the ctree is ready to be displayed

// and call convert_zeroes() to create NULLs

static ssize_t idaapi callback(void *, hexrays_event_t event, va_list va)

{

if ( event == hxe_maturity )

{

cfunc_t *cfunc = va_arg(va, cfunc_t*);

ctree_maturity_t mat = va_argi(va, ctree_maturity_t);

if ( mat == CMAT_FINAL ) // ctree is ready, time to convert zeroes to NULLs

convert_zeroes(cfunc);

}

return 0;

}

 

//————————————————————————–

// Initialize the plugin.

int idaapi init(void)

{

if ( !init_hexrays_plugin() )

return PLUGIN_SKIP; // no decompiler

if ( is_enabled() ) // null plugin is enabled?

{

install_hexrays_callback(callback, NULL);

const char *hxver = get_hexrays_version();

msg(“Hex-rays version %s has been detected, %s ready to use\n”, hxver, PLUGIN.wanted_name);

}

inited = true;

return PLUGIN_KEEP;

}

 

//————————————————————————–

void idaapi term(void)

{

if ( inited )

{

// clean up

remove_hexrays_callback(callback, NULL);

term_hexrays_plugin();

}

}

 

//————————————————————————–

bool idaapi run(size_t)

{

// since all real work is done in the callbacks, use the main plugin entry

// to turn it on and off.

// display a message explaining the purpose of the plugin:

int code = askbuttons(

“~E~nable”,

“~D~isable”,

“~C~lose”,

–1,

“AUTOHIDE NONE\n”

“Sample plugin for Hex-Rays decompiler.\n”

“\n”

“This plugin is fully automatic.\n”

“It detects zeroes in pointer contexts and converts them into NULLs.\n”

“\n”

“The current state of the plugin is: %s\n”,

is_enabled() ? “ENABLED” : “DISABLED”);

switch ( code )

{

case –1: // close

break;

case 0: // disable

case 1: // enable

netnode n;

n.create(nodename);

n.altset(0, code == 0);

if ( code )

install_hexrays_callback(callback, NULL);

else

remove_hexrays_callback(callback, NULL);

info(“The %s plugin has been %s.”, PLUGIN.wanted_name, code ? “ENABLED” : “DISABLED”);

break;

}

return true;

}

 

//————————————————————————–

static char comment[] = “Sample2 plugin for Hex-Rays decompiler”;

 

//————————————————————————–

//

// PLUGIN DESCRIPTION BLOCK

//

//————————————————————————–

plugin_t PLUGIN =

{

IDP_INTERFACE_VERSION,

0, // plugin flags

init, // initialize

term, // terminate. this pointer may be NULL.

run, // invoke plugin

comment, // long comment about the plugin

// it could appear in the status line

// or as a hint

“”, // multiline help about the plugin

“Hex-Rays NULL converter”, // the preferred short name of the plugin

“” // the preferred hotkey to run the plugin

};

展开阅读全文

标签:

读者也访问过这里:
邀请您进入交流群 点击扫码
400-8765-888 kefu@makeding.com

专业销售为您服务

欢迎添加好友,了解更多IDA优惠信息,领逆向工程学习资料礼包1份!
热门文章
exe反编译工具哪个好?反编译能力强的工具盘点
随着软件技术的发展,exe(可执行文件)已经成为了电脑、手机等多个平台上的主要软件运行格式,而对于exe文件的反编译也成为了逆向工程中不可缺少的一个步骤。本文将介绍一些常用的exe反编译工具,并评价其优缺点,帮助读者选择合适的工具。
2023-04-12
idapro怎么改为中文
IDA Pro是一款功能强大的反汇编和反编译工具,广泛应用于逆向工程和软件开发领域。在使用IDA Pro时,如果我们不习惯英文界面,可以将其改为中文界面。本文将介绍IDA Pro怎么改为中文界面。IDA Pro界面改成中文主要有两种方法,下面是详细介绍。
2023-04-19
c++反编译工具有哪些
反编译C++代码的工具一般是针对可执行文件和库文件的反汇编和逆向分析工具。本文将给大家介绍c++反编译工具有哪些的内容。市面说的c++反编译工具有很多,下面介绍几款使用认识较多的软件。
2023-04-23
ida如何转伪代码 ida伪代码怎么看
IDA Pro是一款常用的反汇编和反编译工具,可以帮助我们分析二进制文件的实现细节和执行过程,以便更好地理解程序的执行过程和逻辑。在进行逆向工程的过程中,我们经常需要将反汇编结果转换为伪代码,以便更好地进行分析和修改。本文将介绍如何使用IDA Pro转换为伪代码,并简单讲解ida伪代码怎么看。
2023-04-14
IDA反汇编流程视图的常用基本操作设置
IDA中反汇编窗口中有两种不同的形式,分别是列表模式和图形模式,IDA默认打开是图形模式,就是反汇编流程视图,可以用来分析程序函数的具体运行情况。
2021-06-15
IDA的初始使用说明和界面简介
IDA能帮助我们分析恶意软件、分析系统漏洞、验证编译器的性能,其支持在Mac系统、Windows系统、Linux系统中使用,是一款非常优秀的反编译软件。
2021-03-16
最新文章
ida医学上是什么意思?医学ida指什么?
医学术语往往能够准确地描述健康状况和疾病的特点,对于医疗专业人员来说是日常工作不可或缺的一部分。然而,普通公众对这些术语的理解可能会存在困难,这就需要我们通过更易懂的方式来介绍这些专业知识。其中,“IDA”是一个在医学领域频繁出现的缩写,代表了一种常见的健康问题。本文将详细阐述IDA在医学上的含义,它所指的特定疾病,以及IDA在其他领域的潜在含义,旨在帮助公众更好地理解这一术语。
2024-04-23
mybatis逆向工程是什么?mybatis逆向工程使用什么工具?
mybatis,作为一个流行的Java持久层框架,通过提供一种相对简便的方式来管理数据库操作和数据转换,已经成为众多项目开发中不可或缺的一部分。而mybatis逆向工程,则是在此基础上,通过自动化生成数据库操作代码的方式,进一步提升开发效率,简化开发过程。
2024-04-17
md5可以反编译吗?md5反编译需要用什么工具?
在数字安全和软件开发领域,md5一直是一个广为人知的话题。md5,即Message-Digest Algorithm 5,是一种广泛使用的加密哈希函数,能够产生一个128位(16字节)的哈希值,通常用一个32位的十六进制数表示。但随着计算技术的发展,人们开始探讨md5是否可以反编译,以及进行这种反编译所需的工具是什么。本文将深入探讨这一话题,包括md5的反编译可能性、所需的工具以及IDA反编译原理的详细分析。
2024-04-16
pyc反编译文件怎么做?pyc反编译有什么工具?
Python编程语言以其高效的性能和广泛的应用领域占据了软件开发的重要地位。随之而来的`.pyc`文件作为Python代码编译的产物,对于提高程序运行效率有着不可忽视的作用。但在某些场合下,我们需要将这些编译过的文件还原为源代码形式,以便于代码审查或学习交流。因此,本文将深入探讨`.pyc`文件的反编译过程、介绍有效的反编译工具,并指导如何通过IDA快速掌握反编译技巧,以资助力开发者和逆向工程师。
2024-04-09
易语言反编译是什么?易语言反编译怎么做用什么工具?
软件工程的一个挑战性任务是如何解读和分析那些没有源代码的程序。对于易语言编写的软件,这一挑战尤为突出。反编译技术,作为桥接编译代码与源代码的关键技术,能够揭开编译后程序的神秘面纱。本文将聚焦于易语言反编译的基础知识、实施方法及所需的工具,并详细探讨IDA工具在逆向工程任务中的核心作用,为广大技术从业者提供参考和指导。
2024-04-09
IDA8.4新版发布:界面大更新!反编译精度提升!附下载
逆向工程领域的佼佼者,IDA Pro,再次以其最新版本8.4引领技术潮流。本次更新不仅延续了IDA Pro一贯的专业性能,更在用户体验和功能上带来了一系列创新和改进。我们诚邀广大用户前往IDA中文网站(https://www.idapro.net.cn/)下载并体验IDA 8.4的最新功能。
2024-04-01

通过微信咨询我们

欢迎添加好友,了解更多IDA优惠信息,领取逆向工程学习资料礼包1份!