-
Notifications
You must be signed in to change notification settings - Fork 0
/
34.py
executable file
·128 lines (95 loc) · 3.27 KB
/
34.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
import secrets
import sys
from typing import Any
from math import ceil
from asyncio import create_task, run, sleep
from Crypto.Hash import SHA1
from mycrypto import aes_128_cbc_decrypt, aes_128_cbc_encrypt
# {receiver: data_tuple or data, ...}
messages: dict[str, Any] = dict()
async def send(sender, receiver, *data):
# simulate MITM
if sender != 'M':
receiver = 'M'
if len(data) == 1:
data = data[0]
print(f'[*] {sender}->{receiver}: {data}')
messages[receiver] = data
async def receive(identity) -> Any:
while True:
data = messages.pop(identity, None)
if data is not None:
return data
await sleep(0.1)
async def user_a():
p = 0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff
g = 2
a = secrets.randbelow(sys.maxsize) % p
A = pow(g, a, p)
await send('A', 'B', p, g, A)
B: int = await receive('A')
s = pow(B, a, p)
s = s.to_bytes(ceil(s.bit_length() / 8), 'big')
sha1 = SHA1.new()
sha1.update(s)
key = sha1.digest()[:16]
message = b'Hello from the other side'
iv = secrets.token_bytes(16)
ciphertext = aes_128_cbc_encrypt(message, key, iv)
await send('A', 'B', ciphertext, iv)
b_ciphertext, b_iv = await receive('A')
b_message = aes_128_cbc_decrypt(b_ciphertext, key, b_iv)
assert message == b_message
async def user_b():
p, g, A = await receive('B')
b = secrets.randbelow(sys.maxsize) % p
B = pow(g, b, p)
await send('B', 'A', B)
s = pow(A, b, p)
s = s.to_bytes(ceil(s.bit_length() / 8), 'big')
sha1 = SHA1.new()
sha1.update(s)
key = sha1.digest()[:16]
a_ciphertext, a_iv = await receive('B')
a_message = aes_128_cbc_decrypt(a_ciphertext, key, a_iv)
iv = secrets.token_bytes(16)
ciphertext = aes_128_cbc_encrypt(a_message, key, iv)
await send('B', 'A', ciphertext, iv)
async def user_m():
# from A
p, g, A = await receive('M')
# to B
await send('M', 'B', p, g, p)
# from B
B = await receive('M')
# to A
await send('M', 'A', p)
# from A
a_ciphertext, a_iv = await receive('M')
# to B
await send('M', 'B', a_ciphertext, a_iv)
# from B
b_ciphertext, b_iv = await receive('M')
# to A
await send('M', 'A', b_ciphertext, b_iv)
sha1 = SHA1.new()
sha1.update(b'')
key = sha1.digest()[:16]
print('Attempting to decrypt ciphertext from A...')
a_message = aes_128_cbc_decrypt(a_ciphertext, key, a_iv)
print("A's message:", a_message)
print('Attempting to decrypt ciphertext from B...')
b_message = aes_128_cbc_decrypt(b_ciphertext, key, b_iv)
print("B's message:", b_message)
assert a_message == b_message
print(f'Successfully decrypted message: {a_message.decode()}')
async def main():
a = create_task(user_a())
m = create_task(user_m())
b = create_task(user_b())
await a
await b
await m
if __name__ == '__main__':
run(main())