Reverse Engineering Rust Binaries
Advanced Reverse Engineering Techniques · Advanced Reverse Engineering track
6 min read
In this module
Rust's Unique Compilation Patterns
Rust compiles to native code through LLVM, so its binaries share a lot of DNA with their C and C++ cousins. The differences are everywhere though: ownership, algebraic data types, and a heavy standard library produce assembly patterns you won't see in any other language. Learn the patterns and Rust binaries open up surprisingly fast.
Rust Symbols and Naming
Rust has two mangling schemes, and you'll meet both. The legacy scheme borrows the C++ _ZN shape and appends a 64-bit hash (h plus 16 hex digits) in place of type information; it was the default for years (rustc 1.87, for example, still emits it). The v0 scheme from RFC 2603 opens with _R and encodes crate names, module paths, function names and every generic type parameter. Recent stable releases emit v0 by default (1.97 does), and -C symbol-mangling-version picks either one explicitly. Most binaries in the wild today still carry legacy names.
Source: mycrate::processLegacy: _ZN7mycrate7process17h846937bab3a93663Ev0: _RNvCs9otjnjnDHeO_7mycrate7processThe 17h...E tail is the giveaway that a _ZN symbol came from Rust rather than C++.
rustfilt reverses the encoding. Even after a strip pass, Rust binaries are notoriously generous to reverse engineers — panic messages bake in source file paths, function names, and line numbers. Grep for .rs: or panicked at and the program's internal module structure falls out almost for free.
Enum Discriminants and Layout
Rust enums are tagged unions, and the compiler is unusually clever about laying them out. Simplest case: a small integer discriminant.
enum Color { Red, Green, Blue }// Layout: single byte, values 0, 1, 2For enums with data, the discriminant precedes the variant data:
enum Shape { Circle(f64), // discriminant 0 + f64 Rectangle(f64, f64), // discriminant 1 + two f64s}// Size = 1 (discriminant) + 7 (padding) + 16 (largest variant) = 24 bytesRust pulls a clever trick called niche optimisation. If a type has bit patterns that are guaranteed invalid, the compiler co-opts them as discriminants. The textbook case is Option<&T>: references can never be null, so None becomes the null pointer, and the whole Option collapses down to a raw pointer's size. In disassembly that shows up as a null check where you'd expect a discriminant load.
Look for comparison sequences followed by different code paths. A match on a Rust enum typically loads a byte or integer, compares it against variant indices, and branches to variant-specific handling. The comparison values are usually small consecutive integers starting from 0. After the branch, each path accesses fields at variant-specific offsets within the same structure.
Trait Objects and Dynamic Dispatch
Rust trait objects use fat pointers, a pair of pointers travelling together: one to the data, one to the vtable. C++ stashes the vtable pointer inside the object itself. Rust keeps it on the reference, so the data is leaner and the reference is fatter.
Trait object: (data_ptr, vtable_ptr)Vtable layout: [drop_fn, size, align, method1, method2, ...]A trait object call in disassembly always follows the same beats. Load the vtable from the fat pointer's second word, index into it, then call with the data pointer parked in the first argument register. The vtable's first three slots are reliably the destructor, size, and alignment; the trait methods queue up after that.
Here are both halves for a &dyn Shape on x86-64, as current rustc emits them. Neither layout is a stable guarantee, so confirm against the binary in front of you, but this is the shape to look for.
&dyn Shape (fat pointer)
| Offset | Field | Size | Notes |
|---|---|---|---|
| 0x0 (0) | *const () data_ptr | 8 | The concrete value. Goes in rdi as self. |
| 0x8 (8) | *const VTable vtable_ptr | 8 | Loaded and indexed for every call. |
16 bytes shown.
vtable for Circle as dyn Shape
| Offset | Field | Size | Notes |
|---|---|---|---|
| 0x0 (0) | fn ptr drop_in_place | 8 | Destructor for Circle. |
| 0x8 (8) | usize size | 8 | size_of::<Circle>() |
| 0x10 (16) | usize align | 8 | align_of::<Circle>() |
| 0x18 (24) | fn ptr area | 8 | First trait method: call [vtable + 0x18]. |
32 bytes shown.
Panic Handlers and Unwinding
Rust's panic infrastructure leaves fingerprints everywhere. Every bounds check, every unwrap, every assertion that can panic emits a call to a panic function carrying a pointer to a core::panic::Location: a small read-only struct holding a pointer and length for the source file path string, then the line and column as 32-bit integers. The human-readable message, where there is one, is a separate string.
; Typical bounds checkcmp rax, rcxjb .Loklea rdx, [rip + .Lpanic_location] ; &Location { file: "src/...rs", line, col }call core::panicking::panic_bounds_checkFor reverse engineers, these panic strings are gold. They expose the original source layout, name function purposes, and even leak variable semantics. Panic message says "attempted to divide by zero"? That function is doing division. A reference to src/network/parser.rs:142 tells you exactly how the code is organised on disk.
Monomorphisation and Binary Size
Rust generics are monomorphised. Every generic function gets a fresh compile for each concrete type combination it's used with. HashMap::insert produces one body for HashMap<String, Vec<u8>>, another for HashMap<u32, bool>, and a third for any other shape that appears in the program.
The visible result is dozens of nearly-identical functions in the binary, differing only in sizes and offsets. That's why Rust binaries tend to dwarf equivalent C ones. For a reverse engineer, the upside is that once you've understood one instantiation, the rest fall over quickly.
Standard library code — Vec operations, HashMap methods, iterator adaptors — shows up monomorphised throughout any sizeable Rust binary. Burn the assembly shapes of common std operations into your visual memory: a Vec::push with capacity check and reallocation, the linear-probe sequence inside HashMap, the closure-stitching inside iterator chains. Once you can dismiss those at a glance, what's left is the program's actual logic.
Tools and Techniques
A practical Rust workflow in five moves. Run rustfilt over whatever symbols are left. Grep .rs: to recover the source layout. Read panic strings to figure out what functions do. Spot fat pointers to find trait-object call sites. Recognise the discriminant-check-then-branch shape that gives away enum matches. Tooling helps a lot here: recent Ghidra and IDA releases demangle Rust symbols out of the box, which automates the most tedious bits. Don't expect a rulebook for struct layout, though. The default repr(Rust) layout is deliberately unspecified and the compiler reorders fields, so recover each struct from its field accesses, as you would for C.