/
tests_3.py
126 lines (95 loc) · 4.38 KB
/
tests_3.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
import unittest
import conv
import xortools
import aestools
import encoding
import random
import frequency
import mt
import random
import time
class Challenges(unittest.TestCase):
def test_challenge_17(self):
""" Challenge 17: CBC padding oracle """
input_file = open('files/17.txt', 'r')
lines = [conv.base_64_to_bytes(line.rstrip()) for line in input_file]
input_file.close()
cipher, iv = aestools.provide_cbc_ecrypted()
self.assertTrue(aestools.is_valid_padding(cipher, iv))
cracked = aestools.break_cbc_using_padding(aestools.is_valid_padding, cipher, iv)
self.assertTrue(cracked in lines)
def test_challenge_18(self):
""" Challenge 18: CTR Encryption """
test = 'L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ=='
key = "YELLOW SUBMARINE"
nonce = bytes(8)
self.assertEqual(b"Yo, VIP Let's kick it Ice, Ice, baby Ice, Ice, baby ", aestools.do_ctr(conv.base_64_to_bytes(test), key, nonce))
roundtrip_input = conv.base_64_to_bytes(aestools.TEXT)
roundtrip_encrypt = aestools.do_ctr(roundtrip_input, key, nonce)
self.assertEqual(roundtrip_input, aestools.do_ctr(roundtrip_encrypt, key, nonce))
def test_challenge_19(self):
""" Challenge 19: Break fixed-nonce CTR """
key = aestools.random_key(16)
nonce = bytes(8)
input_file = open('files/19.txt', 'r')
lines = [conv.base_64_to_bytes(line.rstrip()) for line in input_file]
input_file.close()
encrypted_lines = [aestools.do_ctr(line, key, nonce) for line in lines]
index = 0
probable_bytes = bytearray()
while(True):
rotated = "".join([chr(line[index]) if index < len(line) else '' for line in encrypted_lines])
b, all, score = xortools.solve_xor_block(bytes(rotated, 'utf-8'))
probable_bytes.append(b)
index += 1
if len(rotated) == 0: break
for line in encrypted_lines:
close = xortools.xor_bytes(line, bytes(probable_bytes[0 : len(line)]))
readable = " ".join([chr(b) if b in range(32, 127) else 'X' for b in close])
#print(readable)
# no need to automate everything, from here i would manually adjust bytes
def test_challenge_20(self):
""" Challenge 20: Break fixed-nonce CTR Statistically """
key = aestools.random_key(16)
nonce = bytes(8)
input_file = open('files/20.txt', 'r')
lines = [conv.base_64_to_bytes(line.rstrip()) for line in input_file]
input_file.close()
encrypted_lines = [aestools.do_ctr(line, key, nonce) for line in lines]
min_length = min([len(line) for line in encrypted_lines])
concatted = b''.join([line[0:min_length] for line in encrypted_lines])
test = xortools.breakxor(concatted, min_length)
#print(test)
# this is even closer than the last one, still would require tweaking a couple bytes
def test_challenge_22(self):
""" Challenge 22: MT seed finding, uncomment to run """
#start_time = int(round(time.time() * 1000))
#first_rand = mt.wait_seed_wait_rand()
#end_time = int(round(time.time() * 1000))
#for seed in range(start_time, end_time):
# generator = mt.MT19937(seed)
# if(generator.extract_number() == first_rand):
# print("Found seed: ")
# print(seed)
# break
def test_challenge_23(self):
""" Challenge 23: Clone MT """
seed = int(round(time.time() * 1000))
original = mt.MT19937(seed)
cloned = mt.clone_mt(original)
# they should be different objects
self.assertNotEqual(original, cloned)
#run the clone until it catches up to the original, if needed
next_number = original.extract_number()
cloned_number = cloned.extract_number()
timeout = 0
while(next_number != cloned_number):
cloned_number = cloned.extract_number()
timeout += 1
if(timeout > 1000):
raise Exception('cloned twister couldnt catch up to original')
# check next few numbers of each to make sure they're equal
for i in range(5):
self.assertEqual(original.extract_number(), cloned.extract_number())
if __name__ == '__main__':
unittest.main()