Split the frida handler to a separte handler and add a test
This commit is contained in:
32
src/cs.rs
Normal file
32
src/cs.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use cs_eval::{EvalContext, EvalError};
|
||||
|
||||
fn cs_inject() {
|
||||
// Define your C# code to be compiled
|
||||
let csharp_code = r#"
|
||||
using System;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
Console.WriteLine("Hello, C#!");
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
// Create an evaluation context
|
||||
let mut context = EvalContext::new();
|
||||
|
||||
// Compile and execute the C# code
|
||||
match context.eval::<()>(csharp_code) {
|
||||
Ok(_) => {
|
||||
println!("C# code executed successfully");
|
||||
}
|
||||
Err(EvalError::CompilationError(err)) => {
|
||||
println!("Compilation error: {:?}", err);
|
||||
}
|
||||
Err(EvalError::ExecutionError(err)) => {
|
||||
println!("Execution error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
116
src/frida_handler.rs
Normal file
116
src/frida_handler.rs
Normal file
@ -0,0 +1,116 @@
|
||||
#![cfg(feature = "frida")]
|
||||
use frida::{DeviceManager, Frida, ScriptHandler, ScriptOption, ScriptRuntime};
|
||||
use lazy_static::lazy_static;
|
||||
use serde::Deserialize;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref FRIDA: Frida = unsafe { Frida::obtain() };
|
||||
}
|
||||
|
||||
pub fn attach_pid(frida_code: String, pid: u32) {
|
||||
println!("[+] Injecting into PID: {}", pid);
|
||||
|
||||
let device_manager = DeviceManager::obtain(&FRIDA);
|
||||
println!("[*] Device Manager obtained");
|
||||
|
||||
if let Some(device) = device_manager.enumerate_all_devices().first() {
|
||||
println!("[*] First device: {}", device.get_name());
|
||||
|
||||
let session = device.attach(pid).unwrap();
|
||||
|
||||
if !session.is_detached() {
|
||||
println!("[*] Attached");
|
||||
|
||||
let mut script_option = ScriptOption::new()
|
||||
.set_runtime(ScriptRuntime::QJS);
|
||||
println!("[*] Script {}", frida_code);
|
||||
let script = session
|
||||
.create_script(&frida_code, &mut script_option)
|
||||
.unwrap();
|
||||
|
||||
script.handle_message(&mut Handler).unwrap();
|
||||
|
||||
script.load().unwrap();
|
||||
println!("[*] Script loaded");
|
||||
}
|
||||
} else {
|
||||
eprintln!("[!] No device found!");
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct LogEntry {
|
||||
#[serde(rename = "type")]
|
||||
log_type: LogType,
|
||||
level: LogLevel,
|
||||
payload: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
enum LogType {
|
||||
#[serde(rename = "log")]
|
||||
Log,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
enum LogLevel {
|
||||
#[serde(rename = "debug")]
|
||||
Debug,
|
||||
#[serde(rename = "info")]
|
||||
Info,
|
||||
#[serde(rename = "warning")]
|
||||
Warning,
|
||||
#[serde(rename = "error")]
|
||||
Error,
|
||||
}
|
||||
|
||||
struct Handler;
|
||||
|
||||
impl ScriptHandler for Handler {
|
||||
fn on_message(&mut self, message: &str) {
|
||||
if let Ok(log_entry) = serde_json::from_str::<LogEntry>(message) {
|
||||
match log_entry.log_type {
|
||||
LogType::Log => {
|
||||
match log_entry.level {
|
||||
LogLevel::Debug => eprint!("[-] "),
|
||||
LogLevel::Info => eprint!("[i] "),
|
||||
LogLevel::Warning => eprint!("[!] "),
|
||||
LogLevel::Error => eprint!("[X] "),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!("{}", log_entry.payload);
|
||||
return;
|
||||
}
|
||||
|
||||
eprintln!("{message}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[link(name = "mylib", kind = "dylib")]
|
||||
extern {
|
||||
fn mylib_foo() -> u8;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_attach_pid() {
|
||||
assert_eq!(10, unsafe { mylib_foo() });
|
||||
|
||||
let frida_script = r#"
|
||||
const foo = Module.getExportByName(null, "mylib_foo");
|
||||
Interceptor.replace(foo, new NativeCallback(function () {
|
||||
console.log("replaced foo() called");
|
||||
return 20;
|
||||
}, "uint8", []));
|
||||
"#;
|
||||
|
||||
attach_pid(frida_script.to_string(), 0);
|
||||
assert_eq!(20, unsafe { mylib_foo() });
|
||||
}
|
||||
}
|
112
src/injector.rs
112
src/injector.rs
@ -1,99 +1,27 @@
|
||||
use frida::{DeviceManager, Frida, ScriptHandler, ScriptOption, ScriptRuntime};
|
||||
use serde::Deserialize;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static! {
|
||||
static ref FRIDA: Frida = unsafe { Frida::obtain() };
|
||||
#[cfg(all(unix, not(feature = "frida")))]
|
||||
compile_error!("Only Frida injection is supported for Unix targets");
|
||||
|
||||
#[cfg(all(not(feature = "managed_lib"), not(feature = "frida")))]
|
||||
compile_error!("No injection method is selected - please enable either managed_lib (windows-only) and/or frida feature");
|
||||
|
||||
#[cfg(all(not(windows), feature = "managed_lib"))]
|
||||
compile_error!("Managed library injection is only supported for Windows target");
|
||||
|
||||
#[cfg(feature = "frida")]
|
||||
use crate::frida_handler::attach_pid as frida_attach_pid;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn attach(pid: u32) {
|
||||
#[cfg(feature = "frida")]
|
||||
{
|
||||
let frida_code = env!("FRIDA_CODE").to_string();
|
||||
std::thread::spawn(move || frida_attach_pid(frida_code, pid));
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn attach(pid: u32) {
|
||||
let frida_code = env!("FRIDA_CODE").to_string();
|
||||
println!("[+] Injecting into PID: {}", pid);
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let device_manager = DeviceManager::obtain(&FRIDA);
|
||||
println!("[*] Device Manager obtained");
|
||||
|
||||
if let Some(device) = device_manager.enumerate_all_devices().first() {
|
||||
println!("[*] First device: {}", device.get_name());
|
||||
|
||||
let session = device.attach(pid).unwrap();
|
||||
|
||||
if !session.is_detached() {
|
||||
println!("[*] Attached");
|
||||
|
||||
let mut script_option = ScriptOption::new()
|
||||
// .set_name("frida-deepfreeze-rs")
|
||||
.set_runtime(ScriptRuntime::QJS);
|
||||
println!("[*] Script {}", frida_code);
|
||||
let script = session
|
||||
.create_script(&frida_code, &mut script_option)
|
||||
.unwrap();
|
||||
|
||||
script.handle_message(&mut Handler).unwrap();
|
||||
|
||||
script.load().unwrap();
|
||||
println!("[*] Script loaded");
|
||||
}
|
||||
} else {
|
||||
eprintln!("[!] No device found!");
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn attach_self() {
|
||||
pub extern "C" fn attach_self() {
|
||||
println!("[*] Attaching to self");
|
||||
attach(0);
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct LogEntry {
|
||||
#[serde(rename = "type")]
|
||||
log_type: LogType,
|
||||
level: LogLevel,
|
||||
payload: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
enum LogType {
|
||||
#[serde(rename = "log")]
|
||||
Log,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
enum LogLevel {
|
||||
#[serde(rename = "debug")]
|
||||
Debug,
|
||||
#[serde(rename = "info")]
|
||||
Info,
|
||||
#[serde(rename = "warning")]
|
||||
Warning,
|
||||
#[serde(rename = "error")]
|
||||
Error,
|
||||
}
|
||||
|
||||
struct Handler;
|
||||
|
||||
impl ScriptHandler for Handler {
|
||||
fn on_message(&mut self, message: &str) {
|
||||
if let Ok(log_entry) = serde_json::from_str::<LogEntry>(message) {
|
||||
match log_entry.log_type {
|
||||
LogType::Log => {
|
||||
match log_entry.level {
|
||||
LogLevel::Debug => eprint!("[-] "),
|
||||
LogLevel::Info => eprint!("[i] "),
|
||||
LogLevel::Warning => eprint!("[!] "),
|
||||
LogLevel::Error => eprint!("[X] "),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!("{}", log_entry.payload);
|
||||
return;
|
||||
}
|
||||
|
||||
eprintln!("{message}");
|
||||
}
|
||||
}
|
||||
|
21
src/lib.rs
21
src/lib.rs
@ -1,11 +1,13 @@
|
||||
pub mod injector;
|
||||
#[cfg(feature = "frida")]
|
||||
pub mod frida_handler;
|
||||
|
||||
pub use injector::{attach, attach_self};
|
||||
|
||||
#[cfg(unix)]
|
||||
#[cfg(all(unix, not(test)))]
|
||||
use ctor::ctor;
|
||||
|
||||
#[cfg(unix)]
|
||||
#[cfg(all(unix, not(test)))]
|
||||
#[ctor]
|
||||
fn _start() {
|
||||
println!("[+] frida-deepfreeze-rs library injected");
|
||||
@ -16,21 +18,26 @@ fn _start() {
|
||||
// during DeviceManager::obtain. DllMain works fine though.
|
||||
#[cfg(windows)]
|
||||
use std::ffi::c_void;
|
||||
|
||||
#[cfg(windows)]
|
||||
use winapi::um::winnt::DLL_PROCESS_ATTACH;
|
||||
#[cfg(windows)]
|
||||
|
||||
#[cfg(all(windows, feature = "dll_proxy"))]
|
||||
use winapi::um::libloaderapi::LoadLibraryA;
|
||||
|
||||
#[cfg(windows)]
|
||||
#[cfg(all(windows, not(test)))]
|
||||
#[no_mangle]
|
||||
#[allow(non_snake_case, unused_variables)]
|
||||
extern "system" fn DllMain(dll_module: *mut c_void, call_reason: u32, _: *mut ()) -> bool {
|
||||
match call_reason {
|
||||
DLL_PROCESS_ATTACH => {
|
||||
println!("[+] frida-deepfreeze-rs DLL injected");
|
||||
unsafe { LoadLibraryA(env!("LIB_NAME").as_ptr() as *const i8); }
|
||||
println!("[+] Original DLL {} loaded", env!("LIB_NAME"));
|
||||
|
||||
#[cfg(feature = "dll_proxy")]
|
||||
{
|
||||
unsafe { LoadLibraryA(env!("LIB_NAME").as_ptr() as *const i8); }
|
||||
println!("[+] Original DLL {} loaded", env!("LIB_NAME"));
|
||||
}
|
||||
|
||||
attach_self();
|
||||
}
|
||||
// Maybe we should detach? Is it useful?
|
||||
|
@ -1,4 +1,7 @@
|
||||
pub mod injector;
|
||||
#[cfg(feature = "frida")]
|
||||
pub mod frida_handler;
|
||||
|
||||
pub use injector::attach;
|
||||
|
||||
fn main() {
|
||||
@ -12,3 +15,6 @@ fn main() {
|
||||
let pid: u32 = args[1].parse().unwrap();
|
||||
attach(pid);
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod integration_tests;
|
||||
|
Reference in New Issue
Block a user