From ff8135e8a308e8e3e155e6873989423ccad7a27a Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Sat, 16 Jan 2021 22:28:48 +0100 Subject: Add a library for dumping kernels arguments before and after launch (#18) --- zluda_inject/Cargo.toml | 2 +- zluda_inject/src/bin.rs | 65 +++++++++++++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 27 deletions(-) (limited to 'zluda_inject') diff --git a/zluda_inject/Cargo.toml b/zluda_inject/Cargo.toml index 193c36e..7576e08 100644 --- a/zluda_inject/Cargo.toml +++ b/zluda_inject/Cargo.toml @@ -9,5 +9,5 @@ name = "zluda_with" path = "src/main.rs" [target.'cfg(windows)'.dependencies] -winapi = { version = "0.3", features = ["jobapi2", "processthreadsapi", "std", "synchapi"] } +winapi = { version = "0.3", features = ["jobapi2", "processthreadsapi", "std", "synchapi", "winbase"] } detours-sys = { path = "../detours-sys" } diff --git a/zluda_inject/src/bin.rs b/zluda_inject/src/bin.rs index af44d74..ce83fe9 100644 --- a/zluda_inject/src/bin.rs +++ b/zluda_inject/src/bin.rs @@ -1,8 +1,7 @@ -use std::env; -use std::env::Args; use std::mem; use std::path::Path; use std::ptr; +use std::{env, ops::Deref}; use std::{error::Error, process}; use mem::size_of_val; @@ -25,15 +24,15 @@ static ZLUDA_DLL: &'static str = "nvcuda.dll"; include!("../../zluda_redirect/src/payload_guid.rs"); pub fn main_impl() -> Result<(), Box> { - let args = env::args(); - if args.len() == 0 { - print_help(); - process::exit(1); + let args = env::args().collect::>(); + if args.len() <= 1 { + print_help_and_exit(); } - let mut cmd_line = construct_command_line(args); let injector_path = env::current_exe()?; let injector_dir = injector_path.parent().unwrap(); let redirect_path = create_redirect_path(injector_dir); + let (mut inject_path, cmd) = create_inject_path(&args[1..], injector_dir); + let mut cmd_line = construct_command_line(cmd); let mut startup_info = unsafe { mem::zeroed::() }; let mut proc_info = unsafe { mem::zeroed::() }; os_call!( @@ -54,13 +53,12 @@ pub fn main_impl() -> Result<(), Box> { |x| x != 0 ); kill_child_on_process_exit(proc_info.hProcess)?; - let mut zluda_path = create_zluda_path(injector_dir); os_call!( detours_sys::DetourCopyPayloadToProcess( proc_info.hProcess, &PAYLOAD_GUID, - zluda_path.as_mut_ptr() as *mut _, - (zluda_path.len() * mem::size_of::()) as u32 + inject_path.as_mut_ptr() as *mut _, + (inject_path.len() * mem::size_of::()) as u32 ), |x| x != 0 ); @@ -93,22 +91,29 @@ fn kill_child_on_process_exit(child: HANDLE) -> Result<(), Box> { Ok(()) } -fn print_help() { +fn print_help_and_exit() -> ! { + let current_exe = env::current_exe().unwrap(); + let exe_name = current_exe.file_name().unwrap().to_string_lossy(); println!( "USAGE: - zluda [ARGS]... + {0} -- [ARGS]... + {0} -- [ARGS]... ARGS: - Path to the executable to be injected with ZLUDA + DLL to ne injected instead of system nvcuda.dll, if not provided + will use nvcuda.dll from the directory where {0} is located + Path to the executable to be injected with ... Arguments that will be passed to -" +", + exe_name ); + process::exit(1) } // Adapted from https://docs.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way -fn construct_command_line(args: Args) -> Vec { +fn construct_command_line(args: &[String]) -> Vec { let mut cmd_line = Vec::new(); let args_len = args.len(); - for (idx, arg) in args.enumerate().skip(1) { + for (idx, arg) in args.iter().enumerate() { if !arg.contains(&[' ', '\t', '\n', '\u{2B7F}', '\"'][..]) { cmd_line.extend(arg.encode_utf16()); } else { @@ -168,14 +173,22 @@ fn create_redirect_path(injector_dir: &Path) -> Vec { result } -fn create_zluda_path(injector_dir: &Path) -> Vec { - let mut injector_dir = injector_dir.to_path_buf(); - injector_dir.push(ZLUDA_DLL); - let mut result = injector_dir - .to_string_lossy() - .as_ref() - .encode_utf16() - .collect::>(); - result.push(0); - result +fn create_inject_path<'a>(args: &'a [String], injector_dir: &Path) -> (Vec, &'a [String]) { + if args.get(0).map(Deref::deref) == Some("--") { + let mut injector_dir = injector_dir.to_path_buf(); + injector_dir.push(ZLUDA_DLL); + let mut result = injector_dir + .to_string_lossy() + .as_ref() + .encode_utf16() + .collect::>(); + result.push(0); + (result, &args[1..]) + } else if args.get(1).map(Deref::deref) == Some("--") { + let mut dll_path = args[0].encode_utf16().collect::>(); + dll_path.push(0); + (dll_path, &args[2..]) + } else { + print_help_and_exit() + } } -- cgit v1.2.3