Skip to content

Linux shell over a console

LinuxShell works with a Linux console on any tube (serial, telnet, adb shell through process): it recognises the prompt, logs in, runs commands with reliable output framing and exit codes, and moves files in both directions over a plain text console.

from hwntools import *

sh = LinuxShell(serial("/dev/ttyUSB0", 115200))
if sh.detect() == "login":
    sh.login("root", "")
print(sh.run("id"))
shadow = sh.read_file("/etc/shadow")

Every snippet here also runs against FakeLinuxShell from the simulator.

What is on the console?

detect() returns one of "root", "user", "login", "password" or "unknown". It looks at output that is already waiting first, so a fresh login: prompt is recognised without sending anything (an Enter there would count as a failed login); otherwise it pokes the console with Enter. LinuxShell.classify(output) does the same classification on bytes you already have.

Log in

ok = sh.login("root", "admin")         # True when a shell prompt follows

login() copes with a stale Password: prompt and with consoles that log users in without a password. It returns False on Login incorrect.

To check a short list of common embedded default credentials (root with an empty password, root:root, admin:admin, root:vizxv, ubnt:ubnt, pi:raspberry, ... see linux_shell.DEFAULT_CREDENTIALS), call it explicitly; hwntools never tries credentials on its own:

pair = sh.try_default_credentials()                    # ('root', 'xc3511') or None
pair = sh.try_default_credentials([("support", "support")], delay=1.0)

Warning

Only do this on devices you are authorized to test. Failed logins may be logged or trigger a lockout.

Run commands

res = sh.run("cat /proc/mtd")
print(res)                 # the output, a str
print(res.status)          # the exit code
sh.run("mount -o remount,rw /", check=True)            # raise HwnError on a non-zero status
results = sh.run_many(["uname -a", "cat /proc/cmdline"])

Each command is wrapped between two random markers (echo HWN…S; cmd; echo HWN…E$?), so the echo of your command line, the prompt and kernel messages printed around it are discarded reliably, and the exit status comes back with the output. has("tool") tells you whether a program exists on the device (cached).

Copy files off the device

data = sh.read_file("/etc/shadow")
open("shadow", "wb").write(data)

read_file() picks the first transfer method the device supports: base64, then hexdump, then od (force one with method=). Files larger than chunk (16 KiB by default) are read piece by piece with dd, so a glitch on the line only costs one chunk. With verify=True (the default) the result is compared with the device's md5sum when it has one, and the length is always checked.

Push files onto the device

sh.upload(open("busybox-armv7l", "rb").read(), "/tmp/bb", mode="755")
print(sh.run("/tmp/bb --help | head -1"))

upload() writes with echo … | base64 -d when the device has base64, and otherwise with printf octal escapes, in chunk-byte pieces (512 by default). It then checks the size and, when possible, the md5.

Speed

Text transfers are slow on a 115200 baud console: base64 adds a third on top of the data, hexdump doubles it and printf octal escapes make it four times larger. Keep uploads small (a static busybox, a single tool) and prefer the network once you have it.

A complete script is in examples/root_shell_exfil.py.