Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update png fixer script for python3 compatibility #733

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions plaidctf-2015/forensics/png-uncorrupt/fixpng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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])
Expand Down