Skip to content

telnet_opener: add dvrHelper 9530 telnet gate (no-reboot route for XM DVRs/NVRs) - #16

Merged
widgetii merged 3 commits into
masterfrom
xmtelnet-9530-opener
Nov 30, 2026
Merged

widgetii merged 3 commits into
masterfrom
xmtelnet-9530-opener

Conversation

@widgetii

Copy link
Copy Markdown
Member

Add 9530 dvrHelper telnet gate to telnet_opener (verified on HI3520DV200)

What

XM DVR/NVR firmware ships a second telnet-enable mechanism besides the
InstallDesc exploit: dvrHelper listens on TCP 9530 and spawns
telnetd xmforever (root shell on port 23) for any caller that passes its
challenge-response. This PR adds that route to telnet_opener.py as the
preferred, no-reboot path whenever the device has the listener, with
the new self-contained xmtelnet.py module carrying the crypto/protocol.

Why prefer it:

  • No reboot — InstallDesc + telnetctrl needs a device reboot; the
    9530 gate spawns telnetd immediately.
  • No DVRIP credentials — the handshake only needs the OEM password,
    which is a firmware constant (2wj9fsa2) unless the vendor put one in
    /mnt/custom/TelnetOEMPasswd. Overridable with --xm-passwd.
  • Works on NVR firmware where the SkipCheck InstallDesc path is
    unavailable (tested on an NBD6804T-F / HI3520DV200 DVR).

The crypto (xmtelnet.py)

The gate uses XM's in-house DES variant, not textbook 3DES — a naive
"standard DES" implementation fails the verification:

  • All eight tables (E, PC1, PC2, S-boxes, P, IP, FP, shifts) are textbook —
    verified byte-for-byte against dvrHelper's rodata in the XM busybox.
  • But bit cells are unpacked/packed LSB-first ((byte >> i) & 1),
    S-box output nibbles are also stored LSB-first, and there is no final
    half-swap before FP
    .
  • EDE keying with K1 = randNum, K2 = passwd (combined key > 8 bytes, else
    the vendor's own degradation to single DES with K1 is reproduced).

The implementation was derived from the dvrHelper applet disassembly
(sub_2BB28/sub_2BC60/sub_2BD98/sub_2BE88) and proved bit-exact against a
live QEMU twin of the stock NBD6804T-F firmware: it reproduces the
server's own E-expansion scratch cells from a guest RAM dump, and the
embedded known-answer test (self_test()) carries ground truth captured
on the wire.

Protocol notes baked into the code

Every server frame is a single segment with an exact length and no
terminator
(verify:OK is 9 bytes — reading "up to newline" or past the
frame length deadlocks into the server's 15 s select timeout and kills
the session).

Two firmware properties the tooling must respect:

  1. A single wrong OEM password disables the 9530 listener until the
    device reboots
    (the accept thread exits, no respawn). The tool
    never blind-retries and prints an explicit warning on verify:ERROR.
  2. If the device's WAN check succeeds (it pings 114.114.114.114 /
    8.8.8.8 / 180.76.76.76), the gate answers Refuse:Internet and the
    listener survives
    — that state is retryable once the addresses are
    blocked.

Changes

  • xmtelnet.py (new): stdlib-only XM3DES + 9530 handshake, runnable
    standalone (python3 xmtelnet.py <ip>), KAT-protected self-test.
  • telnet_opener.py: enable_telnet_via_9530() probes for the gate and
    runs the handshake; wired into the default flow, -b/--backup flow,
    and --xm-passwd option; xc3511 added to the root credentials (the
    telnetd xmforever login checks the DES-crypt field of
    /etc/xmtelnetdpw — absxcfbgXtb3o == crypt("xc3511", "ab") on the
    tested unit). Also includes the previously-staged fix: telnetd in
    daemon mode so the port probe can't kill the listener.
  • README.md / CLAUDE.md: document the route and its caveats.

Testing

  • python3 xmtelnet.py — KAT only, offline.
  • Live end-to-end against the QEMU twin booted from a real NBD6804T-F
    flash dump: verify:OK → Open:OK → root ash on port 23, both via
    xmtelnet.py standalone and via python3 telnet_opener.py <ip> with
    no flags (dvrHelper route chosen automatically, no reboot).
  • ruff check --select E9,F63,F7,F82 --exclude examples --target-version py310 . — clean (matches CI).

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

Add no-reboot XM dvrHelper telnet gate

✨ Enhancement 📝 Documentation 🕐 40+ Minutes

Grey Divider

AI Description

• Adds an XM3DES client and TCP 9530 challenge-response for dvrHelper devices.
• Prefers the no-reboot gate before the existing DVRIP InstallDesc fallback.
• Documents password, WAN-check, and listener-lockout caveats for supported DVR/NVR firmware.
Merge Risk 🟡 Medium

Blast radius 🟡 Medium 💥 2 capabilities

  • 2 affected capabilities within one bounded functional scope: XM telnet/root-access integration.
  • The 9530 handshake, port discovery, and DVRIP InstallDesc fallback all implement the same end-to-end outcome: making a root telnet shell available.
  • They are alternate routes within one bounded access integration, not independent deployment capabilities.
  • Backup is a distinct user-visible operation from merely enabling telnet, but it remains within the same bounded integration because it consumes the shell produced by the access routes and then runs the existing mount, ipctool, and cleanup sequence.
