Skip to content

Instantly share code, notes, and snippets.

@morkev
Last active May 1, 2025 19:38
Show Gist options
  • Save morkev/154dbdb16aeefaef8296eac5f9c49630 to your computer and use it in GitHub Desktop.
Save morkev/154dbdb16aeefaef8296eac5f9c49630 to your computer and use it in GitHub Desktop.
cIMG extraction
import struct
import subprocess
def read_header(f):
"""Read and parse the cIMG header"""
magic = f.read(4)
if magic != b"cIMG":
raise ValueError("Invalid magic number")
version, width, height, num_directives = struct.unpack("<HBBI", f.read(8))
return version, width, height, num_directives
def process_directives(f, width, height, num_directives):
"""Process all directives in the file and return final frame"""
canvas = [[32] * width for _ in range(height)]
for _ in range(num_directives):
directive_type = struct.unpack("<H", f.read(2))[0]
if directive_type == 2:
x, y, w, h, r, g, b, char = struct.unpack("<BBBBBBBB", f.read(8))
canvas[y][x] = char
elif directive_type == 6:
f.read(1)
continue
elif directive_type == 7:
f.read(4)
continue
else:
raise ValueError(f"Unknown directive type: {directive_type}")
return '\n'.join(''.join(chr(c) for c in row).rstrip() for row in canvas)
def extract_flag(filename):
"""Extract flag from animated cIMG file"""
subprocess.run(["/challenge/generate_flag_image"])
with open(filename, "rb") as f:
version, width, height, num_directives = read_header(f)
final_frame = process_directives(f, width, height, num_directives)
print(final_frame)
if __name__ == "__main__":
extract_flag("/challenge/flag.cimg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment