Skip to content

Commit

Permalink
Lint fixes in signatures & extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
devttys0 committed Oct 21, 2024
1 parent 0d9922c commit c1140ee
Show file tree
Hide file tree
Showing 69 changed files with 265 additions and 292 deletions.
54 changes: 25 additions & 29 deletions src/binwalk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,39 +252,35 @@ impl Binwalk {
let magic_start = FILE_START_OFFSET + signature.magic_offset;
let magic_end = magic_start + magic.len();

if file_data.len() > magic_end {
if file_data[magic_start..magic_end] == magic {
debug!(
"Found {} short magic match at offset {:#X}",
signature.description, magic_start
);

if let Ok(mut signature_result) = (signature.parser)(file_data, magic_start)
{
// Auto populate some signature result fields
signature_result_auto_populate(&mut signature_result, signature);
if file_data.len() > magic_end && file_data[magic_start..magic_end] == magic {
debug!(
"Found {} short magic match at offset {:#X}",
signature.description, magic_start
);

// Add this signature to the file map
file_map.push(signature_result.clone());
info!(
"Found valid {} short signature at offset {:#X}",
signature_result.name, FILE_START_OFFSET
);
if let Ok(mut signature_result) = (signature.parser)(file_data, magic_start) {
// Auto populate some signature result fields
signature_result_auto_populate(&mut signature_result, signature);

// Only update the next_valid_offset if confidence is at least medium
if signature_result.confidence >= signatures::common::CONFIDENCE_MEDIUM
{
next_valid_offset = signature_result.offset + signature_result.size;
}
// Add this signature to the file map
file_map.push(signature_result.clone());
info!(
"Found valid {} short signature at offset {:#X}",
signature_result.name, FILE_START_OFFSET
);

// Only one signature can match at fixed offset 0
break;
} else {
debug!(
"{} short signature match at offset {:#X} is invalid",
signature.description, FILE_START_OFFSET
);
// Only update the next_valid_offset if confidence is at least medium
if signature_result.confidence >= signatures::common::CONFIDENCE_MEDIUM {
next_valid_offset = signature_result.offset + signature_result.size;
}

// Only one signature can match at fixed offset 0
break;
} else {
debug!(
"{} short signature match at offset {:#X} is invalid",
signature.description, FILE_START_OFFSET
);
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/extractors/arcadyan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ pub fn extract_obfuscated_lzma(
let available_data: usize = file_data.len() - offset;

// Sanity check data size
if available_data <= MAX_DATA_SIZE {
if available_data > MIN_DATA_SIZE {
// De-obfuscate the LZMA data
let deobfuscated_data = arcadyan_deobfuscator(&file_data[offset..]);

// Do a decompression on the LZMA data (actual LZMA data starts 4 bytes into the deobfuscated data)
result = lzma_decompress(&deobfuscated_data, LZMA_DATA_OFFSET, output_directory);
}
if available_data <= MAX_DATA_SIZE && available_data > MIN_DATA_SIZE {
// De-obfuscate the LZMA data
let deobfuscated_data = arcadyan_deobfuscator(&file_data[offset..]);

// Do a decompression on the LZMA data (actual LZMA data starts 4 bytes into the deobfuscated data)
result = lzma_decompress(&deobfuscated_data, LZMA_DATA_OFFSET, output_directory);
}

result
Expand Down
10 changes: 4 additions & 6 deletions src/extractors/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,11 +810,9 @@ pub fn execute(
result.do_not_recurse = extractor_definition.do_not_recurse;

// If the extractor reported success, make sure it extracted something other than just an empty file
if result.success {
if !was_something_extracted(&result.output_directory) {
result.success = false;
warn!("Extractor exited successfully, but no data was extracted");
}
if result.success && !was_something_extracted(&result.output_directory) {
result.success = false;
warn!("Extractor exited successfully, but no data was extracted");
}
}
}
Expand Down Expand Up @@ -914,7 +912,7 @@ fn spawn(
Ok(child) => {
// If the process was spawned successfully, return some information about the process
let proc_info = ProcInfo {
child: child,
child,
carved_file: carved_file.clone(),
exit_codes: extractor.exit_codes,
};
Expand Down
6 changes: 2 additions & 4 deletions src/extractors/jpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ fn get_jpeg_data_size(jpeg_data: &[u8]) -> Option<usize> {
let eof_candidate: usize = eof_match.start() + EOF_SIZE;

// Make sure the expected EOF marker is not immediately followed by 0xFF (which would indicate the JPEG continues...)
if eof_candidate < jpeg_data.len() {
if jpeg_data[eof_candidate] == JPEG_DELIM {
continue;
}
if eof_candidate < jpeg_data.len() && jpeg_data[eof_candidate] == JPEG_DELIM {
continue;
}

return Some(eof_match.start() + EOF_SIZE);
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn aes_sbox_parser(
) -> Result<SignatureResult, SignatureError> {
// Successful return value
let result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_LOW,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/androidsparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn android_sparse_parser(
) -> Result<SignatureResult, SignatureError> {
// Default result, returned on success
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_HIGH,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/bzip2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn bzip2_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult,
// Return value
let mut result = SignatureResult {
description: DESCRIPTION.to_string(),
offset: offset,
offset,
confidence: CONFIDENCE_HIGH,
..Default::default()
};
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/cab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn cab_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, Si
cab_header.header_size,
cab_header.total_size
),
offset: offset,
offset,
size: cab_header.total_size,
confidence: CONFIDENCE_MEDIUM,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/chk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn chk_magic() -> Vec<Vec<u8>> {
pub fn chk_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
// Successful return value
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_MEDIUM,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/compressd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn compressd_parser(
) -> Result<SignatureResult, SignatureError> {
// Successful return value; confidence is medium since this only matches magic bytes at the beginning of a file
let result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_MEDIUM,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/copyright.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn copyright_parser(

// Successful return value
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_HIGH,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/cpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn cpio_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, S
let mut header_count: usize = 0;
let mut result = SignatureResult {
description: DESCRIPTION.to_string(),
offset: offset,
offset,
confidence: CONFIDENCE_HIGH,
..Default::default()
};
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/deb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn deb_magic() -> Vec<Vec<u8>> {
pub fn deb_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
// Successful return value
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_HIGH,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/dlob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn dlob_magic() -> Vec<Vec<u8>> {
pub fn dlob_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
// Successful return value
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_MEDIUM,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/dtb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn dtb_magic() -> Vec<Vec<u8>> {
pub fn dtb_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
// Sucessful result
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_MEDIUM,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/ecos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn exception_handler_parser(
) -> Result<SignatureResult, SignatureError> {
// Successful return value
let mut result = SignatureResult {
offset: offset,
offset,
description: EXCEPTION_HANDLER_DESCRIPTION.to_string(),
confidence: CONFIDENCE_LOW,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn elf_magic() -> Vec<Vec<u8>> {
pub fn elf_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
// Successful result
let mut result = SignatureResult {
offset: offset,
offset,
name: "elf".to_string(),
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_MEDIUM,
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/gif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn gif_magic() -> Vec<Vec<u8>> {
pub fn gif_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
// Successful return value
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_HIGH,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/gpg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn gpg_signed_parser(
) -> Result<SignatureResult, SignatureError> {
// Success result; confidence is high since this signature is only reported what it starts at the beginning of a file
let result = SignatureResult {
offset: offset,
offset,
confidence: CONFIDENCE_HIGH,
description: GPG_SIGNED_DESCRIPTION.to_string(),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/gzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn gzip_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, S
gzip_header.size + deflate_data_size + GZIP_CRC_SIZE + GZIP_ISIZE_SIZE;

return Ok(SignatureResult {
offset: offset,
offset,
size: total_size,
confidence: CONFIDENCE_HIGH,
description: format!(
Expand Down
4 changes: 2 additions & 2 deletions src/signatures/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn crc32_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult,
CRC32_DESCRIPTION,
hash_endianess(file_data, offset, crc32_magic())
),
offset: offset,
offset,
size: HASH_MAGIC_LEN,
..Default::default()
};
Expand All @@ -53,7 +53,7 @@ pub fn sha256_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult,
SHA256_DESCRIPTION,
hash_endianess(file_data, offset, sha256_magic())
),
offset: offset,
offset,
size: HASH_MAGIC_LEN,
..Default::default()
};
Expand Down
4 changes: 2 additions & 2 deletions src/signatures/jboot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn jboot_stag_parser(
) -> Result<SignatureResult, SignatureError> {
// Successful return value
let mut result = SignatureResult {
offset: offset,
offset,
description: JBOOT_STAG_DESCRIPTION.to_string(),
confidence: CONFIDENCE_LOW,
..Default::default()
Expand Down Expand Up @@ -117,7 +117,7 @@ pub fn jboot_sch2_parser(
) -> Result<SignatureResult, SignatureError> {
// Successful return value
let mut result = SignatureResult {
offset: offset,
offset,
description: JBOOT_SCH2_DESCRIPTION.to_string(),
confidence: CONFIDENCE_HIGH,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/jffs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn jffs2_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult,

let mut result = SignatureResult {
size: 0,
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_HIGH,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/jpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn jpeg_magic() -> Vec<Vec<u8>> {
pub fn jpeg_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
// Successful return value
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_MEDIUM,
..Default::default()
Expand Down
4 changes: 2 additions & 2 deletions src/signatures/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn linux_boot_image_parser(

let result = SignatureResult {
description: LINUX_BOOT_IMAGE_DESCRIPTION.to_string(),
offset: offset,
offset,
size: 0,
..Default::default()
};
Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn linux_kernel_version_parser(
const GCC_VERSION_STRING: &str = "gcc ";

let mut result = SignatureResult {
offset: offset,
offset,
confidence: CONFIDENCE_LOW,
..Default::default()
};
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/luks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn luks_magic() -> Vec<Vec<u8>> {
pub fn luks_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
// Successful result
let mut result = SignatureResult {
offset: offset,
offset,
name: "luks".to_string(),
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_MEDIUM,
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/lz4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn lz4_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, Si
const CONTENT_CHECKSUM_LEN: usize = 4;

let mut result = SignatureResult {
offset: offset,
offset,
confidence: CONFIDENCE_MEDIUM,
description: DESCRIPTION.to_string(),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/lzfse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn lzfse_magic() -> Vec<Vec<u8>> {
/// Validate LZFSE signatures
pub fn lzfse_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
let mut result = SignatureResult {
offset: offset,
offset,
confidence: CONFIDENCE_HIGH,
description: DESCRIPTION.to_string(),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/lzma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn lzma_magic() -> Vec<Vec<u8>> {
pub fn lzma_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
// Success return value
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_HIGH,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/lzop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn lzop_magic() -> Vec<Vec<u8>> {
pub fn lzop_parser(file_data: &[u8], offset: usize) -> Result<SignatureResult, SignatureError> {
// Success retrun value
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_HIGH,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/signatures/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn openssl_crypt_parser(
) -> Result<SignatureResult, SignatureError> {
// Success return value
let mut result = SignatureResult {
offset: offset,
offset,
description: DESCRIPTION.to_string(),
confidence: CONFIDENCE_LOW,
..Default::default()
Expand Down
Loading

0 comments on commit c1140ee

Please sign in to comment.