Linux shell¶
See the Linux shell guide for an introduction.
linux_shell
¶
Work with a Linux serial console: detect login/root prompts, log in, run commands with reliable output framing, exfiltrate files and upload files over a plain text console.
Example
from hwntools import *
sh = LinuxShell(serial("/dev/ttyUSB0"))
if sh.detect() == "login":
sh.login("root", "")
print(sh.run("id"))
data = sh.read_file("/etc/shadow")
CommandResult
¶
Bases: str
Command output (a str) with the exit status in .status.
LinuxShell
¶
LinuxShell(tube: Tube, timeout: float | None = None)
Helper around a tube connected to a Linux console (serial, telnet, adb shell...).
classify
staticmethod
¶
Classify the tail of console output as a prompt type.
detect
¶
Report which prompt the console shows.
Pending output is checked first, so a fresh login: prompt is recognised without
sending anything (an Enter there would count as a failed login). Otherwise the console
is poked with Enter.
login
¶
Log in at a login: prompt. Returns True on success.
try_default_credentials
¶
try_default_credentials(credentials: Iterable[tuple[str, str]] = DEFAULT_CREDENTIALS, delay: float = 0.0) -> tuple[str, str] | None
Try each (user, password) pair. Returns the working pair or None.
Only use this on devices you are authorised to test: failed logins may be logged or trigger lockouts.
run
¶
run(cmd: str, timeout: float | None = None, check: bool = False) -> CommandResult
Run cmd and return its output; .status holds the exit code.
Output is framed by random markers so that echo, prompts and kernel messages around it are discarded reliably.
read_file
¶
read_file(path: str, method: Literal['auto', 'base64', 'hexdump', 'od'] = 'auto', chunk: int = 16384, verify: bool = True, timeout: float | None = None) -> bytes
Read a remote file over the console.
auto picks base64, then hexdump, then od. Large files are read in
chunk-byte pieces with dd so that a glitch only costs one chunk. With verify,
the result is compared with md5sum when the device has it.
upload
¶
upload(data: bytes, remote_path: str, method: Literal['auto', 'base64', 'printf'] = 'auto', chunk: int = 512, mode: str | None = None, verify: bool = True) -> None
Write data to remote_path using base64 -d or printf octal escapes.
run_many
¶
run_many(cmds: Sequence[str], timeout: float | None = None) -> list[CommandResult]
Run several commands, returning their results in order.