Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:IMG4

From ReverseEngineering.wiki
Format
Vendor Apple
Platforms iOS, macOS (Apple silicon), tvOS, watchOS
Magic DER SEQUENCE containing IA5String "IMG4" / "IM4P"
Extensions .img4, .im4p, .im4m, .im4r
On this wiki Facts · Code · Notes · Topics

Apple's DER (ASN.1) firmware container: an IMG4 sequence holds an IM4P payload (4-character type, version, optionally compressed and encrypted data), an optional IM4M manifest (the signed ticket) and an optional IM4R restore-info; found as .img4, .im4p, .im4m in IPSW files and OTA packages.

Overview

IMG4 files are ASN.1 DER, so there is no fixed-offset magic: the file starts with a SEQUENCE header (30 8x ...) and the first element is an IA5String carrying the tag text. Recognize them by the ASCII IMG4 (full image), IM4P (payload only), IM4M (manifest only) or IM4R near the start of the file. Everything is big-endian DER; no fixed header offsets exist.

Structure as modeled by ipsw's pkg/img4 (blacktop/ipsw, MIT):

IMG4 ::= SEQUENCE {
  tag     IA5String ("IMG4"),
  payload IM4P  OPTIONAL,
  manifest  [0] EXPLICIT IM4M OPTIONAL,
  restoreInfo [1] EXPLICIT IM4R OPTIONAL }
IM4P ::= SEQUENCE {
  tag     IA5String ("IM4P"),
  type    IA5String,      -- four-character component type
  version IA5String,
  data    OCTET STRING,
  compression SEQUENCE { algorithm INTEGER, uncompressedSize INTEGER } OPTIONAL,
  keybag  OCTET STRING OPTIONAL,
  properties [0] EXPLICIT PAYP OPTIONAL,
  hash    OCTET STRING OPTIONAL }

The Go types are IMG4 (img4.go lines 159-165) and IM4P (payload.go lines 200-210). The compression algorithm enumeration in ipsw is LZSS = 0 and LZFSE = 1; ipsw also detects compression from the data header when the ASN.1 compression record is absent (it tests for LZSS and LZFSE signatures first).

Parts

  • IM4P: the payload, for example a kernelcache (Format:Kernelcache), iBoot, device tree or firmware blob. The 4-character type is the component name. The payload is often LZSS- or LZFSE-compressed and, for encrypted components, comes with a keybag (a list of entries with a type, an IV and a key). This wiki does not record keys; recognizing that a keybag is present only tells you that the data is encrypted.
  • IM4M: the manifest (the signed boot ticket) with a certificate chain; it ties the payload hash to one device and boot nonce. Unverified: field details are not described here; read ipsw pkg/img4/manifest.go.
  • IM4R: restore info; ipsw creates it with the boot nonce when it personalizes an image.

Reversing notes

  • To get a Mach-O out of an iOS firmware component: parse the DER, take the data field, decompress it according to the compression record, and only then look for CF FA ED FE.
  • ipsw's validator warns when the IM4P version is missing, so a hand-built IM4P should carry type and version strings.
  • Not every Apple firmware file is IMG4: IPSW contents also use plain Mach-O, DMG and Apple Encrypted Archive wrappers.

Code & repositories