Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,29 @@ on:

jobs:
test:
runs-on: ubuntu-latest
name: test (${{ matrix.python-version }})
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
include:
# 3.9 is EOL and has no build for ubuntu-latest (Ubuntu 26.04),
# so its row is pinned to the last runner image that ships one.
# The job name must stay "test (3.9)": it is a required status
# check in branch protection. Hence the explicit job `name`
# above -- the auto-generated matrix name would otherwise
# include the runner ("test (3.9, ubuntu-24.04)") and the
# required check would never be satisfied.
- python-version: "3.9"
runner: ubuntu-24.04
- python-version: "3.10"
runner: ubuntu-latest
- python-version: "3.11"
runner: ubuntu-latest
- python-version: "3.12"
runner: ubuntu-latest
- python-version: "3.13"
runner: ubuntu-latest
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ dhip 10.0.0.10 -u admin -P admin54321 ptz Left --duration 0.5
dhip 10.0.0.10 -u admin -P admin54321 snapshot out.jpg
dhip 10.0.0.10 -u admin -P admin54321 events --codes VideoMotion

# enable the vendor telnet shell and back up every raw MTD partition
# over it (no UART/TFTP/NFS needed); needs the camera root shell password
dhip 10.0.0.10 -u admin -P admin54321 nanddump ./backup --shell-pass ivideo

# flash a vendor upgrade package (Dahua "zzip") -- destructive: needs --confirm
dhip 10.0.0.10 -u admin -P admin54321 upgrade NC-IPTC2200_DL_4G-4_OpenIPC_gk7205v510.bin --confirm

# raw one-shot RPC (backward compatible with the original tool)
dhip 10.0.0.10 -u admin -P admin54321 -m magicBox.getSystemInfo
dhip 10.0.0.10 -u admin -P admin54321 -m configManager.getConfig --params '{"name":"General"}'
Expand Down Expand Up @@ -226,6 +233,10 @@ what is reconstructed and covered only by the offline test suite.
(`upgrader.prepare` → chunked `appendData` → `execute`) flashed an OpenIPC
package onto a GK7205V510 end-to-end and rebooted into it. **Destructive** —
requires `confirm=True` and a package built for the exact target.
- Full NAND backup over the vendor telnet shell: `nanddump` (the CLI enables
telnet via DHIP, then reads every raw `/dev/mtdNro` with `dd | base64` and
verifies each size against `/proc/mtd`) reproduced a full 128 MiB vendor
flash byte-exact on a GK7205V510. Root shell account required.

**Reconstructed / covered by the offline tests only**

Expand Down
99 changes: 98 additions & 1 deletion dahua/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
dhip HOST ptz Left --duration 0.5
dhip HOST snapshot out.jpg
dhip HOST events --codes VideoMotion
dhip HOST nanddump ./backup --shell-pass <rootpass>
dhip HOST upgrade coupler.bin --confirm
"""

from __future__ import annotations

import argparse
import json
import sys
import time

from . import const
from .client import DahuaClient
Expand Down Expand Up @@ -105,6 +108,31 @@ def build_parser() -> argparse.ArgumentParser:
p_tel = sub.add_parser("telnet", help="enable/disable the built-in telnet server")
p_tel.add_argument("state", nargs="?", choices=("on", "off"),
help="omit to read the current state")

p_nd = sub.add_parser("nanddump",
help="back up every raw MTD partition over the "
"vendor telnet shell (no UART/TFTP needed)")
p_nd.add_argument("output", help="output directory for mtdN-<name>.bin files")
p_nd.add_argument("--telnet-port", type=int, default=23)
p_nd.add_argument("--shell-user", default="root",
help="telnet shell login (camera root account)")
p_nd.add_argument("--shell-pass", default="",
help="telnet shell password (often the same as serial root)")
p_nd.add_argument("--parts", metavar="LIST",
help="comma-separated MTD indices (default: all raw partitions)")
p_nd.add_argument("--no-enable-telnet", action="store_true",
help="do not enable telnet via DHIP first (already enabled)")

p_up = sub.add_parser("upgrade",
help="flash a vendor upgrade package (Dahua zzip) over DHIP")
p_up.add_argument("package", nargs="?",
help="package file, e.g. the coupler .bin from the releases")
p_up.add_argument("--confirm", action="store_true",
help="acknowledge the brick risk and proceed")
p_up.add_argument("--type", dest="fw_type", default="System",
help="upgrader type parameter (default: System)")
p_up.add_argument("--state", action="store_true",
help="only query the current upgrader state (read-only)")
return ap


Expand Down Expand Up @@ -217,6 +245,74 @@ def main(argv: list[str] | None = None) -> int:
else:
cam.set_telnet(False)
print("[+] telnet disabled", file=sys.stderr)
elif cmd == "nanddump":
from .nanddump import backup
# Only touch the device config when needed, and put it back:
# telnet is a root shell -- do not leave it enabled behind us.
was_on = None
enabled_by_us = False
try:
was_on = cam.telnet_enabled()
except Exception:
pass # unreadable state: assume off
if not args.no_enable_telnet:
if not was_on:
cam.set_telnet(True)
enabled_by_us = True
print("[+] telnet enabled via DHIP", file=sys.stderr)
else:
print("[-] telnet already enabled", file=sys.stderr)
try:
parts = ([int(p) for p in args.parts.split(",") if p != ""]
if args.parts else None)
t0 = time.time()
res = backup(args.host, args.output, port=args.telnet_port,
user=args.shell_user, password=args.shell_pass,
parts=parts)
total = sum(r["bytes"] for r in res)
print(f"[+] dumped {len(res)} partitions, {total} bytes "
f"in {time.time() - t0:.0f}s -> {args.output}",
file=sys.stderr)
finally:
if enabled_by_us:
try:
cam.set_telnet(False)
print("[+] telnet disabled again (was off before)",
file=sys.stderr)
except Exception as exc:
print(f"!! could NOT disable telnet again: {exc}\n"
" Disable it yourself: dhip <host> telnet off",
file=sys.stderr)
elif cmd == "upgrade":
if args.state:
if args.package:
print("--state is a read-only probe; drop the package "
"argument to flash", file=sys.stderr)
return 2
try:
_print(cam.firmware_state())
return 0
except Exception as exc: # many firmwares lack getState
print(f"[-] upgrader.getState failed: {exc}", file=sys.stderr)
return 1
if args.package is None:
print("nothing to do: pass a package (with --confirm) or "
"--state", file=sys.stderr)
return 2
if not args.confirm:
print("refusing to flash without --confirm: a wrong package "
"bricks the device. Take an `nanddump` backup first.",
file=sys.stderr)
return 1
resp = cam.upgrade_firmware(
args.package, confirm=True, fw_type=args.fw_type,
progress=lambda done, total: print(
f"\r[+] upload {done}/{total} bytes", end="",
file=sys.stderr))
print(file=sys.stderr)
_print(resp)
print("[+] upgrade started; the device writes it out and reboots "
"itself (give it a few minutes)", file=sys.stderr)
elif cmd == "events":
listener = cam.events(codes=args.codes)
print(f"[+] listening for events {args.codes} (Ctrl-C to stop)",
Expand All @@ -227,7 +323,8 @@ def main(argv: list[str] | None = None) -> int:
listener.stop()
else:
print("nothing to do; pass -m METHOD or a subcommand "
"(info/config/users/ptz/snapshot/events/telnet)", file=sys.stderr)
"(info/config/users/ptz/snapshot/events/telnet/nanddump)",
file=sys.stderr)
return 2
return 0

Expand Down
Loading
Loading