mirror of
https://github.com/Flowseal/tg-ws-proxy.git
synced 2026-11-22 15:36:30 +03:00
Fake TLS: маскировать повторённый ClientHello (#1532)
This commit is contained in:
@@ -10,6 +10,7 @@ import struct
|
||||
import time
|
||||
import logging
|
||||
|
||||
from collections import OrderedDict
|
||||
from typing import Dict, FrozenSet, NamedTuple, Optional, Tuple
|
||||
from .stats import stats
|
||||
from .utils import set_x25519
|
||||
@@ -72,6 +73,43 @@ _SH_RANDOM_OFF = 11
|
||||
# set once at startup by probe_appdata_len()
|
||||
_profiled_appdata_len: Optional[int] = None
|
||||
|
||||
# A ClientHello passes the timestamp check for TIMESTAMP_TOLERANCE on either
|
||||
# side of its creation, plus up to a second because the check truncates the
|
||||
# clock; remembering its random this long refuses any replay
|
||||
REPLAY_WINDOW = 2 * TIMESTAMP_TOLERANCE + 1
|
||||
# Only hellos that passed the HMAC check with the secret are stored, so without
|
||||
# the secret nobody can flood the cache to evict an entry early; reaching the
|
||||
# limit takes this many genuine handshakes within one window
|
||||
REPLAY_CACHE_SIZE = 65536
|
||||
|
||||
|
||||
class _ReplayCache:
|
||||
"""Client randoms of accepted ClientHellos; a repeat is a replay probe.
|
||||
|
||||
One cache per process on purpose: it survives proxy restarts, so a hello
|
||||
accepted just before a restart cannot be replayed right after it.
|
||||
"""
|
||||
|
||||
def __init__(self, window: float = REPLAY_WINDOW, size: int = REPLAY_CACHE_SIZE):
|
||||
self.window = window
|
||||
self.size = size
|
||||
self._expiry: OrderedDict = OrderedDict()
|
||||
|
||||
def first_seen(self, client_random: bytes) -> bool:
|
||||
now = time.monotonic()
|
||||
# Insertion order is expiry order, so stale entries are at the front
|
||||
while self._expiry and next(iter(self._expiry.values())) <= now:
|
||||
self._expiry.popitem(last=False)
|
||||
if client_random in self._expiry:
|
||||
return False
|
||||
if len(self._expiry) >= self.size:
|
||||
self._expiry.popitem(last=False)
|
||||
self._expiry[client_random] = now + self.window
|
||||
return True
|
||||
|
||||
|
||||
replay_cache = _ReplayCache()
|
||||
|
||||
|
||||
class ClientHelloInfo(NamedTuple):
|
||||
cipher_suites: FrozenSet[int]
|
||||
|
||||
@@ -28,7 +28,7 @@ from .raw_websocket import set_sock_opts
|
||||
from .fake_tls import (
|
||||
proxy_to_masking_domain, verify_client_hello, parse_client_hello, build_server_hello,
|
||||
probe_appdata_len, set_profiled_appdata_len, FakeTlsStream, TLS_RECORD_HANDSHAKE,
|
||||
SERVER_HELLO_DELAY,
|
||||
SERVER_HELLO_DELAY, replay_cache,
|
||||
)
|
||||
from .balancer import balancer
|
||||
from .pool import ws_pool, cf_worker_pool
|
||||
@@ -157,11 +157,16 @@ async def _read_client_init(reader, writer, secret, label, masking):
|
||||
client_hello = tls_header + record_body
|
||||
|
||||
tls_result = verify_client_hello(client_hello, secret)
|
||||
# A recorded hello stays valid for minutes; answering it again would
|
||||
# tell an active prober that this is a proxy, not the masking site
|
||||
replayed = (tls_result is not None
|
||||
and not replay_cache.first_seen(tls_result[0]))
|
||||
|
||||
if tls_result is None:
|
||||
log.debug("[%s] Fake TLS verify failed (size=%d rec=%d) "
|
||||
if tls_result is None or replayed:
|
||||
log.debug("[%s] Fake TLS %s (size=%d rec=%d) "
|
||||
"-> masking",
|
||||
label, len(client_hello), record_len)
|
||||
label, "hello replayed" if replayed else "verify failed",
|
||||
len(client_hello), record_len)
|
||||
await proxy_to_masking_domain(
|
||||
reader, writer, client_hello, masking, label)
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user