frnsc-prefetch 0.13.3

Pure rust windows prefetch parser implementation
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use std::{borrow::Cow, path::PathBuf};

use forensic_rs::{
    activity::{ForensicActivity, ProgramExecution, SessionId},
    data::ForensicData,
    dictionary::*,
    err::{ForensicError, ForensicResult},
    field::{Field, Text},
    traits::forensic::{IntoActivity, IntoTimeline, TimeContext, TimelineData},
    utils::time::Filetime,
};

/// By default blocks will be loaded into executable memory sections
pub const FLAG_PROGRAM_BLOCK_EXECUTABLE: u32 = 0x0200;

/// By default blocks will be loaded as resources, not executable
pub const FLAG_PROGRAM_BLOCK_RESOURCE: u32 = 0x0002;

/// By default blocks should not be prefetched, should be pulled from disk.
pub const FLAG_PROGRAM_BLOCK_DONT_PREFETCH: u32 = 0x0001;

/// The block is loaded into a executable memory section
pub const FLAG_BLOCK_EXECUTABLE: u8 = 0x02;

/// The block is loaded as resouce
pub const FLAG_BLOCK_RESOURCE: u8 = 0x04;
/// The block is forced to be prefetched
pub const FLAG_BLOCK_FORCE_PREFETCH: u8 = 0x08;
/// The block will not be prefetched, should be pulled from disk.
pub const FLAG_BLOCK_DONT_PREFETCH: u8 = 0x01;

#[derive(Debug, Clone, Default)]
pub struct PrefetchFile {
    /// Prefetch file version
    pub version: u32,
    /// Executable name
    pub name: String,
    /// List of DLLs/EXEs loaded by the executable
    pub metrics: Vec<Metric>,
    /// Last execution times (max 8)
    pub last_run_times: Vec<Filetime>,
    /// Number of times executed
    pub run_count: u32,
    /// Information about the disks and other volumes
    pub volume: Vec<VolumeInformation>,
}
#[derive(Clone, Debug, Default)]
pub struct PrefetchFileInformation {
    pub metrics_offsets: u32,
    pub metrics_count: u32,
    pub trace_chain_offset: u32,
    pub trace_chain_count: u32,
    pub filename_string_offset: u32,
    pub filename_string_size: u32,
    pub volume_information_offset: u32,
    pub volume_count: u32,
    pub volume_information_size: u32,
    pub last_run_times: Vec<Filetime>,
    pub run_count: u32,
}

/// Files loaded by the executable
#[derive(Debug, Clone, Default)]
pub struct Metric {
    /// Full path to the dependency. Ex: File=\VOLUME{01d962d37536cd21-a2691d2c}\WINDOWS\SYSTEM32\NTDLL.DLL
    pub file: String,
    /// Default flags for loading blocks: executable, resource or non-prefetchable.
    pub flags: PrefetchFlag,
    /// Number of blocks to be prefetched
    pub blocks_to_prefetch: u32,
    /// Traces for this dependency
    pub traces: Vec<Trace>,
}
#[derive(Debug, Clone, Default)]
pub struct Trace {
    /// Flags for loading blocks: executable, resource, non-prefetchable or force prefetch.
    pub flags: BlockFlags,
    /// Memory block offset
    pub block_offset: u32,
    /// Stores whether the block was used in each of the last eight runs (1 bit each)
    pub used_bitfield: u8,
    /// Stores whether the block was prefetched in each of the last eight runs (1 bit each)
    pub prefetched_bitfield: u8,
}
#[derive(Clone, Default)]
pub struct PrefetchFlag(u32);

impl PrefetchFlag {
    pub fn is_executable(&self) -> bool {
        self.0 & FLAG_PROGRAM_BLOCK_EXECUTABLE > 0
    }
    pub fn is_resource(&self) -> bool {
        self.0 & FLAG_PROGRAM_BLOCK_RESOURCE > 0
    }
    pub fn is_not_prefetched(&self) -> bool {
        self.0 & FLAG_PROGRAM_BLOCK_DONT_PREFETCH > 0
    }
}
impl From<u32> for PrefetchFlag {
    fn from(value: u32) -> Self {
        Self(value)
    }
}
impl core::fmt::Debug for PrefetchFlag {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut writed = 0;
        if self.is_executable() {
            f.write_str("X")?;
            writed += 1;
        }
        if self.is_resource() {
            f.write_str("R")?;
            writed += 1;
        }
        if self.is_not_prefetched() {
            f.write_str("D")?;
            writed += 1;
        }
        if writed == 0 {
            f.write_str("-")?;
        }
        Ok(())
    }
}

