|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from unblob.file_utils import File, FileSystem, InvalidInputFormat, iterate_patterns |
| 4 | +from unblob.models import ( |
| 5 | + Extractor, |
| 6 | + ExtractResult, |
| 7 | + HandlerDoc, |
| 8 | + HandlerType, |
| 9 | + HexString, |
| 10 | + Reference, |
| 11 | + StructHandler, |
| 12 | + ValidChunk, |
| 13 | +) |
| 14 | + |
| 15 | +# IM4P (Image4 Payload) DER structure inside an IMG4 container: |
| 16 | +# SEQUENCE |
| 17 | +# IA5String "IM4P" (image type tag) |
| 18 | +# IA5String <name> (component name, e.g. "illb") |
| 19 | +# OCTET STRING <payload> (compressed or raw binary) |
| 20 | +_IM4P_MAGIC = b"IM4P" |
| 21 | +_IA5STRING_TAG = 0x16 |
| 22 | +_OCTET_STRING_TAG = 0x04 |
| 23 | + |
| 24 | + |
| 25 | +def _read_der_length(file: File, offset: int) -> tuple[int, int]: |
| 26 | + """Return (value, header_len) for the DER length field starting at offset.""" |
| 27 | + file.seek(offset) |
| 28 | + first = file.read(1)[0] |
| 29 | + if first < 0x80: # short form: the byte itself is the length |
| 30 | + return first, 1 |
| 31 | + num_bytes = first & 0x7F # long form: low 7 bits count the length bytes |
| 32 | + return int.from_bytes(file.read(num_bytes), "big"), 1 + num_bytes |
| 33 | + |
| 34 | + |
| 35 | +class IM4PExtractor(Extractor): |
| 36 | + def extract(self, inpath: Path, outdir: Path) -> ExtractResult: |
| 37 | + fs = FileSystem(outdir) |
| 38 | + |
| 39 | + with File.from_path(inpath) as file: |
| 40 | + for pos in iterate_patterns(file, _IM4P_MAGIC): |
| 41 | + # pos is the start of the "IM4P" IA5String content (4 bytes). |
| 42 | + p = pos + len(_IM4P_MAGIC) |
| 43 | + |
| 44 | + # component name IA5String: tag, length, name bytes |
| 45 | + file.seek(p) |
| 46 | + if file.read(1)[0] != _IA5STRING_TAG: |
| 47 | + continue |
| 48 | + p += 2 + file.read(1)[0] |
| 49 | + |
| 50 | + # payload OCTET STRING: tag, DER length, payload bytes |
| 51 | + file.seek(p) |
| 52 | + if file.read(1)[0] != _OCTET_STRING_TAG: |
| 53 | + continue |
| 54 | + size, header_len = _read_der_length(file, p + 1) |
| 55 | + payload_start = p + 1 + header_len |
| 56 | + |
| 57 | + available = file.size() - payload_start |
| 58 | + fs.carve( |
| 59 | + Path(f"{inpath.stem}.bin"), |
| 60 | + file, |
| 61 | + payload_start, |
| 62 | + min(size, available), |
| 63 | + ) |
| 64 | + break |
| 65 | + |
| 66 | + return ExtractResult(reports=fs.problems) |
| 67 | + |
| 68 | + |
| 69 | +class IMG4Handler(StructHandler): |
| 70 | + NAME = "img4" |
| 71 | + |
| 72 | + PATTERNS = [ |
| 73 | + # 30 — DER SEQUENCE tag |
| 74 | + # 82 — long-form length: next 2 bytes encode the length |
| 75 | + # ?? ?? — 2-byte big-endian container length (variable) |
| 76 | + # 16 04 — IA5String tag + length 4 |
| 77 | + # 49 4D 34 50 — "IM4P" (image type identifier) |
| 78 | + HexString("30 82 ?? ?? 16 04 49 4D 34 50"), |
| 79 | + # 30 — DER SEQUENCE tag |
| 80 | + # 83 — long-form length: next 3 bytes encode the length |
| 81 | + # ?? ?? ?? — 3-byte big-endian container length (variable) |
| 82 | + # 16 04 — IA5String tag + length 4 |
| 83 | + # 49 4D 34 50 — "IM4P" (image type identifier) |
| 84 | + HexString("30 83 ?? ?? ?? 16 04 49 4D 34 50"), |
| 85 | + ] |
| 86 | + |
| 87 | + # DER header covering the worst case (3-byte length form) |
| 88 | + C_DEFINITIONS = r""" |
| 89 | + typedef struct img4_header { |
| 90 | + uint8 tag; // 0x30 (SEQUENCE) |
| 91 | + uint8 length_type; // 0x82 or 0x83 |
| 92 | + uint8 b2; // high byte of length (or high byte for 0x83) |
| 93 | + uint8 b3; // low byte of length (or middle byte for 0x83) |
| 94 | + uint8 b4; // only valid for 0x83 |
| 95 | + } img4_header_t; |
| 96 | + """ |
| 97 | + HEADER_STRUCT = "img4_header_t" |
| 98 | + |
| 99 | + EXTRACTOR = IM4PExtractor() |
| 100 | + |
| 101 | + DOC = HandlerDoc( |
| 102 | + name="Apple IMG4/IM4P", |
| 103 | + description="IMG4 is Apple's DER-encoded firmware image container used for signed payloads in the iOS and macOS secure boot chain. An IM4P (Image4 Payload) embeds a compressed or raw binary (typically LZFSE or LZSS) together with metadata used for cryptographic verification.", |
| 104 | + handler_type=HandlerType.ARCHIVE, |
| 105 | + vendor="Apple", |
| 106 | + references=[ |
| 107 | + Reference( |
| 108 | + title="libimg4 - Apple open-source IMG4 implementation", |
| 109 | + url="https://github.com/apple-oss-distributions/libimg4", |
| 110 | + ), |
| 111 | + ], |
| 112 | + limitations=[ |
| 113 | + "Only IM4P payload extraction is supported; full IMG4 manifests are not verified" |
| 114 | + ], |
| 115 | + ) |
| 116 | + |
| 117 | + def calculate_chunk(self, file: File, start_offset: int) -> ValidChunk | None: |
| 118 | + header = self.parse_header(file) |
| 119 | + |
| 120 | + if header.length_type == 0x82: |
| 121 | + length = int.from_bytes([header.b2, header.b3], "big") |
| 122 | + total_size = 4 + length |
| 123 | + elif header.length_type == 0x83: |
| 124 | + length = int.from_bytes([header.b2, header.b3, header.b4], "big") |
| 125 | + total_size = 5 + length |
| 126 | + else: |
| 127 | + raise InvalidInputFormat( |
| 128 | + f"IMG4: unexpected DER length type {header.length_type:#x}" |
| 129 | + ) |
| 130 | + |
| 131 | + return ValidChunk( |
| 132 | + start_offset=start_offset, end_offset=start_offset + total_size |
| 133 | + ) |
0 commit comments