Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: 85 en 8 Abril 2013, 00:34 am



Título: MS Detours 2.1 para interceptar Opengl32
Publicado por: 85 en 8 Abril 2013, 00:34 am
http://research.microsoft.com/en-us/projects/detours/
Citar
Overview
Innovative systems research hinges on the ability to easily instrument and extend existing operating system and application functionality. With access to appropriate source code, it is often trivial to insert new instrumentation or extensions by rebuilding the OS or application. However, in today's world systems researchers seldom have access to all relevant source code.

Detours is a library for instrumenting arbitrary Win32 functions Windows-compatible processors. Detours intercepts Win32 functions by re-writing the in-memory code for target functions. The Detours package also contains utilities to attach arbitrary DLLs and data segments (called payloads) to any Win32 binary.

Detours preserves the un-instrumented target function (callable through a trampoline) as a subroutine for use by the instrumentation. Our trampoline design enables a large class of innovative extensions to existing binary software.

We have used Detours to create an automatic distributed partitioning system, to instrument and analyze the DCOM protocol stack, and to create a thunking layer for a COM-based OS API. Detours is used widely within Microsoft and within the industry.

Bueno, la intención no es contar toda la historia sino publicar un código fuente XD.

En síntesis, con esta librería se pueden interceptar funciones. Es una librería de MS que no es pública en su versión completa, pero si tiene una versión de prueba para el público.
Se usaba desde hace años para construir los hacks de distintos juegos, por ejemplo para el Counter-Strike.
En este enlace se puede encontrar una base para un hook de Opengl32 que fue muy conocida cuando salió.
http://board.cheat-project.com/showthread.php?t=9232

Algunas de estas bases se hicieron primero con Detours 1.5 y después se actualizaron a Detours 2.1. Ahora está disponible la versión pública de Detours 3.0.

Lo que hice yo, fue organizar la base publicada en ese enlace que he mostrado. originalmente no compilaba, tenía errores, faltaban archivos, y tenía mucho código que no era necesario para una simple demostración.
Por eso el código fue organizado y arreglado para que funcione, el proyecto es de una librería de vínculos dinámicos (DLL), que debe ser cargada en el juego Counter-Strike (En modo Opengl32).
Al cargarse la DLL se instalan los hooks ('detours'), y se obtiene un "Wallhack" funcionando.

El creador de esta base es un tal Xantrax, el link ya lo he mostrado.
Esto se trata de ver como se implementa esta librería.
Dejo el código completo y al final un enlace de descarga.
La descarga fue preparada especialmente, porque incluye los archivos de instalación de Detours 2.1, el .h y la librería estática .lib.
Y se incluye también el .h y la librería estática .lib correspondiente a Detours 1.5.
El tema es que estos archivos no se consiguen fácilmente porque son versiones anteriores de 'Detours'.
Otra cosa es que para obtener la librería estática .lib se tiene que compilar 'Detours' para que la produzca.

