-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helpers for navigating a PE file resource directory
- Loading branch information
1 parent
927b511
commit fda29ca
Showing
6 changed files
with
714 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
//! A example binary that print the resource hierarchy inside a PE file. | ||
use object::{ | ||
endian::LittleEndian as LE, | ||
pe, | ||
read::pe::{ | ||
language_code_to_bcp47code, resource_type_name, ImageNtHeaders, ResourceDirectoryEntry, | ||
ResourceDirectoryTable, ResourceNameOrId, | ||
}, | ||
}; | ||
use std::{env, fs, process}; | ||
|
||
fn main() { | ||
let mut args = env::args(); | ||
if args.len() != 2 { | ||
eprintln!("Usage: {} <pe_file>", args.next().unwrap()); | ||
process::exit(1); | ||
} | ||
|
||
args.next(); | ||
let file_path = args.next().unwrap(); | ||
|
||
let file = match fs::File::open(&file_path) { | ||
Ok(file) => file, | ||
Err(err) => { | ||
eprintln!("Failed to open file '{}': {}", file_path, err,); | ||
process::exit(1); | ||
} | ||
}; | ||
let data = match unsafe { memmap2::Mmap::map(&file) } { | ||
Ok(mmap) => mmap, | ||
Err(err) => { | ||
eprintln!("Failed to map file '{}': {}", file_path, err,); | ||
process::exit(1); | ||
} | ||
}; | ||
let data = &*data; | ||
|
||
let kind = match object::FileKind::parse(data) { | ||
Ok(file) => file, | ||
Err(err) => { | ||
eprintln!("Failed to parse file: {}", err); | ||
process::exit(1); | ||
} | ||
}; | ||
let res = match kind { | ||
object::FileKind::Pe32 => dump_resource::<pe::ImageNtHeaders32>(data), | ||
object::FileKind::Pe64 => dump_resource::<pe::ImageNtHeaders64>(data), | ||
_ => { | ||
eprintln!("Not a PE file"); | ||
process::exit(1); | ||
} | ||
}; | ||
|
||
if let Err(err) = res { | ||
eprintln!("{}", err); | ||
process::exit(1); | ||
} | ||
} | ||
|
||
fn dump_resource<Pe: ImageNtHeaders>(data: &[u8]) -> Result<(), String> { | ||
let pe_file = object::read::pe::PeFile::<Pe>::parse(data).map_err(|e| e.to_string())?; | ||
match pe_file.resource_directory_table() { | ||
Ok(table) => { | ||
println!("- Root directory:"); | ||
dump_table(&table, 0); | ||
} | ||
Err(err) => return Err(format!("Couldn't found resource: {}", err)), | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn dump_table(dir: &ResourceDirectoryTable, level: usize) { | ||
let indent = level * 2 + 2; | ||
for entry in dir.iter() { | ||
match entry { | ||
ResourceDirectoryEntry::Directory(dir) => { | ||
println!("{:>width$}- Directory", "", width = indent); | ||
let indent = indent + 2; | ||
print_name(dir.name(), level); | ||
|
||
match dir.table() { | ||
Ok(table) => { | ||
dump_table(&table, level + 1); | ||
} | ||
Err(err) => { | ||
println!( | ||
"{:>width$}Could not read dir table: {}", | ||
"", | ||
err, | ||
width = indent | ||
); | ||
} | ||
} | ||
} | ||
ResourceDirectoryEntry::Entry(rsc) => { | ||
println!("{:>width$}- Entry", "", width = indent); | ||
let indent = indent + 2; | ||
print_name(rsc.name(), level); | ||
match rsc.data_entry() { | ||
Ok(data) => { | ||
println!( | ||
"{:>width$}Offset: {}", | ||
"", | ||
data.offset_to_data.get(LE), | ||
width = indent | ||
); | ||
println!("{:>width$}Size: {}", "", data.size.get(LE), width = indent); | ||
} | ||
Err(err) => { | ||
println!( | ||
"{:>width$}Could not read data entry: {}", | ||
"", | ||
err, | ||
width = indent | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn print_name(name: ResourceNameOrId, level: usize) { | ||
let indent = level * 2 + 4; | ||
match name { | ||
ResourceNameOrId::Name(name) => match name.to_string_lossy() { | ||
Ok(name) => println!("{:>width$}Name: {}", "", name, width = indent), | ||
_ => println!( | ||
"{:>width$}Name buffer: {:?}", | ||
"", | ||
name.data(), | ||
width = indent | ||
), | ||
}, | ||
ResourceNameOrId::Id(id) => { | ||
let mut done = false; | ||
if level == 0 { | ||
if let Some(name) = resource_type_name(id) { | ||
println!("{:>width$}Name from id: {}", "", name, width = indent); | ||
done = true; | ||
} | ||
} | ||
|
||
if level == 2 { | ||
if let Some(name) = language_code_to_bcp47code(id) { | ||
println!("{:>width$}Language: {}", "", name, width = indent); | ||
done = true; | ||
} | ||
} | ||
|
||
if !done { | ||
println!("{:>width$}Name id: {}", "", id, width = indent); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.