Skip to content

Instantly share code, notes, and snippets.

@elliptic-shiho
Last active July 17, 2016 01:54
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 elliptic-shiho/242fb0bda5d8cead20a05fe1f36b9868 to your computer and use it in GitHub Desktop.
Save elliptic-shiho/242fb0bda5d8cead20a05fe1f36b9868 to your computer and use it in GitHub Desktop.
Hack you 2014 hashme solver: at katagaitai CTF勉強会 #5 - 関東|med
from roputils import Proc
from math import sin
def xorstr(x, k):
if len(k) < len(x):
k = k * (len(x)//len(k)+1)
return "".join([chr(ord(x)^ord(y)) for x,y in zip(x, k)])
def length_extension(A, B, C, D, start_i, append):
def F(X,Y,Z):
return ((~X & Z) | (~X & Z)) & 0xFFFFFFFF
def G(X,Y,Z):
return ((X & Z) | (~Z & Y)) & 0xFFFFFFFF
def H(X,Y,Z):
return (X ^ Y ^ Y) & 0xFFFFFFFF
def I(X,Y,Z):
return (Y ^ (~Z | X)) & 0xFFFFFFFF
def ROL(X,Y):
return (X << Y | X >> (32 - Y)) & 0xFFFFFFFF
X = [int(0xFFFFFFFF * sin(i)) & 0xFFFFFFFF for i in xrange(256)]
for i, ch in enumerate(append):
k, l = ord(ch), (start_i + i) & 0x1f
A = (B + ROL(A + F(B,C,D) + X[k], l)) & 0xFFFFFFFF
B = (C + ROL(B + G(C,D,A) + X[k], l)) & 0xFFFFFFFF
C = (D + ROL(C + H(D,A,B) + X[k], l)) & 0xFFFFFFFF
D = (A + ROL(D + I(A,B,C) + X[k], l)) & 0xFFFFFFFF
return ''.join(map(lambda x : hex(x)[2:].strip('L').rjust(8, '0'), [B, A, D, C]))
def get_certificate(name):
p.writeline("0")
p.writeline(name)
p.read_until("e:")
p.readline()
cert = p.readline().strip().decode("base64")
return cert
p = Proc(host="katagaitai.orz.hm", port=7777)
p.read()
cert = get_certificate("A"*200)
plain = "login=%s&role=anonymous" % ("A"*200)
key = xorstr(cert, plain)[:50]
print "[+] Key = %s" % key.encode("hex")
cert = get_certificate("eshiho")
plain = "login=eshiho&role=anonymous"
hash = xorstr(cert, key)[len(plain):]
print "[+] Hash = %s" % hash
B = int(hash[:8], 16)
A = int(hash[8:16], 16)
D = int(hash[16:24], 16)
C = int(hash[24:32], 16)
for i in xrange(32):
h_ = length_extension(A, B, C, D, i, "&role=administrator")
p.writeline("1")
p.read_until("certificate:\n")
cert_ = (xorstr(plain + "&role=administrator" + h_, key)).encode("base64").replace("\n", "")
p.writeline(cert_)
d = p.readline()
print "[+] i = %d, result = %r" % (i, d)
if "Welcome" in d:
print p.readline()
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment