Skip to content

Instantly share code, notes, and snippets.

@J4ckKn1ght
Last active February 22, 2019 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save J4ckKn1ght/946e11dda4478b15129a1b8ee763b817 to your computer and use it in GitHub Desktop.
Save J4ckKn1ght/946e11dda4478b15129a1b8ee763b817 to your computer and use it in GitHub Desktop.
randomFile = open('randoms.bin', 'r')
randoms = randomFile.readlines()
randoms = [x.strip() for x in randoms]
randomFile.close()
initFile = open('init.bin', 'rb')
base = initFile.read()
base = [ord(x) for x in base]
initFile.close()
def pmulhuw(a, b):
result = []
for i in range(len(a)):
result.append((a[i] * b[i]) >> 16)
return result
def pmullw(a,b):
result = []
for i in range(len(a)):
result.append((a[i] * b[i]) & 0xffff)
return result
def punpcklwd(a, b):
result = []
for i in range(4):
result.append(a[i])
result.append(b[i])
return result
def punpckhwd(a, b):
result = []
for i in range(4, 8):
result.append(a[i])
result.append(b[i])
return result
def itox(x):
x = hex(x)[2:]
x = '0' * (len(x) % 2) + x
return x
def convertToDW(x):
result = []
for i in range(0, len(x), 2):
result.append(int(itox(x[i + 1]) + itox(x[i]), 16))
return result
def paddd(a, b):
result = []
for i in range(len(a)):
result.append(a[i] + b[i])
return result
def upgrade(x):
result = []
for i in range(0, len(x), 2):
result.append(int(itox(x[i + 1]) + itox(x[i]), 16))
return result
def calGuess(lowBase, highBase, b1, b2):
x1 = pmulhuw(lowBase, b1)
x2 = pmullw(lowBase, b1)
xmm4 = punpcklwd(x2, x1)
xmm0 = punpckhwd(x2, x1)
x3 = pmulhuw(highBase, b2)
x4 = pmullw(highBase, b2)
xmm3 = punpcklwd(x4,x3)
xmm1 = punpckhwd(x4, x3)
xmm0 = convertToDW(xmm0)
xmm1 = convertToDW(xmm1)
xmm3 = convertToDW(xmm3)
xmm4 = convertToDW(xmm4)
xmm0 = paddd(paddd(paddd(xmm0, xmm4), xmm3), xmm1)
xmm0 = paddd(xmm0, xmm0[2:] + [0]*2)
xmm0 = paddd(xmm0, xmm0[1:] + [0])
return xmm0[0]
resultFile = open('results.bin', 'w')
for random in randoms:
random1 = []
random2 = []
count = 0
for i in random.split():
n = int(i)
hexS = hex(n)[2:]
if len(hexS) < 4:
hexS = (4 - len(hexS)) * '0' + hexS
byte1 = hexS[len(hexS) - 2: len(hexS)]
byte2 = hexS[len(hexS) - 4: len(hexS) - 2]
if count < 16:
random1.append(int(byte1, 16))
random1.append(int(byte2, 16))
else:
random2.append(int(byte1, 16))
random2.append(int(byte2, 16))
count += 2
random1 = upgrade(random1)
random2 = upgrade(random2)
result = ''
for index in range(0, len(base), 16):
lowBase = base[index:index + 8]
highBase = base[index + 8 : index + 16]
result += str(calGuess(lowBase, highBase, random1, random2)) + " "
resultFile.write(result + '\n')
resultFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment