Skip to content

Instantly share code, notes, and snippets.

@dennisfischer
Created October 7, 2015 14:53
Show Gist options
  • Save dennisfischer/525003173637929adeea to your computer and use it in GitHub Desktop.
Save dennisfischer/525003173637929adeea to your computer and use it in GitHub Desktop.
TLS callback for x86 & x64 windows OS.
// @source: http://www.unknowncheats.me/forum/c-and-c/84146-tls-callback-tutorial.html
#include "windows.h"
#include <stdio.h>
void NTAPI __stdcall TLSCallbacks(PVOID DllHandle, DWORD dwReason, PVOID Reserved);
//linker spec
#ifdef _M_IX86
#pragma comment (linker, "/INCLUDE:__tls_used")
#pragma comment (linker, "/INCLUDE:__tls_callback")
#else
#pragma comment (linker, "/INCLUDE:_tls_used")
#pragma comment (linker, "/INCLUDE:_tls_callback")
#endif
EXTERN_C
#ifdef _M_X64
#pragma const_seg (".CRT$XLB")
const
#else
#pragma data_seg (".CRT$XLB")
#endif
//end linker
//tls import
PIMAGE_TLS_CALLBACK _tls_callback = TLSCallbacks;
#pragma data_seg ()
#pragma const_seg ()
//end
// tls declaration
void NTAPI __stdcall TLSCallbacks(PVOID DllHandle, DWORD dwReason, PVOID Reserved)
{
MessageBox(nullptr, L"TLS Callback before main :)", L"dZkyXj - Debugger Owned!", 0);
ExitProcess(0);
}
// end declaration
int main(int argc, char* argv[])
{
printf("wow i'm main function but i'll never be executed :(");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment