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
6 changes: 6 additions & 0 deletions docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ The <b>file.moveto</b> method moves a file or directory from `src` to `dst`. If

The <b>file.parent_dir</b> method returns the parent directory of a give path. Eg `/etc/ssh/sshd_config` -> `/etc/ssh`

### file.pwd (V2-Only)

`file.pwd() -> Option<str>`

The <b>file.pwd</b> method returns the current working directory of the process. If it could not be determined, `None` is returned.

### file.read

`file.read(path: str) -> str`
Expand Down
1 change: 1 addition & 0 deletions implants/lib/eldritchv2/eldritchv2/src/bindings_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn test_file_bindings() {
"mkdir",
"move",
"parent_dir",
"pwd",
"read",
"read_binary",
"remove",
Expand Down
4 changes: 4 additions & 0 deletions implants/lib/eldritchv2/stdlib/eldritch-libfile/src/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ impl FileLibrary for FileLibraryFake {
}
}

fn pwd(&self) -> Result<Option<String>, String> {
Ok(Some("/home/user".to_string()))
}

fn remove(&self, path: String) -> Result<(), String> {
let mut root = self.root.lock();
let parts = Self::normalize_path(&path);
Expand Down
7 changes: 7 additions & 0 deletions implants/lib/eldritchv2/stdlib/eldritch-libfile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ pub trait FileLibrary {
/// - Returns an error string if the file cannot be read.
fn read_binary(&self, path: String) -> Result<Vec<u8>, String>;

#[eldritch_method]
/// Returns the current working directory of the process.
///
/// **Returns**
/// - `Option<str>`: The current working directory path, or None if it cannot be determined.
fn pwd(&self) -> Result<Option<String>, String>;

#[eldritch_method]
/// Deletes a file or directory recursively.
///
Expand Down
16 changes: 16 additions & 0 deletions implants/lib/eldritchv2/stdlib/eldritch-libfile/src/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ impl FileLibrary for StdFileLibrary {
fs::read(&path).map_err(|e| format!("Failed to read file {path}: {e}"))
}

fn pwd(&self) -> Result<Option<String>, String> {
Ok(::std::env::current_dir()
.ok()
.map(|p| p.to_string_lossy().to_string()))
}

fn remove(&self, path: String) -> Result<(), String> {
let p = Path::new(&path);
if p.is_dir() {
Expand Down Expand Up @@ -1346,4 +1352,14 @@ cb

Ok(())
}

#[test]
fn test_pwd() -> AnyhowResult<()> {
let lib = StdFileLibrary;
let pwd = lib.pwd().unwrap();
assert!(pwd.is_some());
let pwd = pwd.unwrap();
assert!(std::path::Path::new(&pwd).is_absolute());
Ok(())
}
}
Loading