From 650e2b0a442e9d3827fa27ca470d3027e4234d1f Mon Sep 17 00:00:00 2001 From: Hulto Date: Sat, 2 Apr 2022 11:38:16 -0400 Subject: [PATCH] File.Replace_all (#26) --- docs/_docs/user-guide/eldritch.md | 2 +- implants/eldritch/Cargo.toml | 3 +- .../eldritch/src/file/replace_all_impl.rs | 57 ++++++++++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/docs/_docs/user-guide/eldritch.md b/docs/_docs/user-guide/eldritch.md index fcfb5fa7e..77de828b9 100644 --- a/docs/_docs/user-guide/eldritch.md +++ b/docs/_docs/user-guide/eldritch.md @@ -72,7 +72,7 @@ The file.replace method is very cool, and will be even cooler when Nick d ### file.replace_all `file.replace_all(path: str, pattern: str, value:` str) -> None -The file.replace_all method is very cool, and will be even cooler when Nick documents it. +The file.replace_all method finds all strings matching a regex pattern in the specified file and replaces them with the value. ### file.timestomp `file.timestomp(src: str, dst: str) -> None` diff --git a/implants/eldritch/Cargo.toml b/implants/eldritch/Cargo.toml index 35ef778f6..49efc47aa 100644 --- a/implants/eldritch/Cargo.toml +++ b/implants/eldritch/Cargo.toml @@ -9,5 +9,6 @@ tempfile = "3.3.0" anyhow = "1.0.55" derive_more = "0.99.17" sha256 = "1.0.3" +regex = "1.5.5" reqwest = { version = "0.11.0" , features = ["blocking"] } -httptest = "0.15.4" \ No newline at end of file +httptest = "0.15.4" diff --git a/implants/eldritch/src/file/replace_all_impl.rs b/implants/eldritch/src/file/replace_all_impl.rs index a2faf2c47..9bd2903be 100644 --- a/implants/eldritch/src/file/replace_all_impl.rs +++ b/implants/eldritch/src/file/replace_all_impl.rs @@ -1,5 +1,58 @@ use anyhow::Result; +use std::fs::{write,read_to_string}; +use regex::{Regex,NoExpand}; + +pub fn replace_all(path: String, pattern: String, value: String) -> Result<()> { + let file_contents = read_to_string(path.clone()).unwrap(); + let re = Regex::new(&pattern).unwrap(); + let result = re.replace_all(&file_contents, NoExpand(&value)); + write(path, String::from(result))?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::{NamedTempFile}; + + #[test] + fn test_replace_all_multiline() -> anyhow::Result<()>{ + let tmp_file_new = NamedTempFile::new()?; + let path_new = String::from(tmp_file_new.path().to_str().unwrap()).clone(); + let _ = write(path_new.clone(),"Match User anoncvs\nMatch User anoncvs\nMatch User anoncvs\n"); + + // Run our code + replace_all(path_new.clone(), String::from("Match"), String::from("Not Match"))?; + + let res = read_to_string(path_new)?; + assert_eq!(res, "Not Match User anoncvs\n".repeat(3)); + Ok(()) + } + #[test] + fn test_replace_all_regex_simple() -> anyhow::Result<()>{ + let tmp_file_new = NamedTempFile::new()?; + let path_new = String::from(tmp_file_new.path().to_str().unwrap()).clone(); + let _ = write(path_new.clone(),"Match User anoncvs\nMatch User anoncvs\nMatch User anoncvs\n"); + + // Run our code + replace_all(path_new.clone(), String::from(r".*Match.*"), String::from("Not Match"))?; + + let res = read_to_string(path_new)?; + assert_eq!(res, "Not Match\n".repeat(3)); + Ok(()) + } + #[test] + fn test_replace_all_regex_complex() -> anyhow::Result<()>{ + let tmp_file_new = NamedTempFile::new()?; + let path_new = String::from(tmp_file_new.path().to_str().unwrap()).clone(); + let _ = write(path_new.clone(),"MaxStartups 10:30:100\nListenAddress 0.0.0.0\nMaxAuthTries 6\nMatch User anoncvs\n"); + + // Run our code + replace_all(path_new.clone(), String::from(r"\d\.\d\.\d\.\d"), String::from("127.0.0.1"))?; + + let res = read_to_string(path_new)?; + assert_eq!(res, "MaxStartups 10:30:100\nListenAddress 127.0.0.1\nMaxAuthTries 6\nMatch User anoncvs\n"); + Ok(()) + } -pub fn replace_all(_path: String, _pattern: String, _value: String) -> Result<()> { - unimplemented!("Method unimplemented") } \ No newline at end of file