-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
381 lines (344 loc) · 11.2 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use {
clap::Parser,
dashmap::{DashMap, DashSet},
indicatif::{ParallelProgressIterator, ProgressBar, ProgressFinish, ProgressStyle},
memmap2::Mmap,
rayon::iter::{IntoParallelIterator, ParallelIterator},
regex::bytes::Regex,
std::{
fmt::{Display, Formatter, LowerHex, Result},
fs::File,
hash::Hash,
mem::size_of,
num::TryFromIntError,
ops::{BitAnd, Sub},
slice::from_raw_parts,
thread,
time::Instant,
},
};
const PAGE_OFFSET_MASK: usize = 0xFFF;
enum Size {
Bits32,
Bits64,
}
impl Display for Size {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
Size::Bits32 => write!(f, "32-bit"),
Size::Bits64 => write!(f, "64-bit"),
}
}
}
enum Endian {
Little,
Big,
}
impl Display for Endian {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
Endian::Little => write!(f, "little"),
Endian::Big => write!(f, "big"),
}
}
}
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(help = "Name of the file to process")]
pub filename: String,
#[arg(
long = "32",
help = "File is 32-bit (default)",
conflicts_with = "is_64bit"
)]
is_32bit: bool,
#[arg(long = "64", help = "File is 64-bit", conflicts_with = "is_32bit")]
is_64bit: bool,
#[arg(
long = "little",
help = "File is little-endian (default)",
conflicts_with = "is_big_endian"
)]
is_little_endian: bool,
#[arg(
long = "big",
help = "File is big-endian",
conflicts_with = "is_little_endian"
)]
is_big_endian: bool,
#[arg(long = "max", help = "Maximum string length", default_value = "1024")]
pub max_string_length: usize,
#[arg(long = "min", help = "Minimum string length", default_value = "10")]
pub min_string_length: usize,
#[arg(
short = 's',
long = "max-strings",
help = "Maximum number of strings to sample",
default_value = "100000"
)]
pub max_strings: usize,
#[arg(
short = 'a',
long = "max-addresses",
help = "Maximum number of addresses to sample",
default_value = "1000000"
)]
pub max_addresses: usize,
}
impl Args {
pub fn size(&self) -> Size {
if self.is_64bit {
Size::Bits64
} else {
Size::Bits32
}
}
pub fn endian(&self) -> Endian {
if self.is_big_endian {
Endian::Big
} else {
Endian::Little
}
}
}
impl Display for Args {
fn fmt(&self, f: &mut Formatter) -> Result {
writeln!(f, "ARGS")?;
writeln!(f, "\tfile: {}", self.filename)?;
writeln!(f, "\tsize: {:}", self.size())?;
writeln!(f, "\tendian: {:}", self.endian())?;
writeln!(f, "\tmax: {}", self.max_string_length)?;
writeln!(f, "\tmin: {}", self.min_string_length)?;
writeln!(f, "\tmax strings: {}", self.max_strings)?;
writeln!(f, "\tmax addresses: {}", self.max_addresses)?;
Ok(())
}
}
/* Progress */
fn get_progress_bar(msg: &'static str, length: usize) -> indicatif::ProgressBar {
let progress_bar = ProgressBar::new(length as u64)
.with_message(format!("{msg:<50}"))
.with_finish(ProgressFinish::AndLeave);
progress_bar.set_style(
ProgressStyle::default_bar()
.template(
"{spinner:.green} [{elapsed_precise:.green}] [{eta_precise:.cyan}] {msg:.magenta} ({percent:.bold}%) [{bar:30.cyan/blue}]",
)
.unwrap()
.progress_chars("█░")
);
progress_bar
}
trait RBaseTraits<T, const N: usize>:
Copy
+ Send
+ Sync
+ Default
+ PartialEq
+ Eq
+ Hash
+ BitAnd<Output = T>
+ Sub<Output = T>
+ PartialOrd
+ LowerHex
+ TryFrom<usize, Error = TryFromIntError>
{
}
impl RBaseTraits<u32, { size_of::<u32>() }> for u32 {}
impl RBaseTraits<u64, { size_of::<u64>() }> for u64 {}
fn get_strings_by_page_offset<T: RBaseTraits<T, N>, const N: usize>(
bytes: &[u8],
min_string_length: usize,
max_string_length: usize,
max_strings: usize,
) -> DashMap<T, Vec<T>> {
/* Split the input into a number chunks which overlap by the maximum string length - 1 */
let chunk_size = bytes.len() / thread::available_parallelism().unwrap();
let limit = bytes.len();
let chunks: Vec<(usize, &[u8])> = (0..limit)
.step_by(chunk_size)
.map(|chunk_offset| {
(
chunk_offset,
&bytes
[chunk_offset..(chunk_offset + chunk_size + max_string_length - 1).min(limit)],
)
})
.collect();
/* Search each chunk for strings and collect them in a hash set */
let regex = format!(
"([[:print:][:space:]]{{{},{}}})\0",
min_string_length, max_string_length
);
let re = Regex::new(®ex).unwrap();
let offsets = DashSet::<T>::new();
let progress_bar = get_progress_bar("Finding strings", chunks.len());
chunks
.into_par_iter()
.progress_with(progress_bar)
.for_each(|(chunk_offset, chunk)| {
re.find_iter(chunk).for_each(|m| {
let file_offset = T::try_from(chunk_offset + m.start()).unwrap();
offsets.insert(file_offset);
});
});
println!("Found: {:?} strings", offsets.len());
/* Index each string by its page offset */
let index = DashMap::<T, Vec<T>>::new();
let progress_bar = get_progress_bar("Indexing strings", offsets.len());
let page_offset_mask = T::try_from(PAGE_OFFSET_MASK).unwrap();
offsets
.into_par_iter()
.take_any(max_strings)
.progress_with(progress_bar)
.for_each(|file_offset| {
let page_offset = file_offset & page_offset_mask;
if let Some(mut file_offsets) = index.get_mut(&page_offset) {
file_offsets.push(file_offset);
} else {
index.insert(page_offset, vec![file_offset]);
}
});
index
}
fn get_addresses_by_page_offset<T: RBaseTraits<T, N>, const N: usize>(
bytes: &[u8],
read_address_bytes: fn([u8; N]) -> T,
max_addresses: usize,
) -> DashMap<T, Vec<T>> {
let chunks = bytes
.chunks(size_of::<T>())
.map(|c| c.try_into().unwrap())
.collect::<Vec<[u8; N]>>();
/* Search each chunk for addresses and collect them in a hash set */
let progress_bar = get_progress_bar("Finding addresses", chunks.len());
let addresses = DashSet::<T>::new();
chunks
.into_par_iter()
.progress_with(progress_bar)
.map(|bytes| read_address_bytes(bytes))
.filter(|&address| address != T::default())
.for_each(|address| {
addresses.insert(address);
});
println!("Found: {:?} addresses", addresses.len());
/* Index each address by its page offset */
let index = DashMap::<T, Vec<T>>::new();
let progress_bar = get_progress_bar("Indexing addresses", addresses.len());
let page_offset_mask = T::try_from(PAGE_OFFSET_MASK).unwrap();
addresses
.into_par_iter()
.take_any(max_addresses)
.progress_with(progress_bar)
.for_each(|address| {
let page_offset = address & page_offset_mask;
if let Some(mut v) = index.get_mut(&page_offset) {
v.push(address);
} else {
index.insert(page_offset, vec![address]);
}
});
index
}
fn get_base_address<T: RBaseTraits<T, N>, const N: usize>(
args: &Args,
bytes: &[u8],
read_address_bytes: fn([u8; N]) -> T,
) -> Option<T> {
let strings_index = get_strings_by_page_offset(
bytes,
args.min_string_length,
args.max_string_length,
args.max_strings,
);
let addresses_index =
get_addresses_by_page_offset(bytes, read_address_bytes, args.max_addresses);
/* Subtract the string offsets from the addresses to determine candidate base addresses.
Update a hashtable with the frequency of each candidate base address.*/
let progress_bar = get_progress_bar("Collecting candidate base addresses", strings_index.len());
let base_addresses = DashMap::<T, usize>::new();
strings_index
.into_par_iter()
.progress_with(progress_bar)
.for_each(|(string_page_offset, string_file_offsets)| {
if let Some(addresses) = addresses_index.get(&string_page_offset) {
for &string_file_offset in string_file_offsets.iter() {
for &address in addresses
.iter()
.filter(|&&address| address >= string_file_offset)
{
*base_addresses
.entry(address - string_file_offset)
.or_insert(0) += 1;
}
}
}
});
let num_candidates = base_addresses.len();
println!("Found: {:?} candidate base addresses", num_candidates);
/* Filter out any candidates which don't appear more than once */
let recurring: DashMap<T, usize> = base_addresses
.into_par_iter()
.filter(|&(_k, v)| v > 1)
.collect();
println!(
"Found: {:?} recurring candidate base addresses",
recurring.len()
);
/* Sort the recurring candidates by frequency */
let mut sorted: Vec<(T, usize)> = recurring.into_iter().collect();
sorted.sort_by(|(_a1, v1), (_a2, v2)| v2.cmp(v1));
/* Print the top 10 candidates */
for (idx, (base, frequency)) in sorted.iter().take(10).enumerate() {
let pct = 100.0 * (*frequency as f64) / (num_candidates as f64);
println!(
"{:2}: 0x{base:0width$x}: {frequency} ({pct:.2}%)",
idx + 1,
width = N * 2
);
}
/* Return the most frequent candidate base address */
let (base, _frequency) = sorted.first().cloned()?;
Some(base)
}
fn main() {
let args = Args::parse();
println!("{:}", args);
let file = File::open(&args.filename).unwrap();
let map = unsafe { Mmap::map(&file).unwrap() };
let bytes = unsafe { from_raw_parts(map.as_ptr(), map.len()) };
let start = Instant::now();
match args.size() {
Size::Bits32 => {
if let Some(base) = get_base_address(
&args,
bytes,
match args.endian() {
Endian::Little => u32::from_le_bytes,
Endian::Big => u32::from_be_bytes,
},
) {
println!("Found base: {:0x}", base);
} else {
println!("No base found");
}
}
Size::Bits64 => {
if let Some(base) = get_base_address(
&args,
bytes,
match args.endian() {
Endian::Little => u64::from_le_bytes,
Endian::Big => u64::from_be_bytes,
},
) {
println!("Found base: {:x}", base);
} else {
println!("No base found");
}
}
};
let end = start.elapsed();
println!("Took: {:?}", end);
}