-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path24.py
55 lines (40 loc) · 1.14 KB
/
24.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
from common import xor_str
from mersennetwister import MT19937
from tqdm import tqdm
def stream_cipher(seed, data):
assert(seed.bit_length() <= 16)
def stream():
mt = MT19937(seed)
while True:
x = mt.next()
for i in xrange(4):
yield chr(x & 0xff)
x = (x >> 8)
return xor_str(data, stream())
# Verifying correctness
if (stream_cipher(10, stream_cipher(10, 'aaa')) != 'aaa'):
import sys
print "[!] Something somewhere went terribly wrong"
sys.exit(1)
# Actual oracle
def oracle(data):
global SEED # Used only for verification
from common import randstr
from random import randint
p = randstr(randint(5, 50)) + data
s = randint(0, (1 << 16) - 1)
SEED = s
return stream_cipher(s, p)
# Actual attack
LEN_PLAINTEXT = 14
ciphertext = oracle('A'*LEN_PLAINTEXT)
keys = []
for k in tqdm(xrange(1 << 16)):
if stream_cipher(k, ciphertext)[-LEN_PLAINTEXT:] == 'A'*LEN_PLAINTEXT:
keys.append(k)
print "[+] SEED =", SEED
print "[+] keys =", keys
if SEED in keys:
print "[+] Success!"
else:
print "[!] Fail!"