-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm12.py
executable file
·66 lines (52 loc) · 1.87 KB
/
m12.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
#!/usr/bin/env python3
"""Byte-at-a-time ECB decryption (simple)"""
# pylint: disable=redefined-outer-name
from base64 import b64decode
from typing import Callable
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from m09 import pkcs7, de_pkcs7
from m11 import detect_ecb
RANDOM_KEY = get_random_bytes(16)
def oracle(plaintext: bytes) -> bytes:
with open("data/12.txt", "r") as data_handle:
unknown_string = b64decode(data_handle.read())
plaintext = pkcs7(plaintext + unknown_string, 16)
cypher = AES.new(RANDOM_KEY, AES.MODE_ECB)
return cypher.encrypt(plaintext)
def blocksize(oracle: Callable[[bytes], bytes]) -> int:
smallest = len(oracle(b""))
for i in range(256):
test = i * b"A"
if len(oracle(test)) - smallest > 0:
return len(oracle(test)) - smallest
raise RuntimeError("Can't find oracle blocksize")
def len_string(oracle: Callable[[bytes], bytes]) -> int:
l = len(oracle(b""))
bs = blocksize(oracle)
for i in range(1, bs + 1):
if l < len(oracle(i * b"A")):
return l - i
raise RuntimeError("Can't find oracle string length")
def break_ecb(oracle: Callable[[bytes], bytes]) -> bytes:
bs = blocksize(oracle)
l = len(oracle(b""))
string_length = len_string(oracle)
plaintext = b""
prefix = (l + bs - 1) * b"A"
while len(plaintext) <= string_length:
oracle_prefix = oracle(prefix)
for i in range(127):
test = prefix + plaintext + bytes([i])
if oracle(test)[l:l + bs] == oracle_prefix[l:l + bs]:
prefix = prefix[1:]
plaintext += bytes([i])
break
return de_pkcs7(plaintext)
def main() -> None:
if not detect_ecb(oracle(64 * b"A")):
print("Not ECB")
raise SystemExit
print(break_ecb(oracle).decode())
if __name__ == "__main__":
main()