Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

MTProto 2.0 Record-Layer Replica

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.

What Is Replicated

  • MTProto-style encrypted envelopes: auth_key_id || msg_key || AES-IGE(payload)
  • msg_key and AES key/IV derivation from the long-term auth_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_no ordering
  • a patched --strict-order server mode that accepts only the next expected sequence number and rejects msg_seq_no values that are too low or too high

Important Simplifications

  • The shared auth_key is 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 uses bad_msg_notification, including error code 32 for msg_seq_no too low and 33 for msg_seq_no too high.

Project Layout

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

Run Tests

cd csec5619-vulnerability-mtproto2
python3 -m pytest -q

Expected result:

18 passed

Presentation Demo

Run the complete attack demonstration:

cd csec5619-vulnerability-mtproto2
python3 reorder_attack.py

The script runs three scenarios:

  1. Baseline: messages are delivered in order and all are accepted.
  2. Attack: the proxy releases encrypted frames as M1, M4, M3, M2; the vulnerable server accepts all four, corrupting the transcript semantics.
  3. Mitigation: the strict-order server accepts seq=2, rejects seq=8 and seq=6 as too high, then accepts seq=4 when 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.

Manual Demo Commands

Start a vulnerable server:

python3 server.py --port 4444 --secure

Start a strict-order server:

python3 server.py --port 4444 --secure --strict --strict-order

Send 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,1

Then point the client at port 5555.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages