-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge19.py
More file actions
133 lines (118 loc) · 5.62 KB
/
Copy pathchallenge19.py
File metadata and controls
133 lines (118 loc) · 5.62 KB
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
import challenge3
import challenge5
import challenge11
import challenge18
import base64
def try_chrs_at_offset(cts, known_keystream, chrs, offset=0, choose=False, show_ct_i=False, ct_index=None):
if type(chrs) == str:
chrs = chrs.encode('utf-8')
if show_ct_i:
for i, ct in enumerate(cts):
if len(ct) >= len(known_keystream):
print(i, challenge5.xor(known_keystream, ct))
return b''
for i, ct1 in enumerate(cts):
enc_part = ct1[offset:offset + len(chrs)]
if (len(enc_part) and ct_index == None) or ct_index == i:
keystream_part = challenge5.xor(enc_part, chrs)
enc_chrs = b''.join([ct2[offset:offset + len(chrs)] for ct2 in cts if len(ct2) > len(known_keystream)])
dec_chrs = challenge5.xor(enc_chrs, keystream_part)
if challenge3.probably_txt(dec_chrs) or choose:
print('Plaintext parts :')
print('-' * 80)
for j, ct3 in enumerate(cts):
if len(ct3) > len(known_keystream):
plain_part = challenge5.xor(known_keystream + keystream_part, ct3)
print(j, plain_part)
print('-' * 80)
print('Decrypted chars :')
print(dec_chrs)
if 'n' == input('Does that make sense ? [Y/n]'):
continue
else:
return keystream_part
return b''
def attack_ctr_freq_analysis(cts):
trigrams = ['the','and','ing','her','hat','his','tha','ere','for','ent','ion','ter','was','you','ith','ver','all','wit','thi','tio']
trigrams += ['The','And','Her','His','Tha','For','You','All','Wit','Thi']
keystream = b''
ks_len = len(max(cts, key=len))
while len(keystream) != ks_len:
if 'n' != input('Want to try trigraphs ? [Y/n]'):
for trigram in trigrams:
keystream += try_chrs_at_offset(cts, keystream, trigram, offset=len(keystream))
if 'y' == input('Want to try your own input ? [y/N]'):
keystream += try_chrs_at_offset(cts, keystream, b'', offset=len(keystream), show_ct_i=True)
ct_index = int(input('On which ciphertext do we append your input ?'))
pattern = input('Enter the input you want to try : ')
keystream += try_chrs_at_offset(cts, keystream, pattern, offset=len(keystream), choose=True, ct_index=ct_index)
if 'y' == input('Want to try common chars ? [y/N]'):
for common_chr in challenge3.COMMON_CHARS:
cur_len = len(keystream)
keystream += try_chrs_at_offset(cts, keystream, bytes([common_chr]), offset=len(keystream))
if cur_len != len(keystream):
break
if 'y' == input('Want to bruteforce the possible byte ? [y/N]'):
for i in range(256):
cur_len = len(keystream)
keystream += try_chrs_at_offset(cts, keystream, bytes([i]), offset=len(keystream), choose=True)
if cur_len != len(keystream):
break
return keystream
if __name__ == '__main__':
pts = [
'SSBoYXZlIG1ldCB0aGVtIGF0IGNsb3NlIG9mIGRheQ==',
'Q29taW5nIHdpdGggdml2aWQgZmFjZXM=',
'RnJvbSBjb3VudGVyIG9yIGRlc2sgYW1vbmcgZ3JleQ==',
'RWlnaHRlZW50aC1jZW50dXJ5IGhvdXNlcy4=',
'SSBoYXZlIHBhc3NlZCB3aXRoIGEgbm9kIG9mIHRoZSBoZWFk',
'T3IgcG9saXRlIG1lYW5pbmdsZXNzIHdvcmRzLA==',
'T3IgaGF2ZSBsaW5nZXJlZCBhd2hpbGUgYW5kIHNhaWQ=',
'UG9saXRlIG1lYW5pbmdsZXNzIHdvcmRzLA==',
'QW5kIHRob3VnaHQgYmVmb3JlIEkgaGFkIGRvbmU=',
'T2YgYSBtb2NraW5nIHRhbGUgb3IgYSBnaWJl',
'VG8gcGxlYXNlIGEgY29tcGFuaW9u',
'QXJvdW5kIHRoZSBmaXJlIGF0IHRoZSBjbHViLA==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vbywgaGFzIGJlZW4gY2hhbmdlZCBpbiBoaXMgdHVybiw=',
'VHJhbnNmb3JtZWQgdXR0ZXJseTo=',
'QSB0ZXJyaWJsZSBiZWF1dHkgaXMgYm9ybi4='
]
pts = [base64.b64decode(pt) for pt in pts]
nonce = 0
key = challenge11.gen_key()
cts = []
for pt in pts:
ct = challenge18.aes_ctr_encrypt(pt, key, nonce)
cts.append(ct)
keystream = attack_ctr_freq_analysis(cts)
print('Here is the recovered keystream :')
print(base64.b64encode(keystream))
print('Here are the plaintexts :')
for ct in cts:
pt = challenge5.xor(ct, keystream)
print(pt)