Behavioral complexity 🔴 High 🧩 Multiple interactions

  • Before, telnet enablement used the existing DVRIP/InstallDesc flow and foreground telnetd command.
  • After, it adds a custom XM3DES protocol, fixed-length multi-step socket exchange, WAN-refusal handling, one-shot listener-disable semantics, port discovery, fallback routing, and daemonized telnetd.
  • New correctness conditions are that challenge and command frames use the vendor transformation, each reply length/state is consumed in order, refusal is distinguished from listener-killing verification failure, and fallback does not retry a destructive authentication state.
  • Before: one reboot-based route and a foreground daemon.
  • After: a no-reboot route precedes it and the fallback daemon detaches.
  • New correctness condition: each device transition must leave either a usable telnet endpoint or a safe, non-retrying fallback without misclassifying partial protocol states.
Sources

xmtelnet.py[364-377]

        ct = xm_encrypt(CMD_PLAINTEXT, rand, passwd)
        s.sendall(_frame(b"CMD:" + ct))
        # The server pings 114.114.114.114 / 8.8.8.8 / 180.76.76.76
        # (`ping -w 2` each) before the verdict: allow for three
        # two-second waits plus slack.
        resp = _recv_exact(s, 7, 30)               # "Open:OK" (7 bytes)
        if resp.startswith(b"Refuse:"):            # "Refuse:Internet" (15)
            resp += _recv_exact(s, 8, timeout)
        if verbose:
            print(f"[9530] <- {resp!r}")
        if resp == b"Open:OK":
            if verbose:
                print("[9530] Open:OK -- telnetd is up on port 23")
            return True
Potential damage 🟡 Medium 🩹 Temporary loss

  • If the changed access route regresses, users may be unable to obtain the intended root telnet shell.
  • Connection-level 9530 failures have a documented DVRIP fallback, but a wrong OEM response can leave the listener disabled until reboot.
  • Code rollback restores the prior client route, but a device listener disabled by a wrong digest requires reboot; no automatic repair worker is present.
Assessed commit 0f05972
Diagram

graph TD
  A["telnet_opener CLI"] --> B["9530 gate probe"] --> C["xmtelnet handshake"] --> D["dvrHelper :9530"] --> E["telnetd :23"]
  C --> F{"Gate accepted?"}
  F -->|"no"| G["DVRIP fallback"]
  F -->|"yes"| E
Loading
High-Level Assessment

The preferred-first gate with an explicit DVRIP fallback is appropriate: it avoids reboots and DVRIP credentials where dvrHelper exists, while preserving compatibility with firmware that lacks the listener. A standalone stdlib implementation is justified because the protocol uses a nonstandard DES variant and requires precise frame handling; blindly retrying authentication must remain avoided because firmware disables the listener after one bad digest.

Files changed (4) +476 / -4

Enhancement (2) +435 / -3
telnet_opener.pyPrefer the dvrHelper telnet gate +68/-3

Prefer the dvrHelper telnet gate

• Integrates the TCP 9530 handshake into standard and backup flows before the InstallDesc fallback. Adds an OEM password option and the xmforever telnet credential, and runs fallback telnetd in daemon mode so probes do not terminate it.

telnet_opener.py

xmtelnet.pyImplement XM3DES and the 9530 protocol +367/-0

Implement XM3DES and the 9530 protocol

• Introduces a stdlib-only implementation of XM's LSB-first DES variant, challenge-response framing, explicit unsafe-password and WAN-refusal handling, and a known-answer self-test. Supports standalone handshake execution.

xmtelnet.py

Documentation (2) +41 / -1
CLAUDE.mdDocument the dvrHelper 9530 route +11/-0

Document the dvrHelper 9530 route

• Adds contributor guidance describing the hidden dvrHelper listener, XM3DES behavior, no-reboot preference, and firmware lockout/WAN-check caveats.

CLAUDE.md

README.mdDocument no-reboot DVR/NVR telnet enablement +30/-1

Document no-reboot DVR/NVR telnet enablement

• Adds user-facing instructions for the port 9530 route, automatic credential handling, custom OEM passwords, and retry safety constraints.

README.md

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. The documented command skips the new gate ✓ Resolved Bug Correctness
Description
The new README example passes -t, but open_telnet() calls enable_telnet_via_9530() only when
make_telnet is false. Following that example instead requires a DVRIP login and enters the
firmware-upgrade path, which can prompt for a downgrade rather than open telnet through port 9530.


Remediation recommended

2. A custom port list discards a working gate ✓ Resolved Bug Correctness
Description
enable_telnet_via_9530() probes only the caller's ports after a successful handshake, although
the gate starts telnet on port 23. With --ports set to a list excluding 23, Open:OK is treated
as failure and the default or backup flow proceeds to the rebooting DVRIP fallback.

