Table of Contents for
The IDA Pro Book, 2nd Edition

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition The IDA Pro Book, 2nd Edition by Chris Eagle Published by No Starch Press, 2011
  1. Cover
  2. The IDA Pro Book
  3. PRAISE FOR THE FIRST EDITION OF THE IDA PRO BOOK
  4. Acknowledgments
  5. Introduction
  6. I. Introduction to IDA
  7. 1. Introduction to Disassembly
  8. The What of Disassembly
  9. The Why of Disassembly
  10. The How of Disassembly
  11. Summary
  12. 2. Reversing and Disassembly Tools
  13. Summary Tools
  14. Deep Inspection Tools
  15. Summary
  16. 3. IDA Pro Background
  17. Obtaining IDA Pro
  18. IDA Support Resources
  19. Your IDA Installation
  20. Thoughts on IDA’s User Interface
  21. Summary
  22. II. Basic IDA Usage
  23. 4. Getting Started with IDA
  24. IDA Database Files
  25. Introduction to the IDA Desktop
  26. Desktop Behavior During Initial Analysis
  27. IDA Desktop Tips and Tricks
  28. Reporting Bugs
  29. Summary
  30. 5. IDA Data Displays
  31. Secondary IDA Displays
  32. Tertiary IDA Displays
  33. Summary
  34. 6. Disassembly Navigation
  35. Stack Frames
  36. Searching the Database
  37. Summary
  38. 7. Disassembly Manipulation
  39. Commenting in IDA
  40. Basic Code Transformations
  41. Basic Data Transformations
  42. Summary
  43. 8. Datatypes and Data Structures
  44. Creating IDA Structures
  45. Using Structure Templates
  46. Importing New Structures
  47. Using Standard Structures
  48. IDA TIL Files
  49. C++ Reversing Primer
  50. Summary
  51. 9. Cross-References and Graphing
  52. IDA Graphing
  53. Summary
  54. 10. The Many Faces of IDA
  55. Using IDA’s Batch Mode
  56. Summary
  57. III. Advanced IDA Usage
  58. 11. Customizing IDA
  59. Additional IDA Configuration Options
  60. Summary
  61. 12. Library Recognition Using FLIRT Signatures
  62. Applying FLIRT Signatures
  63. Creating FLIRT Signature Files
  64. Summary
  65. 13. Extending IDA’s Knowledge
  66. Augmenting Predefined Comments with loadint
  67. Summary
  68. 14. Patching Binaries and Other IDA Limitations
  69. IDA Output Files and Patch Generation
  70. Summary
  71. IV. Extending IDA’s Capabilities
  72. 15. IDA Scripting
  73. The IDC Language
  74. Associating IDC Scripts with Hotkeys
  75. Useful IDC Functions
  76. IDC Scripting Examples
  77. IDAPython
  78. IDAPython Scripting Examples
  79. Summary
  80. 16. The IDA Software Development Kit
  81. The IDA Application Programming Interface
  82. Summary
  83. 17. The IDA Plug-in Architecture
  84. Building Your Plug-ins
  85. Installing Plug-ins
  86. Configuring Plug-ins
  87. Extending IDC
  88. Plug-in User Interface Options
  89. Scripted Plug-ins
  90. Summary
  91. 18. Binary Files and IDA Loader Modules
  92. Manually Loading a Windows PE File
  93. IDA Loader Modules
  94. Writing an IDA Loader Using the SDK
  95. Alternative Loader Strategies
  96. Writing a Scripted Loader
  97. Summary
  98. 19. IDA Processor Modules
  99. The Python Interpreter
  100. Writing a Processor Module Using the SDK
  101. Building Processor Modules
  102. Customizing Existing Processors
  103. Processor Module Architecture
  104. Scripting a Processor Module
  105. Summary
  106. V. Real-World Applications
  107. 20. Compiler Personalities
  108. RTTI Implementations
  109. Locating main
  110. Debug vs. Release Binaries
  111. Alternative Calling Conventions
  112. Summary
  113. 21. Obfuscated Code Analysis
  114. Anti–Dynamic Analysis Techniques
  115. Static De-obfuscation of Binaries Using IDA
  116. Virtual Machine-Based Obfuscation
  117. Summary
  118. 22. Vulnerability Analysis
  119. After-the-Fact Vulnerability Discovery with IDA
  120. IDA and the Exploit-Development Process
  121. Analyzing Shellcode
  122. Summary
  123. 23. Real-World IDA Plug-ins
  124. IDAPython
  125. collabREate
  126. ida-x86emu
  127. Class Informer
  128. MyNav
  129. IdaPdf
  130. Summary
  131. VI. The IDA Debugger
  132. 24. The IDA Debugger
  133. Basic Debugger Displays
  134. Process Control
  135. Automating Debugger Tasks
  136. Summary
  137. 25. Disassembler/Debugger Integration
  138. IDA Databases and the IDA Debugger
  139. Debugging Obfuscated Code
  140. IdaStealth
  141. Dealing with Exceptions
  142. Summary
  143. 26. Additional Debugger Features
  144. Debugging with Bochs
  145. Appcall
  146. Summary
  147. A. Using IDA Freeware 5.0
  148. Using IDA Freeware
  149. B. IDC/SDK Cross-Reference
  150. Index
  151. About the Author

Extending IDC

