Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: 85 en 16 Marzo 2013, 12:42 pm



Título: TLS: CallBacks
Publicado por: 85 en 16 Marzo 2013, 12:42 pm
Hola, estaba mirando una nota sobre técnicas anti-debugging y dí contra una información acerca de TLS (Thread Local Storage), referido a callbacks.
Las CallBacks son funciones que se llaman automáticamente al ocurrir cierto evento, la nota decía esto:

Citar

/*
(1) TLS-callback

This anti-debug was not so well-known a few years ago. It consists to instruct the
PE loader that the first entry point of the program is referenced in a Thread Local Storage entry
(10th directory entry number in the PE optional header). By doing so, the program entry-point
won't be executed first. The TLS entry can then perform anti-debug checks in a stealthy way.
Note that in practice, this technique is not widely used.
Though older debuggers (including OllyDbg) are not TLS-aware, counter-measures are quite
easy to take, by the means of plugins of custom patcher tools.
*/


Básicamente, dice que se puede hacer que se ejecute primero un callback TLS antes que el ENTRYPOINT, con lo cual se puede ejecutar código antes que el programa mismo.

La información al respecto:
http://en.wikipedia.org/wiki/Thread-local_storage
http://en.wikipedia.org/wiki/Thread-local_storage#Windows_implementation
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686749(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/6yh4a9k1.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686991(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686997(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682594(v=vs.85).aspx
http://www.chromium.org/nativeclient/design-documents/thread-local-storage-tls-implementation

El código funcional al respecto de esta técnica, hecho por un programador de nombre Benina. Yo sólamente hice las pruebas y me pareció interesante exponerlo por estos lados, a alguien le puede servir XD.

Código
  1.  
  2. //
  3. // Autor: Benina (REA Team 2010)
  4. // Publicado Por: 85
  5. // Para: elhacker.net , etalking.com.ar
  6. // 2013
  7. //
  8.  
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10.  
  11. #include <windows.h>
  12.  
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14.  
  15. typedef struct _IMAGE_TLS_DIRECTORY32_ {
  16. DWORD   StartAddressOfRawData;
  17. DWORD   EndAddressOfRawData;
  18. DWORD   AddressOfIndex;
  19. DWORD   AddressOfCallBacks;
  20. DWORD   SizeOfZeroFill;
  21. DWORD   Characteristics;
  22. } IMAGE_TLS_DIRECTORY32_, * PIMAGE_TLS_DIRECTORY32_;
  23.  
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  25.  
  26. VOID NTAPI on_tls_callback(PVOID handle, DWORD reason, PVOID resv);
  27.  
  28. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29.  
  30. #pragma comment(linker, "/INCLUDE:__tls_used")
  31.  
  32. extern "C" {
  33.  
  34. #pragma data_seg(".tls")
  35. __declspec(allocate(".tls"))
  36. DWORD __tls_start = 0x0;
  37. #pragma data_seg(".tls")
  38. __declspec(allocate(".tls"))
  39. DWORD __tls_end = 0x0;
  40. #pragma data_seg(".tls")
  41. __declspec(allocate(".tls"))
  42. DWORD __tls_index = 0x0;
  43.  
  44. #pragma data_seg(".tls")
  45. __declspec(allocate(".tls"))
  46. DWORD __tls_func = (DWORD) on_tls_callback;
  47.  
  48. #pragma data_seg(".tls")
  49. __declspec(allocate(".tls"))
  50. DWORD __tls_size = 0x0;
  51. #pragma data_seg(".tls")
  52. __declspec(allocate(".tls"))
  53. DWORD __tls_flag = 0x0;
  54. }
  55.  
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57.  
  58. #pragma data_seg(".rdata$T")
  59. __declspec(allocate(".rdata$T"))
  60. extern "C" const _IMAGE_TLS_DIRECTORY32_ _tls_used =
  61. {
  62.    (DWORD) &__tls_start,
  63. (DWORD) &__tls_end,
  64. (DWORD) &__tls_index,
  65. (DWORD ) &__tls_func,
  66. (DWORD ) 0,
  67. (DWORD ) 0
  68. };
  69.  
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71.  
  72. VOID NTAPI on_tls_callback(PVOID handle, DWORD reason, PVOID resv)
  73. {
  74. // Run code here
  75. switch (reason )
  76. {
  77. case  DLL_PROCESS_ATTACH:
  78. {
  79. MessageBox(NULL, TEXT("Thread attach!"), TEXT("TLS Callback"), 0);
  80. break;
  81. }
  82. case DLL_PROCESS_DETACH:
  83. {
  84. MessageBox(NULL, TEXT("Thread detach!"), TEXT("TLS Callback"), 0);
  85. break;
  86. }
  87. default:
  88. break;
  89. };
  90. }
  91.  
  92. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  93.  
  94. int main()
  95. {
  96. // TODO: Place code here.
  97. MessageBox(NULL, TEXT("Hello, Main Program!"), TEXT("Main Proc"), 0);
  98.  
  99. return 0;
  100. }
  101.  
  102. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  103.  
  104.  
  105.  
  106.  

(http://img51.imageshack.us/img51/2638/tls11.png) (http://imageshack.us/photo/my-images/51/tls11.png/)

Proyecto: http://www.mediafire.com/?tcbtpoebhd8j4z7

Hasta luego