Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions implants/lib/eldritch/src/pivot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,28 @@ impl Session {
async fn call(&mut self, command: &str) -> anyhow::Result<CommandResult> {
let mut channel = self.session.channel_open_session().await?;
channel.exec(true, command).await?;
let mut output = Vec::new();
let mut stdout = Vec::new();
let mut stderr = Vec::new();
let mut code = None;
while let Some(msg) = channel.wait().await {
match msg {
russh::ChannelMsg::Data { ref data } => {
std::io::Write::write_all(&mut output, data).unwrap();
std::io::Write::write_all(&mut stdout, data).unwrap();
}
russh::ChannelMsg::ExtendedData { ref data, ext: _ } => {
std::io::Write::write_all(&mut stderr, data).unwrap();
}
russh::ChannelMsg::ExitStatus { exit_status } => {
code = Some(exit_status);
}
_ => {}
}
}
Ok(CommandResult { output, code })
Ok(CommandResult {
stdout,
stderr,
code,
})
}

async fn close(&mut self) -> anyhow::Result<()> {
Expand All @@ -185,12 +193,17 @@ impl Session {
}

struct CommandResult {
output: Vec<u8>,
stdout: Vec<u8>,
stderr: Vec<u8>,
code: Option<u32>,
}

impl CommandResult {
fn output(&self) -> Result<String> {
Ok(String::from_utf8_lossy(&self.output).to_string())
Ok(String::from_utf8_lossy(&self.stdout).to_string())
}

fn error(&self) -> Result<String> {
Ok(String::from_utf8_lossy(&self.stderr).to_string())
}
}
18 changes: 12 additions & 6 deletions implants/lib/eldritch/src/pivot/ssh_exec_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use starlark::const_frozen_string;

struct SSHExecOutput {
stdout: String,
stderr: String,
status: i32,
}

Expand Down Expand Up @@ -40,6 +41,7 @@ async fn handle_ssh_exec(

Ok(SSHExecOutput {
stdout: r.output()?,
stderr: r.error()?,
status: r.code.unwrap_or(0) as i32,
})
}
Expand All @@ -63,7 +65,7 @@ pub fn ssh_exec(
let key_password_ref = key_password.as_deref();
let local_port: u16 = port.try_into()?;

let (out, status, err) = match runtime.block_on(handle_ssh_exec(
let out = match runtime.block_on(handle_ssh_exec(
target,
local_port,
command,
Expand All @@ -73,15 +75,19 @@ pub fn ssh_exec(
key_password_ref,
timeout,
)) {
Ok(local_res) => (local_res.stdout, local_res.status, String::from("")),
Err(local_err) => (String::from(""), -1, local_err.to_string()),
Ok(local_res) => local_res,
Err(local_err) => SSHExecOutput {
stdout: String::from(""),
stderr: local_err.to_string(),
status: -1,
},
};

let res = SmallMap::new();
let mut dict_res = Dict::new(res);
insert_dict_kv!(dict_res, starlark_heap, "stdout", &out, String);
insert_dict_kv!(dict_res, starlark_heap, "stderr", &err, String);
insert_dict_kv!(dict_res, starlark_heap, "status", status, i32);
insert_dict_kv!(dict_res, starlark_heap, "stdout", &out.stdout, String);
insert_dict_kv!(dict_res, starlark_heap, "stderr", &out.stderr, String);
insert_dict_kv!(dict_res, starlark_heap, "status", out.status, i32);

Ok(dict_res)
}
Expand Down
Loading