| 1 |
use crate::error; |
| 2 |
use alloc::string::ToString; |
| 3 |
use alloc::vec::Vec; |
| 4 |
use scroll::Pread; |
| 5 |
|
| 6 |
use super::options; |
| 7 |
use super::section_table; |
| 8 |
|
| 9 |
use crate::pe::data_directories::DataDirectory; |
| 10 |
use core::cmp; |
| 11 |
|
| 12 |
use log::debug; |
| 13 |
|
| 14 |
pub fn is_in_range(rva: usize, r1: usize, r2: usize) -> bool { |
| 15 |
r1 <= rva && rva < r2 |
| 16 |
} |
| 17 |
|
| 18 |
// reference: Peter Ferrie. Reliable algorithm to extract overlay of a PE. https://bit.ly/2vBX2bR |
| 19 |
#[inline] |
| 20 |
fn aligned_pointer_to_raw_data(pointer_to_raw_data: usize) -> usize { |
| 21 |
const PHYSICAL_ALIGN: usize = 0x1ff; |
| 22 |
pointer_to_raw_data & !PHYSICAL_ALIGN |
| 23 |
} |
| 24 |
|
| 25 |
#[inline] |
| 26 |
fn section_read_size(section: §ion_table::SectionTable, file_alignment: u32) -> usize { |
| 27 |
fn round_size(size: usize) -> usize { |
| 28 |
const PAGE_MASK: usize = 0xfff; |
| 29 |
(size + PAGE_MASK) & !PAGE_MASK |
| 30 |
} |
| 31 |
|
| 32 |
// Paraphrased from https://reverseengineering.stackexchange.com/a/4326 (by Peter Ferrie). |
| 33 |
// |
| 34 |
// Handles the corner cases such as mis-aligned pointers (round down) and sizes (round up) |
| 35 |
// Further rounding corner cases: |
| 36 |
// - the physical pointer should be rounded down to a multiple of 512, regardless of the value in the header |
| 37 |
// - the read size is rounded up by using a combination of the file alignment and 4kb |
| 38 |
// - the virtual size is always rounded up to a multiple of 4kb, regardless of the value in the header. |
| 39 |
// |
| 40 |
// Reference C implementation: |
| 41 |
// |
| 42 |
// long pointerToRaw = section.get(POINTER_TO_RAW_DATA); |
| 43 |
// long alignedpointerToRaw = pointerToRaw & ~0x1ff; |
| 44 |
// long sizeOfRaw = section.get(SIZE_OF_RAW_DATA); |
| 45 |
// long readsize = ((pointerToRaw + sizeOfRaw) + filealign - 1) & ~(filealign - 1)) - alignedpointerToRaw; |
| 46 |
// readsize = min(readsize, (sizeOfRaw + 0xfff) & ~0xfff); |
| 47 |
// long virtsize = section.get(VIRTUAL_SIZE); |
| 48 |
// |
| 49 |
// if (virtsize) |
| 50 |
// { |
| 51 |
// readsize = min(readsize, (virtsize + 0xfff) & ~0xfff); |
| 52 |
// } |
| 53 |
|
| 54 |
let file_alignment = file_alignment as usize; |
| 55 |
let size_of_raw_data = section.size_of_raw_data as usize; |
| 56 |
let virtual_size = section.virtual_size as usize; |
| 57 |
let read_size = { |
| 58 |
let read_size = |
| 59 |
((section.pointer_to_raw_data as usize + size_of_raw_data + file_alignment - 1) |
| 60 |
& !(file_alignment - 1)) |
| 61 |
- aligned_pointer_to_raw_data(section.pointer_to_raw_data as usize); |
| 62 |
cmp::min(read_size, round_size(size_of_raw_data)) |
| 63 |
}; |
| 64 |
|
| 65 |
if virtual_size == 0 { |
| 66 |
read_size |
| 67 |
} else if read_size == 0 { |
| 68 |
virtual_size |
| 69 |
} else { |
| 70 |
cmp::min(read_size, round_size(virtual_size)) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
fn rva2offset(rva: usize, section: §ion_table::SectionTable) -> usize { |
| 75 |
(rva - section.virtual_address as usize) |
| 76 |
+ aligned_pointer_to_raw_data(section.pointer_to_raw_data as usize) |
| 77 |
} |
| 78 |
|
| 79 |
fn is_in_section(rva: usize, section: §ion_table::SectionTable, file_alignment: u32) -> bool { |
| 80 |
let section_rva = section.virtual_address as usize; |
| 81 |
is_in_range( |
| 82 |
rva, |
| 83 |
section_rva, |
| 84 |
section_rva + section_read_size(section, file_alignment), |
| 85 |
) |
| 86 |
} |
| 87 |
|
| 88 |
pub fn find_offset( |
| 89 |
rva: usize, |
| 90 |
sections: &[section_table::SectionTable], |
| 91 |
file_alignment: u32, |
| 92 |
opts: &options::ParseOptions, |
| 93 |
) -> Option<usize> { |
| 94 |
if opts.resolve_rva { |
| 95 |
if file_alignment == 0 || file_alignment & (file_alignment - 1) != 0 { |
| 96 |
return None; |
| 97 |
} |
| 98 |
for (i, section) in sections.iter().enumerate() { |
| 99 |
debug!( |
| 100 |
"Checking {} for {:#x} ∈ {:#x}..{:#x}", |
| 101 |
section.name().unwrap_or(""), |
| 102 |
rva, |
| 103 |
section.virtual_address, |
| 104 |
section.virtual_address + section.virtual_size |
| 105 |
); |
| 106 |
if is_in_section(rva, §ion, file_alignment) { |
| 107 |
let offset = rva2offset(rva, §ion); |
| 108 |
debug!( |
| 109 |
"Found in section {}({}), remapped into offset {:#x}", |
| 110 |
section.name().unwrap_or(""), |
| 111 |
i, |
| 112 |
offset |
| 113 |
); |
| 114 |
return Some(offset); |
| 115 |
} |
| 116 |
} |
| 117 |
None |
| 118 |
} else { |
| 119 |
Some(rva) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
pub fn find_offset_or( |
| 124 |
rva: usize, |
| 125 |
sections: &[section_table::SectionTable], |
| 126 |
file_alignment: u32, |
| 127 |
opts: &options::ParseOptions, |
| 128 |
msg: &str, |
| 129 |
) -> error::Result<usize> { |
| 130 |
find_offset(rva, sections, file_alignment, opts) |
| 131 |
.ok_or_else(|| error::Error::Malformed(msg.to_string())) |
| 132 |
} |
| 133 |
|
| 134 |
pub fn try_name<'a>( |
| 135 |
bytes: &'a [u8], |
| 136 |
rva: usize, |
| 137 |
sections: &[section_table::SectionTable], |
| 138 |
file_alignment: u32, |
| 139 |
opts: &options::ParseOptions, |
| 140 |
) -> error::Result<&'a str> { |
| 141 |
match find_offset(rva, sections, file_alignment, opts) { |
| 142 |
Some(offset) => Ok(bytes.pread::<&str>(offset)?), |
| 143 |
None => Err(error::Error::Malformed(format!( |
| 144 |
"Cannot find name from rva {:#x} in sections: {:?}", |
| 145 |
rva, sections |
| 146 |
))), |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
pub fn get_data<'a, T>( |
| 151 |
bytes: &'a [u8], |
| 152 |
sections: &[section_table::SectionTable], |
| 153 |
directory: DataDirectory, |
| 154 |
file_alignment: u32, |
| 155 |
) -> error::Result<T> |
| 156 |
where |
| 157 |
T: scroll::ctx::TryFromCtx<'a, scroll::Endian, Error = scroll::Error>, |
| 158 |
{ |
| 159 |
get_data_with_opts( |
| 160 |
bytes, |
| 161 |
sections, |
| 162 |
directory, |
| 163 |
file_alignment, |
| 164 |
&options::ParseOptions::default(), |
| 165 |
) |
| 166 |
} |
| 167 |
|
| 168 |
pub fn get_data_with_opts<'a, T>( |
| 169 |
bytes: &'a [u8], |
| 170 |
sections: &[section_table::SectionTable], |
| 171 |
directory: DataDirectory, |
| 172 |
file_alignment: u32, |
| 173 |
opts: &options::ParseOptions, |
| 174 |
) -> error::Result<T> |
| 175 |
where |
| 176 |
T: scroll::ctx::TryFromCtx<'a, scroll::Endian, Error = scroll::Error>, |
| 177 |
{ |
| 178 |
let rva = directory.virtual_address as usize; |
| 179 |
let offset = find_offset(rva, sections, file_alignment, opts) |
| 180 |
.ok_or_else(|| error::Error::Malformed(directory.virtual_address.to_string()))?; |
| 181 |
let result: T = bytes.pread_with(offset, scroll::LE)?; |
| 182 |
Ok(result) |
| 183 |
} |
| 184 |
|
| 185 |
pub(crate) fn pad(length: usize, alignment: Option<usize>) -> Option<Vec<u8>> { |
| 186 |
match alignment { |
| 187 |
Some(alignment) => { |
| 188 |
let overhang = length % alignment; |
| 189 |
if overhang != 0 { |
| 190 |
let repeat = alignment - overhang; |
| 191 |
Some(vec![0u8; repeat]) |
| 192 |
} else { |
| 193 |
None |
| 194 |
} |
| 195 |
} |
| 196 |
None => None, |
| 197 |
} |
| 198 |
} |