Getting started with IDAPython
Getting started with IDAPython
Intro
The IDAPython API provides you a range of functions to interact with the disassembler, navigate through the output, and manipulate various elements such as functions, instructions, data, and comments.
This guide is is designed to speed up the learning curve in IDAPython API and kickstart your journey with scripting in IDA, assuming that you are already found your way around IDA and got familiar with IDA basics.
How this guide is structured?
First, check basics for the core concepts like common variables, then dive into our simple, reusable code snippets showing examples of commonly used functions. When you feel comfortable with some of the most popular IDAPython API usage, you can delve into our set of more complex examples.
Basics
Common modules
IDAPython API is organized in modules, however their number may be a bit overwhelming for the first sigh. Here's the list of modules that should catch your attention first:
idautils
: This module extracts the most useful and handy functions allowing you to jump right away and interact with the disassembly without the need to scrutinize the whole IDAPython API from the start. You can find here functions to iterate through whole segments, functions (founded by IDA and also user-defined), named locations, etc.ida_idaapi
: Theida_idaapi
module comes handy when you want to create custom plugins or handle events, as it gives you access to more advanced functions and allows interaction with overall systemidc
: This module provides functions that were originally part of native IDA IDC scripting language, wrapped for use in IDAPython API. This is a great starting point if you're already familiar with IDC scripting.ida_funcs
: This module gives you tools to create and manipulate functions within IDA.ida_kernwin
: This module provides functionality to interact with IDA UI, so you can create custom dialogs and more.
Common variables and constants
When you start working with IDAPython, you'll realize that one of the most commonly passed variables is ea
, which refers to the valid memory address (effective address). On the other hand, the BADADDR
is a constant that indicates an invalid address. It is used to indicate that an operation (such as querying a function) has failed or returned an invalid result, or signify that a specific memory location is unusable. Whenever you're working with functions that return memory addresses, it's a best practice to check whether the result equals BADADDR to ensure the operation was successful.
Code snippets
Here you can check the most common functions grouped by topics and short code samples that can serve as building blocks for longer scripts. They are usually focused on performing one specific action and can be easily reusable in more complex scripts, which you can find later under examples.
Part 1: Navigating the disassembly—addresses and names
Get the current address:**
Set the current address
Get the minimum address in IDB
Get the maximum address in IDB
List all instruction addresses
Get the name associated with a given address
Get the address associated with a given name
Part 2: Reading and Writing Data
Reading Bytes and Words
Writing Bytes and Words
Part 3: Comments
Add a regular (non-repetable) or repeatable comment
Get a regular comment
Segments
Get a segment name
Get the first segment address:
get_first_seg()
Iterate through all segments and return segment names
Add segment
Part X: Functions
Create and delete function
Get the name of the function
Iterate through all functions and print their effective addresses and names
Part: Navigating cross-references (Xrefs)
List cross-references to an address
List cross-references from an address:**
Iterate through all cross-references to the specific address and print the address from where the reference originates.
Part X: UI
Set background color of a function
Display a custom dialog
Complex script examples
If you feel more comfortable with IDAPython now, you can delve into more complex and advanced examples shipped with your IDA instance. You can find them in the python/examples
folder where your IDA is installed or check them from our docs These collection gathered more advanced code samples, which usually make use of more modules and APIs.
What's next?
Delve into our tutorial on how to create your first custom plugin in IDAPython.
Last updated