Skip to main content

InjectorHandler

Struct InjectorHandler 

Source
pub struct InjectorHandler<Os, Mode, T, Bridge = ()>
where Os: InjectorExecutionAdapter<Mode, T, Bridge>, Mode: ExecutionMode, Bridge: BridgeDispatch<Os, u64>,
{ /* private fields */ }
Available on crate features injector and utils only.
Expand description

Generic injector handler that delegates to an OS- and mode-specific implementation.

Prefer the KernelInjectorHandler and UserInjectorHandler type aliases for the common case without a bridge. When a custom BridgeDispatch is needed, use this type directly with an explicit Bridge parameter.

Implementations§

Source§

impl<Os, Mode, T, Bridge> InjectorHandler<Os, Mode, T, Bridge>
where Os: InjectorExecutionAdapter<Mode, T, Bridge>, Mode: ExecutionMode, Bridge: BridgeDispatch<Os, u64>,

Source

pub fn new( vmi: &VmiSession<'_, Os>, recipe: Recipe<Os, T>, ) -> Result<InjectorHandler<Os, Mode, T, Bridge>, VmiError>
where Bridge: Default,

Creates a new injector handler with a default (no-op) bridge.

Examples found in repository?
examples/windows-recipe-messagebox.rs (lines 94-100)
65fn main() -> Result<(), Error> {
66    let session = common::create_vmi_session()?;
67
68    let explorer_pid = {
69        // This block is used to drop the pause guard after the PID is found.
70        // If the `session.handle()` would be called with the VM paused, no
71        // events would be triggered.
72        let paused = session.pause_guard()?;
73
74        let vmi = paused.state();
75
76        let explorer = match vmi.os().find_process("explorer.exe")? {
77            Some(explorer) => explorer,
78            None => {
79                tracing::error!("explorer.exe not found");
80                return Ok(());
81            }
82        };
83
84        tracing::info!(
85            pid = %explorer.id()?,
86            object = %explorer.object()?,
87            "found explorer.exe"
88        );
89
90        explorer.id()?
91    };
92
93    session.handle(|session| {
94        UserInjectorHandler::new(
95            session,
96            recipe_factory(MessageBox::new(
97                "Hello, World!",
98                "This is a message box from the VMI!",
99            )),
100        )?
101        .with_pid(explorer_pid)
102    })?;
103
104    Ok(())
105}
More examples
Hide additional examples
examples/windows-recipe-writefile.rs (lines 234-240)
205fn main() -> Result<(), Error> {
206    let session = common::create_vmi_session()?;
207
208    let explorer_pid = {
209        // This block is used to drop the pause guard after the PID is found.
210        // If the `session.handle()` would be called with the VM paused, no
211        // events would be triggered.
212        let paused = session.pause_guard()?;
213
214        let vmi = paused.state();
215
216        let explorer = match vmi.os().find_process("explorer.exe")? {
217            Some(explorer) => explorer,
218            None => {
219                tracing::error!("explorer.exe not found");
220                return Ok(());
221            }
222        };
223
224        tracing::info!(
225            pid = %explorer.id()?,
226            object = %explorer.object()?,
227            "found explorer.exe"
228        );
229
230        explorer.id()?
231    };
232
233    session.handle(|session| {
234        UserInjectorHandler::new(
235            session,
236            recipe_factory(GuestFile::new(
237                "C:\\Users\\John\\Desktop\\test.txt",
238                "Hello, World!".as_bytes(),
239            )),
240        )?
241        .with_pid(explorer_pid)
242    })?;
243
244    Ok(())
245}
examples/windows-recipe-writefile-advanced.rs (lines 337-343)
303fn main() -> Result<(), Error> {
304    let session = common::create_vmi_session()?;
305
306    let explorer_pid = {
307        // This block is used to drop the pause guard after the PID is found.
308        // If the `session.handle()` would be called with the VM paused, no
309        // events would be triggered.
310        let paused = session.pause_guard()?;
311
312        let vmi = paused.state();
313
314        let explorer = match vmi.os().find_process("explorer.exe")? {
315            Some(explorer) => explorer,
316            None => {
317                tracing::error!("explorer.exe not found");
318                return Ok(());
319            }
320        };
321
322        tracing::info!(
323            pid = %explorer.id()?,
324            object = %explorer.object()?,
325            "found explorer.exe"
326        );
327
328        explorer.id()?
329    };
330
331    let mut content = Vec::new();
332    for c in 'A'..='Z' {
333        content.extend((0..2049).map(|_| c as u8).collect::<Vec<_>>());
334    }
335
336    session.handle(|session| {
337        UserInjectorHandler::new(
338            session,
339            recipe_factory(GuestFile::new(
340                "C:\\Users\\John\\Desktop\\test.txt",
341                content,
342            )),
343        )?
344        .with_pid(explorer_pid)
345    })?;
346
347    Ok(())
348}
Source

pub fn with_bridge( vmi: &VmiSession<'_, Os>, bridge: Bridge, recipe: Recipe<Os, T>, ) -> Result<InjectorHandler<Os, Mode, T, Bridge>, VmiError>

Creates a new injector handler with a custom bridge for guest-host communication.

Source

pub fn with_pid( self, pid: ProcessId, ) -> Result<InjectorHandler<Os, Mode, T, Bridge>, VmiError>

Restricts injection to a specific process.

Examples found in repository?
examples/windows-recipe-messagebox.rs (line 101)
65fn main() -> Result<(), Error> {
66    let session = common::create_vmi_session()?;
67
68    let explorer_pid = {
69        // This block is used to drop the pause guard after the PID is found.
70        // If the `session.handle()` would be called with the VM paused, no
71        // events would be triggered.
72        let paused = session.pause_guard()?;
73
74        let vmi = paused.state();
75
76        let explorer = match vmi.os().find_process("explorer.exe")? {
77            Some(explorer) => explorer,
78            None => {
79                tracing::error!("explorer.exe not found");
80                return Ok(());
81            }
82        };
83
84        tracing::info!(
85            pid = %explorer.id()?,
86            object = %explorer.object()?,
87            "found explorer.exe"
88        );
89
90        explorer.id()?
91    };
92
93    session.handle(|session| {
94        UserInjectorHandler::new(
95            session,
96            recipe_factory(MessageBox::new(
97                "Hello, World!",
98                "This is a message box from the VMI!",
99            )),
100        )?
101        .with_pid(explorer_pid)
102    })?;
103
104    Ok(())
105}
More examples
Hide additional examples
examples/windows-recipe-writefile.rs (line 241)
205fn main() -> Result<(), Error> {
206    let session = common::create_vmi_session()?;
207
208    let explorer_pid = {
209        // This block is used to drop the pause guard after the PID is found.
210        // If the `session.handle()` would be called with the VM paused, no
211        // events would be triggered.
212        let paused = session.pause_guard()?;
213
214        let vmi = paused.state();
215
216        let explorer = match vmi.os().find_process("explorer.exe")? {
217            Some(explorer) => explorer,
218            None => {
219                tracing::error!("explorer.exe not found");
220                return Ok(());
221            }
222        };
223
224        tracing::info!(
225            pid = %explorer.id()?,
226            object = %explorer.object()?,
227            "found explorer.exe"
228        );
229
230        explorer.id()?
231    };
232
233    session.handle(|session| {
234        UserInjectorHandler::new(
235            session,
236            recipe_factory(GuestFile::new(
237                "C:\\Users\\John\\Desktop\\test.txt",
238                "Hello, World!".as_bytes(),
239            )),
240        )?
241        .with_pid(explorer_pid)
242    })?;
243
244    Ok(())
245}
examples/windows-recipe-writefile-advanced.rs (line 344)
303fn main() -> Result<(), Error> {
304    let session = common::create_vmi_session()?;
305
306    let explorer_pid = {
307        // This block is used to drop the pause guard after the PID is found.
308        // If the `session.handle()` would be called with the VM paused, no
309        // events would be triggered.
310        let paused = session.pause_guard()?;
311
312        let vmi = paused.state();
313
314        let explorer = match vmi.os().find_process("explorer.exe")? {
315            Some(explorer) => explorer,
316            None => {
317                tracing::error!("explorer.exe not found");
318                return Ok(());
319            }
320        };
321
322        tracing::info!(
323            pid = %explorer.id()?,
324            object = %explorer.object()?,
325            "found explorer.exe"
326        );
327
328        explorer.id()?
329    };
330
331    let mut content = Vec::new();
332    for c in 'A'..='Z' {
333        content.extend((0..2049).map(|_| c as u8).collect::<Vec<_>>());
334    }
335
336    session.handle(|session| {
337        UserInjectorHandler::new(
338            session,
339            recipe_factory(GuestFile::new(
340                "C:\\Users\\John\\Desktop\\test.txt",
341                content,
342            )),
343        )?
344        .with_pid(explorer_pid)
345    })?;
346
347    Ok(())
348}

Trait Implementations§

Source§

impl<Os, Mode, T, Bridge> VmiHandler<Os> for InjectorHandler<Os, Mode, T, Bridge>
where Os: InjectorExecutionAdapter<Mode, T, Bridge>, Mode: ExecutionMode, Bridge: BridgeDispatch<Os, u64>,

Source§

type Output = <<Os as InjectorExecutionAdapter<Mode, T, Bridge>>::Handler as VmiHandler<Os>>::Output

The output type of the handler.
Source§

fn handle_event( &mut self, vmi: VmiContext<'_, Os>, ) -> VmiEventResponse<<Os as VmiOs>::Architecture>

Called for each VMI event. Read more
Source§

fn poll( &self, ) -> Option<<InjectorHandler<Os, Mode, T, Bridge> as VmiHandler<Os>>::Output>

Checks if the handler has completed. Read more
Source§

fn handle_timeout(&mut self, _session: &VmiSession<'_, Os>)

Called when the event loop times out waiting for the next event. Read more
Source§

fn handle_interrupted(&mut self, _session: &VmiSession<'_, Os>)

Called when the event loop is interrupted by a signal. Read more
Source§

fn cleanup(&mut self, _session: &VmiSession<'_, Os>)

Called once before the session tears down monitoring. Read more

Auto Trait Implementations§

§

impl<Os, Mode, T, Bridge> Freeze for InjectorHandler<Os, Mode, T, Bridge>

§

impl<Os, Mode, T, Bridge> RefUnwindSafe for InjectorHandler<Os, Mode, T, Bridge>

§

impl<Os, Mode, T, Bridge> Send for InjectorHandler<Os, Mode, T, Bridge>

§

impl<Os, Mode, T, Bridge> Sync for InjectorHandler<Os, Mode, T, Bridge>

§

impl<Os, Mode, T, Bridge> Unpin for InjectorHandler<Os, Mode, T, Bridge>

§

impl<Os, Mode, T, Bridge> UnsafeUnpin for InjectorHandler<Os, Mode, T, Bridge>

§

impl<Os, Mode, T, Bridge> UnwindSafe for InjectorHandler<Os, Mode, T, Bridge>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more