Skip to content

Instantly share code, notes, and snippets.

@pawlos
Created February 1, 2020 16:13
Show Gist options
  • Select an option

  • Save pawlos/e34876102af6cdcff63705ca27ee2a79 to your computer and use it in GitHub Desktop.

Select an option

Save pawlos/e34876102af6cdcff63705ca27ee2a79 to your computer and use it in GitHub Desktop.
def encipher(num_rounds, v, key):
v0=v[0]
v1=v[1]
sum=0
delta=0x9E3779B9
for i in range(num_rounds):
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3])
sum += delta
v0 &= 0xFFFFFFFF
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3])
v1 &= 0xFFFFFFFF
v[0]=v0; v[1]=v1;
return (v[0],v[1])
def decipher(num_rounds, v, key):
v0=v[0]
v1=v[1]
delta=0x9E3779B9
sum=delta*num_rounds
for i in range(num_rounds):
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3])
v1 &= 0xFFFFFFFF
sum -= delta;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3])
v0 &= 0xFFFFFFFF
v[0]=v0
v[1]=v1
return (v0,v1)
import struct
import os
import sys
def probe(data, key):
#print(data)
w = decipher(32, data, key)
#print(hex(w[0]))
return struct.pack("<I",w[0]) == b'GIF8'
def decipherFile(file, key):
data = []
with open(file,'rb') as f:
b = os.path.getsize(file)
for i in range(b //8):
v1 = f.read(4)
v1 = struct.unpack("<I", v1)[0]
v2 = f.read(4)
v2 = struct.unpack("<I", v2)[0]
w = decipher(32, [v1,v2], key)
#print(w)
data.append(struct.pack("<I",w[0]))
#print(data[0])
#break
data.append(struct.pack("<I",w[1]))
#sys.stdout.write('.')
#sys.stdout.flush()
with open(file.replace('.Mugatu',''), 'wb') as f:
for b in data:
#print(data[0:1])
f.write(b)
import itertools
if __name__ == "__main__":
print("Start")
if probe([0x4ee2f5c8,0x6b97f416], [0x0,0x0,0x0,0x0]) == True:
print("Probe worked...")
else:
print("Probe didn't work")
sys.exit(-1)
print("Start search...")
i = 0
for (k1,k2,k3) in itertools.product(range(256), repeat=3):
#print(hex(0x31)+hex(k1)+hex(k2)+hex(k3))
if probe([0x50b08e24,0x6f68b2e8],[0x31,k1,k2,k3]) == True:
print("Found key:"+hex(0x31)+hex(k1)+hex(k2)+hex(k3))
decipherFile("best.gif.Mugatu", [0x31,k1,k2,k3])
break
i += 1
if i % 100000 == 0:
print("+")
#sys.stdout.write('.')
#sys.stdout.flush()
#w = encipher(32, [0x361c1f24,0x1e252819],[0x85,0xb8,0xaf,0x3])
#print(hex(w[0])+' - '+hex(w[1]))
print("Finished")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment