From c9a9995b7fd1a92ced03fac2963214aea6533ad9 Mon Sep 17 00:00:00 2001 From: "exersalza (Julian J.)" Date: Thu, 23 May 2024 11:26:07 +0200 Subject: [PATCH] e --- cipher_finder/src/de_obfs.rs | 2 +- cipher_finder/src/file_op.rs | 4 +-- cipher_finder/src/main.rs | 14 +++++--- cipher_finder/src/os.rs | 19 ++++++++-- cipher_finder/src/utils.rs | 70 ++++++++++++++++++++---------------- 5 files changed, 67 insertions(+), 42 deletions(-) diff --git a/cipher_finder/src/de_obfs.rs b/cipher_finder/src/de_obfs.rs index 09183ec..4d809ef 100644 --- a/cipher_finder/src/de_obfs.rs +++ b/cipher_finder/src/de_obfs.rs @@ -7,7 +7,7 @@ lazy_static! { "towlie", "things", ]; static ref TABLE_REGEX: Regex = Regex::new(r"(\{([^{}]+)\})").unwrap(); - static ref VAR_REGEX: Regex = Regex::new(r"((local(\s+)?(\w+)))").unwrap(); + static ref VAR_REGEX: Regex = Regex::new(r"(((local(\s+)?)?(=\w?)(\w+)))").unwrap(); static ref FUNC_REGEX: Regex = Regex::new(r"(function\s*\(((\w+(,(\s?))?)*)\))").unwrap(); } diff --git a/cipher_finder/src/file_op.rs b/cipher_finder/src/file_op.rs index 1cf2c16..9d15618 100644 --- a/cipher_finder/src/file_op.rs +++ b/cipher_finder/src/file_op.rs @@ -20,8 +20,8 @@ impl ScannedFile { Ok(ret) } + /// gets the file contents, converts it to an utf8 but lossy fn get_file_contents(&self) -> std::io::Result> { - println!("{:?}", self.path); let mut file = fs::File::open(&self.path)?; let mut buf = vec![]; @@ -43,7 +43,7 @@ impl ScannedFile { let line = line.as_str(); - check_regex(&CIPHER_REGEX, line); + println!("{:?}", check_regex(&CIPHER_REGEX, line)); check_regex(&SIMPLE_URL_REGEX, line); } diff --git a/cipher_finder/src/main.rs b/cipher_finder/src/main.rs index cc4851a..acee790 100644 --- a/cipher_finder/src/main.rs +++ b/cipher_finder/src/main.rs @@ -35,16 +35,20 @@ struct Args { fn main() -> std::io::Result<()> { // i kissed a girl and i liked it https://images.app.goo.gl/ynuCJ85rmxJFVNBs5 let opt = Args::parse(); - let all_paths = os::get_all_files(opt.path, Some(utils::format_dir_str(opt.exclude))); + let exludes = utils::format_dir_str(opt.exclude); + let mut all_paths = os::get_all_files(opt.path.clone(), Some(exludes.clone())); + // let mut ignores = opt.exclude; if opt.include_git { - let git_ignores = utils::find_gitignores(all_paths.clone()); + let git_ignores = utils::filter_viables(all_paths.clone(), "gitignore"); + // do the readout part and add to exlude thingi - let ignored = utils::load_gitignores(git_ignores); - println!("{:?}", ignored); + let mut ignored = utils::parse_gitignores(git_ignores); + ignored.extend(exludes); + all_paths = os::get_all_files(opt.path, Some(ignored)); } - let paths = utils::filter_viables(all_paths); + let paths = utils::filter_viables(all_paths, "lua"); for i in paths { let infected = ScannedFile::new(i); diff --git a/cipher_finder/src/os.rs b/cipher_finder/src/os.rs index df5c127..bed6b6f 100644 --- a/cipher_finder/src/os.rs +++ b/cipher_finder/src/os.rs @@ -1,5 +1,7 @@ +use core::panic; use std::{ - fs::{self}, + collections::HashSet, + fs, path::{self, PathBuf}, str::FromStr, }; @@ -7,7 +9,7 @@ use std::{ use regex::Regex; /// Gets all files in subdirectories -pub fn get_all_files(path: String, exclude: Option>) -> Vec { +pub fn get_all_files(path: String, exclude: Option>) -> Vec { let mut ret = Vec::new(); walk_dir(path, &mut |e| ret.push(e)); @@ -47,7 +49,18 @@ fn filter_vec(haystack: Vec, needles: Regex) -> Vec { /// Walks through the given directory fn walk_dir(path: String, cb: &mut impl FnMut(path::PathBuf)) { - for i in fs::read_dir(path).unwrap() { + let f = match fs::read_dir(&path) { + Err(e) => { + println!("Can't open {} {}", path, e.kind()); + return; + } + Ok(v) => { + println!("{v:?}"); + v + } + }; + + for i in f { match i { Ok(dir) => { // we dont want to return here, also dont remove the else ples diff --git a/cipher_finder/src/utils.rs b/cipher_finder/src/utils.rs index 1bd8fe0..8e08fa5 100644 --- a/cipher_finder/src/utils.rs +++ b/cipher_finder/src/utils.rs @@ -8,48 +8,49 @@ lazy_static! { pub static ref SIMPLE_URL_REGEX: Regex = Regex::new(r"https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)").unwrap(); } -/// increases or decreases the confidence if the regex finds something or not. -pub fn check_regex(regex: &Regex, haystack: &str) -> bool { - for found in regex.captures_iter(haystack) { - println!("{found:?}") - } - - false +/// checks a regex +pub fn check_regex(regex: &Regex, haystack: &str) -> Vec { + let f: Vec = regex + .captures_iter(haystack) + .filter_map(|found| found.get(0).map(|j| j.as_str().to_string())) + .collect(); + + f } /// just a shortcut to split a string for further usage -pub fn format_dir_str(s: String) -> Vec { +pub fn format_dir_str(s: String) -> HashSet { if s.is_empty() { // handle default - return vec![]; - } - - let mut ret = vec![]; - - for i in s.split(',') { - ret.push(prepare_for_regex(i.to_string())); + return HashSet::new(); } - ret -} - -/// Filter the walk_dir list for viable files like .lua etc. -pub fn filter_viables(haystack: Vec) -> Vec { - haystack + s.split(',') .into_iter() - .filter(|i| i.extension().unwrap_or_default() == "lua") - .collect::>() + .map(|i| prepare_for_regex(i.to_string())) + .collect() } -/// find all .gitignore files in location system -pub fn find_gitignores(haystack: Vec) -> Vec { +/// Filter the walk_dir list for viable files like "lua" etc. +/// +/// Scans the PathBuf vector for the needle, the needle doesn't have to have a [dot] infront of it. +pub fn filter_viables(haystack: Vec, needle: &str) -> Vec { haystack .into_iter() - .filter(|i| i.file_name().unwrap_or_default() == ".gitignore") - .collect::>() + .filter(|i| { + // rust things + let f = i + .file_name() + .unwrap_or_default() + .to_str() + .unwrap_or_default(); + f.split('.').last().unwrap_or_default() == needle + }) + .collect() } -pub fn load_gitignores(stack: Vec) -> HashSet { +/// Get the contents of gitignore files based on the input vector +pub fn parse_gitignores(stack: Vec) -> HashSet { let mut ret = vec![]; for path in stack { @@ -129,10 +130,17 @@ mod test { let tests = vec![ ( String::from("some,cool,string"), - vec!["some".to_string(), "cool".to_string(), "string".to_string()], + HashSet::from_iter(vec![ + "some".to_string(), + "cool".to_string(), + "string".to_string(), + ]), + ), + ( + String::from("some"), + HashSet::from_iter(vec!["some".to_string()]), ), - (String::from("some"), vec!["some".to_string()]), - (String::from(""), vec![]), + (String::from(""), HashSet::new()), ]; for (s, t) in tests {