Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mmap for reading the input file #762

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ aho-corasick = "1.1.3"
serde = { version = "1.0", features = ["derive"]}
clap = { version = "4.5.16", features = ["derive"] }
xxhash-rust = { version = "0.8.12", features = ["xxh32"] }
memmap2 = "0.9.5"

[dependencies.uuid]
version = "1.10.0"
Expand Down
9 changes: 4 additions & 5 deletions src/binwalk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::os::windows;
#[cfg(unix)]
use std::os::unix;

use crate::common::{is_offset_safe, read_file};
use crate::common::{is_offset_safe, mmap_file};
use crate::extractors;
use crate::magic;
use crate::signatures;
Expand Down Expand Up @@ -699,11 +699,10 @@ impl Binwalk {

debug!("Analysis start: {}", target_file);

// Read file into memory
if let Ok(file_data) = read_file(target_file) {
if let Ok(file_mmap) = mmap_file(target_file) {
// Scan file data for signatures
info!("Scanning {}", target_file);
results.file_map = self.scan(&file_data);
results.file_map = self.scan(&file_mmap);

// Only extract if told to, and if there were some signatures found in this file
if do_extraction && !results.file_map.is_empty() {
Expand All @@ -712,7 +711,7 @@ impl Binwalk {
"Submitting {} signature results to extractor",
results.file_map.len()
);
results.extractions = self.extract(&file_data, target_file, &results.file_map);
results.extractions = self.extract(&file_mmap, target_file, &results.file_map);
}
}

Expand Down
27 changes: 11 additions & 16 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
//! Common Functions
use chrono::prelude::DateTime;
use log::{debug, error};
use log::error;
use memmap2::Mmap;
use std::fs::File;
use std::io::Read;

/// Read a file into memory and return its contents.
/// Map the file into memory and return a [Mmap] instance
/// that provides access into the file's contents.
///
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_common_rs_11_0() -> Result<(), Box<dyn std::error::Error>> {
/// use binwalk::common::read_file;
/// use binwalk::common::mmap_file;
///
/// let file_data = read_file("/etc/passwd")?;
/// let file_data = mmap_file("/etc/passwd")?;
/// assert!(file_data.len() > 0);
/// # Ok(())
/// # } _doctest_main_src_common_rs_11_0(); }
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn read_file(file: impl Into<String>) -> Result<Vec<u8>, std::io::Error> {
let mut file_data = Vec::new();
pub fn mmap_file(file: impl Into<String>) -> Result<Mmap, std::io::Error> {
let file_path = file.into();

match File::open(&file_path) {
Err(e) => {
error!("Failed to open file {}: {}", file_path, e);
Err(e)
}
Ok(mut fp) => match fp.read_to_end(&mut file_data) {
Ok(fp) => match unsafe { Mmap::map(&fp) } {
Err(e) => {
error!("Failed to read file {} into memory: {}", file_path, e);
error!("Failed to map file {} into memory: {}", file_path, e);
Err(e)
}
Ok(file_size) => {
debug!("Loaded {} bytes from {}", file_size, file_path);
Ok(file_data)
}
Ok(mmap) => Ok(mmap),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/entropy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::read_file;
use crate::common::mmap_file;
use entropy::shannon_entropy;
use log::error;
use plotters::prelude::*;
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn plot(file_path: impl Into<String>) -> Result<FileEntropy, EntropyError> {
}

// Read in the target file data
if let Ok(file_data) = read_file(&target_file) {
if let Ok(file_data) = mmap_file(&target_file) {
let mut points: Vec<(i32, i32)> = vec![];

// Calculate the entropy for each file block
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ fn carve_file_map(results: &binwalk::AnalysisResults) -> usize {
// No results, don't do anything
if !results.file_map.is_empty() {
// Read in the source file
if let Ok(file_data) = common::read_file(&results.file_path) {
if let Ok(file_data) = common::mmap_file(&results.file_path) {
// Loop through all identified signatures in the file
for signature_result in &results.file_map {
// If there is data between the last signature and this signature, it is some chunk of unknown data
Expand Down