TLS Callback

Created the Monday 18 March 2019. Updated 3 years, 6 months ago.

Most debuggers start at the program entry point as defined by the PE header. A TLS callback can be used to execute code before the entry point and therefore run secretly in a debugger.

This technique can be used to detect that the process is being debugged and thus terminate the process instead to continue execution.

Technique Identifier


Technique Tag


Code Snippets

#include "windows.h"
#include <stdio.h>

void NTAPI __stdcall TLSCallbacks(PVOID DllHandle, DWORD dwReason, PVOID Reserved);

#ifdef _M_IX86
#pragma comment (linker, "/INCLUDE:__tls_used")
#pragma comment (linker, "/INCLUDE:__tls_callback")
#pragma comment (linker, "/INCLUDE:_tls_used")
#pragma comment (linker, "/INCLUDE:_tls_callback")
#ifdef _M_X64
#pragma const_seg (".CRT$XLB")
#pragma data_seg (".CRT$XLB")

PIMAGE_TLS_CALLBACK _tls_callback = TLSCallbacks;
#pragma data_seg ()
#pragma const_seg ()

void NTAPI __stdcall TLSCallbacks(PVOID DllHandle, DWORD dwReason, PVOID Reserved)
	MessageBox(nullptr, "TLS Callback", "", 0);

int main(int argc, char* argv[])
	printf("Main function!");

Detection Rules

rule detect_tlscallback {
        description = "Simple rule to detect tls callback as anti-debug."
        author = "Thomas Roccia | @fr0gger_"
        $str1 = "TLS_CALLBACK" nocase
        $str2 = "TLScallback" nocase
        uint32(uint32(0x3C)) == 0x4550 and any of them

Additional Resources

External Links

Subscribe to our Newsletter

The information entered into this form is mandatory. It will be subjected to computer processing. It is processed by computer in order to support our users and readers. The recipients of the data will be :

According to the Data Protection Act of January 6th, 1978, you have at any time, a right of access to and rectification of all of your personal data. If you wish to exercise this right and gain access to your personal data, please write to Thomas Roccia at

You may also oppose, for legitimate reasons, the processing of your personal data.