inspect.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. use std::collections::BTreeMap;
  2. use std::path::Path;
  3. use anyhow::{Context, Result};
  4. use victory_pe::implib::ImportLibrary;
  5. use victory_pe::{ImageInfo, layout};
  6. pub fn run(file: &Path, list_imports: bool, list_exports: bool) -> Result<()> {
  7. let data = std::fs::read(file).with_context(|| format!("reading {}", file.display()))?;
  8. let info = ImageInfo::parse(&data).with_context(|| format!("parsing {}", file.display()))?;
  9. println!("file {}", file.display());
  10. println!("file size {} bytes", data.len());
  11. println!("blake3 {}", blake3::hash(&data).to_hex());
  12. if let Some(cv) = &info.codeview {
  13. println!("pdb {} (key {})", cv.pdb_path, cv.symbol_key());
  14. }
  15. println!(
  16. "kind {}",
  17. if info.is_dll() { "dll" } else { "exe" }
  18. );
  19. println!(
  20. "subsystem {} ({})",
  21. info.subsystem,
  22. info.subsystem_name()
  23. );
  24. println!(
  25. "image base {:#x} size {:#x}",
  26. info.image_base, info.size_of_image
  27. );
  28. println!("entry rva {:#x}", info.entry_rva);
  29. println!(
  30. "stack reserve {:#x} commit {:#x}",
  31. info.stack_reserve, info.stack_commit
  32. );
  33. println!(
  34. "dll flags {:#06x}{}",
  35. info.dll_characteristics,
  36. dll_flags(info.dll_characteristics)
  37. );
  38. println!(
  39. "relocations {} exception table {} load config {}",
  40. yes(info.has_relocations),
  41. yes(info.has_exception_table),
  42. yes(info.has_load_config)
  43. );
  44. if let Some(tls) = &info.tls {
  45. println!("tls {} callback(s)", tls.callback_rvas.len());
  46. }
  47. println!("\nsections");
  48. for s in &info.sections {
  49. println!(
  50. " {:<8} rva {:#010x} vsize {:#010x} raw {:#010x} flags {:#010x}",
  51. s.name.trim_end_matches('\0'),
  52. s.rva,
  53. s.virtual_size,
  54. s.file_size,
  55. s.characteristics
  56. );
  57. }
  58. println!(
  59. "\nimports {} functions from {} DLLs",
  60. info.import_count(),
  61. info.imports.len()
  62. );
  63. for m in &info.imports {
  64. println!(" {:<48} {:>5}", m.dll, m.entries.len());
  65. if list_imports {
  66. for e in &m.entries {
  67. println!(" {}", e.symbol);
  68. }
  69. }
  70. }
  71. if !info.delay_imports.is_empty() {
  72. println!("\ndelay-load imports");
  73. for m in &info.delay_imports {
  74. println!(" {:<48} {:>5}", m.dll, m.entries.len());
  75. if list_imports {
  76. for e in &m.entries {
  77. println!(" {}", e.symbol);
  78. }
  79. }
  80. }
  81. }
  82. println!("\nexports {}", info.exports.len());
  83. if list_exports {
  84. for e in &info.exports {
  85. let name = e.name.as_deref().unwrap_or("(by ordinal)");
  86. match (&e.rva, &e.forward) {
  87. (_, Some(fwd)) => println!(" {:>5} {name} -> {fwd}", e.ordinal),
  88. (Some(rva), _) => println!(" {:>5} {name} {rva:#x}", e.ordinal),
  89. _ => println!(" {:>5} {name}", e.ordinal),
  90. }
  91. }
  92. }
  93. Ok(())
  94. }
  95. pub fn lib_exports(lib: &Path, list: bool) -> Result<()> {
  96. let data = std::fs::read(lib).with_context(|| format!("reading {}", lib.display()))?;
  97. let parsed =
  98. ImportLibrary::parse(&data).with_context(|| format!("parsing {}", lib.display()))?;
  99. let mut by_dll: BTreeMap<String, Vec<String>> = BTreeMap::new();
  100. for i in &parsed.imports {
  101. by_dll
  102. .entry(i.dll.to_ascii_lowercase())
  103. .or_default()
  104. .push(i.export.to_string());
  105. }
  106. println!(
  107. "{} exports from {} DLLs ({} other members)",
  108. parsed.imports.len(),
  109. by_dll.len(),
  110. parsed.other_members
  111. );
  112. for (dll, names) in &by_dll {
  113. println!("{dll:<60} {:>5}", names.len());
  114. if list {
  115. for n in names {
  116. println!(" {n}");
  117. }
  118. }
  119. }
  120. Ok(())
  121. }
  122. fn yes(b: bool) -> &'static str {
  123. if b { "yes" } else { "no" }
  124. }
  125. fn dll_flags(v: u16) -> String {
  126. use layout::dll_flags::*;
  127. let names = [
  128. (HIGH_ENTROPY_VA, "high-entropy-va"),
  129. (DYNAMIC_BASE, "dynamic-base"),
  130. (FORCE_INTEGRITY, "force-integrity"),
  131. (NX_COMPAT, "nx"),
  132. (APPCONTAINER, "appcontainer"),
  133. (GUARD_CF, "cfg"),
  134. ];
  135. let set: Vec<_> = names
  136. .iter()
  137. .filter(|(f, _)| v & f != 0)
  138. .map(|(_, n)| *n)
  139. .collect();
  140. if set.is_empty() {
  141. String::new()
  142. } else {
  143. format!(" ({})", set.join(", "))
  144. }
  145. }