So far we have presented plug-ins designed primarily to manipulate or extract information from a database. In this section, we present an example of extending the capabilities of the IDC scripting language.[117] As mentioned in Chapter 16, IDC is implemented on top of the IDA API, so it should come as no surprise that the API can be used to enhance IDC when the need arises.

In Chapter 15 and Chapter 16, you learned that IDC global arrays are actually a somewhat restricted abstraction of netnodes. Recall that in IDC you create global arrays by supplying a name and receiving an array ID in return. Internally your name gets prefixed with the string “$ idc_array ”, and the array ID that you receive is actually a netnode index value. How could we go about extending IDC in order to enable access to any netnode in an IDA database? We can already access any netnode whose index we happen to know by using the index as the array ID in IDC, so what we need is the ability to access any netnode whose name we happen to know. IDC currently prevents us from doing this because it prepends “$ idc_array ” to every netnode name we supply. Enter the SDK and the set_idc_func_ex function.

Defined in expr.hpp, set_idc_func_ex may be used to create a new IDC function and map its behavior to C++ implementation. The prototype for set_idc_func_ex is shown here:

typedef error_t (idaapi *idc_func_t)(idc_value_t *argv, idc_value_t *res);
bool set_idc_func_ex(const char *idc_name, idc_func_t idc_impl,
                     const char *args, int extfunc_flags);

Note that we have introduced the idc_func_t datatype here in order to simplify the code somewhat. This datatype is not defined within the SDK. The arguments to set_idc_func_ex specify the name of the new IDC function that we are introducing (idc_name), a pointer to the C++ function that implements our new IDC behavior (idc_impl), a null-terminated array of characters that specify the argument types and sequence for the new IDC function (args), and flags (extfunc_flags) indicating whether an open database is required or whether the function never returns.

The following function, used as the initialization function for a plug-in, completes the process by creating the new IDC function we are designing:

int idaapi init(void) {
    static const char idc_str_args[] = { VT_STR2, 0 };
    set_idc_func_ex("CreateNetnode", idc_create_netnode, idc_str_args, 0);
      return PLUGIN_KEEP;
  }

This function creates the new IDC function CreateNetnode and maps it to our implementation function idc_create_netnode . The arguments to the new IDC function are specified as being a single parameter of type string (VT_STR2) .

The function that actually implements the behavior of CreateNetnode is shown here:

/*
   * native implementation of CreateNetnode.  Returns the id of the new netnode
   * this id can be used with all of the existing IDC Array functions.
   */
  static error_t idaapi idc_create_netnode(idc_value_t *argv, idc_value_t *res) {
   res->vtype = VT_LONG;           //result type is a netnode index
   if (argv[0].vtype == VT_STR2) {  //verify we have the proper input type
      netnode n(argv[0].c_str(), 0, true);  //create the netnode
      res->num = (nodeidx_t)n;          //set the result value
     }
     else {
      res->num = −1;         //If the user supplies a bad argument we fail
     }
     return eOk;
  }

The two arguments to this function represent the input argument array (argv) containing all of the parameters to CreateNetnode (there should be only one in this case) and an output parameter (res) used to receive the result of the IDC function we are implementing. The SDK datatype idc_value_t represents a single IDC value. Fields within this datatype indicate the current type of data represented by the value and the current contents of the value. The function begins by specifying that CreateNetnode returns a long (VT_LONG) value . Since IDC variables are untyped, we must indicate internally what type of value the variable is holding at any given moment. Next, the function verifies that the caller of CreateNetnode has supplied an argument of type string (VT_STR2) . If a valid argument has been supplied, a netnode is created with the supplied name . The resulting netnode index number is returned to the caller as the result of the CreateNetnode function . In this example, the result type is an integer value, so the result is stored into the res->num field. Had the result type been a string, we would have needed to call res->set_string to set the string value of the result. If the user fails to supply a string argument, the function fails and returns the invalid netnode index −1 .

We complete the plug-in with the following functions and PLUGIN structure:

void idaapi term(void) {}   //nothing to do on termination
  void idaapi run(int arg) {} //nothing to do and no way to activate

  plugin_t PLUGIN = {
    IDP_INTERFACE_VERSION,
    //this plugin loads at IDA startup, does not get listed on the Edit>Plugins menu
    //and modifies the database
   PLUGIN_FIX | PLUGIN_HIDE | PLUGIN_MOD,  // plugin flags
    init,                 // initialize
    term,                 // terminate. this pointer may be NULL.
    run,                  // invoke plugin
    "",                   // long comment about the plugin
    "",                   // multiline help about the plugin
    "",                   // the preferred short name of the plugin
    ""                    // the preferred hotkey to run the plugin
  };

The trick to this plug-in is that it loads on IDA startup (PLUGIN_FIX) and remains hidden from the user because it is not added to the Edit ▸ Plugins menu (PLUGIN_HIDE) . The plug-in is kept in memory for all databases, and all of the initialization takes place in the plug-in’s init function. As a result, the plug-in has nothing to do in its run method.

Once this plug-in is installed, an IDC programmer may access any named netnode in an IDA database using the netnode’s name, as in the following example:

auto n, val;
n = CreateNetnode("$ imports");       //no $ idc_array prefix will be added
val = GetArrayElement(AR_STR, n, 0);  //get element zero

More information for using the SDK to interact with IDC is contained in the expr.hpp header file.



[117] Note that there is currently no way to programmatically extend the IDAPython API from within a compiled plug-in.