impl core::fmt::Display for PrefetchFlag {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut writed = 0;
        if self.is_executable() {
            f.write_str("X")?;
            writed += 1;
        }
        if self.is_resource() {
            f.write_str("R")?;
            writed += 1;
        }
        if self.is_not_prefetched() {
            f.write_str("D")?;
            writed += 1;
        }
        if writed == 0 {
            f.write_str("-")?;
        }
        Ok(())
    }
}
#[derive(Clone, Default)]
pub struct BlockFlags(u8);

impl BlockFlags {
    pub fn is_executable(&self) -> bool {
        self.0 & FLAG_BLOCK_EXECUTABLE > 0
    }
    pub fn is_resource(&self) -> bool {
        self.0 & FLAG_BLOCK_RESOURCE > 0
    }
    pub fn is_not_prefetched(&self) -> bool {
        self.0 & FLAG_BLOCK_DONT_PREFETCH > 0
    }
    pub fn is_force_prefetch(&self) -> bool {
        self.0 & FLAG_BLOCK_FORCE_PREFETCH > 0
    }
}

impl core::fmt::Debug for BlockFlags {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut writed = 0;
        if self.is_executable() {
            f.write_str("X")?;
            writed += 1;
        }
        if self.is_resource() {
            f.write_str("R")?;
            writed += 1;
        }
        if self.is_force_prefetch() {
            f.write_str("F")?;
            writed += 1;
        }
        if self.is_not_prefetched() {
            f.write_str("D")?;
            writed += 1;
        }
        if writed == 0 {
            f.write_str("-")?;
        }
        Ok(())
    }
}

impl From<u8> for BlockFlags {
    fn from(value: u8) -> Self {
        Self(value)
    }
}

impl core::fmt::Display for BlockFlags {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut writed = 0;
        if self.is_executable() {
            f.write_str("X")?;
            writed += 1;
        }
        if self.is_resource() {
            f.write_str("R")?;
            writed += 1;
        }
        if self.is_force_prefetch() {
            f.write_str("F")?;
            writed += 1;
        }
        if self.is_not_prefetched() {
            f.write_str("D")?;
            writed += 1;
        }
        if writed == 0 {
            f.write_str("-")?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Default)]
pub struct VolumeInformation {
    pub device_path: String,
    pub file_references: Vec<NtfsFile>,
    pub directory_strings: Vec<String>,
    pub creation_time: u64,
    pub serial_number: u32,
}

#[derive(Debug, Clone, Default)]
pub struct NtfsFile {
    pub mft_entry: u64,
    pub seq_number: u16,
}

pub fn utf16_at_offset(file_buffer: &[u8], offset: usize, size: usize) -> ForensicResult<String> {
    let end_pos = offset + size;
    if end_pos > file_buffer.len() {
        return Err(ForensicError::bad_format_str(
            "The utf16 string position is greater than the file buffer",
        ));
    }
    let txt = &file_buffer[offset..end_pos];
    let txt_u16: &[u16] = unsafe { std::mem::transmute(txt) };
    let end = txt_u16
        .iter()
        .position(|&v| v == 0)
        .unwrap_or(txt_u16.len());
    let txt = String::from_utf16_lossy(&txt_u16[0..end]);
    Ok(txt)
}

pub fn u16_at_pos(buffer: &[u8], pos: usize) -> u16 {
    u16::from_le_bytes(buffer[pos..pos + 2].try_into().unwrap_or_default())
}
pub fn u32_at_pos(buffer: &[u8], pos: usize) -> u32 {
    u32::from_le_bytes(buffer[pos..pos + 4].try_into().unwrap_or_default())
}
pub fn u64_at_pos(buffer: &[u8], pos: usize) -> u64 {
    u64::from_le_bytes(buffer[pos..pos + 8].try_into().unwrap_or_default())
}

impl Metric {
    pub fn has_executable_block(&self) -> bool {
        for trace in self.traces.iter() {
            if trace.flags.is_executable() {
                return true;
            }
        }
        false
    }
}

impl PrefetchFile {
    pub fn new() -> Self {
        PrefetchFile::default()
    }

