windows_recipe_messagebox/common/
mod.rs1use isr::{
2 Profile,
3 cache::{IsrCache, JsonCodec},
4};
5use vmi::{
6 VcpuId, VmiCore, VmiDriver, VmiError, VmiOs, VmiSession, VmiState,
7 arch::amd64::Amd64,
8 driver::xen::VmiXenDriver,
9 os::{VmiOsProcess as _, windows::WindowsOs},
10};
11use xen::XenStore;
12
13pub fn create_vmi_session() -> Result<
14 (
15 VmiSession<'static, VmiXenDriver<Amd64>, WindowsOs<VmiXenDriver<Amd64>>>,
16 Profile<'static>,
17 ),
18 Box<dyn std::error::Error>,
19> {
20 tracing_subscriber::fmt()
21 .with_max_level(tracing::Level::DEBUG)
22 .with_target(false)
23 .init();
24
25 let domain_id = 'x: {
26 for name in &["win7", "win10", "win11", "ubuntu22"] {
27 if let Some(domain_id) = XenStore::new()?.domain_id_from_name(name)? {
28 break 'x domain_id;
29 }
30 }
31
32 panic!("Domain not found");
33 };
34
35 tracing::debug!(?domain_id);
36
37 let driver = VmiXenDriver::<Amd64>::new(domain_id)?;
39 let core = VmiCore::new(driver)?;
40
41 let kernel_info = {
44 let _pause_guard = core.pause_guard()?;
46
47 let registers = core.registers(VcpuId(0))?;
49
50 WindowsOs::find_kernel(&core, ®isters)?.expect("kernel information")
59 };
60
61 let isr = IsrCache::<JsonCodec>::new("cache")?;
64 let entry = isr.entry_from_codeview(kernel_info.codeview)?;
65 let entry = Box::leak(Box::new(entry));
66 let profile = entry.profile()?;
67
68 tracing::info!("Creating VMI session");
70 let os = WindowsOs::<VmiXenDriver<Amd64>>::new(&profile)?;
71
72 let core = Box::leak(Box::new(core));
75 let os = Box::leak(Box::new(os));
76
77 Ok((VmiSession::new(core, os), profile))
78}
79
80pub fn find_process<'a, Driver, Os>(
81 vmi: &VmiState<'a, Driver, Os>,
82 name: &str,
83) -> Result<Option<Os::Process<'a>>, VmiError>
84where
85 Driver: VmiDriver,
86 Os: VmiOs<Driver>,
87{
88 for process in vmi.os().processes()? {
89 let process = process?;
90
91 if process.name()?.to_lowercase() == name {
92 return Ok(Some(process));
93 }
94 }
95
96 Ok(None)
97}