From ea1821480f9f1426722b2a560cff0f49dd29010e Mon Sep 17 00:00:00 2001 From: rootacite <1498045907@qq.com> Date: Fri, 24 Oct 2025 13:44:27 +0800 Subject: [PATCH] [fix] fix race condition. --- 01/project-hbj-attacker/Cargo.toml | 2 +- 01/project-hbj-attacker/src/main.rs | 66 ++++++++++++++++++---------- 01/project-hbj/target | Bin 15896 -> 15944 bytes 01/project-hbj/target.cpp | 6 ++- 4 files changed, 48 insertions(+), 26 deletions(-) diff --git a/01/project-hbj-attacker/Cargo.toml b/01/project-hbj-attacker/Cargo.toml index b39f5be..594bca9 100644 --- a/01/project-hbj-attacker/Cargo.toml +++ b/01/project-hbj-attacker/Cargo.toml @@ -7,7 +7,7 @@ edition = "2024" dynasmrt = "4.0.1" iced-x86 = { version = "1.21.0", features = ["code_asm"] } libc = "0.2.177" -nix = { version = "0.30.1", features = ["ptrace", "uio"] } +nix = { version = "0.30.1", features = ["ptrace", "uio", "signal"] } ctor = "0.6.0" libloading = "0.8.9" diff --git a/01/project-hbj-attacker/src/main.rs b/01/project-hbj-attacker/src/main.rs index c1faa97..564ac69 100644 --- a/01/project-hbj-attacker/src/main.rs +++ b/01/project-hbj-attacker/src/main.rs @@ -3,12 +3,13 @@ mod helper; use nix::sys::ptrace; +use nix::sys::signal::{Signal, kill}; use nix::unistd::Pid; use std::arch::asm; use std::ffi::CString; use helper::*; -use iced_x86::code_asm::{eax, r8, r9, r10, rax, rdi, rdx, rsi, rcx, rsp, rbp}; +use iced_x86::code_asm::{eax, r8, r9, r10, rax, rbp, rcx, rdi, rdx, rsi, rsp}; use libc::{RTLD_NEXT, c_void, dlsym}; use libc::{ptrace, user_regs_struct}; use std::fs; @@ -103,7 +104,7 @@ fn inject2(pid: Pid, seg_rw: (u64, u64)) -> Result<(), Box Result<(), Box> // thread inject +fn inject3(pid: Pid, seg_rw: (u64, u64)) -> Result> // thread inject { let regs = ptrace::getregs(pid)?; // Alloc rwx memory @@ -150,7 +151,11 @@ fn inject3(pid: Pid, seg_rw: (u64, u64)) -> Result<(), Box Result<(), Box Result<(), Box= page_addr as u64 && regs.rip < (page_addr + 0x1000) as u64 { + println!("{GREEN}[trace]{RESET} rip in new thread return to inject payload."); + break; + } + + ptrace::step(pid_new_thread, None)?; + wait(pid_new_thread); + } + + Ok(pid_new_thread.as_raw()) } fn main() -> Result<(), Box> { @@ -262,22 +292,6 @@ fn main() -> Result<(), Box> { ptrace::step(pid, None)?; wait(pid); - // ↓ Old behavior, but maybe the process stay in their libraries forever ? - - // loop{ - // // Single-stepping, so that RIP returns to the user space of the process itself, - // // rather than in some other library - // let regs = ptrace::getregs(pid)?; - // if is_address_in_range(regs.rip, &lines) - // { - // println!("{GREEN}[trace]{RESET} Address: {:#016x}", regs.rip); - // break; - // } - // println!("{GREEN}[trace]{RESET} Skipped: {:#016x}", regs.rip); - // ptrace::step(pid, None)?; - // waitpid(pid, None)?; - //} - // Save context let regs = ptrace::getregs(pid)?; // Save current registers let buffer = read_memory_vm(pid, seg_x.0 as usize, 4096)?; // Save current memory context @@ -296,7 +310,7 @@ fn main() -> Result<(), Box> { // Do inject here - inject3(pid, seg_rw)?; + let c = inject3(pid, seg_rw)?; // End inject logics @@ -306,6 +320,12 @@ fn main() -> Result<(), Box> { write_memory_vm(pid, seg_rw.0 as usize, &buffer_rw)?; ptrace::detach(pid, None)?; + ptrace::detach(Pid::from_raw(c), None)?; + + let mut input = String::new(); + std::io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); Ok(()) } diff --git a/01/project-hbj/target b/01/project-hbj/target index 60be9d0b6ce10b31efbfd324c0e4adaad2f39d87..f8e719b14753b440e1ea168ce90b75304dbff427 100755 GIT binary patch delta 1412 zcmZ8hZ)jUp6u&p`rOAs)me)4E)Yc`Uqw8ev<Nf?u~$3OLhh}(zxwPs%wwHx9rg3wRSKVPyBO2-r|l-WYr^PKmmmYv5t=lsqe z&b{Z{dlzcUwMIm4gqTSMOR(j+zAMs(*Fqa}ziK|oWdX?r?R=bn-mLOoSL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&uKkGW&{>Riqn3FRhcm71Jhg>|*_7C` zBO;-SPs`EFW%4v%jCtE8f^3y%`I_09$nx;-c^2H{8n0)9P{Ym42;^}vn}Tkf${xh^ zY%5&iuOR%+wemNwh`*}PhM8Ox9^)EEbHjKwx1hcC#`t)}JoN0~z`>EMku`eo=)QA} zrk;DyPfk+iDzeVg3xu*FVF76<$NmSb#cp&zvcI #include + int main() { pid_t pid = getpid(); @@ -11,6 +12,7 @@ int main() while(true) { - sleep(0); + usleep(1000 * 300); + write(1, ".", 1); } -} \ No newline at end of file +}