Skip to content

Commit

Permalink
e
Browse files Browse the repository at this point in the history
  • Loading branch information
exersalza committed May 23, 2024
1 parent 387dd4b commit c9a9995
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cipher_finder/src/de_obfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions cipher_finder/src/file_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>> {
println!("{:?}", self.path);
let mut file = fs::File::open(&self.path)?;
let mut buf = vec![];

Expand All @@ -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);
}

Expand Down
14 changes: 9 additions & 5 deletions cipher_finder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 16 additions & 3 deletions cipher_finder/src/os.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use core::panic;
use std::{
fs::{self},
collections::HashSet,
fs,
path::{self, PathBuf},
str::FromStr,
};

use regex::Regex;

/// Gets all files in subdirectories
pub fn get_all_files(path: String, exclude: Option<Vec<String>>) -> Vec<PathBuf> {
pub fn get_all_files(path: String, exclude: Option<HashSet<String>>) -> Vec<PathBuf> {
let mut ret = Vec::new();

walk_dir(path, &mut |e| ret.push(e));
Expand Down Expand Up @@ -47,7 +49,18 @@ fn filter_vec(haystack: Vec<PathBuf>, needles: Regex) -> Vec<PathBuf> {

/// 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
Expand Down
70 changes: 39 additions & 31 deletions cipher_finder/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let f: Vec<String> = 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<String> {
pub fn format_dir_str(s: String) -> HashSet<String> {
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<path::PathBuf>) -> Vec<path::PathBuf> {
haystack
s.split(',')
.into_iter()
.filter(|i| i.extension().unwrap_or_default() == "lua")
.collect::<Vec<path::PathBuf>>()
.map(|i| prepare_for_regex(i.to_string()))
.collect()
}

/// find all .gitignore files in location system
pub fn find_gitignores(haystack: Vec<path::PathBuf>) -> Vec<path::PathBuf> {
/// 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<path::PathBuf>, needle: &str) -> Vec<path::PathBuf> {
haystack
.into_iter()
.filter(|i| i.file_name().unwrap_or_default() == ".gitignore")
.collect::<Vec<path::PathBuf>>()
.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<path::PathBuf>) -> HashSet<String> {
/// Get the contents of gitignore files based on the input vector
pub fn parse_gitignores(stack: Vec<path::PathBuf>) -> HashSet<String> {
let mut ret = vec![];

for path in stack {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c9a9995

Please sign in to comment.