SHARE
TWEET

GCHQ Challenge - PNG Inspector

a guest Feb 5th, 2012 526 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Dr Gareth Owen, University of Greenwich
  2. # PNG Image parser and inspector for stenographic content
  3.  
  4. import sys
  5. import zlib
  6. import struct
  7. import collections
  8. #import file
  9.  
  10. idat = ''
  11. width = 0
  12. height = 0
  13. Header = collections.namedtuple('Header', 'width height bpp coltype comptype filtype intype')
  14. hdr = ''
  15.  
  16. def readChunk(f):
  17.   global idat, hdr
  18.   (len,) = struct.unpack('>L', f.read(4))
  19.   type = f.read(4)
  20.  
  21.   print "Len %d Type %s" % (len, type)
  22.  
  23.   data = f.read(len)
  24.  
  25.   if type == "IHDR":
  26.  
  27.     hdr = Header._make(struct.unpack('>LLBBBBB', data))
  28.     print hdr
  29.   elif type == "iTXt":
  30.     print data
  31.   elif type == "tIME":
  32.     Tm = collections.namedtuple('Tm', 'year month day hour minute second')
  33.     print Tm._make(struct.unpack('>HBBBBB', data))
  34.   elif type == "IDAT":
  35.     idat += data
  36.  
  37.   crc = f.read(4)
  38.  
  39.   return type
  40.  
  41. inf = open('cyber.png', 'r')
  42. sig = inf.read(8)
  43.  
  44. while readChunk(inf) != 'IEND':
  45.   pass
  46.  
  47. unpacked = zlib.decompress(idat)
  48. print "Decom len %d " % (len(unpacked))
  49.  
  50. # has extra byte at beginning of each scan line indicating encoding type
  51. o = open('cyber.raw', 'w')
  52. o.write(unpacked)
  53. o.close()
  54.  
  55. # check Least significant bits for stenographic content
  56. #for y in range(0, hdr.height):
  57. #  for x in range(1, hdr.width):
  58. #    print ord(unpacked[y*hdr.width + x]) & 0x01,
RAW Paste Data
Top