-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcy2-1.py
More file actions
53 lines (44 loc) · 1.2 KB
/
Copy pathcy2-1.py
File metadata and controls
53 lines (44 loc) · 1.2 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
from base64 import b64decode
from Crypto.Cipher import AES
from random import randint, randbytes
def padding(s: bytes):
r = (16 - len(s) % 16) % 16
return s + r.to_bytes(1, 'big') * r
def f1(s: bytes, aes, pre):
c = b'Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg' + \
b'aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq' + \
b'dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg' + \
b'YnkK'
c = b64decode(c)
s = padding(pre + s + c)
return aes.encrypt(s)
def guess(aes, pre):
s = f1(b'', aes, pre)
last = s[:16]
pre_len = 0
for i in range(1, 16):
s = f1(b'a' * i, aes, pre)
if s[:16] == last:
pre_len = 16 - i + 1
break
last = s[:16]
print(pre_len)
max_len = 1024
m = b''
while True:
blank = b'\x00' * (max_len - 1 - pre_len - len(m))
for i in range(1, 256):
s = blank + m + i.to_bytes(1, 'big')
t = blank
if f1(s, aes, pre)[:max_len] == f1(t, aes, pre)[:max_len]:
m = m + i.to_bytes(1, 'big')
break
print(m.decode())
if __name__ == '__main__':
pre_len = randint(1, 16)
print('true pre_len:', pre_len)
pre = randbytes(pre_len)
key = randbytes(16)
iv = randbytes(16)
aes = AES.new(key, AES.MODE_ECB)
guess(aes, pre)