diff --git a/docs/_docs/user-guide/eldritch.md b/docs/_docs/user-guide/eldritch.md index da5d7cce1..ec56e8e6f 100644 --- a/docs/_docs/user-guide/eldritch.md +++ b/docs/_docs/user-guide/eldritch.md @@ -381,6 +381,12 @@ The file.replace method finds the first string matching a regex pattern i The file.replace_all method finds all strings matching a regex pattern in the specified file and replaces them with the value. Please consult the [Rust Regex Docs](https://rust-lang-nursery.github.io/rust-cookbook/text/regex.html) for more information on pattern matching. +### file.tmp_file + +`file.tmp_file(name: Option) -> str` + +The file.temp method returns the path of a new temporary file with a random filename or the optional filename provided as an argument. + ### file.template `file.template(template_path: str, dst: str, args: Dict, autoescape: bool) -> None` diff --git a/implants/lib/eldritch/src/file/mod.rs b/implants/lib/eldritch/src/file/mod.rs index a2ac0a99c..a3134258d 100644 --- a/implants/lib/eldritch/src/file/mod.rs +++ b/implants/lib/eldritch/src/file/mod.rs @@ -14,6 +14,7 @@ mod read_impl; mod remove_impl; mod replace_all_impl; mod replace_impl; +mod temp_file_impl; mod template_impl; mod timestomp_impl; mod write_impl; @@ -176,4 +177,10 @@ fn methods(builder: &mut MethodsBuilder) { follow_impl::follow(path, f, eval)?; Ok(NoneType{}) } + + #[allow(unused_variables)] + fn temp_file(this: &FileLibrary, name: Option) -> anyhow::Result { + temp_file_impl::temp_file(name) + } + } diff --git a/implants/lib/eldritch/src/file/temp_file_impl.rs b/implants/lib/eldritch/src/file/temp_file_impl.rs new file mode 100644 index 000000000..216d906f4 --- /dev/null +++ b/implants/lib/eldritch/src/file/temp_file_impl.rs @@ -0,0 +1,51 @@ +use anyhow::Result; +use std::env; +use std::fs::File; +use tempfile::NamedTempFile; + +pub fn temp_file(name: Option) -> Result { + let mut temp_path; + + match name { + None => { + // Generate a random file name if name is not provided + let tfile = NamedTempFile::new()?; + (_, temp_path) = tfile.keep()?; + } + Some(n) => { + temp_path = env::temp_dir(); + temp_path.push(n); + _ = File::create(&temp_path)?; + } + } + // Create the file in the temporary directory + + Ok(temp_path.display().to_string()) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::path::Path; + + #[test] + fn test_temp_file_w_name() -> anyhow::Result<()> { + // Create file with a name + let p = temp_file(Some("foo".to_string()))?; + // check if file exists + let t = Path::new(&p); + assert!(t.exists()); + assert!(t.file_name().unwrap() == "foo"); + + Ok(()) + } + #[test] + fn test_temp_file_r_name() -> anyhow::Result<()> { + // Create file with a random name + let p = temp_file(None)?; + // check if file exists + assert!(Path::new(&p).exists()); + + Ok(()) + } +} diff --git a/implants/lib/eldritch/src/runtime/mod.rs b/implants/lib/eldritch/src/runtime/mod.rs index 995306dbb..b476f632b 100644 --- a/implants/lib/eldritch/src/runtime/mod.rs +++ b/implants/lib/eldritch/src/runtime/mod.rs @@ -89,7 +89,7 @@ mod tests { parameters: HashMap::new(), file_names: Vec::new(), }, - want_text: format!("{}\n", r#"["append", "compress", "copy", "exists", "find", "follow", "is_dir", "is_file", "list", "mkdir", "moveto", "parent_dir", "read", "remove", "replace", "replace_all", "template", "timestomp", "write"]"#), + want_text: format!("{}\n", r#"["append", "compress", "copy", "exists", "find", "follow", "is_dir", "is_file", "list", "mkdir", "moveto", "parent_dir", "read", "remove", "replace", "replace_all", "temp_file", "template", "timestomp", "write"]"#), want_error: None, }, process_bindings: TestCase { diff --git a/implants/lib/pb/src/generated/c2.rs b/implants/lib/pb/src/generated/c2.rs index 85563379b..c388d9b63 100644 --- a/implants/lib/pb/src/generated/c2.rs +++ b/implants/lib/pb/src/generated/c2.rs @@ -1,3 +1,4 @@ +// This file is @generated by prost-build. /// Agent information to identify the type of beacon. #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] diff --git a/implants/lib/pb/src/generated/eldritch.rs b/implants/lib/pb/src/generated/eldritch.rs index 6c7cac2b2..ac4647c34 100644 --- a/implants/lib/pb/src/generated/eldritch.rs +++ b/implants/lib/pb/src/generated/eldritch.rs @@ -1,3 +1,4 @@ +// This file is @generated by prost-build. /// Tome for eldritch to execute. #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)]