Skip to main content
Be Bitwise

Reverse Engineering Data Structures

Binary Analysis with Ghidra · Reverse Engineering track

8 min read

In this module

Recovering Types from Binaries

Compilation throws away type information. Struct definitions, enum values, class hierarchies — gone unless debug symbols were included. What's left in a stripped binary is raw memory accesses at numeric offsets. Recovering the original data structures from those patterns is one of the most useful skills in binary analysis.

Identifying Structs from Access Patterns

The key insight for struct recovery: when multiple functions access the same pointer with consistent fixed offsets, those offsets correspond to struct fields. Consider this decompiler output:

c
void FUN_00401500(long param_1) {
*(int *)(param_1 + 0) = 1; // Field at offset 0, 4 bytes (int)
*(int *)(param_1 + 4) = 0x100; // Field at offset 4, 4 bytes (int)
*(long *)(param_1 + 8) = malloc(0x100); // Field at offset 8, 8 bytes (pointer)
*(int *)(param_1 + 0x10) = 0; // Field at offset 16, 4 bytes (int)
}

This function initialises four fields of a structure pointed to by param_1. The access pattern reveals:

OffsetSizeLikely TypePurpose (guessed from values)
0x004intType/state (set to 1)
0x044intSize/capacity (set to 256)
0x088pointerBuffer (malloc'd)
0x104intCount/index (set to 0)

Put those four fields in order with x86-64 sizes and the layout tool below fills in the part no instruction touched: 4 bytes of tail padding after length, there so that in an array of these structs every data field stays 8-byte aligned. Each note is the decompiler line that gave the field away.

Recovered from FUN_00401500

0x00x18
OffsetFieldSizeNotes
0x0 (0)int type4*(int *)(param_1 + 0) = 1
0x4 (4)int capacity4*(int *)(param_1 + 4) = 0x100
0x8 (8)void* data8*(long *)(param_1 + 8) = malloc(0x100)
0x10 (16)int length4*(int *)(param_1 + 0x10) = 0
0x14 (20)padding4

sizeof = 24 bytes, 4 of them padding; alignment 8.

Now examine other functions that receive the same type of pointer. If FUN_00401600 reads *(param_1 + 4) and uses it as a size limit for *(param_1 + 8), the picture becomes clearer: this is a dynamically-sized buffer struct with a capacity field.

Creating Custom Data Types in Ghidra

Once you have identified a struct's layout, create it in Ghidra's Data Type Manager:

  1. Open the Data Type Manager (lower-left panel)
  2. Right-click your program's data type folder -> New -> Structure
  3. Name the structure (e.g., buffer_t)
  4. Add fields with appropriate types, names, and sizes

For the example above:

c
struct buffer_t {
int type; // offset 0x00
int capacity; // offset 0x04
void *data; // offset 0x08
int length; // offset 0x10
}; // total size: 0x18 (24 bytes): 4 bytes of tail
// padding keep `data` aligned in an array

After creating the type, you can apply it to function parameters. Right-click on param_1 in the decompiler and select "Retype Variable," choosing your new buffer_t * type. The decompiler output transforms:

Before:

c
*(int *)(param_1 + 4) = 0x100;
*(long *)(param_1 + 8) = malloc(0x100);

After:

c
param_1->capacity = 0x100;
param_1->data = malloc(0x100);

That readability improvement cascades through every function that takes a buffer_t *. One type definition, dozens of functions clarified. That's the payoff.

Iterative Refinement

You rarely get struct definitions right on the first try. Start with what you know, apply the type, and read more code. Additional functions that use the struct will reveal fields you missed, correct field sizes you guessed wrong, and clarify field purposes. Expect to revise struct definitions multiple times as your understanding deepens.

Applying Struct Types to Memory

Beyond function parameters, you can apply struct types to global data, stack variables, and heap allocations:

Global data - if you find a global variable that appears to be an array of structs, select the address in the Listing view, right-click, and choose Data -> your struct type. Ghidra will lay out the struct fields with named labels.

Stack variables - in the decompiler, right-click a local variable and retype it to your struct. The decompiler will show field accesses instead of raw pointer arithmetic.

Arrays of structs - if a function iterates through memory with a stride matching your struct size (e.g., accessing base + i*24 for the 24-byte buffer_t above), define an array type: buffer_t[N]. Check that the structure size Ghidra reports matches the stride: the structure editor only inserts alignment padding when packing is enabled for that structure, so without it (or padding added by hand) you'll get 0x14 rather than 0x18.

Understanding Vtables in C++ Binaries

C++ binaries introduce virtual function tables (vtables) - arrays of function pointers used for dynamic dispatch. Recognizing vtables is essential for understanding C++ object hierarchies in stripped binaries.

A vtable pattern in Ghidra's decompiler looks like:

c
void FUN_00402000(long param_1) {
// Calling a virtual function:
long vtable = *(long *)param_1; // First field is vtable pointer
long vfunc = *(long *)(vtable + 0x10); // Entry at offset 0x10 in vtable
(*(code *)vfunc)(param_1); // Call with 'this' pointer
}

The pattern is:

  1. Dereference the object pointer to get the vtable address (at offset 0 for single inheritance; with multiple inheritance an object holds a vtable pointer for each polymorphic base, at other offsets too)
  2. Index into the vtable to get a specific function pointer
  3. Call through the function pointer, passing the object as the first argument (this)

Recovering a vtable:

  1. Find the vtable address (the constant loaded into the first field during construction)
  2. Navigate to that address in the Listing view
  3. Define each entry as a pointer to a function
  4. The vtable reveals the class's virtual functions and their order

For vulnerability research, vtable corruption is a powerful exploitation technique. If an attacker can overwrite an object's vtable pointer (via use-after-free or heap overflow), they can redirect virtual function calls to arbitrary code.

Enum Recovery

Enums appear in binaries as integer constants used in switch statements or comparisons. You recognise them by:

  • A variable being compared against multiple distinct constants
  • Switch/case tables that branch on these values
  • Functions that map integer values to strings (often for logging)
c
// This suggests an enum for message types
switch(msg_type) {
case 1: handle_login(); break;
case 2: handle_data(); break;
case 3: handle_logout(); break;
case 4: handle_heartbeat(); break;
default: handle_unknown(); break;
}

Create an enum in Ghidra:

  1. Data Type Manager -> right-click -> New -> Enum
  2. Name it (e.g., msg_type_e)
  3. Add values: MSG_TYPE_LOGIN=1, MSG_TYPE_DATA=2, MSG_TYPE_LOGOUT=3, MSG_TYPE_HEARTBEAT=4
  4. Apply the enum type to the variable

The decompiler then shows case MSG_TYPE_LOGIN: instead of case 1:, making the code immediately more readable.

Array Detection

Arrays are identified by indexed access patterns. When code accesses base + index * element_size, you are looking at an array:

c
// Array access pattern (before typing)
int value = *(int *)(buffer + i * 4); // int array[]: buffer[i]
// Struct array pattern (before typing)
int field = *(int *)(list + i * 0x20 + 8); // struct_t list[]: list[i].field_at_8

When you see a loop with:

  • A counter variable incrementing from 0 to some limit
  • Memory accesses at base + counter * stride
  • The stride matching a power of 2 or a struct size you have identified

Define an array type at base and apply it. The decompiler output transforms from pointer arithmetic into readable array indexing.

Practical Recovery Workflow

A structured approach to type recovery:

  1. Start with known functions - imported library functions have known signatures. If recv(fd, buf, len, flags) is called, the parameters tell you something about the surrounding code's types.

  2. Identify constructors/initializers - functions that set multiple fields to default values reveal the struct layout in one place.

  3. Follow the type across functions - once you identify a struct in one function, find all functions that receive the same type (via xrefs to the constructor or common operations). Each function reveals additional fields.

  4. Use string references - if a function accesses a struct field and then passes it to a logging function alongside a format string like "username: %s", you know that field is a string pointer and can guess its name.

  5. Cross-reference with documentation - if you are analysing a program that implements a known protocol, the protocol specification tells you what data structures to expect.

Type Recovery for VR

In vulnerability research, accurate type recovery directly aids bug finding. When you see param_1->length instead of *(int*)(param_1 + 4), you can immediately reason about whether length checks are correct. When you see obj->vtable instead of *(long*)param_1, you can identify vtable corruption opportunities. The investment in type recovery pays dividends throughout the analysis.