3. Gate connection errors abort the opener ✓ Resolved Bug Reliability
Description
open_telnet_9530() lets connection and socket I/O errors escape instead of returning a failed
handshake result. If the listener disappears after the separate port probe, or a connection resets
during the exchange, the default and backup flows terminate rather than reaching their DVRIP
fallback.

4. Split gate replies can fail authentication ✓ Resolved Bug Reliability
Description
_recv_frame() reads at most two chunks and gives the second chunk only 0.3 seconds to arrive,
rather than collecting the known-length reply. If TCP splits a valid challenge or verification
response with a longer delay, the handshake treats the partial bytes as malformed and aborts.

View medium (1)
5. Short custom passwords crash the opener ✓ Resolved Bug Reliability
Description
_schedules() selects the two-key path for any nonempty password but passes a short second key into
_key_schedule(), whose permutation indexes eight bytes. Supplying a one-to-seven-byte value
through --xm-passwd raises IndexError during digest generation instead of producing a controlled
validation error.

Grey Divider

Tip of the day
💡 Did you know, you can hide the parts of a finding you never read, like the evidence or the agent prompt

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@widgetii

Copy link
Copy Markdown
Member Author

Addressed all five review findings in c3d7cd7:

  1. -t bypassed the gate — the make_telnet flow now probes the 9530 gate first too (same root shell, no InstallDesc upload, no reboot); InstallDesc port 4321 remains the fallback for gate-less cameras. The README example under the gate section is corrected as well.
  2. custom --ports without 23 — dvrHelper always spawns telnetd on the vendor default port 23, so port 23 is now probed explicitly after Open:OK regardless of --ports, then the user's list.
  3. uncaught socket errors — connection refused, resets and mid-handshake drops are caught in open_telnet_9530 and returned as False, so callers fall back to their other routes instead of crashing.
  4. truncated split replies — replies are read to their protocol-fixed lengths (16B randNum:…, 9B/12B verify:…, 7B/15B verdict) with a reassembling recv loop, and never past the frame (which would block into the server's 15 s select timeout). Verified against a mock server that splits every reply across segments, including verify:OK as 5+4 bytes.
  5. short --xm-passwd crash — the vendor zero-pads the combined key material to 16 bytes (sub_2BB28: memset/memcpy min(len,16)); the client now reproduces that exactly, and self_test covers 1-3 byte passwords.

KAT digest unchanged (a8ee9fd23ffd7d9a), ruff clean with the CI's rule set.

… DVRs)

XM DVR/NVR firmware ships dvrHelper with a hidden listener on TCP 9530
that spawns "telnetd xmforever" (root on port 23) after an XM3DES
challenge-response. New xmtelnet.py implements the vendor's in-house
DES variant (textbook tables, LSB-first bit cells, no final half-swap)
plus the wire protocol; telnet_opener.py now probes for the gate first
and prefers it when present: no reboot, no DVRIP credentials, and it
works on NVR firmware where the SkipCheck InstallDesc path is absent.

One wrong OEM password disables the listener until reboot (never
blind-retried; --xm-passwd for customized units); a reachable WAN makes
the gate answer Refuse:Internet, which is retryable. Root password
xc3511 added for the xmforever telnetd (checked against /etc/xmtelnetdpw
DES-crypt field).

Verified end-to-end on a QEMU twin of the stock NBD6804T-F (HI3520DV200)
firmware: verify:OK -> Open:OK -> root shell on :23.
Review findings fixed:

1. `-t` bypassed the 9530 gate described in the README example. The
   make_telnet flow now probes the gate first as well (same root shell,
   no InstallDesc upload, no reboot); InstallDesc port 4321 remains the
   fallback for gate-less cameras. README example corrected.
2. A custom --ports list without 23 discarded a working gate: dvrHelper
   always spawns telnetd on the vendor default port 23, so 23 is probed
   explicitly after Open:OK regardless of --ports, then the user list.
3. Socket errors (refused connect, resets, mid-handshake drops) aborted
   the whole opener; they are now caught and returned as False so the
   caller falls back to InstallDesc instead of crashing.
4. The hand-rolled reply reader could truncate frames split across
   TCP segments. Replies are now read to their protocol-fixed lengths
   (16B randNum, 9B/12B verify, 7B/15B verdict) with a reassembling
   recv loop and never past the frame, which would have blocked into
   the server's 15 s select timeout.
5. Short OEM passwords crashed the key setup: dvrHelper zero-pads the
   combined key material to 16 bytes (sub_2BB28 memset/memcpy), so the
   client now does the same; self_test covers 1-3 byte passwords.

Validated against a fragmenting mock server (all replies split across
segments, incl. verify:OK as 5+4) plus the negative paths: verify:ERROR,
garbage, Refuse:Internet, refused connection. KAT unchanged. Ruff clean.
@widgetii
widgetii force-pushed the xmtelnet-9530-opener branch from c3d7cd7 to 0f05972 Compare November 30, 2026 17:25
@widgetii
widgetii merged commit ab17910 into master Nov 30, 2026
6 checks passed
@widgetii
widgetii deleted the xmtelnet-9530-opener branch November 30, 2026 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant