Initial commit - able to produce a working Linux POC

This commit is contained in:
Dimitris Zervas 2023-05-23 17:21:14 +03:00
commit 11ec2f401b
No known key found for this signature in database
GPG Key ID: 5C27D7C9D1901A30
8 changed files with 1561 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

49
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,49 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'frida-deepfreeze-rs'",
"cargo": {
"args": [
"build",
"--bin=frida-deepfreeze-rs",
"--package=frida-deepfreeze-rs"
],
"filter": {
"name": "frida-deepfreeze-rs",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}",
"env": {
"RUST_BACKTRACE": "1",
"FRIDA_CODE": "console.log('Hello from Frida!');"
}
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'frida-deepfreeze-rs'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=frida-deepfreeze-rs",
"--package=frida-deepfreeze-rs"
],
"filter": {
"name": "frida-deepfreeze-rs",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

1368
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

18
Cargo.toml Normal file
View File

@ -0,0 +1,18 @@
[package]
name = "frida-deepfreeze-rs"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
frida = { version = "0.4.0", features = ["auto-download"] }
frida-sys = { version = "0.4.0", features = ["auto-download", "frida-build"] }
lazy_static = "1.4.0"
# [target.'cfg(unix)'.dependencies]
ctor = "0.2.0"
# [target.'cfg(windows)'.dependencies]
# winapi = "0.3.9"

16
build.rs Normal file
View File

@ -0,0 +1,16 @@
use std::env;
fn main() {
// Set the environment variable
env::set_var("MY_STRING", "Hello, world!");
if let Ok(code_file) = env::var("FRIDA_CODE_FILE") {
env::set_var("FRIDA_CODE", &std::fs::read_to_string(&code_file).unwrap());
eprintln!("Using code from file: {}", &code_file);
} else if env::var("FRIDA_CODE").is_ok() {
eprintln!("Using code from environment variable: FRIDA_CODE");
} else {
eprintln!("Please set FRIDA_CODE or FRIDA_CODE_FILE environment variable");
std::process::exit(1);
}
}

55
src/injector.rs Normal file
View File

@ -0,0 +1,55 @@
use frida::{DeviceManager, Frida, ScriptHandler, ScriptOption, ScriptRuntime};
use lazy_static::lazy_static;
lazy_static! {
static ref FRIDA: Frida = unsafe { Frida::obtain() };
}
const FRIDA_CODE: &str = env!("FRIDA_CODE", "Please set FRIDA_CODE environment variable");
#[no_mangle]
pub fn inject(pid: u32) {
let device_manager = DeviceManager::obtain(&FRIDA);
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);
let script = session
.create_script(FRIDA_CODE, &mut script_option)
.unwrap();
script.handle_message(&mut Handler).unwrap();
script.load().unwrap();
println!("[*] Script loaded");
script.unload().unwrap();
println!("[*] Script unloaded");
session.detach().unwrap();
println!("[*] Session detached");
}
};
}
#[no_mangle]
pub fn inject_self() {
println!("[*] Attaching to self self");
inject(0);
}
struct Handler;
impl ScriptHandler for Handler {
fn on_message(&mut self, message: &str) {
println!("[<] {message}");
}
}

40
src/lib.rs Normal file
View File

@ -0,0 +1,40 @@
pub mod injector;
pub use injector::{inject, inject_self};
// #[cfg(unix)]
use ctor::ctor;
// #[cfg(unix)]
#[ctor]
fn _start() {
println!("[+] frida-deepfreeze-rs SO injected");
inject_self();
}
/*
#[cfg(windows)]
use std::ptr;
#[cfg(windows)]
use std::ffi::c_void;
#[cfg(windows)]
use winapi::um::libloaderapi::{DllMain, DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH, DLL_THREAD_ATTACH, DLL_THREAD_DETACH};
#[allow(non_snake_case)]
#[cfg(windows)]
#[no_mangle]
pub extern "system" fn DllMain(hinstDLL: *mut c_void, fdwReason: u32, _: *mut c_void) -> i32 {
match fdwReason {
DLL_PROCESS_ATTACH => {
println!("[+] frida-deepfreeze-rs DLL injected");
inject_self();
}
// DLL_PROCESS_DETACH => {}
// DLL_THREAD_ATTACH => {}
// DLL_THREAD_DETACH => {}
_ => {}
}
1
}
*/

14
src/main.rs Normal file
View File

@ -0,0 +1,14 @@
pub mod injector;
pub use injector::inject;
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
println!("Usage: {} <PID>", args[0]);
return;
}
let pid: u32 = args[1].parse().unwrap();
inject(pid);
}