IDAPython Functions

IDAPython is fully IDC compliant, which means any function call that IDC[55] supports you can also use in IDAPython. We will cover some of the functions that you will commonly use when writing IDAPython scripts in short order. These should provide a solid foundation for you to begin developing your own scripts. The IDC language supports well over 100 function calls, so this is far from an exhaustive list, but you are encouraged to explore it in depth at your leisure.

The following are a couple of utility functions that will come in handy in a lot of your IDAPython scripts:

A binary in IDA is broken down into segments, with each segment having a specific class (CODE, DATA, BSS, STACK, CONST, or XTRN). The following functions provide a way to obtain information about the segments that are contained within the binary:

Iterating over all the functions in a binary and determining function boundaries are tasks that you will encounter frequently when scripting. The following routines are useful when dealing with functions inside a target binary:

Finding code and data cross-references inside a binary is extremely useful when determining data flow and possible code paths to interesting portions of a target binary. IDAPython has a host of functions used to determine various cross references. The most commonly used ones are covered here.

One very cool feature that IDAPython supports is the ability to define a debugger hook within IDA and set up event handlers for the various debugging events that may occur. Although IDA is not commonly used for debugging tasks, there are times when it is easier to simply fire up the native IDA debugger than switch to another tool. We will use one of these debugger hooks later on when creating a simple code coverage tool. To set up a debugger hook, you first define a base debugger hook class and then define the various event handlers within this class. We'll use the following class as an example:

class DbgHook(DBG_Hooks):
    # Event handler for when the process starts
    def dbg_process_start(self, pid, tid, ea, name, base, size):
        return

    # Event handler for process exit
    def dbg_process_exit(self, pid, tid, ea, code):
        return

    # Event handler for when a shared library gets loaded
    def dbg_library_load(self, pid, tid, ea, name, base, size):
        return

    # Breakpoint handler
    def dbg_bpt(self, tid, ea):
        return

This class contains some common debug event handlers that you can use when creating simple debugging scripts in IDA. To install your debugger hook use the following code:

debugger = DbgHook()
debugger.hook()

Now run the debugger, and your hook will catch all of the debugging events, allowing you to have a very high level of control over IDA's debugger. Here are a handful of helper functions that you can use during a debugging run:



[55] For a full IDC function listing, see http://www.hex-rays.com/idapro/idadoc/162.htm.