#python 2.7
import hashlib as H
import struct
from z3 import *
DEBUG=0
def u32(a):
return struct.unpack('<I',a)[0]
def p32(a):
return struct.pack('<I',a)
#print hex(ROR32(0x0000000021111112, 7))
def T1(t0):
a2=[0 for i in range(100)]
#t0%=0x64
t2=t0
for i in range(100):
t1=0
t2^=0xc3
a2[i]=t2 & 0xFF
for j in range(8):
t1^=((t2 >> j) & 1)
t2=t1 | 2*t2
return a2
def FT(input):
m=input[0]
X1=input[1]
X2=input[2]
for i in range(7,-1,-1):
m=RotateLeft(RotateRight(m,i)^0x9E3779B9,6)
for i in range(7,-1,-1):
m=RotateLeft(RotateRight(m,i)^0x9E3779B9,6)
X0=m
x4= (X2 & X1) ^ (X0 & X1) ^ (X0 & X2) ^ (X0 & X2 & X1) ^ (X0|X2|X1)
x5 = ~x4
#print hex(x5 & 0xFFFFFFFF)
return x5 & 0xFFFFFFFF
X=[0 for i in range(4)]
if DEBUG==0:
R=[0x1f7902cc,
0x2fae3d15,
0xceebfe91L,
0xaff6af42L
]
else:
R=[
0x3560d569,
0xe566c865L,
0x1a638fcc,
0x931e84cbL
]
input=[0,0,0]
for t0 in range(0x64):
if DEBUG==0:
md5flag='9036D8C5EA6281976CA9321969262204'
else:
md5flag='2d1dabc34507bb34cf00ca759fa28faf'
md5r1=md5flag.decode('hex')
RT=''.join(map(chr,T1(t0)))
md5r2=H.md5(RT).hexdigest()
md5r2=md5r2.decode('hex')
s=Solver()
for i in range(4):
X[i]=BitVec('X'+str(i),32)
for i in range(4):
input[0]=X[i]
input[1]=u32(md5r1[4*i:4*(i+1)])
input[2]=u32(md5r2[4*i:4*(i+1)])
s.add(FT(input)==R[i])
flag=''
if s.check()==sat:
m=s.model()
for i in range(4):
flag+=p32(int(str(m[X[i]])))
print flag.encode('hex')
if H.md5(flag).hexdigest().upper()==md5flag.upper():
print "found"
flag='flag{'+flag.encode('hex')[0:8]+'-'+flag.encode('hex')[8:12]+'-'+flag.encode('hex')[12:16]+'-'+flag.encode('hex')[16:20]+'-'+flag.encode('hex')[20:]+'}'
print flag
break
#flag{9e573902-0e31-4837-a337-32a475ca007c}