0

There is a byte array that contains the program. How do run it in a new process from Rust code?

Kind of like this:

const PROGRAM: &[u8] = include_bytes!("proxy.exe");

fn main() {
    CreateNewProccess(PROGRAM);
}
E_net4
  • 27,810
  • 13
  • 101
  • 139

1 Answers1

0

The most intuitive way to run the embedded executable is to write it to disk, even if temporarily, and only then run it. Here is how one would do this in Rust.

let program = {
    let mut p = std::env::current_dir()?;
    p.push("proxy.exe");
    p
};
std::fs::write(&program, PROGRAM)?;

let child = std::process::Command::new(program)
    .spawn()?;

Depending on your use case, it might not be worth it to store executables inside the main program like that.

See also:

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • This isn't strictly correct. While you need an on-disk image to create a process, that image need not necessarily be the code you're intending to execute. Using *process hollowing* you can use any PE image (e.g. notepad.exe) to create a process object. – IInspectable Jan 27 '22 at 08:41
  • Edited to remove some assumptions. FWIW this still stands as the most intuitive approach in Rust. – E_net4 Jan 27 '22 at 09:18