6. Get a shell¶
A console in front of the firmware you extracted¶
Lesson 5 found that root has an empty password. On the real board you would now let
Linux boot and log in on the serial console. Here, FakeLinuxShell plays that console: it
shows a login: prompt and then runs every command with /bin/sh on your computer,
inside the folder you give it. Pointing it at the root filesystem you unpacked in lesson 5
makes the files look like the device's.
from hwntools import LinuxShell
from hwntools.sim import FakeLinuxShell, SimTube
ROOTFS = "flash.bin.extracted/00003400.uimage-ramdisk/tar-root"
console = FakeLinuxShell(credentials={"root": ""}, cwd=ROOTFS, hostname="rx-ap100")
sh = LinuxShell(SimTube(console))
print(sh.detect())
print(sh.try_default_credentials())
print(sh.detect())
res = sh.run("cat etc/inittab")
print(res, "status:", res.status)
print(sh.run("grep -n sh etc/init.d/rcS"))
print(sh.run("ls etc/does-not-exist").status)
login
[x] Trying default credentials
[+] Trying default credentials: logged in as root:<empty>
('root', '')
root
::sysinit:/etc/init.d/rcS
ttyS0::respawn:-/bin/sh
status: 0
1:#!/bin/sh
3:telnetd -l /bin/sh
1
What just happened¶
detect()looked at the prompt and saidlogin. After logging in it saysroot(a#prompt). It can also telluser($) andpasswordprompts apart.try_default_credentials()tried a short list of common embedded logins (rootwith an empty password comes first) and returned the pair that worked. Only use it with permission: failed logins are logged and may lock accounts.run()returns a string with an extra.status: the command's exit code (1for the missing file). It wraps each command in random markers, so echoed input, prompts and kernel messages never end up in the output.
Simulator limits
- Paths are relative (
etc/inittab, not/etc/inittab): the commands run on your computer, and/etcwould be your own. For the same reason,ps,uname,ifconfigorcat /proc/...describe your machine, not the router. - It needs a POSIX
/bin/sh(Linux, macOS, or WSL on Windows). - On a real device, the first things to run would be
id,uname -a,cat /proc/mtd(the flash partitions),ps,netstat -lnpandls /dev. Same calls, same API.
Try it¶
Try it
Log in with login("root", "") and copy etc/shadow from the "device" to a local file
shadow.txt with a single hwntools call.
Solution
from pathlib import Path
from hwntools import LinuxShell
from hwntools.sim import FakeLinuxShell, SimTube
ROOTFS = "flash.bin.extracted/00003400.uimage-ramdisk/tar-root"
sh = LinuxShell(SimTube(FakeLinuxShell(credentials={"root": ""}, cwd=ROOTFS)))
sh.login("root", "")
shadow = sh.read_file("etc/shadow") # base64 over the console, md5-checked
print(shadow.decode())
Path("shadow.txt").write_bytes(shadow)
[x] Reading etc/shadow (79 bytes) via base64
[+] Reading etc/shadow (79 bytes) via base64: 79 bytes
root::19700:0:99999:7:::
admin:$1$rx$Hq0bVhJ6Q3Q2x7mJm0bqH/:19700:0:99999:7:::
read_file() picks base64, hexdump or od depending on what the device has, reads
big files in dd chunks, and compares an MD5 checksum when md5sum exists. The
admin hash starts with $1$: MD5-crypt, the weak hash fwsift reported as CRED-003.
upload() does the opposite direction, to push a tool onto the device.