Código
  1.  
  2. //
  3. // Modifications: By 85
  4. // Credits: Xantrax, MS Detours 2.1
  5. // elhacker.net
  6. // etalking.com.ar
  7. // david_bs@live.com
  8. // 2013
  9. //
  10.  
  11. ///////////////////////////////////////////////////////////////////////////////////////
  12.  
  13. #pragma comment(lib,"detours.lib")// 1.5
  14. #pragma comment(lib,"OpenGL32.lib")
  15. #include <windows.h>
  16. #include <gl/gl.h>
  17. #include<stdio.h>
  18.  
  19. #include "detours.h"
  20.  
  21. ///////////////////////////////////////////////////////////////////////////////////////
  22.  
  23. bool bOGLSubtractive = false;
  24. bool g_bSky = false;
  25.  
  26. ///////////////////////////////////////////////////////////////////////////////////////
  27.  
  28. class CDraw {
  29. public:
  30. CDraw();
  31. int m_iVpCounter;
  32. bool m_bDrawing;
  33. void DrawPanel();
  34. };
  35.  
  36. CDraw::CDraw( ){
  37. m_iVpCounter = 0;
  38. m_bDrawing = false;
  39. }
  40.  
  41. void CDraw::DrawPanel( ) {
  42.  
  43. //Drawing Every 5th here
  44. }
  45.  
  46. CDraw* pDraw = new CDraw();
  47.  
  48. ///////////////////////////////////////////////////////////////////////////////////////
  49.  
  50. /* function prototype and original target */
  51.  
  52. DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glBegin( GLenum mode ), glBegin );
  53. DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glViewport( GLint x,  GLint y,  GLsizei width,  GLsizei height ), glViewport );
  54. DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glVertex3fv( const GLfloat *v ), glVertex3fv  );
  55. DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glVertex3f( GLfloat x, GLfloat y, GLfloat z ), glVertex3f );
  56. DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glShadeModel( GLenum mode ), glShadeModel );
  57. DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glClear( GLbitfield mask ), glClear );
  58. DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glColor3f( GLfloat red, GLfloat green, GLfloat blue ), glColor3f );
  59. typedef void (WINAPI* wglSwapBuffers_t)(HDC);
  60.  
  61. ///////////////////////////////////////////////////////////////////////////////////////
  62.  
  63. //#define HIWORD(l)           ((WORD)((DWORD_PTR)(l) >> 16))
  64.  
  65. DETOUR_TRAMPOLINE(FARPROC WINAPI Trampoline_GetProcAddress(HMODULE hModule,
  66.  LPCSTR lpProcName), GetProcAddress);
  67.  
  68. ///////////////////////////////////////////////////////////////////////////////////////
  69.  
  70. VOID WINAPI glBeginDetour(GLenum mode)
  71. {
  72. if(mode == GL_TRIANGLE_STRIP || mode == GL_TRIANGLE_FAN)
  73. glDepthRange( 0, 0.5 );
  74. else
  75. glDepthRange( 0.5, 1 );
  76.  
  77. if(mode == GL_TRIANGLE_STRIP || mode == GL_TRIANGLE_FAN)
  78. glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
  79.  
  80. if( mode == GL_QUADS )
  81. g_bSky = true;
  82. else
  83. g_bSky = false;
  84.  
  85. Trampoline_glBegin(mode);
  86. }
  87.  
  88. ///////////////////////////////////////////////////////////////////////////////////////
  89.  
  90. VOID WINAPI glClearDetour(GLbitfield mask)
  91. {
  92. if(mask==GL_DEPTH_BUFFER_BIT)
  93. {
  94. mask+=GL_COLOR_BUFFER_BIT;
  95. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  96.  
  97. Trampoline_glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
  98. return;
  99. }
  100.  
  101. Trampoline_glClear(mask);
  102. }
  103.  
  104. ///////////////////////////////////////////////////////////////////////////////////////
  105.  
  106. VOID WINAPI glShadeModelDetour(GLenum mode)
  107. {
  108. //XQZ2 wallhack
  109. if( mode == GL_SMOOTH ){
  110. __asm {
  111. push 0x00000100 //GL_DEPTH_BUFFER_BIT
  112. call dword ptr[ Trampoline_glClear ]
  113. }
  114. }
  115.  
  116. Trampoline_glShadeModel(mode);
  117. }
  118.  
  119. ///////////////////////////////////////////////////////////////////////////////////////
  120.  
  121. VOID WINAPI glViewportDetour(GLint x, GLint y, GLsizei width, GLsizei height)
  122. {
  123. pDraw->m_iVpCounter++;
  124. if( pDraw->m_iVpCounter >= 5 ){
  125. pDraw->m_bDrawing = true;
  126. }
  127. Trampoline_glViewport(x, y, width, height);
  128. }
  129.  
  130. ///////////////////////////////////////////////////////////////////////////////////////
  131.  
  132. VOID WINAPI glColor3fDetour(GLfloat red, GLfloat green, GLfloat blue)
  133. {
  134. Trampoline_glColor3f(red, green, blue);
  135. }
  136.  
  137. ///////////////////////////////////////////////////////////////////////////////////////
  138.  
  139. VOID WINAPI glVertex3fvDetour(const GLfloat* v)
  140. {
  141. if(g_bSky && v[2]>300){
  142.  
  143. return;
  144. /*float flZero = 0.0f;
  145. __asm {
  146. push flZero
  147. push flZero
  148. push flZero
  149. call dword ptr[ Trampoline_glColor3f ]
  150. }*/
  151. }
  152. Trampoline_glVertex3fv(v);
  153. }
  154.  
  155. ///////////////////////////////////////////////////////////////////////////////////////
  156.  
  157. VOID WINAPI glVertex3fDetour(GLfloat x, GLfloat y, GLfloat z)
  158. {
  159. Trampoline_glVertex3f(x, y, z);
  160. }
  161.  
  162. ///////////////////////////////////////////////////////////////////////////////////////
  163.  
  164. wglSwapBuffers_t pwglSwapBuffers = NULL;
  165. // DETOUR_TRAMPOLINE(void WINAPI _wglSwapBuffers(HDC hDC), wglSwapBuffers);
  166. // since wglSwapBuffers is undefined, the macro wont work. this is, what it basically does:
  167. static PVOID __fastcall _Detours_GetVA_wglSwapBuffers(VOID)
  168. {
  169.    return pwglSwapBuffers;
  170. }
  171.  
  172. ///////////////////////////////////////////////////////////////////////////////////////
  173.  
  174. __declspec(naked) void WINAPI _Trampoline_wglSwapBuffers(HDC hDC)
  175. {
  176.    __asm { nop };
  177.    __asm { nop };
  178.    __asm { call _Detours_GetVA_wglSwapBuffers };
  179.    __asm { jmp eax };
  180.    __asm { ret };
  181.    __asm { nop };
  182.    __asm { nop };
  183.    __asm { nop };
  184.    __asm { nop };
  185.    __asm { nop };
  186.    __asm { nop };
  187.    __asm { nop };
  188.    __asm { nop };
  189.    __asm { nop };
  190.    __asm { nop };
  191.    __asm { nop };
  192.    __asm { nop };
  193.    __asm { nop };
  194.    __asm { nop };
  195.    __asm { nop };
  196.    __asm { nop };
  197.    __asm { nop };
  198.    __asm { nop };
  199.    __asm { nop };
  200.    __asm { nop };
  201.    __asm { nop };
  202.    __asm { nop };
  203. }
  204.  
  205. ///////////////////////////////////////////////////////////////////////////////////////
  206.  
  207. VOID WINAPI _wglSwapBuffers(HDC hDC)
  208. {
  209. pDraw->m_iVpCounter = 0;
  210. _Trampoline_wglSwapBuffers( hDC );
  211. }
  212.  
  213. ///////////////////////////////////////////////////////////////////////////////////////
  214.  
  215. void DetourOpenGL()
  216. {
  217. #define Detour( pbTrampFunc, pbDetourFunc )\
  218. DetourFunctionWithTrampoline( (PBYTE)##pbTrampFunc,( PBYTE )##pbDetourFunc )
  219.  
  220. Detour( Trampoline_glBegin, glBeginDetour );
  221. Detour( Trampoline_glVertex3fv, glVertex3fvDetour );
  222. Detour( Trampoline_glClear, glClearDetour );
  223. /*
  224. Detour( Trampoline_glShadeModel, glShadeModelDetour );
  225. Detour( Trampoline_glViewport, glViewportDetour );
  226. Detour( Trampoline_glVertex3f, glVertex3fDetour );
  227.  
  228. // Because wglSwapBuffers isnt declared in GL\GL.h...
  229. pwglSwapBuffers = (wglSwapBuffers_t)GetProcAddress(LoadLibrary("opengl32.dll"),
  230. "wglSwapBuffers");
  231. Detour( _Trampoline_wglSwapBuffers, _wglSwapBuffers);
  232.  
  233. Detour( Trampoline_glColor3f, glColor3fDetour );*/
  234. }
  235.  
  236. ///////////////////////////////////////////////////////////////////////////////////////
  237.  
  238. void RemoveOpenGLDetour()
  239. {
  240. #define Destroy( pbTrampFunc, pbDetourFunc )\
  241. DetourRemove( (PBYTE)##pbTrampFunc,( PBYTE )##pbDetourFunc )
  242.  
  243. Destroy( Trampoline_glBegin, glBeginDetour );
  244. Destroy( Trampoline_glVertex3fv, glVertex3fvDetour );
  245. Destroy( Trampoline_glClear, glClearDetour );
  246. /*
  247. Destroy( Trampoline_glShadeModel, glShadeModelDetour );
  248. Destroy( Trampoline_glViewport, glViewportDetour );
  249. Destroy( Trampoline_glVertex3f, glVertex3fDetour );
  250.  
  251. Destroy( Trampoline_glColor3f, glColor3fDetour );*/
  252. }
  253.  
  254. ///////////////////////////////////////////////////////////////////////////////////////
  255.  
  256. FARPROC WINAPI GetProcAddressDetour(HMODULE hModule, LPCSTR lpProcName)
  257. {
  258. FARPROC retval = Trampoline_GetProcAddress(hModule, lpProcName);
  259. return retval;
  260. }
  261.  
  262. ///////////////////////////////////////////////////////////////////////////////////////
  263.  
  264. BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  265. {
  266. switch(ul_reason_for_call)
  267. {
  268. case DLL_PROCESS_ATTACH://Let the magic happen...:->
  269. DetourOpenGL();
  270. //Extras
  271. /* DetourFunctionWithTrampoline((PBYTE)Trampoline_GetProcAddress,
  272. (PBYTE)GetProcAddressDetour);*/
  273. break;
  274. case DLL_PROCESS_DETACH:
  275. RemoveOpenGLDetour();
  276. break;
  277. }
  278.    return TRUE;
  279. }
  280.  
  281. ///////////////////////////////////////////////////////////////////////////////////////
  282.  

PROJECT VCPP6
http://www.mediafire.com/?tl9qprjaytk77v7



Título: Re: MS Detours 2.1 para interceptar Opengl32
Publicado por: Luchoz95 en 8 Abril 2013, 01:01 am
Gracias me sirve de mucho !