Add symbols for non-windows dll proxying

Signed-off-by: Dimitris Zervas <dzervas@dzervas.gr>
This commit is contained in:
Dimitris Zervas 2024-04-13 03:24:16 +03:00
parent ed6f77e71c
commit a4bf277c47
No known key found for this signature in database
3 changed files with 66 additions and 46 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ __handlers__
/examples/cs/bin
/examples/cs/obj
/dotnet
src/symbols.rs

109
build.rs
View File

@ -1,4 +1,7 @@
use std::env;
use std::io::Write;
use std::fs::File;
use std::path::Path;
#[cfg(feature = "dotnet")]
use csbindgen;
@ -6,64 +9,78 @@ fn main() {
println!("cargo:rerun-if-env-changed=FRIDA_CODE");
println!("cargo:rerun-if-env-changed=DLL_PROXY");
if let Ok(lib_path) = env::var("DLL_PROXY") {
println!("cargo:rerun-if-changed={}", &lib_path);
use goblin::Object;
let Ok(lib_path) = env::var("DLL_PROXY") else {
return;
};
let path = std::path::Path::new(&lib_path);
let lib_filename = path.file_name().unwrap().to_str().unwrap();
use goblin::Object;
println!("cargo:rerun-if-changed={}", &lib_path);
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());
let path = std::path::Path::new(&lib_path);
let lib_filename = path.file_name().unwrap().to_str().unwrap();
let (exports, lib_name): (Vec<&str>, String) = match object {
#[cfg(target_os = "windows")]
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", ""))
}
#[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(),
o.soname.expect("Couldn't read the name of the SO.").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", ""))
// },
_ => {
println!("Only PE (.dll) and ELF (.so) files are supported in their respective target platforms.");
std::process::exit(1);
},
};
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());
let (exports, lib_name): (Vec<&str>, String) = match object {
#[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);
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", ""))
}
#[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(),
o.soname.expect("Couldn't read the name of the SO.").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", ""))
// },
_ => {
println!("Only PE (.dll) and ELF (.so) files are supported in their respective target platforms.");
std::process::exit(1);
},
};
#[cfg(not(target_os = "windows"))]
#[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);
}
#[cfg(not(target_os = "windows"))]
{
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);
writeln!(symbols, "#[allow(dead_code)]").unwrap();
writeln!(symbols, "#[link(name = \"{}\")]", lib_name.replace("lib", "")).unwrap();
writeln!(symbols, "extern {{").unwrap();
for e in exports.iter() {
println!("cargo:warning=Exported function: {}", e);
// writeln!(symbols, "\t#[no_mangle]").unwrap();
writeln!(symbols, "\tpub fn {}();", e).unwrap();
}
println!("cargo:warning=Expected library name: {}-orig.dll", lib_name);
println!("cargo:rustc-env=LIB_NAME={}-orig.dll", lib_name);
writeln!(symbols, "}}").unwrap();
}
println!("cargo:warning=Expected library name: {}-orig.dll", lib_name);
println!("cargo:rustc-env=LIB_NAME={}-orig.dll", lib_name);
#[cfg(feature = "dotnet")]
{
let lib_path = concat!(env!("CARGO_MANIFEST_DIR"), "/src/lib.rs");

View File

@ -3,6 +3,8 @@ pub mod injector;
pub mod frida_handler;
// #[cfg(feature = "dotnet")]
// pub mod cs;
#[cfg(not(windows))]
pub mod symbols;
pub use injector::attach_self;