Compare commits
No commits in common. "main" and "6d4e806" have entirely different histories.
6 changed files with 64 additions and 121 deletions
|
@ -1,5 +1 @@
|
||||||
# Remote Code Execution Program
|
# rust-remote
|
||||||
|
|
||||||
I implemented this for my remote server.
|
|
||||||
|
|
||||||
Sometimes ssh can't be possible because of NAT. That's why I add a public remote server as a relay.
|
|
|
@ -66,22 +66,10 @@ async fn serve((ws_sender, mut ws_receiver): (WebSocketSender, WebSocketReceiver
|
||||||
|
|
||||||
async fn execute(payload: Payload, debug: bool) -> Option<Output> {
|
async fn execute(payload: Payload, debug: bool) -> Option<Output> {
|
||||||
if debug {
|
if debug {
|
||||||
payload.print();
|
println!("{:#?}", payload);
|
||||||
}
|
}
|
||||||
let command = if cfg!(target_os = "windows") {
|
match Command::new(payload.command)
|
||||||
"cmd"
|
.args(payload.args)
|
||||||
} else {
|
|
||||||
"sh"
|
|
||||||
};
|
|
||||||
|
|
||||||
let first_arg = if cfg!(target_os = "windows") {
|
|
||||||
"/C"
|
|
||||||
} else {
|
|
||||||
"-c"
|
|
||||||
};
|
|
||||||
match Command::new(command)
|
|
||||||
.arg(first_arg)
|
|
||||||
.arg(payload.args)
|
|
||||||
.output()
|
.output()
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
|
@ -90,7 +78,7 @@ async fn execute(payload: Payload, debug: bool) -> Option<Output> {
|
||||||
if debug {
|
if debug {
|
||||||
eprintln!("Error: Command Execution | {}", err_val);
|
eprintln!("Error: Command Execution | {}", err_val);
|
||||||
}
|
}
|
||||||
None
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,10 +133,6 @@ async fn send(
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if debug {
|
|
||||||
report.print();
|
|
||||||
}
|
|
||||||
|
|
||||||
let report = serde_json::json!(report);
|
let report = serde_json::json!(report);
|
||||||
let result = ws_sender.lock().await.send(report.to_string().into()).await;
|
let result = ws_sender.lock().await.send(report.to_string().into()).await;
|
||||||
match result {
|
match result {
|
||||||
|
|
61
src/lib.rs
61
src/lib.rs
|
@ -11,56 +11,23 @@ pub enum Runner {
|
||||||
Server,
|
Server,
|
||||||
Client,
|
Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Runner {
|
|
||||||
fn print(&self) {
|
|
||||||
println!("-------");
|
|
||||||
match self {
|
|
||||||
Runner::Server => println!("Runner = Server"),
|
|
||||||
Runner::Client => println!("Runner = Client"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum RunnerMode {
|
pub enum RunnerMode {
|
||||||
State(Runner, bool),
|
State(Runner, bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RunnerMode {
|
|
||||||
pub fn print(&self) {
|
|
||||||
match self {
|
|
||||||
RunnerMode::State(runner, debug) => {
|
|
||||||
runner.print();
|
|
||||||
println!("Debug = {}", debug);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub ip: IpAddr,
|
pub ip: IpAddr,
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
|
||||||
pub fn print(&self) {
|
|
||||||
println!("-------");
|
|
||||||
println!("IP = {}", self.ip);
|
|
||||||
println!("Port = {}", self.port);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct Payload {
|
pub struct Payload {
|
||||||
pub args: String,
|
pub sudo: bool,
|
||||||
}
|
pub user: String,
|
||||||
|
pub command: String,
|
||||||
impl Payload {
|
pub args: Vec<String>,
|
||||||
fn print(&self) {
|
|
||||||
println!("-------");
|
|
||||||
println!("args = {}", self.args);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
@ -70,23 +37,3 @@ pub struct Report {
|
||||||
pub stdout: String,
|
pub stdout: String,
|
||||||
pub stderr: String,
|
pub stderr: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Report {
|
|
||||||
fn print(&self) {
|
|
||||||
println!("-------");
|
|
||||||
println!("Payload ↓");
|
|
||||||
self.payload.print();
|
|
||||||
println!("-------");
|
|
||||||
if !self.status.is_empty() {
|
|
||||||
println!("Status ↓ \n{}", self.status);
|
|
||||||
println!("-------");
|
|
||||||
}
|
|
||||||
if !self.stdout.is_empty() {
|
|
||||||
println!("Stdout ↓ \n{}", self.stdout);
|
|
||||||
println!("-------");
|
|
||||||
}
|
|
||||||
if !self.stderr.is_empty() {
|
|
||||||
println!("Stderr ↓ \n{}", self.stderr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -5,27 +5,23 @@ async fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
|
||||||
let args = take_args();
|
let args = take_args();
|
||||||
|
println!("{:#?}", args);
|
||||||
|
|
||||||
match args {
|
match args {
|
||||||
Some((runner_mode, config)) => {
|
Some((runner_mode, config)) => match runner_mode {
|
||||||
runner_mode.print();
|
RunnerMode::State(Runner::Server, false) => {
|
||||||
config.print();
|
rust_remote::server::start(config, false).await
|
||||||
|
|
||||||
match runner_mode {
|
|
||||||
RunnerMode::State(Runner::Server, false) => {
|
|
||||||
rust_remote::server::start(config, false).await
|
|
||||||
}
|
|
||||||
RunnerMode::State(Runner::Server, true) => {
|
|
||||||
rust_remote::server::start(config, true).await
|
|
||||||
}
|
|
||||||
RunnerMode::State(Runner::Client, false) => {
|
|
||||||
rust_remote::client::start(config, false).await
|
|
||||||
}
|
|
||||||
RunnerMode::State(Runner::Client, true) => {
|
|
||||||
rust_remote::client::start(config, true).await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
RunnerMode::State(Runner::Server, true) => {
|
||||||
|
rust_remote::server::start(config, true).await
|
||||||
|
}
|
||||||
|
RunnerMode::State(Runner::Client, false) => {
|
||||||
|
rust_remote::client::start(config, false).await
|
||||||
|
}
|
||||||
|
RunnerMode::State(Runner::Client, true) => {
|
||||||
|
rust_remote::client::start(config, true).await
|
||||||
|
}
|
||||||
|
},
|
||||||
None => {
|
None => {
|
||||||
eprintln!("Error: Take Args");
|
eprintln!("Error: Take Args");
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -10,7 +10,7 @@ use tokio::{
|
||||||
};
|
};
|
||||||
use tokio_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
|
use tokio_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
|
||||||
|
|
||||||
use crate::{Config, Payload, Report};
|
use crate::{Config, Payload};
|
||||||
type WebSocketSender = SplitSink<WebSocketStream<TcpStream>, Message>;
|
type WebSocketSender = SplitSink<WebSocketStream<TcpStream>, Message>;
|
||||||
type WebSocketReceiver = SplitStream<WebSocketStream<TcpStream>>;
|
type WebSocketReceiver = SplitStream<WebSocketStream<TcpStream>>;
|
||||||
|
|
||||||
|
@ -31,22 +31,11 @@ pub async fn start(config: Config, debug: bool) {
|
||||||
match payload_from_input(debug).await {
|
match payload_from_input(debug).await {
|
||||||
Some(payload) => {
|
Some(payload) => {
|
||||||
if !send(payload, ws_sender, debug).await {
|
if !send(payload, ws_sender, debug).await {
|
||||||
if debug {
|
|
||||||
eprintln!("Error: Send");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Some(report) = receive(ws_receiver, debug).await {
|
let report = receive(ws_receiver, debug).await;
|
||||||
match serde_json::from_str::<Report>(&report) {
|
println!("{:#?}", report);
|
||||||
Ok(report) => report.print(),
|
|
||||||
Err(err_val) => {
|
|
||||||
if debug {
|
|
||||||
eprintln!("Error: Deserialize | {}", err_val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
None => continue,
|
None => continue,
|
||||||
|
@ -93,9 +82,38 @@ async fn establish_connection(
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn payload_from_input(debug: bool) -> Option<Payload> {
|
async fn payload_from_input(debug: bool) -> Option<Payload> {
|
||||||
println!("-------");
|
println!("User");
|
||||||
|
// let user = match get_input() {
|
||||||
|
// Some(input) => input,
|
||||||
|
// None => return None,
|
||||||
|
// };
|
||||||
|
let user = "tahinli".to_string();
|
||||||
println!("Command");
|
println!("Command");
|
||||||
get_input(debug).map(|args| Payload { args })
|
match get_input(debug) {
|
||||||
|
Some(input) => {
|
||||||
|
let mut args: Vec<String> = input.split_ascii_whitespace().map(String::from).collect();
|
||||||
|
if args.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
let mut sudo = false;
|
||||||
|
let mut command = args.remove(0);
|
||||||
|
if command == "sudo" {
|
||||||
|
if args.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
sudo = true;
|
||||||
|
command = args.remove(0);
|
||||||
|
}
|
||||||
|
Some(Payload {
|
||||||
|
sudo,
|
||||||
|
user,
|
||||||
|
command,
|
||||||
|
args,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_input(debug: bool) -> Option<String> {
|
fn get_input(debug: bool) -> Option<String> {
|
||||||
|
|
12
src/utils.rs
12
src/utils.rs
|
@ -4,7 +4,7 @@ use crate::{Config, Runner, RunnerMode};
|
||||||
|
|
||||||
pub fn take_args() -> Option<(RunnerMode, Config)> {
|
pub fn take_args() -> Option<(RunnerMode, Config)> {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
let mut runner = Runner::Client;
|
let mut runner = Runner::Server;
|
||||||
let mut debug = false;
|
let mut debug = false;
|
||||||
let mut ip = "127.0.0.1".to_string();
|
let mut ip = "127.0.0.1".to_string();
|
||||||
let mut port = "3444".to_string();
|
let mut port = "3444".to_string();
|
||||||
|
@ -13,8 +13,8 @@ pub fn take_args() -> Option<(RunnerMode, Config)> {
|
||||||
"--server" | "-sv" => runner = Runner::Server,
|
"--server" | "-sv" => runner = Runner::Server,
|
||||||
"--client" | "-cl" => runner = Runner::Client,
|
"--client" | "-cl" => runner = Runner::Client,
|
||||||
"--debug" | "-d" => debug = true,
|
"--debug" | "-d" => debug = true,
|
||||||
"--ip" | "-i" => ip.clone_from(&args[i + 1]),
|
"--ip" | "-i" => ip = args[i + 1].clone(),
|
||||||
"--port" | "-p" => port.clone_from(&args[i + 1]),
|
"--port" | "-p" => port = args[i + 1].clone(),
|
||||||
"--help" | "-h" => {
|
"--help" | "-h" => {
|
||||||
show_help();
|
show_help();
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
|
@ -30,6 +30,8 @@ pub fn take_args() -> Option<(RunnerMode, Config)> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
println!("{:#?}", ip);
|
||||||
|
|
||||||
let port = match port.parse::<u16>() {
|
let port = match port.parse::<u16>() {
|
||||||
Ok(port) => port,
|
Ok(port) => port,
|
||||||
Err(err_val) => {
|
Err(err_val) => {
|
||||||
|
@ -50,8 +52,8 @@ fn show_help() {
|
||||||
println!("----------------------------------------------------------------------");
|
println!("----------------------------------------------------------------------");
|
||||||
println!(" -i -> --ip | Specifies IP Address | 127.0.0.1");
|
println!(" -i -> --ip | Specifies IP Address | 127.0.0.1");
|
||||||
println!(" -p -> --port | Specifies Port Address | 3444");
|
println!(" -p -> --port | Specifies Port Address | 3444");
|
||||||
println!(" -sv -> --server | Starts as a Server | False");
|
println!(" -sv -> --server | Starts as a Server | True");
|
||||||
println!(" -cl -> --client | Starts as a Client | True");
|
println!(" -cl -> --client | Starts as a Client | False");
|
||||||
println!(" -d -> --debug | Starts in Debug Mode | False");
|
println!(" -d -> --debug | Starts in Debug Mode | False");
|
||||||
println!(" -h -> --help | Shows Help | False");
|
println!(" -h -> --help | Shows Help | False");
|
||||||
println!("\n\n\n");
|
println!("\n\n\n");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue