Linux RAT · Malware analysis
Heyiwei / LTM2
A bespoke, encrypted command-and-control backdoor — reverse-engineered from the wire format down to the key schedule.
Heyiwei is a ground-up custom, multi-architecture Linux backdoor that spreads to IoT and Linux hosts through an ADB/telnet stager. It is not Mirai or Gafgyt. It carries its own named, fully engineered encrypted transport — LTM2 — built from X25519 key exchange, a keyed-BLAKE2b key schedule, and XChaCha20-Poly1305 authenticated records. It had run roughly a year with no public documentation and near-zero antivirus coverage before it was caught.
Why it isn’t Mirai
The generic trojan.mirai label is exactly what lets a serious custom implant
pass as commodity noise. The distinction is structural, not stylistic: Heyiwei shares no strings, config
format, or attack-table layout with either common family. Where they use a byte-XOR table or plaintext
ASCII, it uses real asymmetric cryptography.
| Trait | Mirai | Gafgyt / Bashlite | Heyiwei / LTM2 |
|---|---|---|---|
| Config | single-byte XOR table | plaintext | X25519 + BLAKE2b schedule |
| C2 transport | near-plaintext TLV | plaintext ASCII | XChaCha20-Poly1305 AEAD |
| Command set | DDoS attack table | flood methods | shell / exec / push / pull |
| Shared strings | yes, well-known | yes, well-known | none |
Automated triage first flagged “Speck / Salsa20 / RC4.” Those were false positives — Salsa20’s quarter-round is structurally identical to ChaCha’s, and the “RC4” 256-byte buffer is locale parsing, not a cipher. The real stack, confirmed by the constants and the label strings left in the clear, is X25519 + XChaCha20-Poly1305 + keyed BLAKE2b — the shape of Monocypher.
The LTM2 handshake
LTM2 is a compact, Noise-flavored channel: ephemeral elliptic-curve key exchange, a transcript-bound key schedule, then per-direction authenticated records. The whole protocol was recovered from the un-obfuscated x86-64 build and cross-checked against bytes captured from a running sample in an isolated lab.
Client-Hello byte layout
Confirmed against a live 104-byte capture — the sample resolved its C2 to a lab sink and sent this before any reply:
| Offset | Len | Field |
|---|---|---|
| 0 | 8 | "LTM2CHLO" magic |
| 8 | 30 | bot-ID — ASCII hex token from getrandom, fresh per infection |
| 38 | 2 | 00 00 — pad to 32 |
| 40 | 32 | nonce |
| 72 | 32 | X25519 ephemeral public key |
The identity field is the human-readable per-device bot-ID, null-padded — not an
opaque blob. The Server-Hello mirrors it at 72 bytes:
"LTM2SHLO" | nonce[32] | server_pub[32].
Where “bespoke” stops being an opinion
This is the heart of the family. Below is the key derivation straight out of the decompiled x86-64 build — decompiler names kept, primitives annotated. The shared secret is used as the key of a keyed hash, and the entire client-hello and server-hello are absorbed into the master, binding every session key to the exact handshake transcript.
/* master = BLAKE2b( key = X25519 shared secret,
data = "LTM2 session master" || CHLO || SHLO ) */
FUN_00407587(ctx, 0x20, shared, 0x20); // keyed init, key = 32-byte shared secret
FUN_004076c7(ctx, "LTM2 session master", 0x13); // absorb 19-byte label
FUN_004076c7(ctx, chlo, 0x68); // absorb the full 104-byte CHLO
FUN_004076c7(ctx, shlo, 0x48); // absorb the full 72-byte SHLO
FUN_004076d6(ctx, master); // finalize -> 32-byte session master
/* two 48-byte directional secrets, each keyed by the master */
FUN_0040775b(c2s, 0x30, master, 0x20, "LTM2 client to server", 0x15); // -> key[32] | iv[16]
FUN_0040775b(s2c, 0x30, master, 0x20, "LTM2 server to client", 0x15); // -> key[32] | iv[16]
A clean-room reimplementation reproduces it exactly — same labels, same transcript binding, same split into a 32-byte key and 16-byte IV per direction:
master = blake2b(b"LTM2 session master" + chlo + shlo, key=shared, digest_size=32)
c2s = blake2b(b"LTM2 client to server", key=master, digest_size=48)
s2c = blake2b(b"LTM2 server to client", key=master, digest_size=48)
# key, iv = dir[:32], dir[32:48] (per direction)
This is competent, modern cryptographic engineering — the opposite of a reused XOR byte, and nothing any IoT-bot family ships.
Framing and the application layer
Every post-handshake byte is one AEAD record. Header integers are big-endian — the single ordering that makes the whole thing validate — and the 12-byte header is reused verbatim as the authenticated associated data.
record = [ len:u32 BE ] [ seq:u64 BE ] [ tag:16B Poly1305 ] [ XChaCha20( type || payload ) ]
└──────── 12-byte header = AEAD associated data ────────┘
len = len(payload) + 1 // +1 is the type byte
nonce = dir_IV[0:16] || seq(BE,8) // HChaCha20 subkey from IV, 8-byte inner nonce
seq per-direction, from 0, strictly incrementing // anti-replay
Inside the AEAD the application protocol is binary: one type byte, then a payload. The design is SSH-connection-shaped — channels, windows, exit status — not a botnet’s flat command list. Only one channel is open at a time.
| Type | Dir | Meaning | Payload |
|---|---|---|---|
| 0x01 | c→s | beacon | host=… arch=… kernel=… uid=… |
| 0x02 | s→c | ack (required or bot aborts) | empty |
| 0x03/04 | s→c / c→s | ping / pong | empty |
| 0x0A | s→c | open shell — PTY /bin/sh -i | 8-byte winsize |
| 0x0B | s→c | open exec — /bin/sh -c | command, NUL-term |
| 0x0C | s→c | open push — write file | [hdr][dest\0] |
| 0x0D | s→c | open pull — read file | src path\0 |
| 0x14 | both | data | raw channel bytes |
| 0x16/17 | s→c | file-commit / eof | size + BLAKE2b-256 |
| 0x19 | c→s | channel exit | 4-byte status |
The break
The key schedule was not guessed — it was proven. A lab LTM2 server (pure reimplementation of X25519, keyed BLAKE2b, and XChaCha20-Poly1305, self-tested against RFC 7748 / 8439 and the XChaCha vectors) was stood up to talk only to a captured sample in an isolated victim VM. It completed the handshake, derived the session keys, and read the traffic.
The Poly1305 tag verified on the very first record.
A wrong key schedule fails the MAC. A first-try verification means every label, length, endianness, and nonce construction is exactly right — the whole scheme is recovered.
seq=0 type=1 (beacon) len=61
host=<hostname> arch=i686 kernel=5.4.0-216-generic uid=0\x00
All four services were then exercised end-to-end against the sample: interactive shell, one-shot exec, and file push/pull — the last two each carrying an unkeyed BLAKE2b-256 integrity trailer, with push staged to a temp name and atomically renamed only after the hash checks out.
A four-service RAT — and no flood code
The decrypted channel offers exactly four services: interactive shell, one-shot exec, file push, and file pull. The “no DDoS” finding is confirmed, not assumed — a complete inventory of every socket call site found only C2 TCP, the DNS resolver, and one local Unix socket:
// socket() call sites, x86-64 build — complete inventory
no SOCK_RAW no IP_HDRINCL no bind / listen / accept
no flood loop no attack dispatch no rival-bot kill-list
only: C2 TCP · DNS resolver (UDP/TCP) · one AF_UNIX
Port 25565 is simply the C2 port — it blends with Minecraft traffic and the
mc. subdomain is camouflage, not evidence of a booter. The net threat is heavier
than a flood bot: a persistent, encrypted, interactive foothold with arbitrary file read and write on
every infected device.
Tradecraft
Static, stripped ELF built with GCC 15.2.0 on a current 2025 Ubuntu toolchain, in 11 architectures (x86-64, i686, i586, aarch64, ARM v4l–v7l, MIPS, MIPSEL). The x86-64 build was not string-obfuscated the way the 32-bit builds are — which is how the config, the persistence paths, and the LTM2 label strings were readable in the first place. The loader calls itself “Heyiwei stager v3”; its author’s own English developer comments reference a prior run on a 54-device list and a MIPS-endianness bug they fixed — a competent, iterating operator.
Persistence is comprehensive and multi-init: systemd, SysV + rcS, OpenRC, OpenWrt
rc.common, and a custom control script. It installs to
/usr/local/bin/Heyiwei, single-instances on /tmp/Heyiwei.lock,
and masquerades in the process table as the kernel thread khungtaskd.
Signatures
The two 8-byte handshake magics are the one reliable fixed marker on an otherwise fully encrypted channel. Port-agnostic, since the C2 can move off 25565. The CHLO rule was confirmed exact against a live 104-byte Client-Hello.
alert tcp any any -> any any (msg:"Heyiwei/LTM LTM2 C2 client-hello";
flow:established,to_server; dsize:104; content:"LTM2CHLO"; depth:8;
classtype:trojan-activity; sid:9500001; rev:1;)
alert tcp any any -> any any (msg:"Heyiwei/LTM LTM2 C2 server-hello";
flow:established,to_client; dsize:72; content:"LTM2SHLO"; depth:8;
classtype:trojan-activity; sid:9500002; rev:1;)
A passive dissector parses the handshake and walks the AEAD framing for triage. It cannot decrypt — session keys are per-connection ephemerals — but it reliably identifies and structures an LTM2 stream from a single direction of capture.
Indicators of compromise
- C2
- mc.heyiwei.net · TCP 25565 (apex heyiwei.net parked 185.53.179.146 / .128, 199.59.243.228; NS *.dyna-ns.net)
- Protocol magic
- LTM2CHLO (104 B → server) · LTM2SHLO (72 B → client)
- Host artifacts
- /usr/local/bin/Heyiwei · /tmp/Heyiwei.lock · /etc/ltm/Heyiwei-control · Heyiwei.service · env HEYIWEI_SERVICE=1 · proc name khungtaskd
- Delivery
- loader on :8080 colocated with the C2; second loader 143.246.56.120:8080; delivered by 45.144.52.239 (H2NEXUS Cloud, UAE)
- Sample · x86-64
- 62e2b8a6c75cde2ec395c34ac5055be0d0c51d796e93d37b2f1681a833693896
- Sample · i686
- cc1b33edd4b08c51884b3e8da0d1536fa531a7119e6ab08e5d3a29b0c91188e5