-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge_17.py
155 lines (137 loc) · 4.85 KB
/
challenge_17.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
148
149
150
151
152
153
154
155
import os, binascii, random, sys, base64, string
from Crypto.Cipher import AES
from functools import reduce
possibleStrigs = [
"MDAwMDAwTm93IHRoYXQgdGhlIHBhcnR5IGlzIGp1bXBpbmc=" ,
"MDAwMDAxV2l0aCB0aGUgYmFzcyBraWNrZWQgaW4gYW5kIHRoZSBWZWdhJ3MgYXJlIHB1bXBpbic=",
"MDAwMDAyUXVpY2sgdG8gdGhlIHBvaW50LCB0byB0aGUgcG9pbnQsIG5vIGZha2luZw==",
"MDAwMDAzQ28va2luZyBNQydzIGxpa2UgYSBwb3VuZCBvZiBiYWNvbg==",
"MDAwMDA0QnVybmluZyAnZW0sIGlmIHlvdSBhaW4ndCBxdWljayBhbmQgbmltYmxl",
"MDAwMDA1SSBnbyBjcmF6eSB3aGVuIEkgaGVhciBhIGN5bWJhbA==",
"MDAwMDA2QW5kIGEgaGlnaCBoYXQgd2l0aCBhIHNvdXBlZCB1cCB0ZW1wbw==",
"MDAwMDA3SSdtIG9uIGEgcm9sbCwgaXQncyB0aW1lIHRvIGdvIHNvbG8=",
"MDAwMDA4b2xsaW4nIGluIG15IGZpdmUgcG9pbnQgb2g=",
"MDAwMDA5aXRoIG15IHJhZy10b3AgZG93biBzbyBteSBoYWlyIGNhbiBibG93",
]
randomKey = os.urandom(16)
obj = AES.new(randomKey,AES.MODE_ECB)
def xor(stringToEncypt, key):
a = bytes(a ^ b for (a, b) in zip(stringToEncypt, key))
return a
def aesCBC_encrypt(plaintextBytes, iv):
blocks = len(plaintextBytes)//16
ciphertext = bytes()
for i in range(blocks):
temp = plaintextBytes[i*16:(i+1)*16]
xorResult = xor(temp,iv)
cipher = obj.encrypt(xorResult)
ciphertext += cipher
# iv = xorResult
iv = cipher
return ciphertext
def aesCBC_decrypt(ciphertext, iv):
blocks = len(ciphertext)//16
plaintext = bytes()
for i in range(blocks):
temp = ciphertext[i*16:(i+1)*16]
xorOb = obj.decrypt(temp)
plaintext += xor(xorOb, iv)
iv = temp
return plaintext
blocksize = 16
def valid_pkcs(text):
possiblePadding = int.from_bytes(text[-1:],'little')
if possiblePadding == 0:
possiblePadding = 16
val = int.from_bytes(text[-1:], "little")
if all(elem == val for elem in text[-possiblePadding:]):
print(len(text))
return text[:len(text) - possiblePadding]
raise Exception("Sorry, invalid padding")
def pkcs7(plaintext, blocksize):
padding = blocksize - len(plaintext) % blocksize
print("padding : " + str(padding))
# if padding == 16:
# return plaintext
pad = chr(padding % 16)
return ''.join((plaintext, pad*padding))
def firstFunction():
randomNumber = random.randint(0,len(possibleStrigs) - 1)
# randomNumber = 1
randomString = possibleStrigs[randomNumber]
string = binascii.a2b_base64(randomString)
print(string)
print(len(string))
string = pkcs7(string.decode(),16)
print(string)
iv = os.urandom(16)
ciphertext = aesCBC_encrypt(string.encode(),iv)
return (ciphertext, iv)
def secondFunction(ciphertext):
iv = ciphertext[:16]
ciphertext = ciphertext[16:len(ciphertext)]
plain = aesCBC_decrypt(ciphertext, iv)
# print(plain)
try:
valid_pkcs(plain)
print("plain")
print(plain)
print("plain")
return True
except:
return False
def oracleAttack():
ciphertext, iv = firstFunction()
ciphertext = iv + ciphertext
# for i in range(len(ciphertext)):
cipher = list(ciphertext)
# Not by reference
c = ciphertext[:]
print(c)
plaintext = []
count = 0
for l in range (len(ciphertext) // 16 - 1):
# lst = lst[:len(lst)-n]
ciphertext = cipher[:len(cipher) - 16 * l]
c = ciphertext[:]
for i in range(0,256):
# print(c)
c[-17] = i
# print(c)
result = secondFunction(bytes(c))
if(result):
# print("i = " + str(i))
count += 1
if i == ciphertext[-17]:
continue
# print(i)
# print(c[-1])
# print(bytearray(ciphertext)[-1])
# print(ciphertext[-17])
# print(c[-17])
interm = list(xor([c[-17]],[1]))
res = xor([ciphertext[-17]],interm)
# print(res)
plaintext.append(res)
print(plaintext)
print(interm)
for pad in range(2,17):
for j in range(1,pad):
c[-j - 16] = int.from_bytes(xor([interm[j - 1]], [pad]), "little")
# print([interm[j-1]])
for i in range(256):
c[-pad-16] = i
# print(c)
# i = xor([ciphertext[-pad:]],plaintext[-pad:])
# c[-j] = xor(xor(bytearray(bytes([pad])),[plaintext[-j]]),[c[-j]])
result = secondFunction(bytes(c))
if(result):
interm.append(int.from_bytes(xor([c[-pad -16]],[pad]),"little"))
res = xor([ciphertext[-pad - 16]],[interm[-1]])
# print(res)
plaintext.append(res)
print("plaintext")
plaintext.reverse()
print(plaintext)
print(b''.join(plaintext).decode('utf-8'))
oracleAttack()