-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexploit.py
executable file
·54 lines (40 loc) · 1.6 KB
/
exploit.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
#!/usr/bin/env python3
import re
from z3 import *
from string import printable
def build_alphabet(pattern):
regex = re.compile(pattern)
return ''.join(x for x in printable if regex.match(x))
def generate_bitvecs(name, length, size=8):
return [BitVec(name + str(i), size) for i in range(length)]
def make_solution(ciphertext, pt_length, key_length, constraint):
pt_vars = generate_bitvecs('pt', pt_length)
key_vars = generate_bitvecs('key', key_length)
equations = [pt_vars[0] == constraint]
for i, ct in enumerate(ciphertext):
equation = pt_vars[i % pt_length] ^ key_vars[i % key_length] == ct
equations.append(equation)
solver = Solver()
solver.add(equations)
if solver.check().r == -1:
return None
model = solver.model()
return bytes(model[pt].as_long() for pt in pt_vars)
def brute_constraint(pattern, ciphertext, pt_length, key_length):
alphabet = build_alphabet(pattern)
for x in alphabet:
solution = make_solution(ciphertext, pt_length, key_length, ord(x))
if solution is not None:
if re.match(pattern.encode(), solution):
yield solution
def main():
pattern = r'^\w+$'
ciphertext = bytes.fromhex(open('output.txt', 'r').read().strip())
pt_length = len(ciphertext) // 2
max_key_length = 32
for key_length in range(1, max_key_length + 1):
print('key_length = {0}'.format(key_length))
for solution in brute_constraint(pattern, ciphertext, pt_length, key_length):
print('Cup{{{0}}}'.format(solution.decode()))
if __name__ == '__main__':
main()