-
Notifications
You must be signed in to change notification settings - Fork 0
/
38.py
executable file
·166 lines (121 loc) · 3.97 KB
/
38.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
import secrets
import sys
from typing import Any, Callable
from math import ceil
from asyncio import create_task, run, sleep
from random import choice
from Crypto.Hash import SHA256
from mycrypto import xor
N = 0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff
g = 2
k = 3
I = b'john.doe@example.com'
# very small password dictionary for demo purposes
pw_dict = [b'password', b'123456', b'Password123!', b'qwerty', b'qazxswedc12345', b'12345!@#$%qwerty']
P = choice(pw_dict)
# {receiver: data_tuple or data, ...}
messages: dict[str, Any] = dict()
async def send(sender, recipient, *data):
if len(data) == 1:
data = data[0]
print(f'[*] {sender}->{recipient}: {data}\n')
messages[recipient] = data
async def receive(identity) -> Any:
while True:
data = messages.pop(identity, None)
if data is not None:
return data
await sleep(0.1)
def to_bytes(s_: int) -> bytes:
s: bytes = b'\x00'
if s_ != 0:
s = s_.to_bytes(ceil(s_.bit_length() / 8), 'big')
return s
def sha256(*data):
sha256 = SHA256.new()
for d in data:
if isinstance(d, int):
d = to_bytes(d)
assert isinstance(d, bytes)
sha256.update(d)
return sha256.digest()
def hmac(message: bytes, key: bytes, H: Callable[..., bytes]):
BLOCK_SIZE = 64
# pad key
if len(key) > BLOCK_SIZE:
key = H(key)
elif len(key) < BLOCK_SIZE:
key += (BLOCK_SIZE - len(key)) * b'\x00'
return H(xor(key, b'\x5c' * BLOCK_SIZE) + H(xor(key, b'\x36' * BLOCK_SIZE) + message))
def sha256_hmac(message: bytes, key: bytes):
return hmac(message, key, sha256)
async def server():
b = secrets.randbelow(sys.maxsize) % N
salt = secrets.token_bytes(8)
xH = sha256(salt, P)
x = int.from_bytes(xH, 'big')
v: int = pow(g, x, N)
del x
del xH
Ic, A = await receive('S')
if Ic != I:
await send('S', 'C', 'UNAUTHORIZED')
return
assert isinstance(A, int)
B = pow(g, b, N)
u = secrets.randbits(128)
await send('S', 'C', salt, B, u)
S = pow(A * pow(v, u, N), b, N)
K = sha256(S)
token = sha256_hmac(K, salt)
token_c = await receive('S')
if token == token_c:
await send('S', 'C', 'OK')
else:
await send('S', 'C', 'UNAUTHORIZED')
async def client():
a = secrets.randbelow(sys.maxsize) % N
A = pow(g, a, N)
await send('C', 'S', I, A)
salt, B, u = await receive('C')
xH = sha256(salt, P)
x = int.from_bytes(xH, 'big')
S = pow(B, a + u*x, N)
K = sha256(S)
await send('C', 'S', sha256_hmac(K, salt))
result = await receive('C')
assert result == 'OK'
async def attacker_server():
b = secrets.randbelow(sys.maxsize) % N
salt = secrets.token_bytes(8)
Ic, A = await receive('S')
if Ic != I:
await send('S', 'C', 'UNAUTHORIZED')
return
assert isinstance(A, int)
B = pow(g, b, N)
u = secrets.randbits(128)
await send('S', 'C', salt, B, u)
token_c = await receive('S')
await send('S', 'C', 'OK')
for P in pw_dict:
xH = sha256(salt, P)
x = int.from_bytes(xH, 'big')
v: int = pow(g, x, N)
S = pow(A * pow(v, u, N), b, N)
K = sha256(S)
token = sha256_hmac(K, salt)
if token == token_c:
print('[+] Password found!')
return
print('[-] Failed to crack password using dictionary')
async def main(server):
s = create_task(server())
c = create_task(client())
await s
await c
if __name__ == '__main__':
run(main(server))
print(f'[+] Regular login works\n')
run(main(attacker_server))