Skip to content

Commit

Permalink
add tests for got overwrite and function name encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
lapla-cogito committed Aug 23, 2024
1 parent e1861ea commit 7a3c92f
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ fn main() -> crate::error::Result<()> {
std::fs::create_dir_all(dir).unwrap();
std::fs::File::create(&output_path).unwrap();

exec_obfus(entry.to_str().unwrap(), &output_path, &args).unwrap();
match exec_obfus(entry.to_str().unwrap(), &output_path, &args) {
Ok(_) => println!("obfuscation done!"),
Err(e) => {
eprintln!("error while obfuscation: {}", e);
return Err(e);
}
}
}
}

Expand Down Expand Up @@ -160,7 +166,6 @@ fn exec_obfus(input_path: &str, output_path: &str, args: &Args) -> crate::error:
obfuscator.encrypt_function_name(&args.encrypt_f, &args.encrypt_key)?;
}

println!("obfuscation done!");
Ok(())
}
false => Err(crate::error::Error::InvalidMagic),
Expand All @@ -169,6 +174,8 @@ fn exec_obfus(input_path: &str, output_path: &str, args: &Args) -> crate::error:

#[cfg(test)]
mod tests {
use std::process::Output;

#[test]
fn not_elf() {
let file = std::fs::File::open("src/main.rs").unwrap();
Expand Down Expand Up @@ -277,4 +284,43 @@ mod tests {
);
}
}

#[test]
fn got_overwrite() {
{
let loader = crate::obfus::Obfuscator::open("bin/got", "bin/res_got");
let mut obfuscator = loader.unwrap();
obfuscator.got_overwrite("system", "secret").unwrap();
}

let output = std::process::Command::new("./bin/res_got")
.output()
.expect("failed to execute res_got");

assert_eq!(
String::from_utf8(output.stdout).unwrap(),
"secret function called\n"
);
}

#[test]
fn encrypt_function_name() {
{
let loader = crate::obfus::Obfuscator::open("bin/test_64bit", "bin/res_encrypt");
let mut obfuscator = loader.unwrap();
obfuscator.encrypt_function_name("fac", "foo").unwrap();
}

let output = std::process::Command::new("readelf")
.args(["-s", "bin/test_64bit"])
.output()
.expect("failed to execute readelf");
assert!(String::from_utf8(output.stdout).is_ok());

let output = std::process::Command::new("readelf")
.args(["-s", "bin/res_encrypt"])
.output()
.expect("failed to execute readelf");
assert!(String::from_utf8(output.stdout).is_err());
}
}

0 comments on commit 7a3c92f

Please sign in to comment.