cscg24-for3

CSCG 2024 Challenge 'Intro Forensics 3'
git clone https://git.sinitax.com/sinitax/cscg24-for3
Log | Files | Refs | sfeed.txt

solve (737B)


      1#!/usr/bin/env python3
      2
      3import binascii
      4
      5header = b"\x89\x50\x4e\x47\x0D\x0A\x1A\x0A"
      6data = open("./intro-forensics-3", "rb").read()
      7assert(data[:len(header)] == header)
      8
      9pos = len(header)
     10chunks = {}
     11while pos < len(data):
     12    start = pos
     13    clen = int.from_bytes(data[pos:pos+4], "big")
     14    pos += 4
     15    ctype = data[pos:pos+4]
     16    pos += 4
     17    cdata = data[pos:pos+clen]
     18    pos += clen
     19    index = int.from_bytes(data[pos:pos+4], "big")
     20    pos += 4
     21    new_crc = int.to_bytes(binascii.crc32(data[start+4:start+8] + cdata), 4, "big")
     22    chunks[index] = data[start:start+8] + cdata + new_crc
     23
     24chunks = sorted(chunks.items(), key = lambda x : x[0])
     25data = header + b"".join([v for k,v in chunks])
     26open("out.png", "wb+").write(data)