Lesson 12-Scripted Analysis

Script development is a key technology for automation and efficient analysis in reverse engineering. This tutorial will comprehensively and deeply explain the script development techniques of three mainstream reverse engineering tools (IDA Pro, Ghidra, Radare2), including IDAPython, IDC, Ghidra scripts (Java/Python), and Radare2 scripts and commands, and help readers master this key technology through detailed code examples and step-by-step analysis.

IDA Script Development: IDAPython and IDC

IDAPython Basics

IDAPython is the built-in Python interpreter in IDA Pro, allowing users to write automation scripts and plugins to extend IDA’s functionality.

IDAPython Installation and Configuration:

  1. Ensure IDA Pro (version 6.0 and above) is installed
  2. IDAPython is usually installed with IDA Pro
  3. In IDA, Python scripts can be run via “File” -> “Script file…”

IDAPython Basic Syntax:

# Print information
print("Hello, IDAPython!")

# Get current address
current_ea = idc.DbgDword(0)  # Get current instruction address in debug mode
print(f"Current address: {hex(current_ea)}")

# Get function name
func_name = idc.get_func_name(idc.get_screen_ea())
print(f"Current function: {func_name}")

Common IDAPython APIs:

  1. Database Operations:
# Get instruction at current address
insn = idautils.DecodeInstruction(idc.get_screen_ea())
print(f"Instruction: {insn.get_canon_mnem()} {insn.get_op_str()}")

# Iterate over all functions
for func_ea in idautils.Functions():
	print(f"Function at {hex(func_ea)}: {idc.get_func_name(func_ea)}")
  1. Cross-Reference Analysis:
# Get all callers of a certain address
callers = idautils.XrefsTo(0x00401000, idaapi.fl_CN)
for xref in callers:
	print(f"Called from {hex(xref.frm)}")
  1. String Analysis:
# Find all strings
for s in idautils.Strings():
	print(f"String at {hex(s.ea)}: {s.s}")
  1. Modify Database:
# Rename address
idc.set_name(0x00401000, "my_function")

# Add comment
idc.set_cmt(0x00401000, "This is a sample function", 0)

IDC Script Development

IDC (IDA C-like Debugger) is IDA Pro’s old scripting language. Although IDAPython is now recommended, it may still be used in some older versions of IDA or specific scenarios.

IDC Basic Syntax:

// Print information
message("Hello, IDC!");

// Get current address
auto current_ea = get_screen_ea();
message("Current address: %x\n", current_ea);

// Get function name
auto func_name = get_func_name(current_ea);
message("Current function: %s\n", func_name);

Comparison of IDC and IDAPython:

FeatureIDAPythonIDC
LanguagePythonC-like
FunctionalityMore powerful, supports modern Python featuresMore basic
Community SupportExtensiveGradually decreasing
Recommended UsePreferred for new projectsFor older IDA or specific needs

IDAPython Advanced Applications

Writing IDA Plugins:

import idaapi

class MyPlugin(idaapi.plugin_t):
    flags = idaapi.PLUGIN_UNL
    comment = "My IDA Plugin"
    help = "This is my custom IDA plugin"
    wanted_name = "My Plugin"
    wanted_hotkey = "Ctrl+Alt+M"
    
    def init(self):
        print("Plugin initialized")
        return idaapi.PLUGIN_OK
    
    def run(self, arg):
        print("Plugin running")
        # Implement plugin functionality here
    
    def term(self):
        print("Plugin terminated")

def PLUGIN_ENTRY():
    return MyPlugin()

Automated Analysis Script:

import idautils
import idc

def analyze_functions():
    for func_ea in idautils.Functions():
        func_name = idc.get_func_name(func_ea)
        print(f"Analyzing function: {func_name} at {hex(func_ea)}")
        
        # Get all instructions of the function
        for insn_ea in idautils.Heads(func_ea, idc.find_func_end(func_ea)):
            insn = idautils.DecodeInstruction(insn_ea)
            if insn:
                print(f"  {insn.get_canon_mnem()} {insn.get_op_str()}")

if __name__ == "__main__":
    analyze_functions()

Ghidra Script Development: Java and Python

Ghidra Script Environment

Ghidra is an open-source reverse engineering tool from the U.S. National Security Agency (NSA), supporting Java and Python script development.

Ghidra Script Installation and Configuration:

  1. Download and install Ghidra
  2. Scripts are usually placed in the ghidra_scripts directory
  3. In Ghidra, scripts can be run via “Window” -> “Script Manager”

Ghidra Java Script Development

Ghidra’s Java API provides powerful reverse engineering capabilities.

Java Script Basics:

import ghidra.app.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Program;

public class MyGhidraScript extends GhidraScript {
    
    @Override
    protected void run() throws Exception {
        println("Hello, Ghidra Java Script!");
        
        // Get current program
        Program program = currentProgram;
        
        // Get all functions
        for (Function function : program.getFunctionManager().getFunctions(true)) {
            println("Function: " + function.getName() + " at " + function.getEntryPoint());
        }
    }
}

Common Ghidra Java APIs:

  1. Program Analysis:
// Get instruction at current address
Instruction instruction = getInstructionAt(currentAddress);
println("Instruction: " + instruction.getMnemonicString() + " " + instruction.getOpRepresentation());

// Get cross-references
Xref[] xrefs = getXrefsTo(currentAddress);
for (Xref xref : xrefs) {
	println("Xref from " + xref.getFromAddress() + " to " + xref.getToAddress());
}
  1. Data Type Operations:
// Get data type
DataType dataType = getDataTypeManager().resolve("int", null);
println("Data type: " + dataType.getName());

// Create new data type
Structure structure = new StructureDataType("MyStruct", 0);
structure.add(new DWordDataType(), "field1", null);
getDataTypeManager().addDataType(structure, null);
  1. Function Analysis:
// Get all parameters of the function
Function function = getFunctionAt(currentAddress);
if (function != null) {
	for (Parameter parameter : function.getParameters()) {
		println("Parameter: " + parameter.getName() + " " + parameter.getDataType().getName());
	}
}

Ghidra Python Script Development

Ghidra also supports Python scripts, implemented via Jython.

Python Script Basics:

# Print information
print("Hello, Ghidra Python Script!")

# Get current address
current_address = currentProgram.getListing().getAddress(currentAddress)
print(f"Current address: {current_address}")

# Get function name
func = getFunctionContaining(current_address)
if func:
    print(f"Current function: {func.getName()}")

Common Ghidra Python APIs:

Database Operations:

# Iterate over all functions
for func in currentProgram.getFunctionManager().getFunctions(True):
	print(f"Function at {func.getEntryPoint()}: {func.getName()}")

# Get all strings
for string in currentProgram.getStringManager().getStrings...(truncated 128 characters)...  # Get all callers of a certain address
xrefs = list(currentProgram.getReferenceManager().getReferencesTo(current_address))
for xref in xrefs:
	print(f"Called from {xref.getFromAddress()}")

Modify Database:

# Rename address
currentProgram.getListing().setName(current_address, "my_function")

# Add comment
currentProgram.getListing().setComment(current_address, CodeUnit.EOL_COMMENT, "This is a sample function")

Ghidra Script Advanced Applications

Writing Ghidra Plugins:

import ghidra.app.plugin.PluginCategoryNames;
import ghidra.framework.plugintool.*;
import ghidra.framework.plugintool.util.PluginStatus;

@PluginInfo(
    status = PluginStatus.RELEASED,
    packageName = "MyPlugin",
    category = PluginCategoryNames.ANALYSIS,
    shortDescription = "My custom Ghidra plugin",
    description = "This is my custom Ghidra plugin"
)
public class MyGhidraPlugin extends Plugin {
    
    public MyGhidraPlugin(PluginTool tool) {
        super(tool);
    }
    
    @Override
    protected void init() {
        println("Plugin initialized");
    }
    
    @Override
    protected void dispose() {
        println("Plugin disposed");
    }
}

Automated Analysis Script:

# Analyze all functions
for func in currentProgram.getFunctionManager().getFunctions(True):
    print(f"Analyzing function: {func.getName()}")
    
    # Get all instructions of the function
    for insn in list(currentProgram.getListing().getInstructions(func.getBody(), True)):
        print(f"  {insn.getMnemonicString()} {insn.getOpRepresentation()}")

Radare2 Scripts and Commands

Radare2 Basics

Radare2 is an open-source reverse engineering framework that supports multiple architectures and file formats, with a powerful command-line interface and scripting capabilities.

Radare2 Installation and Configuration:

  1. Install on Linux: sudo apt-get install radare2
  2. On Windows, it can be installed using Cygwin or WSL

Radare2 Basic Commands:

# Open file
r2 -A file.exe

# Display help
> ?

Radare2 Script Basics:
Radare2 scripts can use the r2pipe API (via Python or other languages) or directly use Radare2’s command language.

Python r2pipe Script Example:

import r2pipe

# Open file
r2 = r2pipe.open("file.exe")

# Analyze file
r2.cmd("aaa")

# Get function list
functions = r2.cmdj("aflj")
for func in functions:
    print(f"Function at {hex(func['offset'])}: {func['name']}")

# Close connection
r2.quit()

Radare2 Command Language

Radare2’s command language (r2 commands) can be used directly in the Radare2 interactive interface:

