Add integration test for lib injection

This commit is contained in:
Dimitris Zervas 2023-06-03 18:02:46 +03:00
parent 9180d62d67
commit 141bf26048
No known key found for this signature in database
GPG Key ID: 5C27D7C9D1901A30
9 changed files with 109 additions and 21 deletions

13
Cargo.lock generated
View File

@ -217,8 +217,9 @@ dependencies = [
[[package]]
name = "frida"
version = "0.4.0"
source = "git+https://github.com/frida/frida-rust#6be323811b87d204595ded8e4f18e090a954ca81"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f986d678a4b186594a29130c6fce7db3bce32a7506041e4e1fa1a8b4695fb956"
dependencies = [
"frida-sys",
"thiserror",
@ -227,7 +228,8 @@ dependencies = [
[[package]]
name = "frida-build"
version = "0.2.1"
source = "git+https://github.com/frida/frida-rust#6be323811b87d204595ded8e4f18e090a954ca81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2b1525c98a66f38f333d17e56c24562425263a9e07a53e5f59d0857bc360257"
dependencies = [
"reqwest",
"tar",
@ -253,8 +255,9 @@ dependencies = [
[[package]]
name = "frida-sys"
version = "0.4.0"
source = "git+https://github.com/frida/frida-rust#6be323811b87d204595ded8e4f18e090a954ca81"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31cefdb1bb978815a5c1a9d94b0aa691bdaf22779ae5478849b7af13107f496c"
dependencies = [
"bindgen",
"frida-build",

View File

@ -18,8 +18,7 @@ managed_lib = ["dep:windows-sys"]
frida = ["dep:frida", "dep:lazy_static", "dep:serde", "dep:serde_json"]
[dependencies]
# frida = { version = "0.4.0", features = ["auto-download"] }
frida = { git = "https://github.com/frida/frida-rust", features = ["auto-download"], optional = true }
frida = { version = "0.4.1", features = ["auto-download"], optional = true }
lazy_static = { version = "1.4.0", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }
@ -39,3 +38,4 @@ cc = "1.0.67"
[dev-dependencies]
pretty_assertions = "1.1.0"
mylib = { path = "tests/mylib" }
# escargot = "0.5.7"

View File

@ -35,15 +35,4 @@ fn main() {
println!("cargo:warning=Expected library name: {}-orig.dll", lib_name);
println!("cargo:rustc-env=LIB_NAME={}-orig.dll", lib_name);
}
// if env::var("PROFILE").unwrap() == "test" {
// cc::Build::new()
// .shared_flag(true)
// .static_flag(false)
// .cargo_metadata(false)
// .file("tests/mylib.c")
// .compile("mylib");
// println!("cargo:rustc-link-search=native={}/tests", env::var("OUT_DIR").unwrap());
// println!("cargo:rustc-link-lib=dylib=mylib");
// }
}

View File

@ -16,7 +16,10 @@ pub extern "C" fn attach(pid: u32) {
#[cfg(feature = "frida")]
{
let frida_code = env!("FRIDA_CODE").to_string();
#[cfg(windows)]
std::thread::spawn(move || frida_attach_pid(frida_code, pid));
#[cfg(not(windows))]
frida_attach_pid(frida_code, pid);
}
}

View File

@ -16,12 +16,12 @@ fn _start() {
// For some reason ctor doesn't work on Windows - it hangs the process
// during DeviceManager::obtain. DllMain works fine though.
#[cfg(windows)]
#[cfg(all(windows, not(test)))]
use std::ffi::c_void;
#[cfg(windows)]
#[cfg(all(windows, not(test)))]
use winapi::um::winnt::DLL_PROCESS_ATTACH;
#[cfg(all(windows, feature = "dll_proxy"))]
#[cfg(all(windows, feature = "dll_proxy", not(test)))]
use winapi::um::libloaderapi::LoadLibraryA;
#[cfg(all(windows, not(test)))]
@ -46,3 +46,58 @@ extern "system" fn DllMain(dll_module: *mut c_void, call_reason: u32, _: *mut ()
true
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use std::process::Command;
use std::fs;
#[test]
#[cfg(feature = "frida")]
fn test_frida_on_load() {
let lib_status = Command::new("cargo")
.arg("build")
.arg("--lib")
.arg("--target-dir")
.arg("target/test_frida_on_load")
.env("FRIDA_CODE", r#"
const foo = Module.getExportByName(null, "mylib_foo");
Interceptor.replace(foo, new NativeCallback(function () {
console.log("replaced foo() called");
return 40;
}, "uint8", []));
"#)
.status()
.expect("Failed to build dynamic library");
assert!(lib_status.success(), "Failed to build dynamic library");
let bin_status = Command::new("cargo")
.arg("run")
.arg("--manifest-path")
.arg("tests/mybin/Cargo.toml")
.arg("--target-dir")
.arg("target/test_frida_on_load")
.env("RUSTFLAGS", "-C link-arg=-Wl,--no-as-needed -C link-arg=-lfrida_deepfreeze_rs")
.status()
.expect("Failed to build mybin");
assert_eq!(bin_status.code().unwrap(), 40, "Failed to replace foo()");
// assert_eq!(40, unsafe { mylib_foo() });
// escargot::CargoBuild::new()
// .current_release()
// .current_target()
// .env("FRIDA_CODE", r#"
// const foo = Module.getExportByName(null, "mylib_foo");
// Interceptor.replace(foo, new NativeCallback(function () {
// console.log("replaced foo() called");
// return 20;
// }, "uint8", []));
// "#)
// .manifest_path("../../Cargo.toml")
// .run()
// .unwrap();
}
}

14
tests/mybin/Cargo.lock generated Normal file
View File

@ -0,0 +1,14 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "mybin"
version = "0.1.0"
dependencies = [
"mylib",
]
[[package]]
name = "mylib"
version = "0.1.0"

7
tests/mybin/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "mybin"
version = "0.1.0"
edition = "2021"
[dependencies]
mylib = { path = "../mylib" }

12
tests/mybin/src/main.rs Normal file
View File

@ -0,0 +1,12 @@
#[link(name = "mylib", kind = "dylib")]
extern {
fn mylib_foo() -> u8;
fn mylib_bar() -> u8;
}
fn main() {
println!("Hello, world!");
assert_eq!(unsafe { mylib_bar() }, 100);
std::process::exit(unsafe { mylib_foo() } as i32);
}

View File

@ -2,3 +2,8 @@
pub extern "C" fn mylib_foo() -> u8 {
10
}
#[no_mangle]
pub extern "C" fn mylib_bar() -> u8 {
100
}