vmi-utils 0.7.0

Utilities for VMI
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
mod define_events;
mod define_modules;
mod split_attrs;

#[doc(hidden)]
pub mod __private {
    pub mod isr_cache {
        pub use isr_cache::*;
    }

    pub mod vmi_arch_amd64 {
        pub use vmi_arch_amd64::*;
    }

    pub mod vmi_core {
        pub use vmi_core::*;
    }

    pub mod vmi_os_windows {
        pub use vmi_os_windows::*;
    }

    use isr_cache::{Entry as IsrEntry, IsrCache};
    use vmi_core::{
        VmiError, VmiOs, VmiSession, driver::VmiFullDriver, os::AnyProcess, trace::Hex,
    };
    use vmi_os_windows::{ArchAdapter, WindowsOs};

    use super::super::{
        EventMetadata, ModuleMode, ModuleProcessFilter, ReactorEvent, ReactorModule, ResolvedEvent,
        ResolvedModule,
    };

    type CacheSlots = [Option<IsrEntry>];
    type ModuleSlots<'a> = [Option<ResolvedModule<'a>>];

    /// Resolves unresolved module slots in a generated resolver table.
    ///
    /// Existing slots are left unchanged, which lets callers pre-populate
    /// entries with `with_kernel`, `with_module`, or `with_module_in_process`.
    /// Empty slots are resolved from [`Module::METADATA`]: kernel modules use
    /// the kernel resolver, and user modules use the configured process filter.
    ///
    /// Resolved [`CodeView`] signatures are loaded through the ISR cache,
    /// and the resulting profile references are stored in the module table.
    /// Optional modules that cannot be found are skipped. Non-optional missing
    /// modules cause an error.
    ///
    /// [`Module::METADATA`]: ReactorModule::METADATA
    /// [`CodeView`]: isr_cache::CodeView
    pub fn resolve_modules<'a, Driver, Module>(
        session: &VmiSession<WindowsOs<Driver>>,
        isr: &IsrCache,
        cache: &'a mut CacheSlots,
        modules: &mut ModuleSlots<'a>,
    ) -> Result<(), VmiError>
    where
        Driver: VmiFullDriver,
        Driver::Architecture: ArchAdapter<Driver> + crate::resolver::ArchAdapter<Driver>,
        Module: ReactorModule<WindowsOs<Driver>>,
    {
        debug_assert_eq!(
            Module::METADATA.len(),
            cache.len(),
            "length of entries and module metadata must match"
        );

        debug_assert_eq!(
            cache.len(),
            modules.len(),
            "length of entries and table must match"
        );

        let paused = session.pause_guard()?;

        let vmi = paused.state();

        for (meta, (table_slot, cache_slot)) in Module::METADATA
            .iter()
            .zip(modules.iter_mut().zip(cache.iter_mut()))
        {
            if table_slot.is_some() {
                continue;
            }

            let resolved = match &meta.mode {
                ModuleMode::Kernel => crate::resolver::resolve_kernel_module(&vmi, isr, meta.name)?,
                ModuleMode::User { process } => match process {
                    Some(ModuleProcessFilter::Name(name)) => {
                        crate::resolver::resolve_user_module(&vmi, isr, meta.name, *name)?
                    }
                    Some(ModuleProcessFilter::Predicate(predicate)) => {
                        crate::resolver::resolve_user_module(&vmi, isr, meta.name, *predicate)?
                    }
                    None => crate::resolver::resolve_user_module(&vmi, isr, meta.name, AnyProcess)?,
                },
            };

            match resolved {
                Some(resolved) => {
                    let entry = isr.entry_from_codeview(resolved.debug_signature)?;
                    let profile = cache_slot.insert(entry).profile()?;
                    *table_slot = Some(ResolvedModule {
                        process: resolved.process,
                        base_address: resolved.image_base,
                        profile: profile.into(),
                    });
                }
                None if meta.optional => {
                    tracing::debug!("profile not found (optional)");
                    continue;
                }
                None => {
                    tracing::warn!("profile not found");
                    return Err(VmiError::Other("profile not found"));
                }
            }
        }

        Ok(())
    }

    /// Resolves one event metadata entry against a resolved module table.
    ///
    /// The event's module tag selects either the kernel slot or the slot
    /// returned by [`ReactorModule::slot`]. The primary symbol name is tried
    /// first, followed by aliases in declaration order. A match returns a
    /// [`ResolvedEvent`] whose address is the module base plus the symbol
    /// offset.
    ///
    /// If the module or symbol is absent and either the event or its owning
    /// module is optional, `Ok(None)` is returned. Otherwise the missing
    /// module/profile or symbol causes an error.
    #[tracing::instrument(
        skip_all,
        fields(
            module = %meta.module
                          .map(|module| format!("{module:?}"))
                          .unwrap_or_else(|| String::from("kernel")),
            name = %meta.name
        )
    )]
    pub fn resolve_event<Event, Os>(
        modules: &ModuleSlots,
        meta: &EventMetadata<Event>,
    ) -> Result<Option<ResolvedEvent<Event>>, VmiError>
    where
        Event: ReactorEvent,
        Event::Module: ReactorModule<Os>,
        Os: VmiOs + 'static,
    {
        let slot = meta
            .module
            .map(<Event::Module as ReactorModule<Os>>::slot)
            .unwrap_or(<Event::Module as ReactorModule<Os>>::KERNEL_SLOT);

        let module = match &modules[slot] {
            Some(module) => module,
            None if meta.optional => {
                tracing::debug!("profile not found (event optional)");
                return Ok(None);
            }
            None if <Event::Module as ReactorModule<Os>>::METADATA[slot].optional => {
                tracing::debug!("profile not found (module optional)");
                return Ok(None);
            }
            None => {
                tracing::warn!("profile not found");
                return Err(VmiError::Other("profile not found"));
            }
        };

        if let Some(offset) = module.profile.find_symbol(meta.name) {
            tracing::debug!(offset = %Hex(offset), "symbol found");
            return Ok(Some(ResolvedEvent {
                process: module.process,
                address: module.base_address + offset,
                event: meta.event,
            }));
        }

        match meta.alias.iter().find_map(|&alias| {
            module
                .profile
                .find_symbol(alias)
                .map(|offset| (alias, offset))
        }) {
            Some((alias, offset)) => {
                tracing::debug!(offset = %Hex(offset), alias, "symbol found");
                Ok(Some(ResolvedEvent {
                    process: module.process,
                    address: module.base_address + offset,
                    event: meta.event,
                }))
            }
            None if meta.optional => {
                tracing::debug!("symbol not found (event optional)");
                Ok(None)
            }
            None if <Event::Module as ReactorModule<Os>>::METADATA[slot].optional => {
                tracing::debug!("symbol not found (module optional)");
                Ok(None)
            }
            None => {
                tracing::warn!("symbol not found");
                Err(VmiError::Other("symbol not found"))
            }
        }
    }

    /// Resolves all events declared by an event enum.
    ///
    /// Events are processed in metadata order. Entries that resolve to `Some`
    /// are collected, optional missing entries are omitted, and the first
    /// non-optional resolution error is returned.
    pub fn resolve_events<Event, Os>(
        modules: &ModuleSlots,
    ) -> Result<Vec<ResolvedEvent<Event>>, VmiError>
    where
        Event: ReactorEvent,
        Event::Module: ReactorModule<Os>,
        Os: VmiOs + 'static,
    {
        let mut events = Vec::new();
        for meta in Event::METADATA {
            if let Some(resolved) = resolve_event::<Event, Os>(modules, meta)? {
                events.push(resolved);
            }
        }

        Ok(events)
    }
}

// The functions and macro expansions in this module exist solely so the
// compiler exercises every shape of `define_modules!` / `define_events!`.
#[cfg(test)]
mod tests {
    use vmi_core::{VmiError, VmiOs, driver::VmiRead};
    use vmi_os_windows::{ArchAdapter, WindowsOs, WindowsProcess};

    use super::super::{define_events, define_modules};

    /// Os-generic predicate, callable from the blanket impl form
    /// (`define_modules!` without `#[os(...)]`).
    fn match_by_name<Os>(_process: &Os::Process<'_>) -> Result<bool, VmiError>
    where
        Os: VmiOs,
    {
        Ok(true)
    }

    /// Windows-specific predicate, callable from the anchored impl form
    /// (`#[os(<Driver: VmiRead> WindowsOs<Driver> where ...)]`).
    fn match_windows_process<Driver>(_process: &WindowsProcess<Driver>) -> Result<bool, VmiError>
    where
        Driver: VmiRead,
        Driver::Architecture: ArchAdapter<Driver>,
    {
        Ok(true)
    }

    #[test]
    fn test_macro() {
        define_modules! {
            /// Rustdoc before attribute.
            #[allow(dead_code)] // Allow attributes. Can be before or after Rustdoc.
            pub enum Module {
                /// Default kernel mode (no `mode(...)`).
                #[module(name = "module1")]
                Module1,

                /// Explicit kernel mode.
                #[module(name = "module2", mode(kernel))]
                /// Rustdoc after attribute.
                Module2,

                /// User mode, no process filter.
                #[module(name = "module3", mode(user), optional)]
                Module3,

                /// User mode, process filter by literal name.
                #[module(optional, name = "module4", mode(user, process = "lsass.exe"))]
                Module4,

                /// User mode, process filter by bare-ident predicate.
                #[module(name = "module5", mode(user, process = match_by_name))]
                Module5,

                /// User mode, process filter by fully-qualified predicate.
                #[module(name = "module6", mode(user, process = self::match_by_name))]
                Module6,
            }

            // #[resolver] ... is required. It tells the macro to generate a resolver struct.
            // Rustdoc/derives/other attributes are forbidden.
            #[resolver]
            pub struct ModuleResolver;

            // #[cache] ... is optional. If provided, the macro will generate a cache struct.
            // Rustdoc/derives/other attributes are forbidden.
            #[cache]
            pub struct SymbolCache;

            // The order of #[resolver] and #[cache] is not fixed. #[cache] can be placed before #[resolver].
        }

        define_events! {
            #[allow(dead_code)] // Allow attributes. Can be before or after Rustdoc.
            /// Rustdoc
            pub enum Event in Module {
                // BEGIN KERNEL EVENTS

                NonOptionalKernelEvent1,

                /// Rustdoc.
                NonOptionalKernelEvent2,

                /// Rustdoc before attribute.
                #[event(optional)]
                OptionalKernelEvent1,

                #[event(optional)]
                /// Rustdoc before attribute.
                OptionalKernelEvent2,

                #[event(optional)]
                OptionalKernelEvent3,

                // END KERNEL EVENTS

                Module1 {
                    /// Rustdoc.
                    Module1Event1,

                    Module1Event2,
                },

                Module2 {
                    /// Rustdoc before attribute.
                    #[event(optional)]
                    Module2Event1,

                    #[event(optional)]
                    /// Rustdoc after attribute.
                    Module2Event2,

                    Module2Event3,
                },

                Module3 {
                    /// Rustdoc before attribute.
                    #[event(
                        name = "AnotherModule3Event1",
                        alias = "AnotherModule3Event1_Alias1" // no trailing comma
                    )]
                    Module3Event1,

                    #[event(
                        name = "AnotherModule3Event2",
                        alias = "AnotherModule3Event2_Alias1",
                    )]
                    /// Rustdoc after attribute.
                    Module3Event2,

                    #[event(
                        name = "AnotherModule3Event3",
                        alias = ["AnotherModule3Event3_Alias1", "AnotherModule3Event3_Alias2"],
                        optional,
                    )]
                    Module3Event3 // trailing comma optional
                },

                Module4 {
                    /// Rustdoc before attribute. Various attribute order.
                    #[event(
                        optional,
                        name = "AnotherModule4Event1",
                        alias = "AnotherModule4Event1_Alias1"
                    )]
                    Module4Event1,

                    #[event(
                        alias = "AnotherModule4Event2_Alias1",
                        name = "AnotherModule4Event2",
                        optional,
                    )]
                    /// Rustdoc after attribute. Various attribute order.
                    Module4Event2,
                }, // trailing comma optional
            }
        }
    }

    /// Anchored form with generic param and where clause. Exercises the
    /// most common shape: one OS family bound to a `Driver` generic, complex
    /// bounds in the where clause, and a Driver-typed predicate.
    #[test]
    fn test_anchored_with_generic_where() {
        define_modules! {
            #[os(
                <Driver: VmiRead> WindowsOs<Driver>
                where Driver::Architecture: ArchAdapter<Driver>
            )]
            pub enum AnchoredWhereModule {
                #[module(name = "kmod", mode(kernel))]
                Kmod,

                #[module(name = "umod", mode(user, process = match_windows_process))]
                Umod,
            }

            #[resolver]
            pub struct AnchoredWhereResolver;

            #[cache]
            pub struct AnchoredWhereCache;
        }
    }

    /// Anchored form with a generic carrying no inline bound (everything in
    /// the where clause).
    #[test]
    fn test_anchored_bare_generic_all_in_where() {
        define_modules! {
            #[os(
                <Driver> WindowsOs<Driver>
                where Driver: VmiRead, Driver::Architecture: ArchAdapter<Driver>
            )]
            pub enum AnchoredBareModule {
                #[module(name = "a", mode(user, process = match_windows_process))]
                A,
            }

            #[resolver]
            pub struct AnchoredBareResolver;
        }
    }

    /// Bare enum: no `#[resolver]`, no `#[cache]`. Only the enum and its
    /// blanket `ReactorModule` impl are emitted.
    #[test]
    fn test_no_resolver_no_cache() {
        define_modules! {
            #[allow(dead_code)]
            pub enum BareModule {
                #[module(name = "a")]
                A,

                #[module(name = "b", mode(user))]
                B,
            }
        }
    }

    /// `#[resolver]` present, `#[cache]` absent. Resolver gets `with_kernel`,
    /// `with_module`, `into_events` but no `resolve` method.
    #[test]
    fn test_resolver_only() {
        define_modules! {
            pub enum ResolverOnlyModule {
                #[module(name = "a")]
                A,
            }

            #[resolver]
            pub struct ResolverOnlyResolver;
        }
    }

    /// `#[cache]` declared before `#[resolver]`. The trailing-marker arms in
    /// `@finalize` accept either order.
    #[test]
    fn test_cache_before_resolver() {
        define_modules! {
            pub enum SwappedOrderModule {
                #[module(name = "a")]
                A,
            }

            #[cache]
            pub struct SwappedOrderCache;

            #[resolver]
            pub struct SwappedOrderResolver;
        }
    }

    /// `#[os(...)]` position-independence: before rustdoc, after rustdoc,
    /// between other attributes. The walker in `@walk_enum_attrs` peels it
    /// out wherever it appears.
    #[test]
    fn test_os_attr_positions() {
        define_modules! {
            #[os(<Driver: VmiRead> WindowsOs<Driver> where Driver::Architecture: ArchAdapter<Driver>)]
            /// Os attribute before rustdoc.
            #[allow(dead_code)]
            pub enum OsPositionA {
                #[module(name = "a")]
                A,
            }

            #[resolver]
            pub struct OsPositionAResolver;
        }

        define_modules! {
            /// Os attribute after rustdoc.
            #[os(<Driver: VmiRead> WindowsOs<Driver> where Driver::Architecture: ArchAdapter<Driver>)]
            #[allow(dead_code)]
            pub enum OsPositionB {
                #[module(name = "a")]
                A,
            }

            #[resolver]
            pub struct OsPositionBResolver;
        }

        define_modules! {
            /// Os attribute between rustdoc and `#[allow(...)]`.
            #[allow(dead_code)]
            #[os(<Driver: VmiRead> WindowsOs<Driver> where Driver::Architecture: ArchAdapter<Driver>)]
            pub enum OsPositionC {
                #[module(name = "a")]
                A,
            }

            #[resolver]
            pub struct OsPositionCResolver;
        }
    }

    /// `#[module(...)]` argument ordering. `name = "..."`, `mode(...)`, and
    /// `optional` may appear in any order.
    #[test]
    fn test_module_arg_order() {
        define_modules! {
            pub enum ArgOrderModule {
                #[module(name = "a")]
                A,

                #[module(optional, name = "b")]
                B,

                #[module(name = "c", optional)]
                C,

                #[module(mode(user), name = "d")]
                D,

                #[module(optional, mode(kernel), name = "e")]
                E,

                #[module(name = "f", optional, mode(user, process = match_by_name))]
                F,

                #[module(mode(user, process = "lsass.exe"), optional, name = "g")]
                G,
            }

            #[resolver]
            pub struct ArgOrderResolver;
        }
    }

    /// Every `mode(...)` shape in one enum: omitted (default kernel),
    /// `kernel`, `user`, `user, process = "literal"`, `user, process = ident`,
    /// `user, process = path::with::segments`.
    #[test]
    fn test_mode_shapes() {
        define_modules! {
            pub enum ModeShapesModule {
                /// Omitted - defaults to kernel.
                #[module(name = "default")]
                Default,

                /// Explicit kernel.
                #[module(name = "kernel", mode(kernel))]
                Kernel,

                /// User with no process filter.
                #[module(name = "any-user", mode(user))]
                AnyUser,

                /// User with literal-name filter.
                #[module(name = "by-name", mode(user, process = "lsass.exe"))]
                ByName,

                /// User with bare-ident predicate.
                #[module(name = "by-ident", mode(user, process = match_by_name))]
                ByIdent,

                /// User with fully-qualified path predicate.
                #[module(name = "by-path", mode(user, process = self::match_by_name))]
                ByPath,
            }

            #[resolver]
            pub struct ModeShapesResolver;
        }
    }

    /// Events with mixed shapes: kernel events with and without rustdoc,
    /// module groups with the alias array form, optional events, and
    /// implicit names (variant ident becomes the symbol name).
    #[test]
    fn test_event_shapes() {
        define_modules! {
            pub enum EventShapesModule {
                #[module(name = "m1")]
                M1,

                #[module(name = "m2")]
                M2,
            }

            #[resolver]
            pub struct EventShapesResolver;
        }

        define_events! {
            #[allow(dead_code)]
            pub enum EventShapesEvent in EventShapesModule {
                /// Kernel event, implicit name.
                KernelImplicit,

                #[event(name = "RenamedKernel", optional)]
                KernelRenamedOptional,

                M1 {
                    /// Implicit name.
                    M1Implicit,

                    #[event(optional)]
                    M1Optional,
                },

                M2 {
                    #[event(name = "M2Renamed")]
                    M2Renamed,

                    #[event(
                        name = "M2Aliased",
                        alias = ["AliasA", "AliasB", "AliasC"],
                        optional,
                    )]
                    M2Aliased,
                },
            }
        }
    }
}