cybersecurity · intermediate · ~15 min · safe pentest lab

Identify firmware container

Branch on multiple magic numbers.

Challenge

Identify a firmware container by branching on its magic bytes — the first step of any firmware-unpacking pipeline.

Task

Implement const char *firmware_kind(const unsigned char *b) that returns a string naming the container type.

Input

  • b: a byte buffer of at least 4 bytes. The grader passes a fixed buffer.

Output

Returns const char *:

  • "gzip" if the first two bytes are 0x1F 0x8B;
  • "uimage" if the first four bytes are the U-Boot magic 0x27 0x05 0x19 0x56 (big-endian);
  • "unknown" otherwise.

Example

b = 0x27 0x05 0x19 0x56   ->   "uimage"
b = 0x1F 0x8B 0x00 0x00   ->   "gzip"
b = 0x00 0x01 0x02 0x03   ->   "unknown"

Edge cases

  • Check gzip (2 bytes) and U-Boot (4 bytes) in either order; default to "unknown".

Input format

A byte buffer b of at least 4 bytes.

Output format

A const char*: "gzip", "uimage", or "unknown".

Constraints

gzip = 1F 8B; uimage = 27 05 19 56 (big-endian); else unknown.

Starter code

const char *firmware_kind(const unsigned char *b) {
    /* TODO */
    return "unknown";
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.