This project is a local Python replica of the MTProto 2.0 record layer used for coursework demonstrations. It is not a Telegram client and it does not connect to Telegram. The goal is to show, in a controlled environment, how a message reordering attack works and how the paper's proposed mitigation stops it.
The implementation is based on the symmetric record-protocol discussion in:
Martin R. Albrecht, Lenka Marekova, Kenneth G. Paterson, Igors Stepanovs, "Four Attacks and a Proof for Telegram", IEEE S&P 2022 / ePrint 2023/469.
The paper's relevant modelling change is strict metadata validation: a receiver tracks a sequence number and accepts only the next expected value. This rules out reordering, replay, and drop manipulations at the record layer.
- MTProto-style encrypted envelopes:
auth_key_id || msg_key || AES-IGE(payload) msg_keyand AES key/IV derivation from the long-termauth_key- record payload metadata:
server_salt,session_id,msg_id,msg_seq_no,msg_length - an on-path MitM TCP proxy that buffers opaque encrypted frames and releases them in an attacker-chosen order
- a vulnerable server mode that verifies the ciphertext/MAC but does not
enforce
msg_seq_noordering - a patched
--strict-orderserver mode that accepts only the next expected sequence number and rejectsmsg_seq_novalues that are too low or too high
- The shared
auth_keyis hard-coded instead of negotiated with Telegram's Diffie-Hellman/RSA key exchange. - Transport framing is a plain 4-byte length prefix. Real MTProto uses transport variants such as abridged/obfuscated transport.
- The replica uses a simple content-message sequence convention:
2, 4, 6, 8, .... Real Telegram/TDLib has more nuanced parity rules for content-related and non-content-related messages. - Strict ordering state is per connection in the demo. A production server would maintain comparable state per session/authentication context.
- Server rejection replies are custom encrypted
ERR:<reason>messages. Real MTProto usesbad_msg_notification, including error code32formsg_seq_notoo low and33formsg_seq_notoo high.
crypto.py AES-IGE, msg_key, KDF, auth_key_id
protocol.py payload encoding, encryption, secure/vulnerable decrypt
transport.py length-prefixed TCP framing
client.py demo client and reusable Session class
server.py local MTProto-style server with --strict and --strict-order
reorder_proxy.py opaque MitM frame reorder proxy
reorder_attack.py presentation demo for attack and mitigation
timing_attack.py timing-side-channel demo from the same paper
fuzzer.py metadata/padding fuzzer
test_crypto.py crypto and decrypt-path tests
test_reorder.py reordering attack and strict-order tests
cd csec5619-vulnerability-mtproto2
python3 -m pytest -qExpected result:
18 passed
Run the complete attack demonstration:
cd csec5619-vulnerability-mtproto2
python3 reorder_attack.pyThe script runs three scenarios:
- Baseline: messages are delivered in order and all are accepted.
- Attack: the proxy releases encrypted frames as
M1, M4, M3, M2; the vulnerable server accepts all four, corrupting the transcript semantics. - Mitigation: the strict-order server accepts
seq=2, rejectsseq=8andseq=6as too high, then acceptsseq=4when it arrives. The corrupted transcript is prevented.
The proxy never decrypts or modifies messages. It only reorders valid encrypted frames, matching the on-path attacker model.
Start a vulnerable server:
python3 server.py --port 4444 --secureStart a strict-order server:
python3 server.py --port 4444 --secure --strict --strict-orderSend a single message:
python3 client.py --port 4444 "hello"Run a standalone reorder proxy:
python3 reorder_proxy.py --listen-port 5555 --target-port 4444 --perm 0,3,2,1Then point the client at port 5555.