-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
321 lines (269 loc) · 12.1 KB
/
client.py
File metadata and controls
321 lines (269 loc) · 12.1 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env python3
# client.py
import socket
import sys
import base64
import argparse
import logging
from urllib.parse import urlparse
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.exceptions import InvalidTag
import time
# === Clase SecureSession: ECDH + HKDF para clave AES ===
from cryptography.hazmat.primitives.asymmetric import x25519
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives import hashes
import os, sys
# === CONFIGURACIÓN ===
MAX_RESPONSE_SIZE = 10 * 1024 * 1024 # 10 MB
MAX_SELECTOR_LEN = 255
class SecureSession:
"""
Negocia una clave AES efímera mediante ECDH (X25519) y HKDF.
Proporciona métodos para cifrar/descifrar.
"""
INFO = b"gopher2_key_derivation"
AES_KEY_LEN = 32 # 256 bits
def __init__(self):
# Generar par de claves efímero
self._private_key = x25519.X25519PrivateKey.generate()
self._shared_key = None
self._aesgcm = None
def get_public_key_fingerprint(self) -> str:
"""Devuelve la huella SHA256 de la clave pública en formato legible."""
pub_bytes = self.get_public_key_bytes()
digest = hashes.Hash(hashes.SHA256())
digest.update(pub_bytes)
fingerprint = digest.finalize()
# Formato: 00:11:22:... (como SSH)
return ":".join(f"{b:02x}" for b in fingerprint)
def get_public_key_bytes(self) -> bytes:
"""Devuelve la clave pública serializada (32 bytes)."""
return self._private_key.public_key().public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw
)
def derive_shared_key(self, peer_public_key_bytes: bytes):
"""Deriva la clave compartida usando ECDH + HKDF."""
if len(peer_public_key_bytes) != 32:
raise ValueError("Clave pública debe ser 32 bytes (X25519)")
try:
peer_public = x25519.X25519PublicKey.from_public_bytes(peer_public_key_bytes)
shared_secret = self._private_key.exchange(peer_public)
except Exception as e:
raise ValueError(f"Fallo en ECDH: {e}")
# Derivar clave AES con HKDF
hkdf = HKDF(
algorithm=hashes.SHA256(),
length=self.AES_KEY_LEN,
salt=None,
info=self.INFO,
)
aes_key = hkdf.derive(shared_secret)
self._aesgcm = AESGCM(aes_key)
def encrypt(self, plaintext: str) -> bytes:
"""Cifra texto plano → nonce (12) + ciphertext + tag (16)."""
if self._aesgcm is None:
raise RuntimeError("Clave compartida no derivada")
if isinstance(plaintext, str):
plaintext = plaintext.encode("utf-8")
nonce = os.urandom(12)
ciphertext = self._aesgcm.encrypt(nonce, plaintext, None)
return nonce + ciphertext
def decrypt(self, data: bytes) -> str:
"""Descifra nonce + ciphertext → texto plano."""
if self._aesgcm is None:
raise RuntimeError("Clave compartida no derivada")
if len(data) < 28: # 12 (nonce) + 16 (tag) mínimo
raise ValueError("Datos cifrados demasiado cortos")
nonce = data[:12]
ciphertext = data[12:]
try:
plaintext = self._aesgcm.decrypt(nonce, ciphertext, None)
return plaintext.decode("utf-8", errors="replace")
except Exception as e:
raise ValueError(f"Fallo de autenticación o descifrado: {e}")
def decrypt_response(b64_data: str) -> str:
try:
data = base64.b64decode(b64_data)
if len(data) < 12 + 16: # nonce (12) + tag (16) mínimo
raise ValueError("Datos cifrados demasiado cortos")
nonce = data[:12]
ciphertext = data[12:]
aesgcm = AESGCM(AES_KEY)
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
return plaintext.decode("utf-8", errors="replace")
except InvalidTag:
raise ValueError("Fallo de autenticación: clave incorrecta o datos corruptos")
except Exception as e:
raise ValueError(f"Error al descifrar: {e}")
def get_known_hosts_path() -> str:
home = os.path.expanduser("~")
return os.path.join(home, ".gopher2", "known_hosts")
def save_server_fingerprint(host: str, port: int, fingerprint: str):
os.makedirs(os.path.dirname(get_known_hosts_path()), exist_ok=True)
with open(get_known_hosts_path(), "a") as f:
f.write(f"{host}:{port} {fingerprint}\n")
def get_saved_fingerprint(host: str, port: int) -> str | None:
try:
with open(get_known_hosts_path()) as f:
for line in f:
parts = line.strip().split(" ", 1)
if len(parts) == 2:
saved_addr, saved_fp = parts
if saved_addr == f"{host}:{port}":
return saved_fp
except FileNotFoundError:
return None
return None
def fetch_gopher2(url: str) -> tuple[str, str, int, SecureSession]:
"""
Devuelve (contenido, host, puerto, sesión) para permitir animación posterior.
"""
parsed = urlparse(url)
if parsed.scheme != "gopher":
raise ValueError("Solo se admite gopher://")
host = parsed.hostname or "127.0.0.1"
port = parsed.port or 7070
selector = parsed.path or "/"
if len(selector) > MAX_SELECTOR_LEN:
raise ValueError(f"Selector demasiado largo (> {MAX_SELECTOR_LEN} bytes)")
with socket.create_connection((host, port), timeout=10) as sock:
client_session = SecureSession()
sock.sendall(client_session.get_public_key_bytes())
server_pubkey = sock.recv(32)
if len(server_pubkey) != 32:
raise ValueError("Clave pública del servidor inválida")
client_session.derive_shared_key(server_pubkey)
# TOFU: verificar huella
digest = hashes.Hash(hashes.SHA256())
digest.update(server_pubkey)
current_fingerprint = ":".join(f"{b:02x}" for b in digest.finalize())
saved_fingerprint = get_saved_fingerprint(host, port)
if saved_fingerprint is None:
print(f"Advertencia: clave del servidor no conocida.", file=sys.stderr)
print(f"Huella: {current_fingerprint}", file=sys.stderr)
print("¿Confiar en este servidor? (s/N): ", end="", file=sys.stderr)
if input().lower() != 's':
raise RuntimeError("Conexión abortada por el usuario")
save_server_fingerprint(host, port, current_fingerprint)
elif saved_fingerprint != current_fingerprint:
raise RuntimeError(
f"¡ALERTA DE SEGURIDAD!\n"
f"La huella del servidor ha cambiado.\n"
f"Guardada: {saved_fingerprint}\n"
f"Actual: {current_fingerprint}\n"
f"Posible ataque MITM."
)
encrypted_selector = client_session.encrypt(selector)
sock.sendall(encrypted_selector)
len_bytes = sock.recv(4)
if len(len_bytes) != 4:
raise ValueError("No se recibió longitud de respuesta")
response_len = int.from_bytes(len_bytes, 'big')
if response_len > MAX_RESPONSE_SIZE:
raise ValueError("Respuesta demasiado grande")
response_data = b""
while len(response_data) < response_len:
chunk = sock.recv(min(4096, response_len - len(response_data)))
if not chunk:
break
response_data += chunk
if len(response_data) != response_len:
raise ValueError("Respuesta incompleta")
content = client_session.decrypt(response_data)
return content, host, port, client_session
def play_animation_if_needed(base_selector: str, host: str, port: int, session: SecureSession):
"""
Reproduce animación si base_selector == '/anim'.
Se detiene al primer frame inexistente (detectado por contenido de error 404).
"""
if base_selector.rstrip('/') != '/anim':
return False
print("\033[90mIniciando animación...\033[0m", file=sys.stderr)
frame_count = 0
delay = 0.15
for i in range(100): # límite de seguridad
frame_selector = f"/anim/frame{i:02d}"
os.system('clear')
print(f"\033[90mCargando {frame_selector}...\033[0m", flush=True)
try:
# Nueva conexión segura para este frame
with socket.create_connection((host, port), timeout=5) as sock2:
new_session = SecureSession()
sock2.sendall(new_session.get_public_key_bytes())
server_pubkey = sock2.recv(32)
if len(server_pubkey) != 32:
raise ValueError("Clave pública del servidor inválida")
# Verificar huella (TOFU)
digest = hashes.Hash(hashes.SHA256())
digest.update(server_pubkey)
current_fingerprint = ":".join(f"{b:02x}" for b in digest.finalize())
saved_fingerprint = get_saved_fingerprint(host, port)
if saved_fingerprint != current_fingerprint:
raise RuntimeError("Huella del servidor cambió durante animación")
new_session.derive_shared_key(server_pubkey)
encrypted_selector = new_session.encrypt(frame_selector)
sock2.sendall(encrypted_selector)
# Recibir respuesta
len_bytes = sock2.recv(4)
if len(len_bytes) != 4:
raise ValueError("No se recibió longitud")
response_len = int.from_bytes(len_bytes, 'big')
if response_len > MAX_RESPONSE_SIZE:
raise ValueError("Respuesta demasiado grande")
response_data = b""
while len(response_data) < response_len:
chunk = sock2.recv(min(4096, response_len - len(response_data)))
if not chunk:
break
response_data += chunk
if len(response_data) != response_len:
raise ValueError("Respuesta incompleta")
content = new_session.decrypt(response_data)
# 🔍 DETECCIÓN DE ERROR 404
if "ERROR 404" in content or "Selector no encontrado" in content:
if i == 0:
print(f"\033[91mError: primer frame no disponible.\033[0m", file=sys.stderr)
return False
else:
# Fin natural: no hay más frames
time.sleep(0.3)
break
# Mostrar frame válido
print("\033[1A\033[2K", end="")
print(content, end="", flush=True)
frame_count += 1
except (ValueError, RuntimeError, socket.timeout, ConnectionRefusedError, OSError) as e:
if i == 0:
print(f"\033[91mError crítico al cargar el primer frame: {e}\033[0m", file=sys.stderr)
return False
else:
time.sleep(0.3)
break
except KeyboardInterrupt:
print("\n\033[90mAnimación interrumpida.\033[0m", file=sys.stderr)
return True
time.sleep(delay)
print(f"\n\033[92m✅ Animación completada ({frame_count} frames).\033[0m", file=sys.stderr)
return True
def main():
parser = argparse.ArgumentParser(description="Cliente Gopher 2.0: contenido dinámico + cifrado + animaciones")
parser.add_argument("url", help="URL en formato gopher://host:port/selector")
args = parser.parse_args()
try:
content, host, port, session = fetch_gopher2(args.url)
selector = urlparse(args.url).path or "/"
# Detectar si es una animación
if play_animation_if_needed(selector, host, port, session):
return # animación ya mostrada
# Si no es animación, mostrar contenido normal
print(content, end="")
except KeyboardInterrupt:
sys.exit("\nInterrumpido por el usuario.")
except Exception as e:
sys.exit(f"Error: {e}")
if __name__ == "__main__":
main()