Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hulto eldritch process kill #35

Merged
merged 8 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ The <b>file.write</b> method is very cool, and will be even cooler when Nick doc
### process.kill
`process.kill(pid: int) -> None`

The <b>process.kill</b> method is very cool, and will be even cooler when Nick documents it.
The <b>process.kill</b> will kill a process using the KILL signal given its process id.

### process.list
`process.list() -> List<str>`
Expand Down
70 changes: 67 additions & 3 deletions implants/eldritch/src/process/kill_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
use anyhow::Result;
use sysinfo::{ProcessExt,System,SystemExt,PidExt,Pid,Signal};

pub fn kill(_pid: i32) -> Result<()> {
unimplemented!("Method unimplemented")
}
pub fn kill(pid: i32) -> Result<()> {
if !System::IS_SUPPORTED {
return Err(anyhow::anyhow!("This OS isn't supported for process functions.
Pleases see sysinfo docs for a full list of supported systems.
https://docs.rs/sysinfo/0.23.5/sysinfo/index.html#supported-oses\n\n"));
}

let mut sys = System::new();
sys.refresh_processes();
if let Some(process) = sys.process(Pid::from_u32(pid as u32)) {
process.kill_with(Signal::Kill);
}

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use core::time;
use std::{process::Command, thread};

#[test]
fn test_process_kill() -> anyhow::Result<()>{
let mut commandstring = "sleep 5";
if cfg!(target_os = "linux") ||
cfg!(target_os = "ios") ||
cfg!(target_os = "macos") ||
cfg!(target_os = "android") ||
cfg!(target_os = "freebsd") ||
cfg!(target_os = "openbsd") ||
cfg!(target_os = "netbsd") {
commandstring = "sleep";
} else if cfg!(target_os = "windows") {
commandstring = "timeout";
}

let child = Command::new(commandstring)
.arg("120")
.spawn()?;

let mut sys = System::new();
sys.refresh_processes();
for (pid, process) in sys.processes() {
if pid.as_u32() == child.id(){
let i32_pid = pid.as_u32() as i32;
kill(i32_pid)?;
assert_eq!(true, true)
}
}
let mut sys = System::new();
sys.refresh_processes();
for (pid, process) in sys.processes() {
if pid.as_u32() == child.id() {
if cfg!(target_os = "linux") {
// Linux child PID will become Zombie
assert_eq!(process.status().to_string(), "Zombie")
}else if cfg!(target_os = "macos") || cfg!(target_os = "windows") {
//MacOS Child PID should not exist.
assert_eq!(false, true);
}
}
}
return Ok(())
}
}