Skip to content

Commit

Permalink
Fixed Clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTas committed Dec 21, 2022
1 parent 19b659c commit b191d00
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 43 deletions.
8 changes: 4 additions & 4 deletions src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn generate_random_chars(min: Option<u32>, max: Option<u32>, used: &HashSet<

// Numbers and *most* symbols can be included in variable names as long as they aren't directly adjacent to the leading or trailing '%'.
#[allow(non_snake_case)]
for N in 0..=(thread_rng().gen_range(min_len..=max_len)-1) {
for N in 0..(thread_rng().gen_range(min_len..=max_len)-1) {
if N == 0 {
rng_chars.push(*CharSet::Letters.values().choose(&mut rng).expect("CharSet::Letters should not be empty!"));
}else {
Expand All @@ -98,8 +98,8 @@ pub fn generate_random_chars(min: Option<u32>, max: Option<u32>, used: &HashSet<
let rng_string: String = rng_chars.into_iter().collect();

if !used.contains(&rng_string) {
return rng_string;
rng_string
}else {
return generate_random_chars(Some(min_len), Some(max_len), used);
};
generate_random_chars(Some(min_len), Some(max_len), used)
}
}
47 changes: 29 additions & 18 deletions src/batch/deobfuscator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use std::{
///
/// // prints: "Deobfuscated code was written to: deobfuscated.bat".
/// println!("Deobfuscated code was written to: {}", deobfuscated_script);
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct BatchDeobfuscator {
pub set_str: String,
pub space_str: String,
Expand Down Expand Up @@ -84,21 +84,29 @@ impl BatchDeobfuscator {
// Pattern matching to identify set, space, and equals variables.
let re_set = Regex::new(
r"set [a-zA-Z0-9!#\$\*\(\)\[\]\{\},-\.\?@_~]+=set"
).expect("Regex not valid!");
).expect("Regex pattern invalid!");
let re_space = Regex::new(
r"%[a-zA-Z0-9!#\$\*\(\)\[\]\{\},-\.\?@_~]+% [a-zA-Z0-9!#\$\*\(\)\[\]\{\},-\.\?@_~]+= "
).expect("Regex not valid!");
).expect("Regex pattern invalid!");
let re_equal = Regex::new(
r"%[a-zA-Z0-9!#\$\*\(\)\[\]\{\},-\.\?@_~]+%[a-zA-Z0-9!#\$\*\(\)\[\]\{\},-\.\?@_~]+=="
).expect("Regex not valid!");
).expect("Regex pattern invalid!");
let set_match: Vec<&str> = re_set.find_iter(&src).map(|mat| mat.as_str()).collect();
let space_match: Vec<&str> = re_space.find_iter(&src).map(|mat| mat.as_str()).collect();
let equal_match: Vec<&str> = re_equal.find_iter(&src).map(|mat| mat.as_str()).collect();

if set_match.is_empty() ||
space_match.is_empty() ||
equal_match.is_empty()
{
eprintln!("\nInput script does not appear to be compatible with deobfuscation!");
std::process::exit(1);
};

// Extract the proper variable strings based on the structure of the obfuscation.
let set_str: &str = set_match[0].split(" ").collect::<Vec<&str>>()[1];
let space_str: &str = space_match[0].split(" ").collect::<Vec<&str>>()[1];
let eq_str: &str = equal_match[0].split("%").collect::<Vec<&str>>()[2];
let set_str: &str = set_match[0].split(' ').collect::<Vec<&str>>()[1];
let space_str: &str = space_match[0].split(' ').collect::<Vec<&str>>()[1];
let eq_str: &str = equal_match[0].split('%').collect::<Vec<&str>>()[2];
self.set_str = set_str[0..set_str.len()-4].to_string();
self.space_str = space_str[0..space_str.len()-2].to_string();
self.eq_str = eq_str[0..eq_str.len()-2].to_string();
Expand All @@ -117,9 +125,12 @@ impl BatchDeobfuscator {
/// **This method panics if file creation/writing fails.**
pub fn write_deobfuscated_script(&self, file_name: Option<String>) -> String {

if !self.initialized { panic!("Deobfuscator must first be initialized!"); };
if !self.initialized {
eprintln!("\nDeobfuscator must first be initialized!");
std::process::exit(1);
};

let handle_name: String = file_name.unwrap_or(String::from("deobfuscated.bat"));
let handle_name: String = file_name.unwrap_or_else(|| String::from("deobfuscated.bat"));
let handle_clone: String = handle_name.clone();

let mut file = File::create(handle_clone.as_str()).expect("Failed to create file!");
Expand All @@ -140,7 +151,7 @@ impl BatchDeobfuscator {
for mtch in matches {
let chr: String = format!("{}", mtch.chars().nth(mtch.len()-2).unwrap());

let name: String = String::from(mtch.split("%").collect::<Vec<&str>>()[0]);
let name: String = String::from(mtch.split('%').collect::<Vec<&str>>()[0]);

self.alphabet.insert(name, chr);
};
Expand All @@ -158,7 +169,7 @@ impl BatchDeobfuscator {
mtch.chars().nth(mtch.len()-2).unwrap()
);

let name: String = String::from(mtch.split("%").collect::<Vec<&str>>()[0]);
let name: String = String::from(mtch.split('%').collect::<Vec<&str>>()[0]);

self.alphabet.insert(name, blob);
};
Expand All @@ -172,10 +183,10 @@ impl BatchDeobfuscator {
for mtch in matches {
match mtch.find(&self.eq_str) {
Some(index) => {
let name_haystack: Vec<&str> = mtch.split("%").collect();
let name_haystack: Vec<&str> = mtch.split('%').collect();
let name: String = String::from(name_haystack[name_haystack.len()-3]);
let blob: &str = &mtch[(index+self.eq_str.len()+1)..];
println!("name {:#?}", mtch.split("%").collect::<Vec<&str>>());
println!("name {:#?}", mtch.split('%').collect::<Vec<&str>>());
self.alphabet.insert(name, blob.to_string());
},
None => {
Expand All @@ -188,13 +199,13 @@ impl BatchDeobfuscator {
/// Deobfuscates a sample of obfuscated batch commands using a reverse-engineered obfuscation alphabet.
fn deobfuscate(&mut self, src: String) {

let src: Vec<&str> = src.split("\n").collect();
let src: Vec<&str> = src.split('\n').collect();

// Iterate over the remaining obfuscated text and map the obfuscated strings to cleartext characters.
let mut cleaned_chars: Vec<String> = Vec::new();
for line in src {

if line == "\n" || line == "\r\n" || line == "" { continue; };
if line == "\n" || line == "\r\n" || line.is_empty() { continue; };

if line.contains(&self.set_str) || line.contains(&self.space_str) || line.contains(&self.eq_str) {
continue;
Expand All @@ -205,7 +216,7 @@ impl BatchDeobfuscator {
continue;
};

let code: Vec<&str> = line.split("%").collect();
let code: Vec<&str> = line.split('%').collect();

for blob in code {
if let Some(chr) = self.alphabet.get(&blob.to_string()) {
Expand All @@ -215,7 +226,7 @@ impl BatchDeobfuscator {
if blob.contains(c.as_str()) { continue };
};

if !blob.contains(" ") && blob != "" && blob != "\r" {
if !blob.contains(' ') && !blob.is_empty() && blob != "\r" {
let mut skip: bool = false;
for character in blob.chars() {
if CharSet::BadChars.values().contains(&character) {
Expand All @@ -239,7 +250,7 @@ impl BatchDeobfuscator {
};

// Clean up the last remaining artifacts of the obfuscation.
cleaned_chars = cleaned_chars.join("").split("\n").map(|chr| chr.to_string()).collect();
cleaned_chars = cleaned_chars.join("").split('\n').map(|chr| chr.to_string()).collect();

// Reassemble the cleartext code and finalize the initialization.
self.cleaned_code = cleaned_chars.join("\n").trim_end().to_string();
Expand Down
43 changes: 23 additions & 20 deletions src/batch/obfuscator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use std::{
use crate::{
input,
batch::{
generate_random_chars,
CharSet
CharSet,
generate_random_chars
}
};

Expand All @@ -51,7 +51,7 @@ use crate::{
///
/// // prints: "Obfuscated code was written to: obfuscated.bat".
/// println!("Obfuscated code was written to: {}", obfuscated_script);
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct BatchObfuscator {
pub set_str: String,
pub space_str: String,
Expand Down Expand Up @@ -119,7 +119,7 @@ impl BatchObfuscator {
self.prep_commands.push(format!("%{}%%{}%{}==", self.set_str, self.space_str, self.eq_str));

// Build an obfuscated alphabet with variables and push their assignment statements into the prep_commands Vec.
self.build_alphabet(min.clone(), max.clone());
self.build_alphabet(min, max);

// Obfuscate the cleartext source code using our newly-created obfuscated alphabet.
self.obfuscate(src);
Expand All @@ -132,9 +132,12 @@ impl BatchObfuscator {
/// **This method panics if file creation/writing fails.**
pub fn write_obfuscated_script(&self, file_name: Option<String>) -> String {

if !self.initialized { panic!("Obfuscator must first be initialized!"); };
if !self.initialized {
eprintln!("\nObfuscator must first be initialized!");
std::process::exit(1);
};

let handle_name: String = file_name.unwrap_or(String::from("obfuscated.bat"));
let handle_name: String = file_name.unwrap_or_else(|| String::from("obfuscated.bat"));
let handle_clone: String = handle_name.clone();

let mut file = File::create(handle_clone.as_str()).expect("Failed to create file!");
Expand All @@ -156,14 +159,14 @@ impl BatchObfuscator {
self.alphabet.insert(chr, varname.clone());

if !self.prep_commands.contains(&BatchObfuscator::define_batch_variable(
format!("{}", varname.to_owned()),
varname.to_owned().to_string(),
format!("{}", chr.to_owned()),
&self))
self))
{
self.prep_commands.push(BatchObfuscator::define_batch_variable(
format!("{}", varname.to_owned()),
varname.to_owned().to_string(),
format!("{}", chr.to_owned()),
&self));
self));
};
}else {
self.alphabet.insert(chr, format!("{}", chr));
Expand All @@ -176,7 +179,7 @@ impl BatchObfuscator {

let match_variable_lines: Regex = Regex::new("%[a-zA-Z0-9_-~!@#$^&/.,<>;:'\"=]+%").expect("Regex not valid!");
let match_set_lines: Regex = Regex::new("set .+=.+").expect("Regex not valid!");
let src_list: Vec<&str> = src.split("\n").collect();
let src_list: Vec<&str> = src.split('\n').collect();
let mut warned: bool = false;

for line in src_list {
Expand All @@ -190,12 +193,12 @@ impl BatchObfuscator {
};
};

if perc_index.len() <= 0 { return None };
if perc_index.is_empty() { return None };

Some(perc_index)
};

if line.contains("%") && !match_variable_lines.is_match(&line) {
if line.contains('%') && !match_variable_lines.is_match(line) {

let perc_index: Vec<usize> = find_percent_index().expect("No percent symbols in sample!");

Expand All @@ -208,10 +211,10 @@ impl BatchObfuscator {
};

if perc_index.contains(&i) {
let blob: &str = &line.clone()[i..=i+1];
let blob: &str = &line[i..=i+1];
let mut obfuscate_blob = || {
let varname: String = generate_random_chars(None, None, &self.used_variable_strings);
let varline: String = BatchObfuscator::define_batch_variable(varname.clone(), blob.to_string(), &self);
let varline: String = BatchObfuscator::define_batch_variable(varname.clone(), blob.to_string(), self);

self.prep_commands.push(varline);
self.exec_commands.push(format!("%{}%", varname));
Expand All @@ -232,9 +235,9 @@ impl BatchObfuscator {
};

// If the input script contains custom/environment vars, warn about this method's limitations.
}else if match_variable_lines.is_match(&line) ||
(line.starts_with(":") && !line.starts_with("::")) ||
(match_set_lines.is_match(&line) && line.to_lowercase().starts_with("set")) {
}else if match_variable_lines.is_match(line) ||
(line.starts_with(':') && !line.starts_with("::")) ||
(match_set_lines.is_match(line) && line.to_lowercase().starts_with("set")) {

let mut heed: String = String::new();
if !warned && self.warn_mode {
Expand All @@ -246,8 +249,8 @@ impl BatchObfuscator {
heed = input("\nContinue Anyway? [Y/N] ~> ");
};

if !self.warn_mode || heed.to_lowercase().contains("y") || warned {
self.exec_commands.push(format!("{}", line));
if !self.warn_mode || heed.to_lowercase().contains('y') || warned {
self.exec_commands.push(line.to_owned());
warned = true;
}else {
println!("\nObfuscation aborted!");
Expand Down
2 changes: 1 addition & 1 deletion src/bin/0xidiz3r.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn main() {

if let Some(min_value) = args.min {
if let Some(max_value) = args.max {
if min_value >= max_value {
if min_value > max_value {
args.max = Some(args.min.unwrap() * 2);
};
};
Expand Down

0 comments on commit b191d00

Please sign in to comment.