Skip to content

Instantly share code, notes, and snippets.

@livz

livz/ExeTLS.c Secret

Created August 29, 2017 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save livz/47d128220af3357a0616fb2f762ddcfd to your computer and use it in GitHub Desktop.
Save livz/47d128220af3357a0616fb2f762ddcfd to your computer and use it in GitHub Desktop.
TLS callbacks
/*
* Compile:
* > cl /nologo /EHsc ExeTLS.c
*
*/
#pragma comment(lib, "User32.lib") /* for MessageBox() */
#include <Windows.h>
/* Pointer to a TLS callback function */
typedef void (__stdcall *TLS_CALLBACK_PTR)(void *instance, int reason, void *reserved);
/* Semi-documented in tlssup.c
* Defined in C:\Program Files\Windows Kits\8.0\include\um\winnt.h, as IMAGE_TLS_DIRECTORY
*/
typedef struct TLS_DATA {
int tls_start; // start of TLS data
int tls_end; // end of TLS data
int* ls_index; // address of tls_index
TLS_CALLBACK_PTR * tls_functions; // array of addresses of TLS callbacks
int fill_size; // size of TLS zero fill (0)
int characteristics; // TLS characteristics (0)
} TLS_DATA;
/* Thread Local Storage index for this .EXE */
long _tls_index = 0;
void __stdcall callback(void *instance, int reason, void *reserved) {
if(reason == DLL_PROCESS_ATTACH) {
MessageBox(NULL, "Hidden message", "Callback", MB_OK);
ExitProcess(0);
}
}
void __stdcall callback2(void *instance, int reason, void *reserved) {
if(reason == DLL_PROCESS_ATTACH) {
MessageBox(NULL, "Hidden message 2", "Callback 2", MB_OK);
ExitProcess(0);
}
}
/* Thread callbacks */
TLS_CALLBACK_PTR tls_functions[3] = {&callback, &callback2, NULL};
/* Variable name MUST be _tls_used, as the linker looks for a variable by that name */
extern TLS_DATA _tls_used={0, 0, &_tls_index, tls_functions, 0, 0};
int main(int argc, char* argv[]) {
/* Will never be called, as the last TLS callback does ExitProcess() */
MessageBox(NULL, "Hello, world!", "From main!", MB_OK);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment