Skip to content

Using the Rust Library

devttys0 edited this page Oct 31, 2024 · 4 revisions

Binwalk API

Binwalk includes a library that can be integrated into other Rust projects.

You will need to install the following system dependencies:

sudo apt install build-essential libfontconfig1-dev liblzma-dev

Tip

Detailed API documentation can be found in Binwalk's Rust documentation:

cargo doc --open

Example

Step 1

Create a new Rust project:

cargo new my_project

Step 2

To use the latest release, add the following dependency to your project's Cargo.toml file:

binwalk = "3.1.0"

To build against the the latest master branch (possibly unstable), add the following dependency to your project's Cargo.toml file:

binwalk = { git = "https://github.com/ReFirmLabs/binwalk.git" }

Step 3

Edit your project's src/main.rs file:

use binwalk::Binwalk;

fn main() {
    // Create a new Binwalk instance
    let binwalker = Binwalk::new();

    // Read in the data you want to analyze
    let file_data = std::fs::read("/tmp/firmware.bin").expect("Failed to read from file");

    // Scan the file data and print the results
    for result in binwalker.scan(&file_data) {
        println!("{:#?}", result);
    }
}

Step 4

Build and run your project:

$ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s
     Running `target/debug/my_project`
SignatureResult {
    offset: 0,
    id: "82df43be-c867-4291-8d81-7c46f1769f31",
    size: 116,
    name: "dlob",
    confidence: 128,
    description: "DLOB firmware header, header size: 116 bytes, data size: 19996704",
    always_display: false,
    extraction_declined: false,
    preferred_extractor: None,
}
SignatureResult {
    offset: 116,
    id: "49385de3-0265-44d5-9923-f6d5fb888851",
    size: 1808138,
    name: "lzma",
    confidence: 250,
    description: "LZMA compressed data, properties: 0x5D, dictionary size: 33554432 bytes, compressed size: 1808138 bytes, uncompressed size: 4897632 bytes",
    always_display: false,
    extraction_declined: false,
    preferred_extractor: None,
}
SignatureResult {
    offset: 1835124,
    id: "3900f0df-2ca2-4213-89c3-13ba2e9754e5",
    size: 32,
    name: "packimg",
    confidence: 0,
    description: "PackImg firmware header, header size: 32 bytes, data size: 2102529 bytes",
    always_display: false,
    extraction_declined: false,
    preferred_extractor: None,
}
SignatureResult {
    offset: 1835156,
    id: "ee1af66f-e64f-4195-bc07-9ffc5f046550",
    size: 18159869,
    name: "squashfs",
    confidence: 250,
    description: "SquashFS file system, little endian, version: 4.0, compression: xz, inode count: 2673, block size: 131072, image size: 18159869 bytes, created: 2016-05-24 09:50:50",
    always_display: false,
    extraction_declined: false,
    preferred_extractor: None,
}