From 3271116c4cd6b818a4c3474403386de6493d38f7 Mon Sep 17 00:00:00 2001 From: adm1npanda Date: Tue, 6 Aug 2024 04:33:49 +0000 Subject: [PATCH 1/8] adding file.temp_file to eldritch --- docs/_docs/user-guide/eldritch.md | 6 +++ implants/lib/eldritch/src/file/mod.rs | 7 +++ .../lib/eldritch/src/file/temp_file_impl.rs | 45 +++++++++++++++++++ implants/lib/eldritch/src/runtime/mod.rs | 2 +- 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 implants/lib/eldritch/src/file/temp_file_impl.rs diff --git a/docs/_docs/user-guide/eldritch.md b/docs/_docs/user-guide/eldritch.md index da5d7cce1..048439f4b 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(path: str) -> str` + +The file.temp method returns the path of a new temporary file with a random name. + ### 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..26b205175 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, path: String) -> anyhow::Result { + temp_file_impl::temp_file(path) + } + } 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..eaf37589a --- /dev/null +++ b/implants/lib/eldritch/src/file/temp_file_impl.rs @@ -0,0 +1,45 @@ +use anyhow::Result; +use rand::{thread_rng, Rng}; +use std::io::Write; +use tempfile::NamedTempFile; + +pub fn temp_file(path: String) -> Result { + let mut tmp_file = NamedTempFile::new_in(path)?; + let mut rng = thread_rng(); + let random_data: Vec = (0..100).map(|_| rng.gen()).collect(); + tmp_file.write_all(&random_data)?; + + let (_file, path) = tmp_file.keep()?; + Ok(String::from(path.to_str().unwrap()).clone()) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + use std::path::Path; + + #[test] + fn test_temp_file() -> anyhow::Result<()> { + // Create file + let p = temp_file("".to_string())?; + // Run our code + assert!(Path::new(&p).exists()); + + Ok(()) + } + #[test] + fn test_temp_no_file() -> anyhow::Result<()> { + // Create file and then delete it (so we know it doesnt exist) + let p = temp_file("".to_string())?; + if Path::new(&p).exists() { + // delete the file + fs::remove_file(&p)?; + } + + // Run our code + 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 { From abd5fb56b763629b7122d493212e6353ac51e577 Mon Sep 17 00:00:00 2001 From: adm1npanda Date: Thu, 8 Aug 2024 23:39:55 +0000 Subject: [PATCH 2/8] temp_file() now takes name as argument and returns path to file --- docs/_docs/user-guide/eldritch.md | 4 +-- implants/lib/eldritch/src/file/mod.rs | 4 +-- .../lib/eldritch/src/file/temp_file_impl.rs | 27 ++++++++++--------- implants/lib/pb/src/generated/c2.rs | 1 + implants/lib/pb/src/generated/eldritch.rs | 1 + 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/docs/_docs/user-guide/eldritch.md b/docs/_docs/user-guide/eldritch.md index 048439f4b..ffb8aa531 100644 --- a/docs/_docs/user-guide/eldritch.md +++ b/docs/_docs/user-guide/eldritch.md @@ -383,9 +383,9 @@ The file.replace_all method finds all strings matching a regex pattern in ### file.tmp_file -`file.tmp_file(path: str) -> str` +`file.tmp_file(name: str) -> str` -The file.temp method returns the path of a new temporary file with a random name. +The file.temp method returns the path of a new temporary file with the name provided as the argument. ### file.template diff --git a/implants/lib/eldritch/src/file/mod.rs b/implants/lib/eldritch/src/file/mod.rs index 26b205175..29a4d7114 100644 --- a/implants/lib/eldritch/src/file/mod.rs +++ b/implants/lib/eldritch/src/file/mod.rs @@ -179,8 +179,8 @@ fn methods(builder: &mut MethodsBuilder) { } #[allow(unused_variables)] - fn temp_file(this: &FileLibrary, path: String) -> anyhow::Result { - temp_file_impl::temp_file(path) + fn temp_file(this: &FileLibrary, name: String) -> 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 index eaf37589a..3113f5cde 100644 --- a/implants/lib/eldritch/src/file/temp_file_impl.rs +++ b/implants/lib/eldritch/src/file/temp_file_impl.rs @@ -1,29 +1,30 @@ use anyhow::Result; -use rand::{thread_rng, Rng}; -use std::io::Write; +use std::fs; +use std::path::PathBuf; use tempfile::NamedTempFile; -pub fn temp_file(path: String) -> Result { - let mut tmp_file = NamedTempFile::new_in(path)?; - let mut rng = thread_rng(); - let random_data: Vec = (0..100).map(|_| rng.gen()).collect(); - tmp_file.write_all(&random_data)?; +pub fn temp_file(name: String) -> Result { + //create a file in temp folder + let tmp_file = NamedTempFile::new()?; + let tdir: PathBuf = tmp_file.path().parent().unwrap().into(); + let new_path = tdir.join(name); + let (_tf, tpath) = tmp_file.keep()?; - let (_file, path) = tmp_file.keep()?; - Ok(String::from(path.to_str().unwrap()).clone()) + fs::rename(&tpath, &new_path)?; + + Ok(String::from(new_path.to_str().unwrap()).clone()) } #[cfg(test)] mod tests { use super::*; - use std::fs; use std::path::Path; #[test] fn test_temp_file() -> anyhow::Result<()> { // Create file - let p = temp_file("".to_string())?; - // Run our code + let p = temp_file("foo".to_string())?; + // check if file exists assert!(Path::new(&p).exists()); Ok(()) @@ -31,7 +32,7 @@ mod tests { #[test] fn test_temp_no_file() -> anyhow::Result<()> { // Create file and then delete it (so we know it doesnt exist) - let p = temp_file("".to_string())?; + let p = temp_file("foo".to_string())?; if Path::new(&p).exists() { // delete the file fs::remove_file(&p)?; 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)] From 58f0e070f72b8f742442cdc6189830dfeaf44fb6 Mon Sep 17 00:00:00 2001 From: adm1npanda Date: Fri, 9 Aug 2024 00:14:53 +0000 Subject: [PATCH 3/8] fix wrong comment --- implants/lib/eldritch/src/file/temp_file_impl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/implants/lib/eldritch/src/file/temp_file_impl.rs b/implants/lib/eldritch/src/file/temp_file_impl.rs index 3113f5cde..55a5936c3 100644 --- a/implants/lib/eldritch/src/file/temp_file_impl.rs +++ b/implants/lib/eldritch/src/file/temp_file_impl.rs @@ -38,7 +38,7 @@ mod tests { fs::remove_file(&p)?; } - // Run our code + // check if file exists assert!(!Path::new(&p).exists()); Ok(()) From 9d1b6e6c28a11e170b945f6167e4435064ddffbf Mon Sep 17 00:00:00 2001 From: adm1npanda Date: Sat, 10 Aug 2024 03:36:34 +0000 Subject: [PATCH 4/8] name arg is now optional. --- docs/_docs/user-guide/eldritch.md | 2 +- .../lib/eldritch/src/file/temp_file_impl.rs | 59 ++++++++++++++----- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/docs/_docs/user-guide/eldritch.md b/docs/_docs/user-guide/eldritch.md index ffb8aa531..2b4e223d7 100644 --- a/docs/_docs/user-guide/eldritch.md +++ b/docs/_docs/user-guide/eldritch.md @@ -385,7 +385,7 @@ The file.replace_all method finds all strings matching a regex pattern in `file.tmp_file(name: str) -> str` -The file.temp method returns the path of a new temporary file with the name provided as the argument. +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 diff --git a/implants/lib/eldritch/src/file/temp_file_impl.rs b/implants/lib/eldritch/src/file/temp_file_impl.rs index 55a5936c3..286170336 100644 --- a/implants/lib/eldritch/src/file/temp_file_impl.rs +++ b/implants/lib/eldritch/src/file/temp_file_impl.rs @@ -1,28 +1,36 @@ use anyhow::Result; -use std::fs; -use std::path::PathBuf; +use std::env; +use std::fs::File; use tempfile::NamedTempFile; pub fn temp_file(name: String) -> Result { - //create a file in temp folder - let tmp_file = NamedTempFile::new()?; - let tdir: PathBuf = tmp_file.path().parent().unwrap().into(); - let new_path = tdir.join(name); - let (_tf, tpath) = tmp_file.keep()?; + let mut temp_path; + let _file; - fs::rename(&tpath, &new_path)?; + if name.is_empty() { + // Generate a random file name if name is not provided + let tfile = NamedTempFile::new()?; + (_file, temp_path) = tfile.keep()?; + } else { + temp_path = env::temp_dir(); + temp_path.push(name); + _file = File::create(&temp_path)?; + } + + // Create the file in the temporary directory - Ok(String::from(new_path.to_str().unwrap()).clone()) + Ok(temp_path.display().to_string()) } #[cfg(test)] mod tests { use super::*; + use std::fs; use std::path::Path; #[test] - fn test_temp_file() -> anyhow::Result<()> { - // Create file + fn test_temp_file_w_name() -> anyhow::Result<()> { + // Create file with a name let p = temp_file("foo".to_string())?; // check if file exists assert!(Path::new(&p).exists()); @@ -30,15 +38,38 @@ mod tests { Ok(()) } #[test] - fn test_temp_no_file() -> anyhow::Result<()> { - // Create file and then delete it (so we know it doesnt exist) + fn test_temp_file_r_name() -> anyhow::Result<()> { + // Create file with a random name + let p = temp_file("".to_string())?; + // check if file exists + assert!(Path::new(&p).exists()); + + Ok(()) + } + #[test] + fn test_temp_no_file_w_name() -> anyhow::Result<()> { + // Create file with a name and then delete it (so we know it doesnt exist) let p = temp_file("foo".to_string())?; if Path::new(&p).exists() { // delete the file fs::remove_file(&p)?; } - // check if file exists + // check file doesn't exists + assert!(!Path::new(&p).exists()); + + Ok(()) + } + #[test] + fn test_temp_no_file_r_name() -> anyhow::Result<()> { + // Create file with a random name and then delete it (so we know it doesnt exist) + let p = temp_file("".to_string())?; + if Path::new(&p).exists() { + // delete the file + fs::remove_file(&p)?; + } + + // check file doesn't exists assert!(!Path::new(&p).exists()); Ok(()) From 6a83d66606881bb02fb567536a7420350151a034 Mon Sep 17 00:00:00 2001 From: adm1npanda Date: Sun, 11 Aug 2024 04:14:41 +0000 Subject: [PATCH 5/8] added Optional like find function --- docs/_docs/user-guide/eldritch.md | 2 +- implants/lib/eldritch/src/file/mod.rs | 2 +- .../lib/eldritch/src/file/temp_file_impl.rs | 30 ++++++++++--------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/docs/_docs/user-guide/eldritch.md b/docs/_docs/user-guide/eldritch.md index 2b4e223d7..ec56e8e6f 100644 --- a/docs/_docs/user-guide/eldritch.md +++ b/docs/_docs/user-guide/eldritch.md @@ -383,7 +383,7 @@ The file.replace_all method finds all strings matching a regex pattern in ### file.tmp_file -`file.tmp_file(name: str) -> str` +`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. diff --git a/implants/lib/eldritch/src/file/mod.rs b/implants/lib/eldritch/src/file/mod.rs index 29a4d7114..a3134258d 100644 --- a/implants/lib/eldritch/src/file/mod.rs +++ b/implants/lib/eldritch/src/file/mod.rs @@ -179,7 +179,7 @@ fn methods(builder: &mut MethodsBuilder) { } #[allow(unused_variables)] - fn temp_file(this: &FileLibrary, name: String) -> anyhow::Result { + 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 index 286170336..ed505d8a0 100644 --- a/implants/lib/eldritch/src/file/temp_file_impl.rs +++ b/implants/lib/eldritch/src/file/temp_file_impl.rs @@ -3,20 +3,22 @@ use std::env; use std::fs::File; use tempfile::NamedTempFile; -pub fn temp_file(name: String) -> Result { +pub fn temp_file(name: Option) -> Result { let mut temp_path; let _file; - if name.is_empty() { - // Generate a random file name if name is not provided - let tfile = NamedTempFile::new()?; - (_file, temp_path) = tfile.keep()?; - } else { - temp_path = env::temp_dir(); - temp_path.push(name); - _file = File::create(&temp_path)?; + match name { + None => { + // Generate a random file name if name is not provided + let tfile = NamedTempFile::new()?; + (_file, temp_path) = tfile.keep()?; + } + Some(n) => { + temp_path = env::temp_dir(); + temp_path.push(n); + _file = File::create(&temp_path)?; + } } - // Create the file in the temporary directory Ok(temp_path.display().to_string()) @@ -31,7 +33,7 @@ mod tests { #[test] fn test_temp_file_w_name() -> anyhow::Result<()> { // Create file with a name - let p = temp_file("foo".to_string())?; + let p = temp_file(Some("foo".to_string()))?; // check if file exists assert!(Path::new(&p).exists()); @@ -40,7 +42,7 @@ mod tests { #[test] fn test_temp_file_r_name() -> anyhow::Result<()> { // Create file with a random name - let p = temp_file("".to_string())?; + let p = temp_file(None)?; // check if file exists assert!(Path::new(&p).exists()); @@ -49,7 +51,7 @@ mod tests { #[test] fn test_temp_no_file_w_name() -> anyhow::Result<()> { // Create file with a name and then delete it (so we know it doesnt exist) - let p = temp_file("foo".to_string())?; + let p = temp_file(Some("foo".to_string()))?; if Path::new(&p).exists() { // delete the file fs::remove_file(&p)?; @@ -63,7 +65,7 @@ mod tests { #[test] fn test_temp_no_file_r_name() -> anyhow::Result<()> { // Create file with a random name and then delete it (so we know it doesnt exist) - let p = temp_file("".to_string())?; + let p = temp_file(None)?; if Path::new(&p).exists() { // delete the file fs::remove_file(&p)?; From 9f8d8fa0f7f6dbe960661400963d6cb6199292bc Mon Sep 17 00:00:00 2001 From: adm1npanda Date: Tue, 13 Aug 2024 01:31:12 +0000 Subject: [PATCH 6/8] cleaning up commit --- .../lib/eldritch/src/file/temp_file_impl.rs | 32 ++----------------- 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/implants/lib/eldritch/src/file/temp_file_impl.rs b/implants/lib/eldritch/src/file/temp_file_impl.rs index ed505d8a0..7749f33ab 100644 --- a/implants/lib/eldritch/src/file/temp_file_impl.rs +++ b/implants/lib/eldritch/src/file/temp_file_impl.rs @@ -27,7 +27,6 @@ pub fn temp_file(name: Option) -> Result { #[cfg(test)] mod tests { use super::*; - use std::fs; use std::path::Path; #[test] @@ -35,7 +34,8 @@ mod tests { // Create file with a name let p = temp_file(Some("foo".to_string()))?; // check if file exists - assert!(Path::new(&p).exists()); + let t = Path::new(&p); + assert!(t.exists() && (t.file_name().unwrap() == "foo")); Ok(()) } @@ -46,34 +46,6 @@ mod tests { // check if file exists assert!(Path::new(&p).exists()); - Ok(()) - } - #[test] - fn test_temp_no_file_w_name() -> anyhow::Result<()> { - // Create file with a name and then delete it (so we know it doesnt exist) - let p = temp_file(Some("foo".to_string()))?; - if Path::new(&p).exists() { - // delete the file - fs::remove_file(&p)?; - } - - // check file doesn't exists - assert!(!Path::new(&p).exists()); - - Ok(()) - } - #[test] - fn test_temp_no_file_r_name() -> anyhow::Result<()> { - // Create file with a random name and then delete it (so we know it doesnt exist) - let p = temp_file(None)?; - if Path::new(&p).exists() { - // delete the file - fs::remove_file(&p)?; - } - - // check file doesn't exists - assert!(!Path::new(&p).exists()); - Ok(()) } } From 0ef3a78548f8189b4737175f108744866ada119b Mon Sep 17 00:00:00 2001 From: adm1npanda Date: Wed, 14 Aug 2024 02:59:50 +0000 Subject: [PATCH 7/8] code cleanup --- implants/lib/eldritch/src/file/temp_file_impl.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/implants/lib/eldritch/src/file/temp_file_impl.rs b/implants/lib/eldritch/src/file/temp_file_impl.rs index 7749f33ab..705de456d 100644 --- a/implants/lib/eldritch/src/file/temp_file_impl.rs +++ b/implants/lib/eldritch/src/file/temp_file_impl.rs @@ -5,18 +5,18 @@ use tempfile::NamedTempFile; pub fn temp_file(name: Option) -> Result { let mut temp_path; - let _file; + let _: File; match name { None => { // Generate a random file name if name is not provided let tfile = NamedTempFile::new()?; - (_file, temp_path) = tfile.keep()?; + (_, temp_path) = tfile.keep()?; } Some(n) => { temp_path = env::temp_dir(); temp_path.push(n); - _file = File::create(&temp_path)?; + _ = File::create(&temp_path)?; } } // Create the file in the temporary directory @@ -35,7 +35,8 @@ mod tests { let p = temp_file(Some("foo".to_string()))?; // check if file exists let t = Path::new(&p); - assert!(t.exists() && (t.file_name().unwrap() == "foo")); + assert!(t.exists()); + assert!(t.file_name().unwrap() == "foo"); Ok(()) } From 474d284a6f0a27ee56aff6d3693145ce98c90aa0 Mon Sep 17 00:00:00 2001 From: adm1npanda Date: Wed, 14 Aug 2024 03:36:49 +0000 Subject: [PATCH 8/8] more cleanup --- implants/lib/eldritch/src/file/temp_file_impl.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/implants/lib/eldritch/src/file/temp_file_impl.rs b/implants/lib/eldritch/src/file/temp_file_impl.rs index 705de456d..216d906f4 100644 --- a/implants/lib/eldritch/src/file/temp_file_impl.rs +++ b/implants/lib/eldritch/src/file/temp_file_impl.rs @@ -5,7 +5,6 @@ use tempfile::NamedTempFile; pub fn temp_file(name: Option) -> Result { let mut temp_path; - let _: File; match name { None => {