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

File.Replace_all impl, test, doc. #26

Merged
merged 2 commits into from
Apr 2, 2022
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
2 changes: 1 addition & 1 deletion docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ The <b>file.replace</b> 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 <b>file.replace_all</b> method is very cool, and will be even cooler when Nick documents it.
The <b>file.replace_all</b> 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`
Expand Down
3 changes: 2 additions & 1 deletion implants/eldritch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
httptest = "0.15.4"
57 changes: 55 additions & 2 deletions implants/eldritch/src/file/replace_all_impl.rs
Original file line number Diff line number Diff line change
@@ -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")
}