diff --git a/Cargo.lock b/Cargo.lock index 59aedf8..0dedde0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 53fafda..8210136 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/build.rs b/build.rs index 16d540a..8d9a5b8 100644 --- a/build.rs +++ b/build.rs @@ -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"); - // } } diff --git a/src/injector.rs b/src/injector.rs index 8fd114e..49e4866 100644 --- a/src/injector.rs +++ b/src/injector.rs @@ -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); } } diff --git a/src/lib.rs b/src/lib.rs index bbe9f17..6ab84c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(); + } +} diff --git a/tests/mybin/Cargo.lock b/tests/mybin/Cargo.lock new file mode 100644 index 0000000..5713c07 --- /dev/null +++ b/tests/mybin/Cargo.lock @@ -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" diff --git a/tests/mybin/Cargo.toml b/tests/mybin/Cargo.toml new file mode 100644 index 0000000..3d3deab --- /dev/null +++ b/tests/mybin/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "mybin" +version = "0.1.0" +edition = "2021" + +[dependencies] +mylib = { path = "../mylib" } diff --git a/tests/mybin/src/main.rs b/tests/mybin/src/main.rs new file mode 100644 index 0000000..e38f115 --- /dev/null +++ b/tests/mybin/src/main.rs @@ -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); +} diff --git a/tests/mylib/src/lib.rs b/tests/mylib/src/lib.rs index 037ed7e..d1c060c 100644 --- a/tests/mylib/src/lib.rs +++ b/tests/mylib/src/lib.rs @@ -2,3 +2,8 @@ pub extern "C" fn mylib_foo() -> u8 { 10 } + +#[no_mangle] +pub extern "C" fn mylib_bar() -> u8 { + 100 +}