Created
April 28, 2019 00:13
-
-
Save ducnhse130201/aaee8689ff2fc23a2d9403ea9e405275 to your computer and use it in GitHub Desktop.
crypto2_solve_matess3r4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from telnetlib import * | |
import json | |
import hashlib | |
from Crypto.Util.number import * | |
from libnum import * | |
r = Telnet('125.235.240.166', 13337) | |
def get_key(): | |
data = json.loads(r.read_until("\n").strip()) | |
N, g, k = data['N'], data['g'], data['k'] | |
return N,g,k | |
def exchanged_key(A, usn): | |
data = '{"A":%s,"username":"%s"}' % (A, usn) | |
r.write(data + "\n") | |
def get_back(): | |
data = json.loads(r.read_until("\n").strip()) | |
salt, B = data['salt'], data['B'] | |
return salt,B | |
def check_token(token): | |
data = '{"token":"%s"}' % str(token) | |
r.write(data + "\n") | |
r.interact() | |
def hmac(salt, data, hex): | |
if hex: | |
return hashlib.sha512(salt + data).hexdigest() | |
return hashlib.sha512(salt + data).digest() | |
def create_key(salt,A,B,k,g,rand_num,N,password): | |
xH = hmac(str(salt), password, 0) | |
x = bytes_to_long(xH) | |
uH = hmac(str(A), str(B), 0) | |
u = bytes_to_long(uH) | |
S = pow(B - k * pow(g, x, N), rand_num + u * x, N) | |
K = hmac('', str(S), 0) | |
return hmac(K, str(salt), 1) | |
N,g,k = get_key() | |
exchanged_key(str(N**2), "peterjson") | |
salt,B = get_back() | |
K = hmac('', str(0), 0) | |
h = hmac(K, str(salt), 1) | |
print h | |
check_token(h) | |
# h = create_key(salt, A, B, k, g, 17, N, "hehe") | |
# check_token(h,str(s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment