nlitsme/ubidump
Sobre
Tool for viewing and extracting files from an UBIFS image
Design e mídiaCrescimento medido de estrelas
- em 24 h
- 0
- em 7 dias
- 0
- em 15 dias
- 0
- em 1 mês
- Coletando histórico
Variações líquidas em períodos sobrepostos, não totais diários. Medições ausentes ficam em branco. Estrelas indicam interesse, não qualidade.
README.md
OriginalUBIFS Dumper
ubidump.py lists, inspects, and extracts files from UBI images and raw UBIFS
filesystem images without mounting them. It also provides block and node dumps,
surviving-history inspection, and best-effort recovery from damaged images.
Input images are opened read-only.
About UBI and UBIFS
UBIFS is a filesystem designed for raw flash, commonly NAND. UBI provides the eraseblock management layer below it:
- Physical eraseblocks (PEBs) contain UBI erase-count (EC) and volume-ID (VID) headers. These describe erase counts, volume membership, logical block numbers, sequence numbers, and geometry.
- Volumes map logical eraseblocks (LEBs) to physical eraseblocks. A UBI image can contain several volumes, and a volume need not contain UBIFS.
- UBIFS nodes hold the superblock, redundant masters, index, journal records, and filesystem objects. A committed master points to a filesystem index root.
- Filesystem keys identify inodes, directory entries by parent inode and name hash, and file data by inode and 4096-byte block number.
A raw UBIFS image contains the logical filesystem blocks without the UBI EC/VID
headers or volume table. The tool recognizes either image type from its initial
magic and determines the block geometry automatically. For embedded images,
use --offset and optionally --length to select the image region.
Mounting images on Linux
As an alternative to extracting files with ubidump.py, a UBI image can be
loaded into a simulated NAND device with nandsim, attached through UBI, and
mounted with the kernel's UBIFS driver.
The original example uses an eraseblock size of 0x40000 and a page size of
4096 bytes. It assumes the simulated NAND device is /dev/mtd0; check that
this is the simulator before running nandwrite, which writes to that device.
sudo modprobe nandsim first_id_byte=0x2c second_id_byte=0xac third_id_byte=0x90 fourth_id_byte=0x26
sudo nandwrite /dev/mtd0 firmware-image.ubi
sudo modprobe ubi mtd=/dev/mtd0,4096
mkdir -p mnt
sudo mount -t ubifs -o ro /dev/ubi0_0 mnt
For an image with eraseblock size 0x20000, use fourth_id_byte=0x15 in the
nandsim command and replace 4096 with 2048 in the UBI attachment command.
The simulated NAND geometry must match the image. The example mounts volume
zero read-only.
Installation
Use Python 3. The current script uses Python 3 features despite retaining some older Python 2 compatibility code.
When working from a checkout with a requirements file:
python3 -m pip install -r requirements.txt
The project is also distributed as a pip package:
python3 -m pip install ubidump
The examples below invoke ubidump.py directly and describe the supplied script;
an installed package version may have a different set of options.
The script uses the standard library for CRC checks and uncompressed or zlib
data. crcmod is no longer required. Install additional codecs only when needed:
| Compression | Dependency |
|---|---|
| None or zlib | Python standard library |
| LZO | python-lzo (version 1.09 or newer) or lzokay; python-lzo is preferred when both are installed |
| Zstandard | zstandard |
For example:
python3 -m pip install lzokay zstandard
Alternatively, install python-lzo. Building it may require the system LZO
development library:
# Debian / Ubuntu
sudo apt install liblzo2-dev
python3 -m pip install python-lzo
# macOS with Homebrew
brew install lzo
LDFLAGS="-L$(brew --prefix lzo)/lib" CFLAGS="-I$(brew --prefix lzo)/include/lzo" python3 -m pip install python-lzo
Usage
python3 ubidump.py [OPTIONS] IMAGE [IMAGE ...]
Multiple input files are processed independently, in argument order; they are
not concatenated. Without an action option, a UBI image prints a short volume
summary. Use -v to inspect the volume table and mappings.
List and extract files
List files in all named UBIFS volumes, or in one selected volume:
python3 ubidump.py -l image.ubi
python3 ubidump.py --volume 0 -l image.ubi
Normal listings include mode, link count, UID, GID, size or device numbers, modification time, path, and symlink target where applicable.
Write one file to standard output:
python3 ubidump.py --volume 0 -c /etc/passwd image.ubi
python3 ubidump.py --volume 0 -c /bin/busybox image.ubi > busybox
With --cat, informational output and diagnostics go to standard error so the
file payload can be redirected. Repeat -c to output several files; their bytes
are concatenated in request order, with textual file labels on standard error.
Select one volume and input image when redirecting a single file.
Extract the filesystem tree, optionally preserving metadata:
python3 ubidump.py -s extracted image.ubi
python3 ubidump.py --volume 0 -s extracted -p image.ubi
Each volume is placed under extracted/<volume-name>/. For a raw UBIFS image,
the subdirectory is extracted/raw/:
python3 ubidump.py -l rootfs.ubifs
python3 ubidump.py -s extracted rootfs.ubifs
Normal extraction handles regular files, directories, symlinks, FIFOs, Unix
sockets, and device nodes where the host permits them. -p restores access and
modification times, modes, and ownership where permitted, except for symlinks.
Device creation and ownership changes may require elevated privileges; some
permission failures are ignored. Hard links are extracted as separate objects.
Use a fresh destination directory; existing regular files can be overwritten.
Extract a raw UBI volume
python3 ubidump.py --volume 0 --saveraw volume.bin image.ubi
This exports logical volume payloads without UBI headers, including volumes
whose contents are not UBIFS. For example, a SquashFS volume can be exported
for use with unsquashfs or another filesystem-specific tool.
For dynamic volumes, output spans the reserved LEBs and fills unmapped LEBs
with 0xff. For static volumes, the recorded used-block count and per-block
data sizes determine the output; missing static LEBs cause an error. Short
mapped reads are reported and padded with 0xff.
Always specify --volume for this operation on a UBI image. Saving an entire
UBI image without volume selection, and --saveraw on a raw UBIFS input, are
currently unimplemented and print TODO.
Embedded images
python3 ubidump.py --offset 0x100000 --length 0x800000 -l firmware.bin
Offsets and lengths are byte counts. Reads are restricted to the selected region, with the requested length capped at the remaining input size. Image coordinates are relative to the start of that region.
Inspect blocks and nodes
python3 ubidump.py -v image.ubi
python3 ubidump.py --volume 0 -d image.ubi
python3 ubidump.py --volume 0 --nodedump 3:0x100 image.ubi
python3 ubidump.py --volume 0 --hexdump 3:0x100:0x80 image.ubi
python3 ubidump.py --rawdump image.ubi
--hexdump uses physical eraseblock coordinates when no UBI volume is selected,
and logical volume coordinates with --volume. A raw UBIFS input always uses
logical coordinates. Omitting the byte offset defaults to zero; omitting the
length defaults to 0x100 bytes. --nodedump defaults its byte offset to zero.
--rawdump scans the selected input region for aligned UBI/UBIFS signatures and
prints low-level dumps independently of volume selection and filesystem
traversal. It reads the entire region into memory. Signature matches are not
proof of valid or reachable filesystem objects.
History and recovery
Committed filesystem views
Normal traversal uses a surviving committed master. Candidates are read from both redundant master LEBs and ordered by commit number and node sequence number. The tool prefers a candidate whose root can be read as an index node; this does not verify the whole tree. The journal is not replayed, so newer uncommitted changes may be absent.
List surviving masters, duplicate physical-block candidates, and a summary of surviving leaf versions:
python3 ubidump.py --volume 0 --history image.ubi
Select an older master using a coordinate reported by --history:
python3 ubidump.py --volume 0 --masteroffset 1:0x1000 -l image.ubi
For advanced inspection, --root overrides the index root used by listing,
directory extraction, and tree dumping:
python3 ubidump.py --volume 0 --root 3:1000 -d image.ubi
Unlike the other coordinate options, both fields of --root are parsed as
hexadecimal, even without 0x. It does not override the root used by --cat
or salvage recovery and still requires filesystem initialization and a master.
Surviving physical blocks
Normally, duplicate (volume, LEB) mappings select the candidate with the
highest VID sequence number after checking payload CRCs for copied and static
blocks. To explore older surviving mappings:
python3 ubidump.py --volume 0 --peb-sqnum 5000 --history image.ubi
python3 ubidump.py --volume 0 --peb-choice 0:3:42 -l image.ubi
--peb-sqnum excludes PEBs newer than the specified VID sequence number.
--peb-choice VOL:LEB:PEB selects an explicit physical block and can be repeated.
The selected block must be a header-valid candidate remaining after the cutoff;
this explicit override bypasses the automatic copied/static payload CRC filter.
Cutoffs apply to all UBI volumes, including the layout volume, so an overly low
cutoff can make the volume table unavailable.
Salvage leaf nodes
python3 ubidump.py --volume 0 --salvage -l image.ubi
python3 ubidump.py --volume 0 --salvage -s recovered image.ubi
python3 ubidump.py --volume 0 --salvage -c /etc/passwd image.ubi
Salvage scans mapped LEBs for aligned, CRC-valid inode, data, and directory-entry nodes and builds a synthetic index from the newest surviving candidates. Distinct directory-entry names sharing a hash are retained. A readable superblock and at least one master are still required, but the committed root index can be missing or unreadable.
Recovered paths may include:
_orphan_dir_<inum>/...for a disconnected parent directory._unlinked/inode_<inum>for regular-file inodes or data without a surviving name._cycle/...when the recovered directory ancestry contains a cycle.
Listings report absent data blocks and estimated sizes when an inode is missing.
Absent blocks may be genuine sparse holes or unavailable data; extraction
represents those gaps as holes/zero bytes. Salvage directory extraction creates
directories, symlinks, and regular files, but does not restore special files or
apply --preserve metadata.
Inspect leaf-node versions for an inode, or limit the salvage view by UBIFS node sequence number:
python3 ubidump.py --volume 0 --node-history 123 image.ubi
python3 ubidump.py --volume 0 --salvage --node-sqnum 10000 -l image.ubi
The UBIFS node sequence number and UBI VID sequence number are separate counters.
--node-sqnum filters the leaf scan; it does not select a historical master.
--history and --node-history also enable salvage internally. Combining either
with listing, extraction, or --cat therefore uses the salvage view.
Recovery is heuristic: deleted or superseded entries can reappear, and selected nodes can come from different filesystem states. Truncation records, journal transactions, and deletion history are not replayed. Only the currently selected physical block for each LEB is scanned; use PEB selection to inspect alternatives. Erased data is unavailable, and sequence cutoffs do not guarantee a consistent historical snapshot.
Command-line reference
Except for --root, numeric values below accept decimal or a 0x hexadecimal
prefix. Volume and eraseblock numbers are zero-based.
| Option | Meaning |
|---|---|
-h, --help |
Show command-line help. |
-l, --listfiles |
List filesystem entries. |
-s, --savedir DIRECTORY |
Extract into per-volume subdirectories. |
-p, --preserve |
Preserve metadata during normal extraction. |
-c, --cat FILE |
Write a file to stdout; repeat for multiple paths. |
--volume VOLNR |
Select a UBI volume for filesystem actions, hexdumps, or raw export. |
-d, --dumptree |
Dump the filesystem index and leaf nodes. |
-v, --verbose |
Print volume and filesystem details; accepts repetition. |
--debug |
Re-raise caught volume/input exceptions. |
-e, --encoding ENCODING |
Filename encoding, default utf-8. |
-m, --masteroffset LEB:OFF |
Select a committed master by logical coordinate. |
--history |
Report surviving masters, duplicate PEBs, and leaf-version counts. |
--salvage |
Build a heuristic filesystem view from surviving leaf nodes. |
--node-sqnum N |
Exclude leaf nodes newer than N from the salvage scan. |
--node-history INUM |
Report surviving scanned leaf versions whose key belongs to INUM. |
--peb-sqnum N |
Exclude UBI blocks with VID sequence numbers newer than N. |
--peb-choice VOL:LEB:PEB |
Override one physical-block mapping; repeatable. |
-R, --root LEB:OFF |
Override the traversal root; both fields hexadecimal. |
--rawdump |
Scan the input region for aligned header/node signatures. |
--hexdump BLOCK[:OFF[:N]] |
Dump bytes from a physical block or selected logical volume. |
--nodedump LEB[:OFF] |
Decode one UBIFS node. |
--saveraw FILENAME |
Export a selected UBI volume's logical payload. |
--offset N |
Start the image at byte offset N in each input. |
--length N |
Limit the selected image region to N bytes. |
-v means verbose; use --volume 0, not -v 0, to select volume zero.
Validation and limitations
The parser checks UBI EC/VID header and volume-table CRCs, UBIFS node CRCs and lengths, and copied/static UBI payload CRCs during automatic block selection. It can fall back to the other layout LEB if the first volume table is unusable. Invalid block candidates can be skipped, and trailing partial physical blocks are reported. These checks do not establish that the recovered filesystem is complete or internally consistent.
Authenticated UBIFS index layouts are parsed for recognized hash lengths, but cryptographic hashes and HMACs are not verified. Unknown authentication hash algorithms are rejected. NAND OOB removal, ECC correction, and bad-block repair are outside this tool's scope; supply an image with the expected UBI or UBIFS layout.
Errors are generally printed while processing continues with other volumes or
input files. A successful process exit alone does not establish successful
extraction. --debug exposes caught exceptions, while some per-file extraction
errors remain locally handled.
TODO
- Replay the journal and account for truncation/deletion history.
- Analyze committed-tree reachability for unused nodes, inodes, and directory entries.
- Expand data-size and filesystem consistency checks.
- Verify authenticated UBIFS hashes and HMACs.
- Implement raw copying for whole UBI inputs and raw UBIFS inputs.
References and related tools
- Linux MTD / UBI / UBIFS documentation and tools
- Infradead Git repositories
- ubi_reader
- Closed-source Windows UBIFS dumper
- squashfs-tools
- squashfs-tools-ng
- UBI dumper discussion on the MTD mailing list
Author
Willem Hengeveld [email protected]
Explore projetos relacionados
Carregando…