Skip to content

Commit

Permalink
Implement process.name (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
adm1nPanda authored Oct 28, 2023
1 parent 3c33ce0 commit 8ccfa21
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ The <b>process.list</b> method returns a list of dictionaries that describe each
### process.name
`process.name(pid: int) -> str`

The <b>process.name</b> method is very cool, and will be even cooler when Nick documents it.
The <b>process.name</b> method returns the name of the process from it's given process id.

## Sys
### sys.dll_inject
Expand Down
67 changes: 64 additions & 3 deletions implants/lib/eldritch/src/process/name_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
use anyhow::Result;
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};

pub fn name(_pid: i32) -> Result<String> {
unimplemented!("Method unimplemented")
}
pub fn name(pid: i32) -> Result<String> {
if !System::IS_SUPPORTED {
return Err(anyhow::anyhow!("This OS isn't supported for process functions.
Please 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();

let mut res = "";

if let Some(process) = sys.process(Pid::from_u32(pid as u32)) {
res = process.name()
}

Ok(res.to_string())
}

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

#[test]
fn test_process_name() -> anyhow::Result<()>{
let mut commandstring = "";
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";
} else {
return Err(anyhow::anyhow!("OS Not supported please re run on Linux, Windows, or MacOS"));
}

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

let pname = name(child.id() as i32)?;
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") {
//If linux or Mac, Process Name should be 'sleep'
assert_eq!(pname, "sleep")
}else if cfg!(target_os = "windows") {
//If windows,Pprocess Name should be 'timeout.exe'
assert_eq!(pname, "timeout.exe")
}

return Ok(())
}
}

0 comments on commit 8ccfa21

Please sign in to comment.