    pub fn executable_path(&self) -> &str {
        for loaded in &self.metrics {
            if loaded.file.ends_with(&self.name) {
                return &loaded.file;
            }
        }
        &self.name
    }
    /// Gets for which user was the program executed. Its not precise.
    pub fn user(&self) -> Option<&str> {
        for volume in &self.volume {
            for file in &volume.directory_strings {
                if !file.starts_with(r"\") {
                    continue;
                }
                let filename = &file[1..];
                let mut splited = filename.split(r"\");
                if splited.next().is_none() {
                    continue;
                };
                match splited.next() {
                    Some("USERS") => {}
                    _ => continue,
                };
                let user = match splited.next() {
                    Some(v) => v,
                    None => continue,
                };
                match splited.next() {
                    Some("APPDATA") => {}
                    _ => continue,
                };
                return Some(user);
            }
        }
        None
    }
}

pub struct PrefetchTimelineIterator<'a> {
    prefetch: &'a PrefetchFile,
    time_pos: usize,
}
impl<'a> Iterator for PrefetchTimelineIterator<'a> {
    type Item = TimelineData;
    fn next(&mut self) -> Option<Self::Item> {
        let actual_pos = self.time_pos;
        if actual_pos >= self.prefetch.last_run_times.len() {
            return None;
        }
        self.time_pos += 1;
        let mut data = ForensicData::default();
        data.add_field(
            FILE_ACCESSED,
            Field::Date(self.prefetch.last_run_times[actual_pos]),
        );
        data.add_field(FILE_PATH, Field::Path(PathBuf::from(&self.prefetch.name)));
        let dependencies: Vec<Text> = self
            .prefetch
            .metrics
            .iter()
            .map(|v| Cow::Owned(v.file.clone()))
            .collect();
        data.add_field(PE_IMPORTS, Field::Array(dependencies));
        data.add_field("prefetch.execution_times", self.prefetch.run_count.into());
        data.add_field("prefetch.version", self.prefetch.version.into());
        let mut volume_files = Vec::with_capacity(1024);
        for volumn in self.prefetch.volume.iter() {
            for files in volumn.directory_strings.iter() {
                volume_files.push(Cow::Owned(files.clone()))
            }
        }
        data.add_field("prefetch.volume_files", Field::Array(volume_files));
        Some(TimelineData {
            time: self.prefetch.last_run_times[actual_pos],
            data,
            time_context: TimeContext::Accessed,
        })
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.time_pos, Some(self.prefetch.last_run_times.len()))
    }
}

pub struct PrefetchActivityIterator<'a> {
    prefetch: &'a PrefetchFile,
    time_pos: usize,
}
impl<'a> Iterator for PrefetchActivityIterator<'a> {
    type Item = ForensicActivity;
    fn next(&mut self) -> Option<Self::Item> {
        let actual_pos = self.time_pos;
        if actual_pos >= self.prefetch.last_run_times.len() {
            return None;
        }
        self.time_pos += 1;
        Some(ForensicActivity {
            timestamp: self.prefetch.last_run_times[actual_pos],
            activity: ProgramExecution::new(self.prefetch.executable_path().to_string()).into(),
            user: self
                .prefetch
                .user()
                .map(|v| v.to_string())
                .unwrap_or_default(),
            session_id: SessionId::Unknown,
        })
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.time_pos, Some(self.prefetch.last_run_times.len()))
    }
}

impl<'a> IntoActivity<'a> for &'a PrefetchFile {
    fn activity(&'a self) -> Self::IntoIter {
        PrefetchActivityIterator {
            prefetch: self,
            time_pos: 0,
        }
    }

    type IntoIter = PrefetchActivityIterator<'a> where Self: 'a;
}

impl<'a> IntoActivity<'a> for PrefetchFile {
    fn activity(&'a self) -> Self::IntoIter {
        PrefetchActivityIterator {
            prefetch: self,
            time_pos: 0,
        }
    }

    type IntoIter = PrefetchActivityIterator<'a> where Self: 'a;
}

impl<'a> IntoTimeline<'a> for &'a PrefetchFile {
    fn timeline(&'a self) -> Self::IntoIter {
        PrefetchTimelineIterator {
            prefetch: self,
            time_pos: 0,
        }
    }

    type IntoIter = PrefetchTimelineIterator<'a> where Self: 'a;
}

impl<'a> IntoTimeline<'a> for PrefetchFile {
    fn timeline(&'a self) -> Self::IntoIter {
        PrefetchTimelineIterator {
            prefetch: self,
            time_pos: 0,
        }
    }

    type IntoIter = PrefetchTimelineIterator<'a> where Self: 'a;
}