refactor(metadata): centralize verified layout definitions

This commit is contained in:
Momoko-Ayase committed 2026-11-05 21:36:32 +08:00
1 parent 682beae9e7
commit 1d64c732d9
8 files changed
+729 -152

No files matched your search

Generated
+1
View File
@@ -488,6 +488,7 @@ name = "senbei-metadata"
version = "1.3.4"
dependencies = [
"serde",
"serde_json",
"thiserror",
]
+3
View File
@@ -8,3 +8,6 @@ description = "Unity il2cpp metadata de-obfuscation for Senbei"
[dependencies]
serde.workspace = true
thiserror.workspace = true
[dev-dependencies]
serde_json.workspace = true
@@ -8,11 +8,12 @@
use serde::Serialize;
use crate::common::MAGIC;
use crate::layout::{HEADER, MetadataVersion, V24_1_METHOD_INDEX};
const RAW_VERSION_24: u32 = 24;
const HDR_METHODS: usize = 0x30;
const V24_1_METHOD_STRIDE: usize = 0x34;
const V24_1_METHOD_INDEX_OFFSET: usize = 0x14;
const RAW_VERSION_24: u32 = MetadataVersion::V24.raw();
const HDR_METHODS: usize = HEADER.methods;
const V24_1_METHOD_STRIDE: usize = V24_1_METHOD_INDEX.stride;
const V24_1_METHOD_INDEX_OFFSET: usize = V24_1_METHOD_INDEX.index;
const V24_1_SELECTOR: u32 = 1;
const EXCEPTION_COUNT: usize = 256;
+66 -127
View File
@@ -3,48 +3,39 @@
use serde::Serialize;
use crate::common::MAGIC;
#[cfg(test)]
use crate::layout::V31;
use crate::layout::v39 as v39_layout;
use crate::layout::{self, HEADER, MetadataVersion, V29};
/// Seed embedded in the current `libil2cpp` module `0x0C`.
pub const DEFAULT_METHOD_TOKEN_SEED: u32 = 0xa6fa_e968;
const SUPPORTED_V29: u32 = 29;
const SUPPORTED_V31: u32 = 31;
const HDR_METHODS: usize = 0x30;
const HDR_TYPES: usize = 0xa0;
const HDR_IMAGES: usize = 0xa8;
const TYPE_STRIDE: usize = 0x58;
const TYPE_METHOD_START_OFFSET: usize = 0x24;
const TYPE_METHOD_COUNT_OFFSET: usize = 0x40;
const IMAGE_STRIDE: usize = 0x28;
const IMAGE_TYPE_START_OFFSET: usize = 0x08;
const IMAGE_TYPE_COUNT_OFFSET: usize = 0x0c;
#[cfg(test)]
const SUPPORTED_V29: u32 = MetadataVersion::V29.raw();
#[cfg(test)]
const SUPPORTED_V31: u32 = MetadataVersion::V31.raw();
const SUPPORTED_V39: u32 = MetadataVersion::V39.raw();
const HDR_METHODS: usize = HEADER.methods;
const HDR_TYPES: usize = HEADER.types;
const HDR_IMAGES: usize = HEADER.images;
const TYPE_STRIDE: usize = V29.types.stride;
const TYPE_METHOD_START_OFFSET: usize = V29.types.method_start;
const TYPE_METHOD_COUNT_OFFSET: usize = V29.types.method_count;
const IMAGE_STRIDE: usize = V29.images.stride;
const IMAGE_TYPE_START_OFFSET: usize = V29.images.type_start;
const IMAGE_TYPE_COUNT_OFFSET: usize = V29.images.type_count;
const METHOD_TOKEN_TABLE: u32 = 0x0600_0000;
// The aliases keep the v31 synthetic fixtures readable; production paths use
// the version-specific layout returned by `layout_for_version`.
#[cfg(test)]
const METHOD_STRIDE: usize = 0x24;
const METHOD_STRIDE: usize = V31.method.stride;
#[cfg(test)]
const METHOD_TOKEN_OFFSET: usize = 0x18;
const METHOD_TOKEN_OFFSET: usize = V31.method.token;
#[derive(Clone, Copy)]
struct Layout {
method_stride: usize,
method_token_offset: usize,
}
fn layout_for_version(version: u32) -> Option<Layout> {
match version {
SUPPORTED_V29 => Some(Layout {
method_stride: 0x20,
method_token_offset: 0x14,
}),
SUPPORTED_V31 => Some(Layout {
method_stride: 0x24,
method_token_offset: 0x18,
}),
_ => None,
}
fn layout_for_version(version: u32) -> Option<layout::FixedLayout> {
layout::fixed_layout(version)
}
/// Summary of one metadata restoration pass.
@@ -198,7 +189,7 @@ pub fn restore_method_tokens(data: &[u8], seed: u32) -> Result<(Vec<u8>, Report)
return Err(Error::NotMetadata);
}
let version = read_u32(data, 4)?;
if version == 39 {
if version == SUPPORTED_V39 {
return restore_v39(data, seed);
}
let Some(layout) = layout_for_version(version) else {
@@ -208,13 +199,13 @@ pub fn restore_method_tokens(data: &[u8], seed: u32) -> Result<(Vec<u8>, Report)
let (method_offset, method_size) = table(data, HDR_METHODS)?;
let (type_offset, type_size) = table(data, HDR_TYPES)?;
let (image_offset, image_size) = table(data, HDR_IMAGES)?;
if method_size % layout.method_stride != 0
if method_size % layout.method.stride != 0
|| type_size % TYPE_STRIDE != 0
|| image_size % IMAGE_STRIDE != 0
{
return malformed("method/type/image table size is not divisible by its entry stride");
}
let method_count = method_size / layout.method_stride;
let method_count = method_size / layout.method.stride;
let type_count = type_size / TYPE_STRIDE;
let image_count = image_size / IMAGE_STRIDE;
let mut owners = vec![u32::MAX; method_count];
@@ -295,7 +286,7 @@ pub fn restore_method_tokens(data: &[u8], seed: u32) -> Result<(Vec<u8>, Report)
let mut image_already_clean = true;
for &method_index in &methods {
let token_offset =
method_offset + method_index * layout.method_stride + layout.method_token_offset;
method_offset + method_index * layout.method.stride + layout.method.token;
let token = read_u32(data, token_offset)?;
if token & 0xff00_0000 != METHOD_TOKEN_TABLE {
return malformed(format!(
@@ -399,7 +390,7 @@ pub fn discover_method_token_seeds(data: &[u8]) -> Result<SeedDiscoveryReport> {
return Err(Error::NotMetadata);
}
let version = read_u32(data, 4)?;
if version == 39 {
if version == SUPPORTED_V39 {
return discover_v39(data);
}
let Some(layout) = layout_for_version(version) else {
@@ -412,13 +403,13 @@ pub fn discover_method_token_seeds(data: &[u8]) -> Result<SeedDiscoveryReport> {
let (method_offset, method_size) = table(data, HDR_METHODS)?;
let (type_offset, type_size) = table(data, HDR_TYPES)?;
let (image_offset, image_size) = table(data, HDR_IMAGES)?;
if method_size % layout.method_stride != 0
if method_size % layout.method.stride != 0
|| type_size % TYPE_STRIDE != 0
|| image_size % IMAGE_STRIDE != 0
{
return malformed("method/type/image table size is not divisible by its entry stride");
}
let method_count = method_size / layout.method_stride;
let method_count = method_size / layout.method.stride;
let type_count = type_size / TYPE_STRIDE;
let image_count = image_size / IMAGE_STRIDE;
let mut reports = Vec::with_capacity(image_count);
@@ -480,7 +471,7 @@ pub fn discover_method_token_seeds(data: &[u8]) -> Result<SeedDiscoveryReport> {
for method_index in methods {
let token = read_u32(
data,
method_offset + method_index * layout.method_stride + layout.method_token_offset,
method_offset + method_index * layout.method.stride + layout.method.token,
)?;
if token & 0xff00_0000 != METHOD_TOKEN_TABLE {
return validation(format!(
@@ -584,12 +575,12 @@ fn decrypt_rid_with_key(rid: u32, low: u32, high: u32, key: u32) -> u32 {
value + low
}
const V39_METHODS: usize = 5;
const V39_PARAMETERS: usize = 10;
const V39_GENERIC_CONTAINERS: usize = 14;
const V39_INTERFACE_OFFSETS: usize = 18;
const V39_TYPES: usize = 19;
const V39_IMAGES: usize = 20;
const V39_METHODS: usize = v39_layout::SECTION_METHODS;
const V39_PARAMETERS: usize = v39_layout::SECTION_PARAMETERS;
const V39_GENERIC_CONTAINERS: usize = v39_layout::SECTION_GENERIC_CONTAINERS;
const V39_INTERFACE_OFFSETS: usize = v39_layout::SECTION_INTERFACE_OFFSETS;
const V39_TYPES: usize = v39_layout::SECTION_TYPES;
const V39_IMAGES: usize = v39_layout::SECTION_IMAGES;
#[derive(Debug, Clone, Copy)]
struct V39Layout {
@@ -609,10 +600,10 @@ struct V39Layout {
}
fn v39_section(data: &[u8], index: usize) -> Result<(usize, usize, usize)> {
let header = 8_usize
let header = v39_layout::SECTION_HEADER_BASE
.checked_add(
index
.checked_mul(12)
.checked_mul(v39_layout::SECTION_HEADER_STRIDE)
.ok_or_else(|| Error::Malformed("v39 section header offset overflow".to_owned()))?,
)
.ok_or_else(|| Error::Malformed("v39 section header offset overflow".to_owned()))?;
@@ -623,16 +614,6 @@ fn v39_section(data: &[u8], index: usize) -> Result<(usize, usize, usize)> {
Ok((offset, size, count))
}
fn v39_index_width(count: usize) -> usize {
if count <= u8::MAX as usize {
1
} else if count <= u16::MAX as usize {
2
} else {
4
}
}
fn read_v39_index(data: &[u8], offset: usize, width: usize) -> Result<usize> {
match width {
1 => Ok(bytes(data, offset, 1)?[0] as usize),
@@ -650,90 +631,48 @@ fn parse_v39(data: &[u8]) -> Result<V39Layout> {
v39_section(data, V39_INTERFACE_OFFSETS)?;
let (type_offset, type_size, type_count) = v39_section(data, V39_TYPES)?;
let (image_offset, image_size, image_count) = v39_section(data, V39_IMAGES)?;
let parameter_index_width = v39_index_width(parameter_count);
let generic_container_index_width = v39_index_width(generic_count);
let type_definition_index_width = v39_index_width(type_count);
let type_index_width = if interface_count == 0 {
4
} else {
let element_size = interface_size
.checked_div(interface_count)
.ok_or_else(|| Error::Malformed("v39 interface-offset size is invalid".to_owned()))?;
element_size
.checked_sub(4)
.filter(|width| matches!(width, 1 | 2 | 4))
.ok_or_else(|| Error::Malformed("v39 type-index width is invalid".to_owned()))?
};
let method_stride = 20_usize
.checked_add(type_definition_index_width)
.and_then(|size| size.checked_add(type_index_width))
.and_then(|size| size.checked_add(parameter_index_width))
.and_then(|size| size.checked_add(generic_container_index_width))
.ok_or_else(|| Error::Malformed("v39 method stride overflow".to_owned()))?;
let type_stride = 68_usize
.checked_add(
type_index_width
.checked_mul(3)
.ok_or_else(|| Error::Malformed("v39 type stride overflow".to_owned()))?,
)
.and_then(|size| size.checked_add(generic_container_index_width))
.ok_or_else(|| Error::Malformed("v39 type stride overflow".to_owned()))?;
let image_stride = 32_usize
.checked_add(
type_definition_index_width
.checked_mul(2)
.ok_or_else(|| Error::Malformed("v39 image stride overflow".to_owned()))?,
)
.ok_or_else(|| Error::Malformed("v39 image stride overflow".to_owned()))?;
if method_count.checked_mul(method_stride) != Some(method_size)
|| type_count.checked_mul(type_stride) != Some(type_size)
|| image_count.checked_mul(image_stride) != Some(image_size)
let type_definition_index_width = v39_layout::index_width(type_count);
let type_index_width = v39_layout::type_index_width(interface_size, interface_count)
.ok_or_else(|| Error::Malformed("v39 type-index width is invalid".to_owned()))?;
let records = v39_layout::Records::from_widths(v39_layout::Widths {
type_definition: type_definition_index_width,
type_index: type_index_width,
parameter: v39_layout::index_width(parameter_count),
generic_container: v39_layout::index_width(generic_count),
})
.ok_or_else(|| Error::Malformed("v39 compact record layout overflow".to_owned()))?;
if method_count.checked_mul(records.method_stride) != Some(method_size)
|| type_count.checked_mul(records.type_stride) != Some(type_size)
|| image_count.checked_mul(records.image_stride) != Some(image_size)
|| parameter_count == 0 && parameter_size != 0
{
return malformed("v39 table size does not match its compact entry layout");
}
let type_method_start_offset = 16_usize
.checked_add(
type_index_width
.checked_mul(3)
.ok_or_else(|| Error::Malformed("v39 type method offset overflow".to_owned()))?,
)
.and_then(|offset| offset.checked_add(generic_container_index_width))
.ok_or_else(|| Error::Malformed("v39 type method offset overflow".to_owned()))?;
let type_method_count_offset = type_method_start_offset
.checked_add(7 * 4)
.ok_or_else(|| Error::Malformed("v39 type method count offset overflow".to_owned()))?;
let method_token_offset = 4_usize
.checked_add(type_definition_index_width)
.and_then(|offset| offset.checked_add(type_index_width))
.and_then(|offset| offset.checked_add(4))
.and_then(|offset| offset.checked_add(parameter_index_width))
.and_then(|offset| offset.checked_add(generic_container_index_width))
.ok_or_else(|| Error::Malformed("v39 method token offset overflow".to_owned()))?;
if method_token_offset
if records
.method_token_offset
.checked_add(4)
.is_none_or(|end| end > method_stride)
|| type_method_count_offset
.is_none_or(|end| end > records.method_stride)
|| records
.type_method_count_offset
.checked_add(2)
.is_none_or(|end| end > type_stride)
.is_none_or(|end| end > records.type_stride)
{
return malformed("v39 compact layout fields exceed their records");
}
// Touch the section base so malformed headers fail before any output copy.
Ok(V39Layout {
method_offset,
method_count,
method_stride,
method_token_offset,
method_stride: records.method_stride,
method_token_offset: records.method_token_offset,
type_definition_index_width,
type_offset,
type_count,
type_stride,
type_method_start_offset,
type_method_count_offset,
type_stride: records.type_stride,
type_method_start_offset: records.type_method_start_offset,
type_method_count_offset: records.type_method_count_offset,
image_offset,
image_count,
image_stride,
image_stride: records.image_stride,
})
}
@@ -793,7 +732,7 @@ fn v39_report(
seed: u32,
) -> Report {
Report {
version: 39,
version: SUPPORTED_V39,
seed: format!("0x{seed:08X}"),
encryption_status: if changed_tokens == 0 {
"clean".to_owned()
@@ -1005,7 +944,7 @@ fn discover_v39(data: &[u8]) -> Result<SeedDiscoveryReport> {
// The caller still tries the known default seed and validates it fully.
if anchor.modulus < 1024 {
return Ok(SeedDiscoveryReport {
version: 39,
version: SUPPORTED_V39,
images: reports,
seed_candidates: candidates,
});
@@ -1030,7 +969,7 @@ fn discover_v39(data: &[u8]) -> Result<SeedDiscoveryReport> {
candidates.sort_unstable();
candidates.dedup();
Ok(SeedDiscoveryReport {
version: 39,
version: SUPPORTED_V39,
images: reports,
seed_candidates: candidates,
})
+1 -2
View File
@@ -1,7 +1,6 @@
//! Shared IL2CPP metadata header primitives.
/// IL2CPP global-metadata sanity magic.
pub(crate) const MAGIC: u32 = 0xFAB1_1BAF;
pub(crate) use crate::layout::MAGIC;
/// Cheap check used by both platform scanners before opening a full metadata
/// file.
+634
View File
@@ -0,0 +1,634 @@
//! Known il2cpp metadata byte layouts and format identification.
//!
//! This module owns the byte-level facts used by the restoration algorithms:
//! header field offsets, record strides and field offsets. Parsing, validation
//! and recovery stay in the sibling modules; nothing here opens an input or
//! decides that a restore will succeed.
//!
//! [`identify`] is intentionally a raw-version check. A `global-metadata.dat`
//! can carry a version whose record layout is not known, and version 24 does
//! not by itself identify the context-dependent v24.1 method-index layout. The
//! returned [`Capabilities`] list the entry points implemented for a version;
//! they are not a protection verdict, and an attempted restore may still
//! reject the input. Identification reads only the 8-byte header, so a
//! [`LayoutStatus::Known`] result states that a layout definition exists for
//! that raw version — never that this particular input validated against it.
use serde::Serialize;
/// il2cpp `global-metadata.dat` sanity magic.
pub(crate) const MAGIC: u32 = 0xFAB1_1BAF;
/// Fixed header fields shared by the layouts in this module. Table entries are
/// little-endian `i32` (offset, size) pairs.
#[derive(Debug, Clone, Copy)]
pub(crate) struct HeaderLayout {
pub magic: usize,
pub version: usize,
pub methods: usize,
pub types: usize,
pub images: usize,
}
pub(crate) const HEADER: HeaderLayout = HeaderLayout {
magic: 0x00,
version: 0x04,
methods: 0x30,
types: 0xA0,
images: 0xA8,
};
/// `Il2CppMethodDefinition` record.
#[derive(Debug, Clone, Copy)]
pub(crate) struct MethodRecord {
pub stride: usize,
pub token: usize,
}
/// `Il2CppTypeDefinition` record.
#[derive(Debug, Clone, Copy)]
pub(crate) struct TypeRecord {
pub stride: usize,
pub method_start: usize,
pub method_count: usize,
}
/// `Il2CppImageDefinition` record.
#[derive(Debug, Clone, Copy)]
pub(crate) struct ImageRecord {
pub stride: usize,
pub type_start: usize,
pub type_count: usize,
}
/// Fixed record layout shared by the non-compact metadata versions.
#[derive(Debug, Clone, Copy)]
pub(crate) struct FixedLayout {
pub method: MethodRecord,
pub types: TypeRecord,
pub images: ImageRecord,
}
pub(crate) const V29: FixedLayout = FixedLayout {
method: MethodRecord {
stride: 0x20,
token: 0x14,
},
types: TypeRecord {
stride: 0x58,
method_start: 0x24,
method_count: 0x40,
},
images: ImageRecord {
stride: 0x28,
type_start: 0x08,
type_count: 0x0C,
},
};
pub(crate) const V31: FixedLayout = FixedLayout {
method: MethodRecord {
stride: 0x24,
token: 0x18,
},
types: V29.types,
images: V29.images,
};
/// The context-validated v24.1 `Il2CppMethodDefinition.methodIndex` record.
///
/// This layout is applied only after the accompanying decoded module 0x0C has
/// supplied and validated a profile; the raw version 24 tag is not enough.
#[derive(Debug, Clone, Copy)]
pub(crate) struct MethodIndexRecord {
pub stride: usize,
pub index: usize,
}
pub(crate) const V24_1_METHOD_INDEX: MethodIndexRecord = MethodIndexRecord {
stride: 0x34,
index: 0x14,
};
/// Version 39 uses a compact section directory instead of the fixed header
/// table pairs. Each directory record is (offset, size, count). The record
/// layout is the known observed definition derived from the compact index
/// widths, not a fixed-stride one; it is validated per input by the parser.
pub(crate) mod v39 {
/// First section record in the compact header.
pub(crate) const SECTION_HEADER_BASE: usize = 0x08;
/// Byte stride of one section record.
pub(crate) const SECTION_HEADER_STRIDE: usize = 0x0C;
/// Section indices used by method-token restoration.
pub(crate) const SECTION_METHODS: usize = 5;
pub(crate) const SECTION_PARAMETERS: usize = 10;
pub(crate) const SECTION_GENERIC_CONTAINERS: usize = 14;
pub(crate) const SECTION_INTERFACE_OFFSETS: usize = 18;
pub(crate) const SECTION_TYPES: usize = 19;
pub(crate) const SECTION_IMAGES: usize = 20;
/// Widths of the compact indices stored inside v39 records.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Widths {
pub type_definition: usize,
pub type_index: usize,
pub parameter: usize,
pub generic_container: usize,
}
/// Record strides and field offsets derived from [`Widths`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Records {
pub method_stride: usize,
pub method_token_offset: usize,
pub type_stride: usize,
pub type_method_start_offset: usize,
pub type_method_count_offset: usize,
pub image_stride: usize,
}
/// Compact index width for a table of `count` entries.
pub(crate) fn index_width(count: usize) -> usize {
if count <= u8::MAX as usize {
1
} else if count <= u16::MAX as usize {
2
} else {
4
}
}
/// Width of the type index stored in interface-offset records. An absent
/// interface table keeps the full-width form.
pub(crate) fn type_index_width(interface_size: usize, interface_count: usize) -> Option<usize> {
if interface_count == 0 {
return Some(4);
}
interface_size
.checked_div(interface_count)?
.checked_sub(4)
.filter(|width| matches!(width, 1 | 2 | 4))
}
impl Records {
/// Derive the record layout for one set of compact index widths.
///
/// The constants are the fixed portions of the observed records; the
/// compact index fields follow them in the order used by the v39
/// parser.
pub(crate) fn from_widths(widths: Widths) -> Option<Self> {
let method_stride = 20_usize
.checked_add(widths.type_definition)?
.checked_add(widths.type_index)?
.checked_add(widths.parameter)?
.checked_add(widths.generic_container)?;
let type_stride = 68_usize
.checked_add(widths.type_index.checked_mul(3)?)?
.checked_add(widths.generic_container)?;
let image_stride = 32_usize.checked_add(widths.type_definition.checked_mul(2)?)?;
let type_method_start_offset = 16_usize
.checked_add(widths.type_index.checked_mul(3)?)?
.checked_add(widths.generic_container)?;
let type_method_count_offset = type_method_start_offset.checked_add(7 * 4)?;
let method_token_offset = 4_usize
.checked_add(widths.type_definition)?
.checked_add(widths.type_index)?
.checked_add(4)?
.checked_add(widths.parameter)?
.checked_add(widths.generic_container)?;
Some(Self {
method_stride,
method_token_offset,
type_stride,
type_method_start_offset,
type_method_count_offset,
image_stride,
})
}
}
}
/// Raw metadata format version reported by the header.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum MetadataVersion {
V24,
V29,
V31,
V39,
Unknown(u32),
}
impl MetadataVersion {
/// Classify a raw header version. Unknown versions keep their numeric value.
#[must_use]
pub const fn from_raw(version: u32) -> Self {
match version {
24 => Self::V24,
29 => Self::V29,
31 => Self::V31,
39 => Self::V39,
other => Self::Unknown(other),
}
}
/// Numeric header value for this version.
#[must_use]
pub const fn raw(self) -> u32 {
match self {
Self::V24 => 24,
Self::V29 => 29,
Self::V31 => 31,
Self::V39 => 39,
Self::Unknown(version) => version,
}
}
/// How much the raw version alone establishes about the record layout.
#[must_use]
pub const fn layout_status(self) -> LayoutStatus {
match self {
Self::V24 => LayoutStatus::ContextRequired,
Self::V29 | Self::V31 | Self::V39 => LayoutStatus::Known,
Self::Unknown(_) => LayoutStatus::Unverified,
}
}
/// Restoration entry points implemented for this version.
#[must_use]
pub const fn capabilities(self) -> Capabilities {
match self {
Self::V24 => Capabilities {
seeded_method_tokens: false,
structural_method_tokens: false,
method_index_restore: true,
method_index_requires_context: true,
},
Self::V29 => Capabilities {
seeded_method_tokens: true,
structural_method_tokens: false,
method_index_restore: false,
method_index_requires_context: false,
},
Self::V31 => Capabilities {
seeded_method_tokens: true,
structural_method_tokens: true,
method_index_restore: false,
method_index_requires_context: false,
},
Self::V39 => Capabilities {
seeded_method_tokens: true,
structural_method_tokens: false,
method_index_restore: false,
method_index_requires_context: false,
},
Self::Unknown(_) => Capabilities::NONE,
}
}
}
/// How much the raw version alone establishes about the record layout.
///
/// This describes the layout definition and the restoration entry points for
/// a version — not whether any particular input validated. Identification
/// reads only the 8-byte header.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum LayoutStatus {
/// A known layout definition exists for this raw version. The input body
/// has not been validated; the transform still checks its tables.
Known,
/// Version 24 records are applied only after context validation; the
/// version tag does not establish the v24.1 layout.
ContextRequired,
/// No record layout is asserted for this version.
Unverified,
}
/// Restoration entry points implemented for an identified version.
///
/// A capability means the crate has an entry point that can attempt the
/// transform. It does not mean the input is protected, that the input uses
/// that transform, or that the attempt will validate and produce output.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct Capabilities {
/// Seeded five-round `Il2CppMethodDefinition.token` restoration.
pub seeded_method_tokens: bool,
/// Structural per-image method-token remap.
pub structural_method_tokens: bool,
/// v24.1 `methodIndex` permutation restoration.
pub method_index_restore: bool,
/// When [`Self::method_index_restore`] is set, the per-application decoded
/// module 0x0C must be supplied and validated before the layout is used.
pub method_index_requires_context: bool,
}
impl Capabilities {
/// No restoration entry point is implemented for the version.
pub const NONE: Self = Self {
seeded_method_tokens: false,
structural_method_tokens: false,
method_index_restore: false,
method_index_requires_context: false,
};
}
/// Raw version and layout/capability facts for one metadata header.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct MetadataIdentity {
/// Numeric header version, including versions with no known layout.
pub raw_version: u32,
pub version: MetadataVersion,
pub layout: LayoutStatus,
pub capabilities: Capabilities,
}
impl MetadataIdentity {
/// Build the identity for a raw version.
#[must_use]
pub const fn new(version: MetadataVersion) -> Self {
Self {
raw_version: version.raw(),
version,
layout: version.layout_status(),
capabilities: version.capabilities(),
}
}
}
/// Identify a raw il2cpp metadata header.
///
/// Returns `None` when the magic or the complete 8-byte header is absent. The
/// check deliberately reads only the header: a body truncated after the header
/// still identifies, and every restore entry point validates the tables it
/// needs. This reports a layout/magic match, not a protection or obfuscation
/// verdict.
#[must_use]
pub fn identify(data: &[u8]) -> Option<MetadataIdentity> {
let magic: [u8; 4] = data.get(HEADER.magic..HEADER.magic + 4)?.try_into().ok()?;
if u32::from_le_bytes(magic) != MAGIC {
return None;
}
let version: [u8; 4] = data
.get(HEADER.version..HEADER.version + 4)?
.try_into()
.ok()?;
Some(MetadataIdentity::new(MetadataVersion::from_raw(
u32::from_le_bytes(version),
)))
}
/// Record layout used by the seeded and structural token transforms.
pub(crate) fn fixed_layout(version: u32) -> Option<FixedLayout> {
match MetadataVersion::from_raw(version) {
MetadataVersion::V29 => Some(V29),
MetadataVersion::V31 => Some(V31),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn header(version: u32) -> Vec<u8> {
let mut data = MAGIC.to_le_bytes().to_vec();
data.extend_from_slice(&version.to_le_bytes());
data
}
#[test]
fn identifies_each_known_raw_version() {
for (raw, version) in [
(24, MetadataVersion::V24),
(29, MetadataVersion::V29),
(31, MetadataVersion::V31),
(39, MetadataVersion::V39),
] {
let identity = identify(&header(raw)).expect("metadata header");
assert_eq!(identity.version, version);
assert_eq!(identity.version.raw(), raw);
}
}
#[test]
fn v24_is_context_required_and_not_claimed_as_v24_1() {
let identity = identify(&header(24)).expect("metadata header");
assert_eq!(identity.layout, LayoutStatus::ContextRequired);
assert!(identity.capabilities.method_index_restore);
assert!(identity.capabilities.method_index_requires_context);
assert!(!identity.capabilities.seeded_method_tokens);
assert!(!identity.capabilities.structural_method_tokens);
}
#[test]
fn known_versions_expose_their_entry_points() {
let v29 = identify(&header(29)).expect("metadata header");
assert_eq!(v29.layout, LayoutStatus::Known);
assert!(v29.capabilities.seeded_method_tokens);
assert!(!v29.capabilities.structural_method_tokens);
let v31 = identify(&header(31)).expect("metadata header");
assert_eq!(v31.layout, LayoutStatus::Known);
assert!(v31.capabilities.seeded_method_tokens);
assert!(v31.capabilities.structural_method_tokens);
let v39 = identify(&header(39)).expect("metadata header");
assert_eq!(v39.layout, LayoutStatus::Known);
assert!(v39.capabilities.seeded_method_tokens);
assert!(!v39.capabilities.structural_method_tokens);
}
#[test]
fn unknown_versions_assert_no_layout_or_capability() {
let identity = identify(&header(30)).expect("metadata header");
assert_eq!(identity.version, MetadataVersion::Unknown(30));
assert_eq!(identity.layout, LayoutStatus::Unverified);
assert_eq!(identity.capabilities, Capabilities::NONE);
}
#[test]
fn rejects_non_metadata_and_incomplete_headers() {
assert!(identify(b"").is_none());
assert!(identify(b"not metadata at all....").is_none());
// Magic only, and magic plus a partial version, are not identified.
assert!(identify(&MAGIC.to_le_bytes()).is_none());
assert!(identify(&header(31)[..7]).is_none());
assert!(crate::common::is_metadata(&header(31)[..4]));
}
#[test]
fn known_layout_values_are_kept_explicit() {
assert_eq!(
(HEADER.methods, HEADER.types, HEADER.images),
(0x30, 0xA0, 0xA8)
);
assert_eq!(
(V29.method.stride, V29.method.token),
(0x20, 0x14),
"v29 method record"
);
assert_eq!(
(V31.method.stride, V31.method.token),
(0x24, 0x18),
"v31 method record"
);
assert_eq!(
(V24_1_METHOD_INDEX.stride, V24_1_METHOD_INDEX.index),
(0x34, 0x14),
"context-validated v24.1 method-index record"
);
assert_eq!(V29.types.stride, 0x58);
assert_eq!(V29.types.method_start, 0x24);
assert_eq!(V29.types.method_count, 0x40);
assert_eq!(V29.images.stride, 0x28);
assert_eq!(V29.images.type_start, 0x08);
assert_eq!(V29.images.type_count, 0x0C);
}
#[test]
fn v39_compact_layout_matches_observed_records() {
let records = v39::Records::from_widths(v39::Widths {
type_definition: 1,
type_index: 2,
parameter: 1,
generic_container: 1,
})
.expect("record layout");
assert_eq!(
(records.method_stride, records.method_token_offset),
(25, 13)
);
assert_eq!(records.type_stride, 75);
assert_eq!(
(
records.type_method_start_offset,
records.type_method_count_offset
),
(23, 51)
);
assert_eq!(records.image_stride, 34);
}
#[test]
fn v39_index_width_tracks_table_counts() {
assert_eq!(v39::index_width(0), 1);
assert_eq!(v39::index_width(u8::MAX as usize), 1);
assert_eq!(v39::index_width(u8::MAX as usize + 1), 2);
assert_eq!(v39::index_width(u16::MAX as usize), 2);
assert_eq!(v39::index_width(u16::MAX as usize + 1), 4);
assert_eq!(v39::type_index_width(0, 0), Some(4));
assert_eq!(v39::type_index_width(6, 1), Some(2));
}
#[test]
fn a_known_layout_does_not_mean_the_input_validated() {
// The header alone identifies as known-layout v31 and advertises the
// structural transform, but the table bounds are missing, so the
// transform still rejects the input. `Known` is a layout definition,
// not an input-validation result.
let data = header(31);
let identity = identify(&data).expect("metadata header");
assert_eq!(identity.layout, LayoutStatus::Known);
assert!(identity.capabilities.structural_method_tokens);
assert_eq!(
crate::deobfuscate(&data).unwrap_err(),
crate::Error::Malformed
);
}
#[test]
fn identification_needs_only_the_eight_byte_header() {
// A header with no tables still identifies; every transform then
// reports its own structural error instead of a version error.
for version in [24, 29, 31, 39] {
assert!(identify(&header(version)).is_some(), "v{version} header");
}
}
#[test]
fn unknown_version_is_not_reported_as_unsupported_by_identification() {
// Identification is descriptive: an unknown version is carried through
// as `Unknown(n)` with no capabilities, rather than collapsing into a
// restore error or a layout claim.
let identity = identify(&header(27)).expect("metadata header");
assert_eq!(identity.version, MetadataVersion::Unknown(27));
assert_eq!(identity.raw_version, 27);
assert_eq!(identity.layout, LayoutStatus::Unverified);
assert_eq!(identity.capabilities, Capabilities::NONE);
assert_eq!(
crate::deobfuscate(&header(27)).unwrap_err(),
crate::Error::UnsupportedVersion(27)
);
}
#[test]
fn truncated_table_body_still_preserves_unsupported_input_behavior() {
// Build a real v31 header whose method table claims bytes that the
// truncated input does not contain. Identification succeeds; the
// structural transform rejects it as malformed, as before.
let mut data = header(31);
data.resize(0x100, 0);
data[HEADER.methods..HEADER.methods + 4].copy_from_slice(&0x200_u32.to_le_bytes());
data[HEADER.methods + 4..HEADER.methods + 8].copy_from_slice(&0x24_u32.to_le_bytes());
let identity = identify(&data).expect("metadata header");
assert_eq!(identity.version, MetadataVersion::V31);
assert!(identity.capabilities.structural_method_tokens);
assert_eq!(
crate::deobfuscate(&data).unwrap_err(),
crate::Error::Malformed
);
}
#[test]
fn serde_shape_is_stable_for_reporting() {
let identity = identify(&header(24)).expect("metadata header");
let json = serde_json::to_value(identity).expect("serialize");
assert_eq!(json["raw_version"], 24);
assert_eq!(json["version"], "v24");
assert_eq!(json["layout"], "context_required");
assert_eq!(json["capabilities"]["method_index_restore"], true);
assert_eq!(json["capabilities"]["method_index_requires_context"], true);
assert_eq!(json["capabilities"]["seeded_method_tokens"], false);
// A known layout definition serializes as "known", never "verified":
// identification does not validate the input body.
let known = identify(&header(31)).expect("metadata header");
let json = serde_json::to_value(known).expect("serialize");
assert_eq!(json["layout"], "known");
assert_eq!(json["raw_version"], 31);
let unknown = identify(&header(30)).expect("metadata header");
let json = serde_json::to_value(unknown).expect("serialize");
assert_eq!(json["version"]["unknown"], 30);
assert_eq!(json["layout"], "unverified");
}
#[test]
fn truncated_metadata_keeps_the_old_unsupported_or_malformed_behavior() {
let v24 = header(24);
assert!(matches!(
crate::android::restore_method_indices_v24_1(&v24, &[]),
Err(crate::android::MethodIndexError::Malformed(_))
));
let v29 = header(29);
assert!(matches!(
crate::android::restore_method_tokens(&v29, 0),
Err(crate::android::Error::Malformed(_))
));
let v31 = header(31);
assert!(matches!(
crate::android::restore_method_tokens(&v31, 0),
Err(crate::android::Error::Malformed(_))
));
let v39 = header(39);
assert!(matches!(
crate::android::discover_method_token_seeds(&v39),
Err(crate::android::Error::Malformed(_))
));
}
}
+2
View File
@@ -2,8 +2,10 @@
pub mod android;
mod common;
mod layout;
mod structural;
pub mod windows;
pub use common::is_metadata;
pub use layout::{Capabilities, LayoutStatus, MetadataIdentity, MetadataVersion, identify};
pub use structural::*;
+17 -19
View File
@@ -27,29 +27,32 @@
//! metadata (its tokens already equal `local_index + 1`), so it is safe to run on
//! any il2cpp game — `remapped == 0` then reports that nothing changed.
pub use crate::common::is_metadata;
use crate::common::MAGIC;
use crate::layout::{HEADER, MetadataVersion, V31};
/// Metadata format version this de-obfuscator understands. The struct strides
/// and header field offsets below are specific to it; other versions are left
/// untouched rather than risk corrupting a layout we have not verified.
/// (Observed on real games shipping version 31 / Unity 2022.3.)
const SUPPORTED_VERSION: u32 = 31;
const SUPPORTED_VERSION: u32 = MetadataVersion::V31.raw();
// --- Il2CppGlobalMetadataHeader field byte-offsets (each is an i32 offset/size
// pair). Shared layout across recent versions. ---
const HDR_METHODS: usize = 0x30; // methodsOffset / methodsSize
const HDR_TYPES: usize = 0xA0; // typeDefinitionsOffset / size
const HDR_IMAGES: usize = 0xA8; // imagesOffset / size
// pair). ---
const HDR_METHODS: usize = HEADER.methods; // methodsOffset / methodsSize
const HDR_TYPES: usize = HEADER.types; // typeDefinitionsOffset / size
const HDR_IMAGES: usize = HEADER.images; // imagesOffset / size
// --- version-31 struct strides and field offsets ---
const METHOD_STRIDE: usize = 0x24; // sizeof(Il2CppMethodDefinition)
const METHOD_TOKEN_OFF: usize = 0x18; // .token (u32)
const TYPE_STRIDE: usize = 0x58; // sizeof(Il2CppTypeDefinition)
const TYPE_METHOD_START_OFF: usize = 0x24; // .methodStart (i32)
const TYPE_METHOD_COUNT_OFF: usize = 0x40; // .method_count (u16)
const IMAGE_STRIDE: usize = 0x28; // sizeof(Il2CppImageDefinition)
const IMAGE_TYPE_START_OFF: usize = 0x08; // .typeStart (i32)
const IMAGE_TYPE_COUNT_OFF: usize = 0x0C; // .typeCount (u32)
// --- verified version-31 struct strides and field offsets ---
const METHOD_STRIDE: usize = V31.method.stride;
const METHOD_TOKEN_OFF: usize = V31.method.token;
const TYPE_STRIDE: usize = V31.types.stride;
const TYPE_METHOD_START_OFF: usize = V31.types.method_start;
const TYPE_METHOD_COUNT_OFF: usize = V31.types.method_count;
const IMAGE_STRIDE: usize = V31.images.stride;
const IMAGE_TYPE_START_OFF: usize = V31.images.type_start;
const IMAGE_TYPE_COUNT_OFF: usize = V31.images.type_count;
/// `IMAGE_CODE_GEN_MODULE` method-definition token table id (`0x06 << 24`).
const METHOD_TOKEN_TABLE: u32 = 0x0600_0000;
@@ -93,11 +96,6 @@ impl std::fmt::Display for Error {
}
impl std::error::Error for Error {}
/// Cheap check for the il2cpp metadata sanity magic, for scanning prefixes.
pub fn is_metadata(data: &[u8]) -> bool {
rd_u32(data, 0) == Some(MAGIC)
}
/// De-obfuscate the method tokens in an il2cpp `global-metadata.dat`.
///
/// On success returns the (possibly-rewritten) file bytes and a [`Report`]. The