9
9
10
10
import argparse
11
11
import re
12
- import binascii
13
12
import sys
14
13
import time
15
- from binascii import unhexlify , hexlify
16
- from itertools import cycle , izip
17
- from Crypto .Cipher import AES
18
- from Crypto import Random
14
+ from itertools import cycle
15
+ from Cryptodome .Cipher import AES
19
16
20
17
"""
21
18
AES-CBC
22
19
function encrypt, decrypt, pad, unpad)
23
20
"""
24
21
25
22
def pad (s ):
26
- return s + (16 - len (s ) % 16 ) * chr (16 - len (s ) % 16 )
23
+ pad_byte = 16 - len (s ) % 16
24
+ for i in range (pad_byte ):
25
+ s .append (pad_byte )
26
+ return s
27
27
28
28
def unpad (s ):
29
- t = s .encode ("hex" )
30
- exe = re .findall ('..' ,t )
29
+ exe = re .findall ('..' ,s .hex ())
31
30
padding = int (exe [- 1 ], 16 )
32
31
exe = exe [::- 1 ]
33
32
@@ -41,12 +40,11 @@ def unpad(s):
41
40
42
41
def encrypt ( msg , iv ):
43
42
raw = pad (msg )
44
- key = Random .new ().read ( AES .block_size )
45
- cipher = AES .new ('V38lKILOJmtpQMHp' , AES .MODE_CBC , iv )
43
+ cipher = AES .new (b'V38lKILOJmtpQMHp' , AES .MODE_CBC , iv )
46
44
return cipher .encrypt ( raw ), iv
47
45
48
46
def decrypt ( enc , iv ):
49
- decipher = AES .new ('V38lKILOJmtpQMHp' , AES .MODE_CBC , iv )
47
+ decipher = AES .new (b 'V38lKILOJmtpQMHp' , AES .MODE_CBC , iv )
50
48
return unpad (decipher .decrypt ( enc ))
51
49
52
50
@@ -59,7 +57,7 @@ def test_validity(error):
59
57
60
58
61
59
def call_oracle (up_cipher , iv ):
62
- if decrypt ( up_cipher , iv ) == 0 :
60
+ if decrypt ( bytes . fromhex ( up_cipher ) , iv ) == 0 :
63
61
return 404
64
62
return 200
65
63
@@ -80,8 +78,11 @@ def block_padding(size_block, i):
80
78
def split_len (seq , length ):
81
79
return [seq [i :i + length ] for i in range (0 , len (seq ), length )]
82
80
83
- def hex_xor (s1 ,s2 ):
84
- return hexlify ('' .join (chr (ord (c1 ) ^ ord (c2 )) for c1 , c2 in zip (unhexlify (s1 ), cycle (unhexlify (s2 )))))
81
+ def hex_xor (s1 , s2 ):
82
+ b = bytearray ()
83
+ for c1 , c2 in zip (bytes .fromhex (s1 ), cycle (bytes .fromhex (s2 ))):
84
+ b .append (c1 ^ c2 )
85
+ return b .hex ()
85
86
86
87
def run (cipher ,size_block ):
87
88
cipher = cipher .upper ()
@@ -92,15 +93,15 @@ def run(cipher,size_block):
92
93
cipher_block = split_len (cipher , len_block )
93
94
94
95
if len (cipher_block ) == 1 :
95
- print "[-] Abort there is only one block, i can't influence the IV. Tried a longer message"
96
+ print ( "[-] Abort there is only one block, i can't influence the IV. Tried a longer message" )
96
97
sys .exit ()
97
98
98
99
#for each cipher_block
99
100
for block in reversed (range (1 ,len (cipher_block ))):
100
101
if len (cipher_block [block ]) != len_block :
101
- print "[-] Abort length block doesn't match the size_block"
102
+ print ( "[-] Abort length block doesn't match the size_block" )
102
103
break
103
- print "[+] Search value block : " , block , "\n "
104
+ print ( "[+] Search value block : " , block , "\n " )
104
105
#for each byte of the block
105
106
for i in range (0 ,size_block ):
106
107
# test each byte max 255
@@ -118,7 +119,7 @@ def run(cipher,size_block):
118
119
#time.sleep(0.5)
119
120
120
121
# we call the oracle, our god
121
- error = call_oracle (up_cipher . decode ( 'hex' ) ,iv )
122
+ error = call_oracle (up_cipher ,iv )
122
123
123
124
if args .verbose == True :
124
125
exe = re .findall ('..' ,cb )
@@ -138,19 +139,19 @@ def run(cipher,size_block):
138
139
valide_value .insert (0 ,value [size_block - (i + 1 )])
139
140
140
141
if args .verbose == True :
141
- print ''
142
- print "[+] Block M_Byte : %s" % bk
143
- print "[+] Block C_{i-1}: %s" % bp
144
- print "[+] Block Padding: %s" % bc
145
- print ''
142
+ print ( '' )
143
+ print ( "[+] Block M_Byte : %s" % bk )
144
+ print ( "[+] Block C_{i-1}: %s" % bp )
145
+ print ( "[+] Block Padding: %s" % bc )
146
+ print ( '' )
146
147
147
148
bytes_found = '' .join (valide_value )
148
- if i == 0 and bytes_found . decode ( "hex" ) > hex ( size_block ) and block == len (cipher_block )- 1 :
149
- print "[-] Error decryption failed the padding is > " + str (size_block )
149
+ if i == 0 and int ( bytes_found , 16 ) > size_block and block == len (cipher_block )- 1 :
150
+ print ( "[-] Error decryption failed the padding is > " + str (size_block ) )
150
151
sys .exit ()
151
152
152
- print '\033 [36m' + '\033 [1m' + "[+]" + '\033 [0m' + " Found" , i + 1 , "bytes :" , bytes_found
153
- print ''
153
+ print ( '\033 [36m' + '\033 [1m' + "[+]" + '\033 [0m' + " Found" , i + 1 , "bytes :" , bytes_found )
154
+ print ( '' )
154
155
155
156
break
156
157
if found == False :
@@ -159,34 +160,35 @@ def run(cipher,size_block):
159
160
value = re .findall ('..' ,bk )
160
161
valide_value .insert (0 ,"01" )
161
162
if args .verbose == True :
162
- print ''
163
- print '[-] No padding found, but maybe the padding is length 01 :)'
164
- print "[+] Block M_Byte : %s" % bk
165
- print "[+] Block C_{i-1}: %s" % bp
166
- print "[+] Block Padding: %s" % bc
167
- print ''
163
+ print ( '' )
164
+ print ( '[-] No padding found, but maybe the padding is length 01 :)' )
165
+ print ( "[+] Block M_Byte : %s" % bk )
166
+ print ( "[+] Block C_{i-1}: %s" % bp )
167
+ print ( "[+] Block Padding: %s" % bc )
168
+ print ( '' )
168
169
bytes_found = '' .join (valide_value )
169
170
else :
170
- print "\n [-] Error decryption failed"
171
+ print ( "\n [-] Error decryption failed" )
171
172
result .insert (0 , '' .join (valide_value ))
172
173
hex_r = '' .join (result )
173
174
if len (hex_r ) > 0 :
174
- print "[+] Partial Decrypted value (HEX):" , hex_r .upper ()
175
+ print ( "[+] Partial Decrypted value (HEX):" , hex_r .upper () )
175
176
padding = int (hex_r [len (hex_r )- 2 :len (hex_r )],16 )
176
- print "[+] Partial Decrypted value (ASCII):" , hex_r [0 :- (padding * 2 )].decode ("hex" )
177
+ print ( "[+] Partial Decrypted value (ASCII):" , bytes . fromhex ( hex_r [0 :- (padding * 2 )]) .decode () )
177
178
sys .exit ()
178
179
found = False
179
180
180
181
result .insert (0 , '' .join (valide_value ))
181
182
valide_value = []
182
183
183
- print ''
184
+ print ( '' )
184
185
hex_r = '' .join (result )
185
- print "[+] Decrypted value (HEX):" , hex_r .upper ()
186
+ print ( "[+] Decrypted value (HEX):" , hex_r .upper () )
186
187
padding = int (hex_r [len (hex_r )- 2 :len (hex_r )],16 )
187
- print "[+] Decrypted value (ASCII):" , hex_r [0 :- (padding * 2 )].decode ("hex" )
188
+ decoded = bytes .fromhex (hex_r [0 :- (padding * 2 )]).decode ()
189
+ print ("[+] Decrypted value (ASCII):" , decoded )
188
190
189
- return hex_r [ 0 : - ( padding * 2 )]. decode ( "hex" )
191
+ return decoded
190
192
191
193
if __name__ == '__main__' :
192
194
@@ -195,10 +197,9 @@ def run(cipher,size_block):
195
197
parser .add_argument ('-v' , "--verbose" , help = 'debug mode, you need a large screen' , action = "store_true" )
196
198
args = parser .parse_args ()
197
199
198
- print "[+] Encrypt" , args .message
199
- cipher , iv = encrypt (args .message , "1234567812345678" )
200
- cipher_intercepted = cipher .encode ("hex" )
201
- print "[+] %s ---> %s" % (args .message , cipher_intercepted )
200
+ print ("[+] Encrypt" , args .message )
201
+ cipher , iv = encrypt (bytearray (args .message , 'UTF-8' ), b"1234567812345678" )
202
+ print ("[+] %s ---> %s" % (args .message , cipher .hex ()))
202
203
plaintext = decrypt (cipher , iv )
203
204
204
- run (cipher_intercepted , 16 )
205
+ run (cipher . hex (), 16 )
0 commit comments