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

Implement sys.get_os #238

Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,21 @@ sys.execute("/bin/bash",["-c", "ls /nofile"])
}
```

### sys.get_os
`sys.get_os() -> Dict`

The <b>sys.get_os</b> method returns a dictionary that describes the current systems OS.
An example is below:

```json
{
"arch": "x86_64",
"desktop_env": "Unknown: Unknown",
"distro": "Debian GNU/Linux 10 (buster)",
"platform": "Linux"
}
```

### sys.is_linux
`sys.is_linux() -> bool`

Expand Down
1 change: 1 addition & 0 deletions implants/lib/eldritch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ windows-sys = { workspace = true, features = [
"Win32_System_Diagnostics_Debug",
"Win32_Security",
]}
whoami = { workspace = true }

[dev-dependencies]
httptest = { workspace = true }
2 changes: 1 addition & 1 deletion implants/lib/eldritch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mod tests {
r#"
dir(file) == ["append", "compress", "copy", "download", "exists", "hash", "is_dir", "is_file", "mkdir", "read", "remove", "rename", "replace", "replace_all", "template", "timestomp", "write"]
dir(process) == ["kill", "list", "name"]
dir(sys) == ["dll_inject", "exec", "is_linux", "is_macos", "is_windows", "shell"]
dir(sys) == ["dll_inject", "exec", "get_os", "is_linux", "is_macos", "is_windows", "shell"]
dir(pivot) == ["arp_scan", "bind_proxy", "ncat", "port_forward", "port_scan", "smb_exec", "ssh_exec", "ssh_password_spray"]
dir(assets) == ["copy","list"]
"#,
Expand Down
5 changes: 5 additions & 0 deletions implants/lib/eldritch/src/sys.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod exec_impl;
mod get_os_impl;
mod is_linux_impl;
mod is_windows_impl;
mod is_macos_impl;
Expand Down Expand Up @@ -61,6 +62,10 @@ fn methods(builder: &mut MethodsBuilder) {
if false { println!("Ignore unused this var. _this isn't allowed by starlark. {:?}", this); }
exec_impl::exec(starlark_heap, path, args, disown)
}
fn get_os<'v>(this: SysLibrary, starlark_heap: &'v Heap) -> anyhow::Result<Dict<'v>> {
if false { println!("Ignore unused this var. _this isn't allowed by starlark. {:?}", this); }
get_os_impl::get_os(starlark_heap)
}
fn dll_inject(this: SysLibrary, dll_path: String, pid: u32) -> anyhow::Result<NoneType> {
if false { println!("Ignore unused this var. _this isn't allowed by starlark. {:?}", this); }
dll_inject_impl::dll_inject(dll_path, pid)
Expand Down
58 changes: 58 additions & 0 deletions implants/lib/eldritch/src/sys/get_os_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use anyhow::Result;
use starlark::collections::SmallMap;
use starlark::const_frozen_string;
use starlark::values::Heap;
use starlark::values::dict::Dict;

#[derive(Debug)]
struct OsInfo {
arch: String,
desktop_env: String,
distro: String,
platform: String,
}

pub fn get_os(starlark_heap: &Heap) -> Result<Dict> {

let cmd_res = handle_get_os()?;

let res = SmallMap::new();
let mut dict_res = Dict::new(res);
let arch_value = starlark_heap.alloc_str(&cmd_res.arch);
dict_res.insert_hashed(const_frozen_string!("arch").to_value().get_hashed().unwrap(), arch_value.to_value());

let desktop_env_value = starlark_heap.alloc_str(&cmd_res.desktop_env);
dict_res.insert_hashed(const_frozen_string!("desktop_env").to_value().get_hashed().unwrap(), desktop_env_value.to_value());

let distro = starlark_heap.alloc_str(&cmd_res.distro);
dict_res.insert_hashed(const_frozen_string!("distro").to_value().get_hashed().unwrap(), distro.to_value());

let platform = starlark_heap.alloc_str(&cmd_res.platform);
dict_res.insert_hashed(const_frozen_string!("platform").to_value().get_hashed().unwrap(), platform.to_value());

Ok(dict_res)
}

fn handle_get_os() -> Result<OsInfo> {
return Ok(OsInfo {
arch: whoami::arch().to_string(),
desktop_env: whoami::desktop_env().to_string(),
distro: whoami::distro().to_string(),
platform: whoami::platform().to_string(),
});
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_sys_get_os() -> anyhow::Result<()>{

let test_heap = Heap::new();
let res = get_os(&test_heap)?;
println!("{}", res.to_string());
assert!(res.to_string().contains(r#""arch": "x86_64""#));
hulto marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}
}