inspect.rs 4.5 KB

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