Nytro Posted April 19, 2014 Report Posted April 19, 2014 [h=1]Detect debugger with TLS callback[/h][h=3]zwclose7[/h]TLS callback is a function that called before the process entry point executes. If you run the executable with a debugger, the TLS callback will be executed before the debugger breaks. This means you can perform anti-debugging checks before the debugger can do anything. Therefore, TLS callback is a very powerful anti-debugging technique.To add a TLS callback to your program, you need to create a section called .CRT$XLB in the executable image, and then put the TLS callback function address into this section. You also need to add the __tls_used symbol to the executable image.The following stack trace shows how the TLS callback called (from Process Hacker)0, ntoskrnl.exe!KiDeliverApc+0x1c71, ntoskrnl.exe!KiCommitThreadWait+0x3dd2, ntoskrnl.exe!KeWaitForSingleObject+0x19f3, win32k.sys!xxxRealSleepThread+0x2574, win32k.sys!xxxSleepThread+0x595, win32k.sys!NtUserWaitMessage+0x466, ntoskrnl.exe!KiSystemServiceCopyEnd+0x137, wow64cpu.dll!CpupSyscallStub+0x98, wow64cpu.dll!Thunk0Arg+0x59, wow64.dll!RunCpuSimulation+0xa10, wow64.dll!Wow64LdrpInitialize+0x42a11, ntdll.dll!LdrpInitializeProcess+0x17e312, ntdll.dll! ?? ::FNODOBFM::`string'+0x28ff013, ntdll.dll!LdrInitializeThunk+0xe14, user32.dll!NtUserWaitMessage+0x1515, user32.dll!DialogBox2+0x22216, user32.dll!InternalDialogBox+0xe517, user32.dll!SoftModalMessageBox+0x75718, user32.dll!MessageBoxWorker+0x26919, user32.dll!MessageBoxTimeoutW+0x5220, user32.dll!MessageBoxTimeoutA+0x7621, user32.dll!MessageBoxExA+0x1b22, user32.dll!MessageBoxA+0x1823, tls.exe!TlsCallback+0x3c24, ntdll.dll!LdrpCallInitRoutine+0x1425, ntdll.dll!LdrpCallTlsInitializers+0x9e26, ntdll.dll!LdrpRunInitializeRoutines+0x3ab27, ntdll.dll!LdrpInitializeProcess+0x140028, ntdll.dll!_LdrpInitialize+0x7829, ntdll.dll!LdrInitializeThunk+0x10You can see the TLS callback is called by the loader during process startup. Here is example code.#include <stdio.h>#include <Windows.h>#pragma comment(lib,"ntdll.lib")#pragma comment(linker,"/include:__tls_used") // This will cause the linker to create the TLS directory#pragma section(".CRT$XLB",read) // Create a new sectionextern "C" NTSTATUS NTAPI NtQueryInformationProcess(HANDLE hProcess,ULONG InfoClass,PVOID Buffer,ULONG Length,PULONG ReturnLength);#define NtCurrentProcess() (HANDLE)-1// The TLS callback is called before the process entry point executes, and is executed before the debugger breaks// This allows you to perform anti-debugging checks before the debugger can do anything// Therefore, TLS callback is a very powerful anti-debugging techniquevoid WINAPI TlsCallback(PVOID Module,DWORD Reason,PVOID Context){ PBOOLEAN BeingDebugged=(PBOOLEAN)__readfsdword(0x30)+2; HANDLE DebugPort=NULL; if(*BeingDebugged) // Read the PEB { MessageBox(NULL,"Debugger detected!","TLS callback",MB_ICONSTOP); } else { MessageBox(NULL,"No debugger detected","TLS callback",MB_ICONINFORMATION); } // Another check if(!NtQueryInformationProcess( NtCurrentProcess(), 7, // ProcessDebugPort &DebugPort, // If debugger is present, it will be set to -1 | Otherwise, it is set to NULL sizeof(HANDLE), NULL)) { if(DebugPort) { MessageBox(NULL,"Debugger detected!","TLS callback",MB_ICONSTOP); } else { MessageBox(NULL,"No debugger detected","TLS callback",MB_ICONINFORMATION); } }}__declspec(allocate(".CRT$XLB")) PIMAGE_TLS_CALLBACK CallbackAddress[]={TlsCallback,NULL}; // Put the TLS callback address into a null terminated array of the .CRT$XLB section// The entry point is executed after the TLS callbackint main(){ printf("Hello world"); getchar(); return 0;}[h=4]Attached Files[/h] tls.zip 350.48KB 15 downloadsSursa: Detect debugger with TLS callback - Source Codes - rohitab.com - Forums Quote
giv Posted May 11, 2014 Report Posted May 11, 2014 (edited) E useless.In LordPe vezi Tls.Olly 1 are un plugin pentru asa ceva iar Olly 2 are by default oprire la Tls si deci acest trick era bun acum 10 ani. Acum nu mai e valabil. Edited May 11, 2014 by giv Quote