Skip to content
sunbird-toolsPublic

About

Recovers Rust struct and enum layouts from stripped binaries by undoing what serde and Debug derives generated

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

20 Commits

Folders and files

Repository files navigation

underive

Recovers Rust struct and enum shapes from a stripped binary by reading the code serde's and #[derive(Debug)]'s derives generate. The first target is Discord's Desktop Phoenix (a Rust GPUI app).

underive <binary | setup.exe> [--entry Discord.exe] [--json out.json] [--rust out.rs] [--type Name] [--names other.json]

--rust writes the types as Rust source: #[repr(C)] structs with each placed field at its recovered offset and an offset_of! assertion per field, so the file compiles only if the layout matches. Unknown spans are byte padding. A field gets a real type when the guess pins its layout: a primitive, RawVec<T> (24-byte sequence), RawSlice<T> (16-byte sequence) or RawPtr<T> (8-byte Box/&/Option of one) pointing at another recovered struct, or that struct inline when its whole size is known and equals the field's. Everything else stays bytes with the guess in a comment. A field serializer that reaches a type's serializer through a loop marks the field as a sequence, and when the loop calls that serializer directly, its pointer step is size_of the type, which becomes a size_of assertion. Enums keep only their variant names, since neither derive reveals discriminants.

Without --json or --rust it prints each recovered type with its kind, fields (name, offset, size, a type guess, and which derives saw it), enum variants, and the bytes between known fields that nothing covers. --json writes the same data as JSON, and --type keeps only types with that exact name. --names takes another build's --json output and names serializers this binary leaves nameless after the struct there with the same wire keys at the same offsets (at least 2 and 3 in 4, with no other name tied); those types list reference among their sources. Every field also carries type_fn, the RVA of its Debug fmt or serde field serializer, so equal ids mean equal types even when the type has no name.

Build

A setup executable works as input too: its payload is a zip of zstd entries appended after the PE, and underive scans the Discord.exe inside it (or the entry --entry names) without writing it out.

phoenix-sigs and phoenix-hook come from Sunbird, pinned to a commit in Cargo.toml. On Linux, cargo build --release is enough. On Windows they need the GNU toolchain set up as Sunbird's AGENTS.md describes, which build.ps1 does (target dir E:\phoenix-build\underive-target):

.\build.ps1                    # cargo build --release
.\build.ps1 clippy --release
.\build.ps1 test --release     # runs against the Discord binaries if present

CI

.github/workflows/scan.yml builds underive on Linux, downloads a Windows setup (SETUP_URL, or the url input of a manual run), scans the Discord.exe inside it, checks that the generated Rust compiles, and commits windows-x64/Discord.{rs,json,txt} plus a BUILD note to the results branch. Every scan that changes anything is one commit there, so git log -p results or GitHub's compare view shows what a new build or an underive change moved.

How it reads types

Every function is walked once on all cores (src/flow.rs). Registers carry constants, addresses, stack slots, and "entry argument plus offset" (including up to three loads through an argument), and stores to the stack are remembered. That way each call's arguments, register or stack, read as values. Call sites are then classified by argument shape alone (src/scan.rs), so no callee needs a symbol:

Shape Gives
serialize_struct(ser, "Name", len, n) then serialize_field(state, "key", len, &self.f) type name, wire keys in order, exact offsets
serialize_key(state, "key") then serialize_value(&self.f) the same, for map-backed serializers
serialize_*_variant(ser, "Enum", len, index, "Variant", len, ..) enum name, variant names and indices
debug_struct_field{N}_finish(f, "Name", ("a", &self.a, &vtable) * N), debug_struct_fields_finish, debug_tuple_* Rust field names, offsets, sizes from the vtable's size word, types from its fmt
write_str(f, "Variant") on the same formatter beside those unit variants
deserialize_struct(de, "Name", len, &FIELDS, n) field names in order, no offsets

src/model.rs deduplicates monomorphized copies by name and shape, merges serde and Debug views of a type when their offsets agree (Debug lists every field under its Rust name; serde adds the wire key where rename changed it), adopts serializer copies whose serialize_struct call was inlined away when their keys and offsets match a named shape, and resolves field types. src/leaf.rs names leaf types from their Debug code: bool loads "true"/"false"; integers test the {:x?} flags; floats use SSE compares; str writes '"'; String, &str, Arc<str>, Vec<T> and &[T] are pointer/length forwarders into the str or slice fmt, which passes each element's vtable to entry; and Option/Result are recovered enums named by their variants.

Known limits

  • Offsets are read from the code, never summed, so they stay exact around fields a derive skips. Sizes come only from Debug vtables. A field without one shows <=N, the distance to the next known offset, and unknown bytes lists spans after a sized field that no recovered field starts in: padding, or a field serde skips and Debug never saw. Total struct size isn't recovered yet (drop glue, allocation sizes or memcpy lengths could supply it).
  • #[serde(rename)] shows up as wire, and only where Debug also saw the field.
  • skip_serializing_if fields are found (they sit behind a branch). flatten and serialize_with fields get the offset of what the wrapper points at.
  • Fields serialized without passing a pointer (unit enums matched inline into a string, for example) have no offset.
  • Integers are named by width only (u64/i64). Signedness isn't read.
  • Debug doesn't print enum names. An enum is named when its variant set equals a serde enum's, or is Option, Result or Cow. Otherwise it shows as enum{A|B|...}.
  • Types sharing a name across modules (there are many Messages) stay separate shapes under the same name.
  • On the Linux build, serde_json's deserialize_struct is inlined and the FIELDS tables are gone, so Deserialize contributes almost nothing there.
  • Android: scan the liblibdiscord_wasm_host.so from an APK's config.x86_64 split (native Rust despite the name). Most serde serializers there have serialize_struct inlined and the types have no Debug impl to name them, so far fewer types come out named than on desktop (1,003 types on 356207 against 4,740 on Windows). The unnamed serializers are found and, without --names, dropped, Message among them. With --names pointing at a Windows scan, 467 more types are named on x86_64 (1,464 in all) and 190 on arm64 (1,108), Message with id at +0x1d8 on both. Checked on the mobile types that do carry their own name, the reference rule gives the same name for 107 and a different one for 2 (AnnouncementMessageLinkClicked read as AnnouncementMessageViewed, SummaryItem as Health); a struct mobile adds, or one desktop never names, stays unnamed. The arm64-v8a split reads straight from the APK (--entry lib/arm64-v8a/liblibdiscord_wasm_host.so) and gives 923 types without --names. Against x86_64 on the same build, 94% of shared fields agree on offset; most of the rest are whole types shifted by 16 bytes, unverified either way.
  • arm64 (macOS) is experimental: outlined stubs are flattened through Sunbird's flat_steps, and offsets agree with x86-64 where checked, but leaf-type signatures are x86-only, so type guesses there are wrong or missing.

Sunbird API gaps worked around locally

  • index::flow::Val has no "argument plus offset" value and keeps no stack slots, so src/flow.rs has its own walk.
  • index::flow::calls and index::across are crate-private. underive matches calls on Native itself and lists every function through Index::heads(0, ..), then splits them over threads itself.

About

Recovers Rust struct and enum layouts from stripped binaries by undoing what serde and Debug derives generated

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages