-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (69 loc) · 2.6 KB
/
main.py
File metadata and controls
76 lines (69 loc) · 2.6 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
from btn710.oracle import btnUnpad, encrypt, decrypt
from btn710.attack import elementForgery, sequenceXor, positionalByteForgery
from Crypto.Util.py3compat import bchr
if __name__ =='__main__':
# Normal Client
plainText = b'Hello world!'
cipherText = encrypt(plainText)
print(b'Cipher Text: ' + cipherText)
print(b'Plain Text: ' + decrypt(cipherText))
# Attack Client
iv = cipherText[:16]
lastBlock = cipherText[-16:]
forgery = b'0000000000000000'
intermediateValue = []
# Finding last byte correct padding
correctPadding = False
count = 0
while(not correctPadding):
try:
decrypt(forgery + lastBlock)
print(b'Correct padding forgery: ' + forgery)
correctPadding = True
except ValueError:
forgery = positionalByteForgery(forgery, count, 15)
count += 1
tempLastIV = forgery.decode('utf-8')[-1]
# Modify each element until we know the padding length
count = 0
while(correctPadding):
try:
decrypt(forgery + lastBlock)
forgery = elementForgery(forgery, count)
count += 1
except ValueError:
correctPadding = False
print('Amount of padding ' + str(17 - count))
# Save last correct byte into intermediate value
correctPaddingLength = 17 - count
if(correctPaddingLength == 1):
intermediateValue.insert(0, tempLastIV)
# Creating correct padding for full block size
position = 14
value = 0
while(position >= 0):
forgery = sequenceXor(forgery,intermediateValue, value)
print(b'Setting forgery: ' + forgery)
correctPadding = False
forgeryCounter = 0
while (not correctPadding):
try:
decrypt(forgery + lastBlock)
correctPadding = True
temp = [forgery[i] for i in range(0, len(forgery))]
intermediateValue.insert(0, chr(temp[position]))
except ValueError:
forgery = positionalByteForgery(forgery, forgeryCounter, position)
forgeryCounter += 1
position -= 1
value += 1
print('Completed Intermediate Value: ')
print(intermediateValue)
# Crack plaintext by xoring intermediate value and IV
seperateIV = list(iv)
crackedPlainText = []
for i in range(16):
crackedPlainText.append( bchr(seperateIV[i] ^ int.from_bytes(bytes(intermediateValue[i], 'utf-8'),"big")))
crackedPlainText = b''.join(crackedPlainText)
crackedPlainText = btnUnpad(crackedPlainText, 16, 'btn710')
print(crackedPlainText)