From 286296f3e54a57f1d6e066cb7e2729998857bf42 Mon Sep 17 00:00:00 2001 From: Onur Gunduz Date: Wed, 19 Aug 2020 01:36:58 -0400 Subject: [PATCH] Update png fixer script for python3 compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed print statements and used encode(‘ascii’) for ASCII encoding to avoid relying on no longer implicit conversion between unicode (str) type and bytes objects (or decode to get a string) which inhibits use of unicode str objects as arguments in functions that demand byte type or concatenation in such cases --- .../forensics/png-uncorrupt/fixpng.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/plaidctf-2015/forensics/png-uncorrupt/fixpng.py b/plaidctf-2015/forensics/png-uncorrupt/fixpng.py index 869f0a1b..76e90b66 100644 --- a/plaidctf-2015/forensics/png-uncorrupt/fixpng.py +++ b/plaidctf-2015/forensics/png-uncorrupt/fixpng.py @@ -5,10 +5,10 @@ def fbytes(gfile): def findA(blob): a = [] - ptr = blob.find('\x0a') + ptr = blob.find('\x0a'.encode('ascii')) while ptr!=-1: a.append(ptr) - ptr = blob.find('\x0a', ptr+1) + ptr = blob.find('\x0a'.encode('ascii'), ptr+1) return a def get_missing(blob, offsets): @@ -24,7 +24,7 @@ def get_crcs(blob, offsets): crcs = [] for i in range(0,len(offsets)-1): crcs.append(binascii.hexlify(blob[offsets[i+1]-8:offsets[i+1]-4])) - i = blob.find('IEND', offsets[len(offsets)-1]) + i = blob.find('IEND'.encode('ascii'), offsets[len(offsets)-1]) crcs.append(binascii.hexlify(blob[i-8:i-4])) return crcs @@ -37,9 +37,9 @@ def get_crcs(blob, offsets): # Get bytes from input file blob = fbytes(args.fpng) # Fix first 0d0a -blob = blob[:4] + '\x0d' + blob[4:] +blob = blob[:4] + '\x0d'.encode('ascii') + blob[4:] # Get all IDAT offsets to fix -offsets = [m.start() for m in re.finditer('IDAT', blob)] +offsets = [m.start() for m in re.finditer('IDAT'.encode('ascii'), blob)] # Get all missing \x0a missing = get_missing(blob, offsets) # Get all 32bit-CRCs @@ -49,18 +49,18 @@ def get_crcs(blob, offsets): for i in range(0,len(offsets)): if i!=len(offsets)-1: end=offsets[i+1] - else: end = blob.find('IEND', offsets[len(offsets)-1]) + else: end = blob.find('IEND'.encode('ascii'), offsets[len(offsets)-1]) saveblob = blob[offsets[i]:end-8] - print binascii.hexlify(blob[end-8:end-4]) + print(binascii.hexlify(blob[end-8:end-4])) for c in itertools.combinations(findA(saveblob),abs(missing[i])): tmpblob = saveblob if missing[i]==0: crc = binascii.crc32(tmpblob) & 0xffffffff if (crc & 0xffffffff) != int(crcs[i],16): - print 'No missing bytes. CRC not the same, should be ' + str(hex(crc)[2:]) + print('No missing bytes. CRC not the same, should be ' + str(hex(crc)[2:])) blob = blob[:end-8] + binascii.unhexlify(hex(crc)[2:]) + blob[end-4:] continue - for idx, p in enumerate(c): tmpblob = tmpblob[:p+idx] + '\x0d' + tmpblob[p+idx:] + for idx, p in enumerate(c): tmpblob = tmpblob[:p+idx] + '\x0d'.encode('ascii') + tmpblob[p+idx:] crc = binascii.crc32(tmpblob) & 0xffffffff if crc != int(crcs[i],16): continue for z in c: fix.append(z+offsets[i])