2023-05-23 17:21:14 +03:00
|
|
|
use std::env;
|
2024-04-13 03:24:16 +03:00
|
|
|
use std::io::Write;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::path::Path;
|
2023-05-23 17:21:14 +03:00
|
|
|
|
|
|
|
fn main() {
|
2023-05-26 15:40:45 +03:00
|
|
|
println!("cargo:rerun-if-env-changed=FRIDA_CODE");
|
2023-05-26 19:23:37 +03:00
|
|
|
println!("cargo:rerun-if-env-changed=DLL_PROXY");
|
2023-05-26 15:40:45 +03:00
|
|
|
|
2024-04-13 03:24:16 +03:00
|
|
|
let Ok(lib_path) = env::var("DLL_PROXY") else {
|
2024-04-13 04:41:03 +03:00
|
|
|
println!("cargo:warning=No DLL_PROXY set, the resulting library has to be manually injected or compiled into the target binary");
|
2024-04-13 03:24:16 +03:00
|
|
|
return;
|
|
|
|
};
|
2023-05-25 17:48:18 +03:00
|
|
|
|
2024-04-13 03:24:16 +03:00
|
|
|
use goblin::Object;
|
|
|
|
println!("cargo:rerun-if-changed={}", &lib_path);
|
2023-05-25 17:48:18 +03:00
|
|
|
|
2024-04-13 03:24:16 +03:00
|
|
|
let path = std::path::Path::new(&lib_path);
|
|
|
|
let lib_filename = path.file_name().unwrap().to_str().unwrap();
|
2023-05-25 17:48:18 +03:00
|
|
|
|
2024-04-13 03:24:16 +03:00
|
|
|
let lib_bytes = std::fs::read(path).expect(format!("Failed to open given library file {}", &lib_filename).as_str());
|
|
|
|
let object = Object::parse(&lib_bytes).expect(format!("Failed to parse given libary file {}", &lib_filename).as_str());
|
2023-05-25 17:48:18 +03:00
|
|
|
|
2024-04-13 03:24:16 +03:00
|
|
|
let (exports, lib_name): (Vec<&str>, String) = match object {
|
2024-03-03 13:10:58 +02:00
|
|
|
#[cfg(target_os = "windows")]
|
2024-04-13 03:24:16 +03:00
|
|
|
Object::PE(o) => {
|
|
|
|
(o.exports
|
|
|
|
.iter()
|
|
|
|
.map(|e| e.name.unwrap())
|
|
|
|
.collect(),
|
|
|
|
o.name.expect("Couldn't read the name of the DLL. Is it a .NET DLL? It's not supported").replace(".dll", ""))
|
2023-05-25 17:48:18 +03:00
|
|
|
}
|
2024-04-13 03:24:16 +03:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
Object::Elf(o) => {
|
|
|
|
(o.dynsyms
|
|
|
|
.iter()
|
|
|
|
.filter(|e| e.is_function() && !e.is_import())
|
|
|
|
.map(|e| o.dynstrtab.get_at(e.st_name).unwrap())
|
|
|
|
.collect(),
|
2024-04-14 00:29:53 +03:00
|
|
|
// o.soname.expect("Couldn't read the name of the SO.").replace(".so", ""))
|
|
|
|
lib_filename.replace(".so", ""))
|
|
|
|
},
|
|
|
|
#[cfg(target_os = "darwin")]
|
|
|
|
Object::Mach(goblin::mach::Mach::Binary(o)) => {
|
|
|
|
(o.dynsyms
|
|
|
|
.iter()
|
|
|
|
.filter(|e| e.is_function() && !e.is_import())
|
|
|
|
.map(|e| o.dynstrtab.get_at(e.st_name).unwrap())
|
|
|
|
.collect(),
|
|
|
|
o.name.expect("Couldn't read the name of the DLL. Is it a .NET DLL? It's not supported").replace(".dll", ""))
|
2024-04-13 03:24:16 +03:00
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
println!("Only PE (.dll) and ELF (.so) files are supported in their respective target platforms.");
|
|
|
|
std::process::exit(1);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
for e in exports.iter() {
|
|
|
|
println!("cargo:warning=Exported function: {} => {}-orig.{}", e, lib_name, e);
|
|
|
|
println!("cargo:rustc-link-arg=/export:{}={}-orig.{}", e, lib_name, e);
|
|
|
|
}
|
2024-03-03 13:10:58 +02:00
|
|
|
|
2024-04-14 00:29:53 +03:00
|
|
|
#[cfg(unix)]
|
2024-04-13 03:24:16 +03:00
|
|
|
{
|
|
|
|
let symbols_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("src").join("symbols.rs");
|
|
|
|
let mut symbols = File::create(&symbols_path).unwrap();
|
|
|
|
println!("cargo:rerun-if-changed={:?}", symbols_path);
|
2024-04-14 00:29:53 +03:00
|
|
|
println!("cargo:rustc-cfg=symbols");
|
|
|
|
|
|
|
|
let lib_name = if lib_name.starts_with("lib") {
|
|
|
|
lib_name.replacen("lib", "", 1)
|
|
|
|
} else {
|
|
|
|
lib_name.clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
writeln!(symbols, "#![allow(dead_code)]").unwrap();
|
|
|
|
|
|
|
|
for e in exports.iter() {
|
|
|
|
writeln!(symbols, "#[no_mangle]").unwrap();
|
|
|
|
writeln!(symbols, r#"pub unsafe extern "C" fn {e}() {{ original::{e}() }}"#).unwrap();
|
|
|
|
}
|
2024-04-13 03:24:16 +03:00
|
|
|
|
2024-04-14 00:29:53 +03:00
|
|
|
writeln!(symbols, "pub mod original {{").unwrap();
|
|
|
|
writeln!(symbols, "\t#[link(name = \"{}\")]", lib_name).unwrap();
|
|
|
|
writeln!(symbols, "\textern {{").unwrap();
|
2024-03-03 13:10:58 +02:00
|
|
|
for e in exports.iter() {
|
|
|
|
println!("cargo:warning=Exported function: {}", e);
|
2024-04-14 00:29:53 +03:00
|
|
|
// writeln!(symbols, "\t#[no_mangle]").unwrap();
|
|
|
|
writeln!(symbols, "\t\tpub fn {e}();").unwrap();
|
2024-03-03 13:10:58 +02:00
|
|
|
}
|
2024-04-14 00:29:53 +03:00
|
|
|
writeln!(symbols, "\t}}").unwrap();
|
2024-04-13 03:24:16 +03:00
|
|
|
writeln!(symbols, "}}").unwrap();
|
2023-05-25 17:48:18 +03:00
|
|
|
}
|
2024-02-27 03:01:29 +02:00
|
|
|
|
2024-04-13 03:24:16 +03:00
|
|
|
println!("cargo:warning=Expected library name: {}-orig.dll", lib_name);
|
|
|
|
println!("cargo:rustc-env=LIB_NAME={}-orig.dll", lib_name);
|
2023-05-23 17:21:14 +03:00
|
|
|
}
|