Skip to content

Commit de0add7

Browse files
committed
AntiDebugging
Adding Antidebugging code snippets
1 parent 48822d2 commit de0add7

File tree

9 files changed

+222
-0
lines changed

9 files changed

+222
-0
lines changed

AntiDebugging/CheckRemoteDebuggerPresent/Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "CheckRemoteDebuggerPresent"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
8+
[dependencies.windows-sys]
9+
version = "0.61.2"
10+
features = [
11+
"Win32_Foundation",
12+
"Win32_System_Diagnostics_Debug",
13+
"Win32_System_Threading"
14+
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use windows_sys::Win32::System::{
2+
Diagnostics::Debug::CheckRemoteDebuggerPresent,
3+
Threading::{ExitProcess, GetCurrentProcess},
4+
};
5+
6+
fn main() {
7+
unsafe {
8+
let mut is_debugger_present = 0;
9+
10+
let status = CheckRemoteDebuggerPresent(GetCurrentProcess(), &mut is_debugger_present);
11+
12+
if status != 0 && is_debugger_present == 1 {
13+
println!("Debugger detected! Exiting process...");
14+
15+
let mut string = String::new();
16+
std::io::stdin().read_line(&mut string).unwrap();
17+
18+
ExitProcess(u32::MAX);
19+
} else {
20+
println!("No debugger detected. Proceeding...");
21+
}
22+
}
23+
}

AntiDebugging/ProcessDebugPort/Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "ProcessDebugPort"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
8+
[dependencies.windows-sys]
9+
version = "0.61.2"
10+
features = [
11+
"Win32_Foundation",
12+
"Win32_System_Diagnostics_Debug",
13+
"Win32_System_Threading",
14+
"Win32_System_LibraryLoader",
15+
]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use std::ffi::CString;
2+
3+
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA};
4+
use windows_sys::Win32::System::Threading::{ExitProcess, GetCurrentProcess};
5+
6+
const PROCESS_DEBUG_PORT: u32 = 7;
7+
8+
use windows_sys::Win32::Foundation::{HANDLE, NTSTATUS};
9+
10+
use windows_sys::core::s;
11+
12+
#[allow(non_snake_case)]
13+
type NtQueryInfoProc = unsafe extern "system" fn(
14+
ProcessHandle: HANDLE,
15+
ProcessInformationClass: u32,
16+
ProcessInformation: *mut i32,
17+
ProcessInformationLength: u32,
18+
ReturnLength: *mut u32,
19+
) -> NTSTATUS;
20+
21+
fn main() {
22+
unsafe {
23+
let h_ntdll = LoadLibraryA(s!("ntdll.dll"));
24+
25+
if !h_ntdll.is_null() {
26+
let func_name = CString::new("NtQueryInformationProcess").unwrap();
27+
let func_ptr = GetProcAddress(h_ntdll, func_name.as_ptr() as *const u8);
28+
29+
if let Some(func_ptr) = func_ptr {
30+
let nt_query_info_process: NtQueryInfoProc = std::mem::transmute(func_ptr);
31+
32+
let mut debug_port: i32 = 0;
33+
let mut return_len: u32 = 0;
34+
35+
let status = nt_query_info_process(
36+
GetCurrentProcess(),
37+
PROCESS_DEBUG_PORT,
38+
&mut debug_port,
39+
std::mem::size_of::<i32>() as u32,
40+
&mut return_len,
41+
);
42+
43+
if status >= 0 && debug_port == -1 {
44+
println!("Debugger detected via ProcessDebugPort! Exiting...");
45+
ExitProcess(u32::MAX);
46+
} else {
47+
println!("No debugger detected (Port value: {:#X}).", debug_port);
48+
}
49+
}
50+
}
51+
}
52+
53+
let mut string = String::new();
54+
std::io::stdin().read_line(&mut string).unwrap();
55+
}

AntiDebugging/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## AntiDebugging in Rust
2+
3+
AntiDebug Code Snippets ....
4+
5+
## Resources
6+
7+
* https://anti-debug.checkpoint.com/

AntiDebugging/debug_teb/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "debug_teb"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
8+
[dependencies.windows-sys]
9+
version = "0.61.2"
10+
features = [
11+
"Win32_Foundation",
12+
"Win32_System_Threading",
13+
"Win32_System_Memory",
14+
"Win32_Security",
15+
"Win32_Storage_FileSystem",
16+
"Win32_System_Diagnostics_Debug",
17+
"Win32_System_LibraryLoader",
18+
"Win32_Security_Cryptography",
19+
"Win32_System_Com",
20+
"Win32_System_Registry",
21+
"Win32_UI_Shell_Common",
22+
"Win32_UI_WindowsAndMessaging"
23+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use windows_sys::Win32::UI::WindowsAndMessaging::{MB_OK, MessageBoxA};
2+
3+
use std::arch::asm;
4+
use windows_sys::core::s;
5+
6+
fn check_teb() -> i32 {
7+
8+
#[allow(unused_assignments)]
9+
let mut is_being_debugged: i32 = 0;
10+
11+
unsafe {
12+
asm!(
13+
"mov rax, gs:[0x60]",
14+
"movzx eax, byte ptr [rax + 0x02]",
15+
"mov {0:e}, eax",
16+
out(reg) is_being_debugged,
17+
);
18+
}
19+
is_being_debugged
20+
}
21+
22+
fn main() {
23+
if check_teb() != 0 {
24+
unsafe {
25+
MessageBoxA(
26+
std::ptr::null_mut(),
27+
s!("Debugger Detected"),
28+
s!("Info"),
29+
MB_OK,
30+
);
31+
}
32+
}
33+
}
34+
35+

0 commit comments

Comments
 (0)