Skip to content

Commit

Permalink
Merge pull request #804 from ReFirmLabs/delink_integration
Browse files Browse the repository at this point in the history
Delink integration
  • Loading branch information
devttys0 authored Dec 22, 2024
2 parents 8c8326a + 8c37ee3 commit dda0001
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 260 deletions.
342 changes: 332 additions & 10 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ serde = { version = "1.0", features = ["derive"]}
clap = { version = "4.5.16", features = ["derive"] }
xxhash-rust = { version = "0.8.12", features = ["xxh32"] }
hex = "0.4.3"
delink = { git = "https://github.com/devttys0/delink" }

[dependencies.uuid]
version = "1.10.0"
Expand Down
4 changes: 1 addition & 3 deletions src/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,11 @@ pub mod cab;
pub mod common;
pub mod csman;
pub mod dahua_zip;
pub mod dlink_tlv;
pub mod dlke;
pub mod dmg;
pub mod dtb;
pub mod dumpifs;
pub mod dxbc;
pub mod encfw;
pub mod gif;
pub mod gpg;
pub mod gzip;
Expand All @@ -176,7 +175,6 @@ pub mod rar;
pub mod riff;
pub mod romfs;
pub mod sevenzip;
pub mod shrs;
pub mod squashfs;
pub mod srec;
pub mod svg;
Expand Down
66 changes: 0 additions & 66 deletions src/extractors/dlink_tlv.rs

This file was deleted.

86 changes: 0 additions & 86 deletions src/extractors/dlke.rs

This file was deleted.

55 changes: 55 additions & 0 deletions src/extractors/encfw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::extractors::common::{Chroot, ExtractionResult, Extractor, ExtractorType};

/// Defines the internal extractor function for decrypting known encrypted firmware
///
/// ```
/// use std::io::ErrorKind;
/// use std::process::Command;
/// use binwalk::extractors::common::ExtractorType;
/// use binwalk::extractors::encfw::encfw_extractor;
///
/// match encfw_extractor().utility {
/// ExtractorType::None => panic!("Invalid extractor type of None"),
/// ExtractorType::Internal(func) => println!("Internal extractor OK: {:?}", func),
/// ExtractorType::External(cmd) => {
/// if let Err(e) = Command::new(&cmd).output() {
/// if e.kind() == ErrorKind::NotFound {
/// panic!("External extractor '{}' not found", cmd);
/// } else {
/// panic!("Failed to execute external extractor '{}': {}", cmd, e);
/// }
/// }
/// }
/// }
/// ```
pub fn encfw_extractor() -> Extractor {
Extractor {
utility: ExtractorType::Internal(encfw_decrypt),
..Default::default()
}
}

/// Attempts to decrypt known encrypted firmware images
pub fn encfw_decrypt(
file_data: &[u8],
offset: usize,
output_directory: Option<&str>,
) -> ExtractionResult {
const OUTPUT_FILE_NAME: &str = "decrypted.bin";

let mut result = ExtractionResult {
..Default::default()
};

if let Ok(decrypted_data) = delink::decrypt(&file_data[offset..]) {
result.success = true;

// Write to file, if requested
if output_directory.is_some() {
let chroot = Chroot::new(output_directory);
result.success = chroot.create_file(OUTPUT_FILE_NAME, &decrypted_data);
}
}

result
}
45 changes: 28 additions & 17 deletions src/extractors/mh01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn extract_mh01_image(
const IV_FILE_NAME: &str = "iv.bin";
const SIGNATURE_FILE_NAME: &str = "signature.bin";
const ENCRYPTED_DATA_FILE_NAME: &str = "encrypted.bin";
const DECRYPTED_DATA_FILE_NAME: &str = "decrypted.bin";

let mut result = ExtractionResult {
..Default::default()
Expand All @@ -55,23 +56,33 @@ pub fn extract_mh01_image(
if output_directory.is_some() {
let chroot = Chroot::new(output_directory);

// Extract each part of the firmware image, ensuring that each one extracts without error
result.success = chroot.carve_file(
IV_FILE_NAME,
mh01_data,
mh01_header.iv_offset,
mh01_header.iv_size,
) && chroot.carve_file(
SIGNATURE_FILE_NAME,
mh01_data,
mh01_header.signature_offset,
mh01_header.signature_size,
) && chroot.carve_file(
ENCRYPTED_DATA_FILE_NAME,
mh01_data,
mh01_header.encrypted_data_offset,
mh01_header.encrypted_data_size,
);
// Try to decrypt the firmware
match delink::mh01::decrypt(mh01_data) {
Ok(decrypted_data) => {
// Write decrypted data to disk
result.success =
chroot.create_file(DECRYPTED_DATA_FILE_NAME, &decrypted_data);
}
Err(_) => {
// Decryption failture; extract each part of the firmware image, ensuring that each one extracts without error
result.success = chroot.carve_file(
IV_FILE_NAME,
mh01_data,
mh01_header.iv_offset,
mh01_header.iv_size,
) && chroot.carve_file(
SIGNATURE_FILE_NAME,
mh01_data,
mh01_header.signature_offset,
mh01_header.signature_size,
) && chroot.carve_file(
ENCRYPTED_DATA_FILE_NAME,
mh01_data,
mh01_header.encrypted_data_offset,
mh01_header.encrypted_data_size,
);
}
}
// No extraction requested, just return success
} else {
result.success = true;
Expand Down
71 changes: 0 additions & 71 deletions src/extractors/shrs.rs

This file was deleted.

Loading

0 comments on commit dda0001

Please sign in to comment.