7 from itertools import chain, product
9 prompt1_template = "Please submit a printable string X, such that {}(X)[-6:] = {} and len(X) = {}"
10 vars_cd_template = " c = {}, d = {};"
11 vars_ab_template = " int a = {}, b = {};"
12 HASH = {"md5": md5, "sha1": sha1, "sha224": sha224, "sha256": sha256, "sha384": sha384, "sha512": sha512}
14 def pow(algo, hash, len):
15 candidates = product(string.printable, repeat=int(len))
16 for candidate in candidates:
17 if HASH[algo](''.join(candidate)).hexdigest()[-6:] == hash:
18 return ''.join(candidate)
20 def find_solution_strings(a, b, c, d):
21 solution_string = "++--"
22 for c_diff in range(-509, 509):
23 for d_diff in range(-509, 509):
24 if a*(c + c_diff) + b*(d + d_diff) == 1:
25 print("Solution found!")
26 solution_string_c = abs(c_diff)*"--" if c_diff < 0 else c_diff*"++"
27 solution_string_d = abs(d_diff)*"--" if d_diff < 0 else d_diff*"++"
28 return solution_string + solution_string_c, solution_string + solution_string_d
31 r = remote("76.74.177.238", "7777")
33 print("##### Step 1: Proof of work #####")
34 prompt1_request = r.recvline()
35 prompt1_data = parse(prompt1_template, prompt1_request)
36 print("Attempting PoW: {}(X) = {}, len(X) = {}".format(prompt1_data[0], prompt1_data[1], prompt1_data[2]))
37 correct_X = pow(prompt1_data[0], prompt1_data[1], prompt1_data[2])
38 print("Found PoW: {}".format(correct_X))
41 print("##### Step 2: Find equation solution #####")
42 r.recvline_endswith("[Q]uit!")
44 r.recvline_endswith("d = 0;")
45 vars_cd_line = r.recvline()
46 vars_ab_line = r.recvline()
47 vars_cd_data = parse(vars_cd_template, vars_cd_line)
48 vars_ab_data = parse(vars_ab_template, vars_ab_line)
49 a = int(vars_ab_data[0])
50 b = int(vars_ab_data[1])
51 c = int(vars_cd_data[0])
52 d = int(vars_cd_data[1])
53 print("Challenge: a = {}, b = {}, c = {}, d = {}".format(a, b, c, d))
54 solution_c, solution_d = find_solution_strings(a, b, c, d)
55 r.recvline_endswith("[Q]uit!")
57 r.recvline_endswith("first variable:")
58 r.sendline(solution_c)
59 r.recvline_endswith("second variable:")
60 r.sendline(solution_d)
61 flag_line = r.recvline()
65 if __name__ == "__main__":