diff --git a/Cargo.lock b/Cargo.lock index 452041e..d16d0f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,38 +2,184 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "bindgen" +version = "0.69.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0" +dependencies = [ + "bitflags 2.6.0", + "cexpr", + "clang-sys", + "itertools", + "lazy_static", + "lazycell", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn", + "which", +] + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "cc" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "504bdec147f2cc13c8b57ed9401fd8a147cc66b67ad5cb241394244f2c947549" + +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clang-sys" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" +dependencies = [ + "glob", + "libc", + "libloading", +] + +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "elfloader" version = "0.1.0" dependencies = [ + "bindgen", + "cc", "memfd-exec", "winapi", ] +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "home" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +[[package]] +name = "libloading" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" +dependencies = [ + "cfg-if", + "windows-targets", +] + +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + [[package]] name = "memfd-exec" version = "0.2.1" @@ -53,25 +199,158 @@ dependencies = [ "autocfg", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "nix" version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cfg-if", "libc", "memoffset", "pin-utils", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + [[package]] name = "pin-utils" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "prettyplease" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + +[[package]] +name = "rustix" +version = "0.38.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +dependencies = [ + "bitflags 2.6.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix", +] + [[package]] name = "winapi" version = "0.3.9" @@ -93,3 +372,76 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index 503e648..a853f0d 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,7 @@ winapi = { version = "0.3.9", features = [ "winbase", "winnt", ] } + +[build-dependencies] +cc = "1" +bindgen = "0" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..545b58c --- /dev/null +++ b/build.rs @@ -0,0 +1,18 @@ +use std::path::PathBuf; + +fn main() { + #[cfg(target_os = "windows")] + winbuild(); +} + +fn winbuild() { + println!("cargo::rerun-if-changed=csrc/winpe.c"); + println!("cargo::rerun-if-changed=csrc/winpe.h"); + cc::Build::new().file("csrc/winpe.c").compile("winpe"); + let out_path = PathBuf::from(std::env::var("OUR_DIR").unwrap()); + let _ = bindgen::Builder::default() + .header("csrc/winpe.h") + .generate() + .unwrap() + .write_to_file(out_path.join("bindings.rs")); +} diff --git a/csrc/winpe.c b/csrc/winpe.c new file mode 100644 index 0000000..8a469a0 --- /dev/null +++ b/csrc/winpe.c @@ -0,0 +1,51 @@ +#include +#include + +int runpe(char *name, void *pe, int pe_len, void *stdin_data, int stdin_len) { + STARTUPINFO si; + PROCESS_INFORMATION pi; + + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); + ZeroMemory(&pi, sizeof(pi)); + + // Create a new process + if (!CreateProcess(name, NULL, NULL, NULL, FALSE, + CREATE_NEW_CONSOLE | DETACHED_PROCESS, NULL, NULL, &si, + &pi)) { + printf("CreateProcess failed (%d).\n", GetLastError()); + return -1; + } + + // Inject the PE file into the new process's memory space + // This part is highly simplified and requires a deeper understanding of PE + // format and memory injection techniques + DWORD oldProtect; + VirtualAllocEx(pi.hProcess, NULL, pe_len, MEM_COMMIT, PAGE_EXECUTE_READWRITE); + + SIZE_T bytesWritten; + WriteProcessMemory( + pi.hProcess, (LPVOID)0x10000000, pe, pe_len, + &bytesWritten); // Simplified injection at address 0x10000000 + + // Execute the injected code + HANDLE threadHandle = OpenThread(THREAD_ALL_ACCESS, FALSE, pi.dwThreadId); + if (threadHandle != INVALID_HANDLE_VALUE) { + ResumeThread(threadHandle); + CloseHandle(threadHandle); + } else { + printf("OpenThread failed (%d).\n", GetLastError()); + return -1; + } + + // Write to the child process's stdin + // This assumes the child process has a console attached and uses stdin/stdout + DWORD written; + WriteFile((HANDLE)pi.hThread, stdin_data, stdin_len, &written, NULL); + + // Cleanup + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + + return 0; // Success +} diff --git a/csrc/winpe.h b/csrc/winpe.h new file mode 100644 index 0000000..f7d2161 --- /dev/null +++ b/csrc/winpe.h @@ -0,0 +1 @@ +int runpe(char *name, void *pe, int pe_len, void *stdin, int stdin_len); diff --git a/src/win.rs b/src/win.rs index e31e418..3caab13 100644 --- a/src/win.rs +++ b/src/win.rs @@ -1,101 +1 @@ -use std::{os::windows::raw::HANDLE, ptr, thread}; - -use winapi::um::{ - fileapi::WriteFile, - memoryapi::{VirtualAllocEx, VirtualProtectEx, WriteProcessMemory}, - namedpipeapi::CreatePipe, - processenv::GetStdHandle, - processthreadsapi::{CreateProcessA, OpenProcess}, - winbase::{CREATE_NO_WINDOW, STD_INPUT_HANDLE}, - winnt::{ - MEM_COMMIT, MEM_RESERVE, PAGE_EXECUTE_READWRITE, PROCESS_ALL_ACCESS, SECURITY_ATTRIBUTES, - }, -}; - -pub fn run(name: String, pe: Vec, stdin: Vec) -> std::io::Result<()> { - let target_process_handle = unsafe { OpenProcess(PROCESS_ALL_ACCESS, false, ptr::null_mut()) }; - let base_address = unsafe { - VirtualAllocEx( - target_process_handle, - ptr::null_mut(), - 4096, - MEM_COMMIT | MEM_RESERVE, - PAGE_EXECUTE_READWRITE, - ) - }; - - unsafe { - WriteProcessMemory( - target_process_handle, - base_address, - pe.as_ptr() as *const _, - pe.len(), - ptr::null_mut(), - ); - } - - unsafe { - VirtualProtectEx( - target_process_handle, - base_address, - ptr::null_mut(), - 4096, - PAGE_EXECUTE_READWRITE, - ); - } - - let mut sa = SECURITY_ATTRIBUTES::default(); - sa.nLength = std::mem::size_of::() as u32; - sa.lpSecurityDescriptor = ptr::null_mut(); - sa.bInheritHandle = 1; - - let mut stdout_pipe_read_handle: HANDLE = ptr::null_mut(); - - let process_creation_and_interaction = || unsafe { - if CreatePipe(&mut stdout_pipe_read_handle, &mut ptr::null_mut(), &sa, 0) == 0 { - panic!("Failed to create stdout pipe"); - } - - let success = CreateProcessA( - ptr::null(), - ptr::null(), - &sa, - ptr::null_mut(), - true, - CREATE_NO_WINDOW, - ptr::null_mut(), - ptr::null_mut(), - base_address as *mut _, - ptr::null_mut(), - &mut ptr::null_mut(), - &mut stdout_pipe_read_handle, - ptr::null_mut(), - ); - - if success == 0 { - println!("Failed to create process."); - } else { - println!("Process created successfully."); - - let stdin_handle = GetStdHandle(STD_INPUT_HANDLE); - if stdin_handle.is_null() { - panic!("Failed to get stdin handle"); - } - unsafe { - if !WriteFile( - stdin_handle, - stdin.as_ptr() as *const _, - stdin.len(), - ptr::null_mut(), - ptr::null_mut(), - ) { - panic!("Failed to write to stdin"); - } - } - } - }; - - thread::spawn(process_creation_and_interaction); - - Ok(()) -} +pub fn run(name: String, pe: Vec, stdin: Vec) -> std::io::Result<()> { Ok(()) }