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

D-Link TLV file carver #758

Merged
merged 1 commit into from
Nov 22, 2024
Merged
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
1 change: 1 addition & 0 deletions src/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pub mod cab;
pub mod common;
pub mod csman;
pub mod dahua_zip;
pub mod dlink_tlv;
pub mod dmg;
pub mod dtb;
pub mod dumpifs;
Expand Down
66 changes: 66 additions & 0 deletions src/extractors/dlink_tlv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::extractors::common::{Chroot, ExtractionResult, Extractor, ExtractorType};
use crate::structures::dlink_tlv::parse_dlink_tlv_header;

/// Defines the internal extractor function for carving out D-Link TLV firmware images
///
/// ```
/// use std::io::ErrorKind;
/// use std::process::Command;
/// use binwalk::extractors::common::ExtractorType;
/// use binwalk::extractors::dlink_tlv::dlink_tlv_extractor;
///
/// match dlink_tlv_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 dlink_tlv_extractor() -> Extractor {
Extractor {
utility: ExtractorType::Internal(extract_dlink_tlv_image),
..Default::default()
}
}

/// Internal extractor for carve pieces of D-Link TLV images to disk
pub fn extract_dlink_tlv_image(
file_data: &[u8],
offset: usize,
output_directory: Option<&String>,
) -> ExtractionResult {
const OUTPUT_FILE_NAME: &str = "image.bin";

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

// Get the D-Link TLV image data
if let Some(tlv_data) = file_data.get(offset..) {
// Parse the TLV header
if let Ok(tlv_header) = parse_dlink_tlv_header(tlv_data) {
result.success = true;
result.size = Some(tlv_header.header_size + tlv_header.data_size);

// If extraction was requested, do it
if output_directory.is_some() {
let chroot = Chroot::new(output_directory);
result.success = chroot.carve_file(
OUTPUT_FILE_NAME,
tlv_data,
tlv_header.header_size,
tlv_header.data_size,
);
}
}
}

result
}
2 changes: 1 addition & 1 deletion src/magic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ pub fn patterns() -> Vec<signatures::common::Signature> {
magic: signatures::dlink_tlv::dlink_tlv_magic(),
parser: signatures::dlink_tlv::dlink_tlv_parser,
description: signatures::dlink_tlv::DESCRIPTION.to_string(),
extractor: None,
extractor: Some(extractors::dlink_tlv::dlink_tlv_extractor()),
},
];

Expand Down
12 changes: 11 additions & 1 deletion src/signatures/dlink_tlv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::signatures::common::{SignatureError, SignatureResult, CONFIDENCE_HIGH};
use crate::signatures::openssl::openssl_crypt_parser;
use crate::structures::dlink_tlv::parse_dlink_tlv_header;

/// Human readable description
Expand Down Expand Up @@ -37,7 +38,7 @@ pub fn dlink_tlv_parser(

// Make sure the MD5 hashes match
if payload_md5 == tlv_header.data_checksum {
result.size = tlv_header.header_size;
result.size = tlv_header.header_size + tlv_header.data_size;
result.description = format!(
"{}, model name: {}, board ID: {}, header size: {} bytes, data size: {} bytes",
result.description,
Expand All @@ -46,6 +47,15 @@ pub fn dlink_tlv_parser(
tlv_header.header_size,
tlv_header.data_size,
);

// Check if the firmware data is OpenSSL encrypted
if let Some(crypt_data) = file_data.get(offset + tlv_header.header_size..) {
if let Ok(openssl_signature) = openssl_crypt_parser(crypt_data, 0) {
result.description =
format!("{}, {}", result.description, openssl_signature.description);
}
}

return Ok(result);
}
}
Expand Down