-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeygen.py
147 lines (109 loc) · 4.01 KB
/
keygen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
import io
import re
import sys
import hashlib
import random
from subprocess import Popen, PIPE, STDOUT
from coefficients import coeffs
init_bits = [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0]
file_header = """Galois Field : GF(2)
Number of variables (n) : 50
Number of polynomials (m) : 40
Seed : 0
Order : graded reverse lex order
*********************"""
def parse_output(bitstring):
ks = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
li = list(bitstring)
li = [list(reversed(li[i:i+5])) for i in range(0, len(li), 5)]
key = ""
for a in li:
s = ""
for x in a:
s += "0" if x[0] == "0" else "1"
i = int(s, 2)
key += ks[i]
print (key[0:5], key[5:], sep="-")
def fix(fix, eqns):
fix = [int(v) for v in fix]
f = io.StringIO()
try:
for data in eqns:
data = data.strip()
if len(data) == 0:
print(data, file=f)
continue
if not (data[0] == '0' or data[0] =='1'):
if data.startswith("Number of variables"):
m = re.match(r'[^0-9]*([0-9]*)', data)
n = int(m.group(1))
print("Number of variables (n) : {0}".format(n-len(fix)), file=f)
else:
print(data, file=f)
continue
data = data.replace(" ", "")
data = data.replace(";", "")
if not (((n*(n-1)) >> 1) + 2*n + 1 == len(data)):
sys.stderr.write("Input length error!")
sys.exit(-1)
data = [int(v) for v in data]
new_n = n-len(fix)
sq = []
lin = [0] * new_n
const = 0
for i in range(n):
for j in range(i+1):
val = data[0]
data = data[1:]
if i >= new_n:
if j >= new_n:
const ^= val & fix[i-new_n] & fix[j-new_n]
else:
lin[j] ^= val & fix[i-new_n]
else:
if j >= new_n:
lin[i] ^= val & fix[j-new_n]
else:
sq.append(val)
for i in range(n):
val = data[0]
data = data[1:]
if i >= new_n:
const ^= val & fix[i-new_n]
else:
lin[i] ^= val
const ^= data[0]
print(" ".join([str(v) for v in (sq + lin + [const])]) + ";", file=f)
return f
except BrokenPipeError:
pass
def main(username):
f = io.StringIO()
hsh = hashlib.md5(username.encode("utf8")).digest()
hash_bytes = [hsh[2], hsh[5], hsh[8], hsh[11], hsh[14]]
target = ""
for h in hash_bytes:
target += "{0:08b}".format(h)[::-1]
target_bits = list(map(int, list(target)))
print (file_header, file=f)
for idx, line in enumerate(coeffs):
print (line, end = " ", file=f)
print (init_bits[idx] ^ target_bits[idx], ";", file=f)
fixed_bits = "{0:010b}".format(random.getrandbits(10))
fixed_system = fix(fixed_bits, io.StringIO(f.getvalue()))
f.close()
p = Popen(['./guess'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
stdout, stderr = p.communicate(input=bytes(fixed_system.getvalue(), 'utf8'))
# print (stdout)
# print (stderr)
try:
soln = int(stdout.split()[0].strip(), 16)
output = '{0:040b}'.format(soln)
print ("Username: ", username)
print ("Password: ", end = "")
parse_output((fixed_bits[::-1] + output)[::-1])
except:
print ("[-] Unable to generate password! Please retry")
if __name__ == "__main__":
main(sys.argv[1])