Common Commands:

  1. File Analysis: # Automatic analysis aaa # Display function list afl # Display cross-references ax
  2. Disassembly: # Disassemble current address pdf # Disassemble function pdc # Disassemble range pd 10 # Disassemble 10 instructions
  3. Data Viewing: # View memory content px 16 # View 16 bytes in hexadecimal # View string psz # View string starting at current address # View registers dr
  4. Modify Database: # Rename address afn new_name current_address # Add comment C comment_text current_address

Radare2 Advanced Scripts

Writing Radare2 Plugins:
Radare2 supports writing plugins in C language, but it is more common to use the r2pipe API to write external scripts.

Automated Analysis Script:

import r2pipe

def analyze_binary(file_path):
    r2 = r2pipe.open(file_path)
    
    # Analyze file
    r2.cmd("aaa")
    
    # Get function list
    functions = r2.cmdj("aflj")
    
    # Analyze each function
    for func in functions:
        print(f"Analyzing function: {func['name']} at {hex(func['offset'])}")
        
        # Get disassembly of function
        insn = r2.cmdj(f"pdfj {func['offset']}")
        for op in insn['ops']:
            print(f"  {op['mnem']} {op['disasm']}")
    
    r2.quit()

if __name__ == "__main__":
    analyze_binary("file.exe")

Comparison of Radare2 Scripts with IDAPython/Ghidra Scripts:

FeatureRadare2 ScriptsIDAPythonGhidra Scripts
Languager2 commands/r2pipe PythonPythonJava/Python
FunctionalityPowerful, but fewer APIsVery powerfulVery powerful
Community SupportActiveVery activeActive
Recommended UseCommand-line analysisPreferred for new projectsNSA open-source tool

Comprehensive Case Study

Case: Automated Analysis of IoT Device Firmware

Goal: Use IDAPython, Ghidra scripts, and Radare2 scripts to automate the analysis of an IoT device firmware

Steps:

Firmware Extraction:

  • Use binwalk to extract firmware
binwalk -e firmware.bin

IDAPython Analysis:

  • Write IDAPython script to analyze functions and cross-references
import idautils

# Analyze all functions
for func_ea in idautils.Functions():
	func_name = idc.get_func_name(func_ea)
	print(f"Function: {func_name} at {hex(func_ea)}")
	
	# Get all callers of the function
	callers = idautils.XrefsTo(func_ea, idaapi.fl_CN)
	for xref in callers:
		print(f"  Called from {hex(xref.frm)}")

Ghidra Script Analysis:

  • Write Ghidra Python script to analyze data types and structures
# Get all structures
for struct in currentProgram.getDataTypeManager().getStructures():
	print(f"Structure: {struct.getName()}")
	
	# Get fields of the structure
	for field in struct.getFields():
		print(f"  Field: {field.getName()} {field.getDataType().getName()}")

Radare2 Script Analysis:

  • Write Radare2 Python script to analyze binary file
import r2pipe

r2 = r2pipe.open("firmware.bin")
r2.cmd("aaa")

# Get all functions
functions = r2.cmdj("aflj")
for func in functions:
	print(f"Function at {hex(func['offset'])}: {func['name']}")
	
	# Get disassembly of function
	insn = r2.cmdj(f"pdfj {func['offset']}")
	for op in insn['ops']:
		print(f"  {op['mnem']} {op['disasm']}")

r2.quit()

Result Integration:

  • Compare and integrate analysis results from the three tools
  • Identify potential vulnerabilities and interesting code regions

    Case Analysis Results:

    • Successfully extracted firmware and analyzed it
    • Mutually verified analysis results from the three tools
    • Identified multiple potential vulnerability points
    • Generated a detailed analysis report

    Summary and Learning Path

    Script development is the core technology for automation and efficient analysis in reverse engineering. This tutorial has covered in detail:

    1. IDAPython and IDC: IDA Pro’s Python and old scripting language
    2. Ghidra Scripts: Java and Python script development
    3. Radare2 Scripts: Command language and Python API
    4. Comprehensive Case: Automated analysis of IoT device firmware

    Learning Path Recommendations:

    1. Basic Stage:
      • Learn assembly language (ARM, MIPS)
      • Master basic operations of IDA Pro
      • Learn IDAPython basic syntax
    2. Intermediate Stage:
      • Deeply learn IDAPython API
      • Learn Ghidra basic operations and Java/Python scripts
      • Master Radare2 basic commands and r2pipe API
    3. Advanced Stage:
      • Research IDAPython advanced applications (plugin development)
      • Learn Ghidra advanced script development
      • Master Radare2 advanced script writing
    4. Practice Stage:
      • Participate in open-source reverse engineering projects
      • Conduct actual automated analysis
      • Research the latest script development technologies

    Through continuous learning and practice, you will master advanced techniques in reverse engineering script development and contribute to security research and reverse engineering.

    Share your love