Background
I am developing a reverse shell in rust. I am using a PTY in order to create the sense of interactivity.
The crate i’m using is called portable_pty.
Their documents are pretty scarce, and the examples they show do not include reading the output of a command.
/// Struct to handle interaction with the pseudo terminal
pub struct Pty {
/// The pseudo terminal to interact with
pty_pair : PtyPair,
/// What shell is spawned inside the pseudo terminal
shell_handle : Box<dyn Child + Sync + Send>
}
impl Pty {
/// Create a new pty instance. ``shell_name`` represents the name or path of the shell you wish to spawn.
///
/// This function can fail. When a terminal is spawned, errors can occur. The error will be gracefully sent back to the caller.
pub fn try_new(shell_name : &str) -> Result<Self, Error> {
let pty_system = native_pty_system();
let pty_pair = pty_system.openpty(PtySize {
rows: 24,
cols: 80,
pixel_width: 0,
pixel_height: 0,
})?;
let program_shell = CommandBuilder::new(shell_name);
let shell_handle: Box<dyn Child + Sync + Send> = pty_pair.slave.spawn_command(program_shell)?;
Ok(Self {
pty_pair,
shell_handle
})
}
/// Writes a new command to the pseudo terminal. It returns back the output as a Vec<u8>.
pub fn exec(&mut self, input_buff : &str) -> Result<Vec<u8>, Error> {
let pty_pair = &self.pty_pair;
let mut pty_in = pty_pair.master.take_writer()?;
let mut pty_out = pty_pair.master.try_clone_reader()?;
writeln!(pty_in, "{}", input_buff)?;
let mut output = [0u8;1024];
pty_out.read(&mut output)?;
println!("Output of {} : {}", input_buff, String::from_utf8(output.to_vec())?);
/*
Only output given:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
*/
Ok(output.to_vec())
}
}
As shown, the only output given is the PowerShell splash screen. I doubt that the line where i print the output is executed, as the format is not shown. And not the output of the command that is supposed to be executed.
Razvan Sky9z is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.