
Hardware Hacking Cheatsheet
[[TOC]]
250-350 CVCC and GND well exposed is very usefulGND, IN or RX, OUT or TX and VCC3/4 pins on the PCB
GND
GNDVCC
VCCGND (multimeter to resistance measurement, usually 200k)3.3V or 5V), else the board or the serial adapter will be damagedRX to adapter TX and board TX with adapter RX
VCC pin is not neededsudo lsusb to locate the adapterls -lart /dev to locate all device files; our should be one of the last, usually ttyUSB0dialout group (or be root), to check our groups 0 or 1 internal bitsTCK, TDI, TDO, TMS and TRST (optional)
TCK (Test Clock): the drummer, or metronome that dictates the speed of the controller. Voltage on this pin simply pulses up and down in a rhythmic, steady beat. On every "beat" of the clock, the controller takes a single action.TMS (Test Mode Select): voltages on the Mode Select pin control what action JTAG takes. By manipulating the voltage on this pin, you tell JTAG what you want it to do.3.3V or 5V), else the board or the serial adapter will be damagedopenocd -f $FT232HCONFIGFILE -f $BOARDCONFIGFILE
$FT232HCONFIGFILE: Focaccia board reference$BOARDCONFIGFILE: the board's you are hacking config file (useful but maybe you won't have it, optional)
/usr/local, maybe here you could find some useful $BOARDCONFIGFILEtelnet localhost 4444
U-Boot 1.1.3)ASIC MT7621A...)mtd->writesize=2048: page size (bytes)mtd->oobsize=64: data used for error correction (bytes)devinfo.iowidth=8: data written/read per operation (bytes)mtd->erasesize=131072: EEPROM remaining writes? (more or less)TODO SPI DUMP
TODO GDB ATTACH????
/etc/rc/etc/rc.local/etc/rc.conf for information/etc/rc.d//etc/inittab
runlevel
1: single user mode, root shell, no password, no deamon running3: text based multiuser mode, login prompt5: graphical login/etc/init.d//sbin/init searching for information (above) identifying typesfile commandLocate QEMU executable format: qemu-system-$PROCESSOR$ARCHITECTURE
qemu-system-mipselIf you know the processor family you could specify it to help QEMU better emulate the environment
$QEMUBIN -cpu helpWe need kernel and root filesystem
Find kernel version, libc version and list of libraries used by the executable we are interested in (readelf -d $EXECUTABLE)
libfoo.X.Y.Z ( is the version)
VCC (multimeter to resistance measurement, usually 200kOhm)GND (multimeter to voltage measurement, usually 20V)TX UART pin voltage against GND (multimeter to voltage measurement, usually 20V); if the voltage is oscillating then this pin is probably TX (because it's sending data)RX UART pin voltage against GND (multimeter to voltage measurement, usually 20V); if the voltage is stuck at 0 then this pin is probably RX (because it's waiting to receive data)Table
| PIN | GND resistance | VCC resistance | V | Notes |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 |
Example
115200)H is the print help function, use it everywhereGND to Jtagulator GND, board pins 1,2,3 to Jtagulator channels 1,2,3V: set working voltageU: enter UART identification menuU: start identificationText string to output: defaultStarting channel: channel where we put the pin 1 of the boardEnding channel: channel where we put the pin 3 of the boardIgnore non-printable characters: Yesgroups $USERscreen /dev/ttyUSB0 $BAUDRATE to attach to the TTY
$BAUDRATE can be one of the ones found here$BAUDRATE
1152009600576003840019200$BAUDRATE we could see gibberish or also NOTHINGctrl + a -> k -> y: close screen\r\n or \n?
pyserial, python's library for serial communication, example:
#!/usr/bin/env python3
import serial
ser = serial.Serial('/dev/ttyUSB0', 115200, tmieout = 0.1)
ser.write(b"HELLO\r\n")
ser.write(b"HELLO\n")
TDI (Test Data-In): the pin that feeds data into the chip. The JTAG standard does not define protocols for communication over this pin. That is left up to the manufacturer. As far as JTAG is concerned, this pin is simply an ingress method for 1s and 0s to get into the chip. What the chip does with them is irrelevant to JTAG.TDO (Test Data-Out): the pin for data coming out of the chip. Like the Data-In pin, communication protocols are not defined by JTAG.TRST (Test Reset, optional): this signal is used to reset JTAG to a known good state.5/6 pins or a double row of 10, 12, 14, 20 pins on the PCB
GND
GNDVCC
VCCGND (multimeter to resistance measurement, usually 200k)VCC (multimeter to resistance measurement, usually 200kOhm)GND (multimeter to voltage measurement, usually 20V)Table
115200)H is the print help function, use it everywhereGND to Jtagulator GND, board pins 1,2,3... to Jtagulator channels 1,2,3...V: set working voltageJ: enter JTAG identification menuI: identify with IDCODE scan, will not find TDI (fast), better if i have a lot pins to identifyB: identify with BYPASS scan, will find TDI (slow), better if i have less pins to identifyStarting channel: channel where we put the pin 1 of the boardEnding channel: channel where we put the pin n of the boardAlready known pins: No but it could speed the process up if we already know some pins300Ohm or 1kOhm between this pin and VCChalt: halt CPU (like freeze)
reset: reset CPUreg: read CPU registersflash info bank $BANKID or flash info $BANKID: prints information about flash memory bank $BANKID (i think that banks = chunk of memory)flash list: retrieves a list of associative arrays for each device that was declared using flash bank (in $BOARDCONFIGFILE), numbered from zeroflash banks: prints a one-line summary of each device that was declared using flash bank (in $BOARDCONFIGFILE), numbered from zeroflash write_image erase "$BINTOWRITE" $ADDRTOSTART: flash memory
$BINTOWRITE: could be bin (binary), ihex (Intel hex), elf (ELF file), s19 (Motorola s19), mem...$ADDRTOSTART: address where to start writing (i think that default is 0)flash dump_image $OUTFILE $ADDRTOSTART $SIZETODUMP: dump memory
$OUTFILE: binary file where to save the dump$ADDRTOSTART: address where to start reading (i think that default is 0)$SIZETODUMP: number of bytes to dumpinit started or something like this, this probably will be near BusyBox string or something analogoushelp command is your friendbinwalk, file and hexdump -C to check if the file dumped is ok or not and if is compressed or encrypted
binwalk -E we analyze the entropy of the file
1: random, compressed or encrypted file1: normal executable or filebinwalk -e to extract identifiable segments of the filebinwalk doesn't fully understand the dumped image we could use the EEPROM partition table (if previously found) to manually split the dumped image in multiple useful images
dd if=$IN_DUMPED_IMAGE of=$OUT_FILE bs=1024 skip=$BYTES_TO_SKIP_FROM_THE_START count=$HOW_MANY_BYTES_TO_WRITEsha1sum, md5sum or binwalk -W -i to compare images (if for example we think they could be the same image)binwalk (or dd if we can find online how our specific kernel image is structured) to read the root filesystemfakeroot -s fakeroot.dat usquashfs -d squashfs-root u04-sqfs.dat
fakeroot: create a fake root environment, useful to emulate file permissions, device files...
-s fakeroot.dat: save fake root environment to further restore with command fakeroot -i fakeroot.dat bashusquashfs: extract squashfs filesystem (could be different in your case)
-d squashfs-root: destination folderu04-sqfs.dat: filesystem image to extractfactory mode string; if we succeed in putting the device in factory mode (if it exists) then hacking it is a lot easiergrepfindxargsstringsX.Y.ZX incompatible ABIY backward compatible ABIZ no ABI changesX.Y equal to the original library
X, higher YBuild using a build system (select features and autotrack dependencies)
Start emulating
#!/bin/bash
# This script will build an environment without password for the user root
export QEMU_AUDIO_DRV="none" # ignore audio drivers
qemu-system-${PROCESSOR}${ARCHITECTURE} -M $CPUFAMILY \ # See point 2
-m $RAMSIZE \
-kernel $KERNELPATH \
-nographic \ # No GUI
-hda $FILESYSTEM \
-net nic,model=$NETCARDMODEL \ # Model of net card, driver must be included in kernel
-net user, hostfw=tcp::2222-:22, hostfw=tcp::9000-:9000 \ # 2222 as ssh and 9000 for GDB server
-no-reboot \ # Terminate the machine when is halted
-append "root=/dev/hda console=uart0" # Set root filesystem and console
LD_LIBRARY_PATH as follow (inside the machine): export LD_LIBRARY_PATH=/lib:/usr/lib:$PATHTOORIGINALFILESYSTEMLIBFOLDER#!/bin/bash
# Part 1: Identify bytes for kernel module
modprobe nandsim first_id_byte=$FIRSTBYTE \
second_id_byte=$SECONDBYTE \
third_id_byte=$THIRDBYTE \
fourth_id_byte=$FOURTHBYTE \
cache_file=/root/nandsim.bin \
parts=x,y,z,... # Define partitons size in number of erase blocks; the number of partitions depends on your device, partitions are usually print on boot
# Part 2: Erase partitions created (analyze EEPROM partitions)
flash_erase /dev/mtd0 0 8
flash_erase /dev/mtd1 0 20
# ...
# Part 3: Load partitions dumped from device in the ones just created
nandwrite /dev/mtd0 part0.bin
nandwrite /dev/mtd1 part1.bin
# ...
# Part 4: Create mountpoint for filesystem and attach (if UBIFS)
mkdir /mnt/filesystem
ubiattach -O $N -m $MTDDVENUM -d $UBIDEVNUM
# Part 5: Mount
mount -tubifs /dev/ubi${UBIDEVNUM}_0 /mnt/filesystem
NAND ID, bytes print are in order first, second and fourth bytewritesize, oobsize, erasesize, iowidth to find here the correct commandflash_erase command is their size correlated to the erasesize (same as parts=x,y,z,...)-O: specify the volume id header offset, if wrong the system should tell you the right value anyway you could try different values like 512, 1024, 2048 (trial and error)-m: mtd device number (see point 3)-d: UBI device number (see point 5)qemu-$PROCESSOR$ARCHITECTURE
qemu-mips64-L (or see man)
readelf -l $EXECUTABLEuClibc, uClibc-ng, musl, dietlibc...)make manual to create buildroot's manualmake help buildroot prints all supported devices (boards); make $YOURBOARDNAME to create a buildroot configuration file of your boardmake menuconfig (text based) or make xconfig (GUI) to select kernel modules to add to our build, we'll use make xconfig
Edit->Find to search for modulesTarget options
Show options and packages that are deprecated or obsoleteBuild packages with debugging symbols with highest debug levelStrip command for binaries on target to NoneGCC optimization level to 0Toolchain
Toolchain type to Buildroot toolchainKernel headers to Manually specified linux versionmake savedconfigmake linux-menuconfig (text based) or make linux-xconfig (GUI), here we'll use CLI version
Kernel type -> Preemption model (Preemptible Kernel (Low-Latency Desktop)) -> Preemptible Kernel (Low-Latency Desktop)Kernel type -> Device drivers -> Memory technology device (MTD) support -> NAND device support -> Support for NAND flash simulatorKernel type -> Device drivers -> -> -> uClibc (or your c lib) using uclibc-menuconfig (as always)
Development/Debugging options -> Enable debugging symbols, if this doesn't work (compiling errors) then Development/Debugging options -> (Wall) compiler warnings -> add -Wall -ggdb -g3
-ggdb: provides debugging info to use with GDB-g3: provide extra debugging informationSavemake, if problems iterate back
Need to use -fPIC
--enable-shared in kernel modules (point 7) under Toolchain -> Additional gcc options or patch buildrootbr2)
+-- board/
| +-- <company>/ (not always used)
| +-- <boardname>/
| +-- linux.config
| +-- busybox.config
| +-- kernel-defconfig (kernel config file)
| +-- <other configuration files>
| +-- post_build.sh (executed just before building the image, useful to copy root filesystem into the image)
| +-- post_image.sh
| +-- rootfs_overlay/ (everythin here will be copied in the final image)
| | +-- etc/
| | +-- <some file>
| +-- patches/
| +-- foo/
| | +-- <some patch>
| +-- libbar/
| +-- <some other patches>
|
+-- configs/
| +-- <boardname>_defconfig (buildroot config for our board)
| +-- uClibc.config (optional)
+-- patches/
| +-- (here patches to be applied)
|
+-- Config.in (if using a br2-external tree)
+-- external.mk (if using a br2-external tree)
+-- external.desc (if using a br2-external tree)
make BR2_EXTERNAL=$PATHTOEXTTREE $COMMANDmake BR2_EXTERNAL=$PATHTOEXTTREE savedefconfig| PIN | GND resistance | VCC resistance | V | Notes |
|---|
| 1 | 30kOhm | 0Ohm | 3.3V | VCC |
| 2 | 4.7kOhm | 34kOhm | 3.3V | 1.6-3.3V on boot - TX |
| 3 | INFOhm (multimeter 1) | INFOhm (multimeter 1) | 3.3V | 0V on boot - RX |
| 4 | 0Ohm | 30kOhm | 0V | GND |
| PIN | GND resistance | VCC resistance | V | Notes |
|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 | ||||
| 8 | ||||
| 9 | ||||
| 10 | ||||
| ... |
Example
| PIN | GND resistance | VCC resistance | V | Notes |
|---|---|---|---|---|
| 1 | 1kOhm | 1kOhm | 0V | |
| 2 | 0Ohm | 90Ohm | 0V | GND |
| 3 | INFOhm (multimeter 1) | INFOhm (multimeter 1) | 2.1V | High impedance, TDO? |
| 4 | 90Ohm | 0Ohm | 3.3V | VCC |
| 5 | 4.7kOhm | 4.7kOhm | 3.3V | |
| 6 | INFOhm (multimeter 1) | INFOhm (multimeter 1) | 0V | Not connected? |
| 7 | 5.7kOhm | 5.7kOhm | 3.3V | |
| 8 | INFOhm (multimeter 1) | INFOhm (multimeter 1) | 0V | Not connected? |
| 9 | 4.7kOhm | 4.7kOhm | 3.3V | |
| 10 | 0Ohm | 90Ohm | 0V | GND |
Custom kernel headers series to $DEVICEKERNELVERSIONLinux version to $DEVICEKERNELVERSIONC library to $DEVICECLIBRARY$DEVICECLIBRARY version to $DEVICECLIBRARY $DEVICELIBRARYVERSIONEnable large filesEnable IPv6Enable RPCEnable WCHARThread library implementation to linuxthreadsThread library debuggingBuild cross gdb for the hostTUI supportPython supportGDB debugger version to $LATESTGDBVERSIONSystem configuration
Passwords encoding to MD5Init system to $DEVICEINITSYSTEM (or BusyBox)/dev management to Dynamic using devtmpfs only/bin/sh to Busybox default shellInstall timezone infoKernel
Kernel version to $DEVICEKERNELVERSIONKernel binary format to vmlinuxTarget packages
Compressors and decompressors
bzip2 and xz-utilsDebugging profiling and benchmark
gdb and full debuggerDevelopment tools
Filesystem and flash utilities
mtd, jffs2 and ubi/ubifs tools (or what you will need)Libraries
Crypto
libsha1libssh2opensslJSON/XML
expatjson-cNetworking applications
rsync and what you needShell and utilities
fileFilesystem images
ext2Host utilities (not about target device, we are talking about host device here)
host mtd, jffs2 and ubi/ubifs toolshost util-linuxMemory technology device (MTD) supportUBI - Unsorted block imagesEnable UBIFile systems -> Miscellaneous filesystem -> JFFS2 supportFile systems -> Miscellaneous filesystem -> UBIFS filesystem supportSavemake BR2_EXTERNAL=$PATHTOEXTTREE linux-update-defconfigmake BR2_EXTERNAL=$PATHTOEXTTREE BR2_UCLIBC_CONFIG=$PATHWHERETOSAVEUCLIBCCONFIG uclibc-update-defconfig