elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


  Mostrar Temas
Páginas: [1]
1  Programación / ASM / Ayuda con hooks en: 24 Diciembre 2013, 05:48 am
Hola, realmente no se nada de ASM, en c++ hice unos hooks buscando en internet y apoyandome con detours, en windows mi hook funciona a la perfeccion, pero resulta que quiero hacer uno para linux, y con una funcion que encontre, segun lo que lei, esta ingresa un jmp en la funcion original para hacer la llamada a mi funcion, eso funciona hasta ahi, lo que pasa es que yo quiero que al finalizar mi funcion, esta continue en la original, un esquema sería algo así:

Código
  1. void funcion_original()
  2. {
  3.    hook_funcion(); // Esperar a que termine
  4.    // continuamos
  5. }

El código que tengo es el siguiente, el que incluye mi hook hecho en windows con detours, por si acaso tienen duda, el hook lo hago a una función que está en un ejecutable del cual no tengo el código fuente( samp-server.exe ), ahí viendo el código ustedes se darán cuenta de lo que intento hacer..

Código
  1. #include "main.h"
  2.  
  3.  
  4. DWORD OnQUERYCallBack;
  5.  
  6. typedef pair<string, int> packet;
  7. map<string, int> packetsLog;
  8.  
  9. map<string, int> bannedIPs;
  10.  
  11.  
  12. #ifdef __linux__
  13. void HookFunction ( BYTE *origen, BYTE *destino )
  14. {
  15. mprotect((void*)(((int)origen / 4096) * 4096), 4096, PROT_WRITE | PROT_READ | PROT_EXEC);
  16.        *( DWORD* )( origen ) = 0xE9;
  17.        *( DWORD* )(origen + 0x01 ) = destino - ( origen+ 5 );
  18. return;
  19. }
  20. #endif
  21.  
  22. void BanIP(const char *host)
  23. {
  24. char Regla[255];
  25. #ifdef _WIN32
  26. sprintf(Regla, "netsh advfirewall firewall add rule name=\"SA-MP Ban - %s\" dir=in action=block remoteip=%s enable=yes", host, host);
  27. #else
  28. sprintf(Regla, "iptables -A INPUT -s %s -j DROP", host);
  29. #endif
  30. system(Regla);
  31. }
  32.  
  33.  
  34. int OnSAMPQuery(struct in_addr in, u_short host, char *buffer, int len, SOCKET s)
  35. {
  36. if(bannedIPs.find(inet_ntoa(in)) != bannedIPs.end()) // for prevent add multiple rules
  37. {
  38. return 0;
  39. }
  40.  
  41. map<string, int>::iterator iter = packetsLog.find(inet_ntoa(in));
  42. if(iter == packetsLog.end())
  43. {
  44. packetsLog.insert(packet(inet_ntoa(in), 1));
  45. }
  46. else
  47. {
  48. if(iter->second >= 350)
  49. {
  50. logprintf("[FIREWALL] %s was banned - reason: query flood", iter->first.c_str());
  51. bannedIPs.insert(packet(iter->first.c_str(), iter->second));
  52. BanIP(iter->first.c_str());
  53. }
  54. iter->second++;
  55. }
  56.  
  57. #ifdef _WIN32
  58. return OnSAMPQuery_O(in, host, buffer, len, s);
  59. #else
  60. // Aquí que pongo ??
  61. #endif
  62. }
  63. #ifdef _WIN32
  64. void LimpiarDatos(void *arg)
  65. #else
  66. void *LimpiarDatos(void *arg)
  67. #endif
  68. {
  69. while(1)
  70. {
  71. #ifdef _WIN32
  72. Sleep(6000);
  73. #else
  74. sleep(6);
  75. #endif
  76. packetsLog.clear();
  77. bannedIPs.clear();
  78.  
  79. }
  80. }
  81. PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
  82. {
  83.    return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
  84. }
  85.  
  86.  
  87. PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
  88. {
  89. #ifdef _WIN32
  90. _beginthread(LimpiarDatos, 0, 0);
  91. #else
  92. pthread_t thread1;
  93. pthread_create(&thread1, NULL, LimpiarDatos, NULL);
  94. #endif
  95.  
  96. pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
  97.  
  98. logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
  99. DWORD version = (DWORD)ppData[PLUGIN_DATA_LOGPRINTF];
  100.  
  101.  
  102. if(version == SAMP_03x)
  103. {
  104. logprintf("  - Server version: SA-MP 0.3x");
  105. OnQUERYCallBack = ADDR_03x;
  106. }
  107. else if(version == SAMP_03xR12)
  108. {
  109. logprintf("  - Server version: SA-MP 0.3x R1-2");
  110. OnQUERYCallBack = ADDR_03xR12;
  111. }
  112. else if(version == SAMP_03xR2)
  113. {
  114. logprintf("  - Server version: SA-MP 0.3x R2");
  115. OnQUERYCallBack = ADDR_03xR2;
  116. }
  117. else
  118. {
  119. logprintf("  - Your version of SA-MP is not supported.");
  120. return true;
  121. }
  122.  
  123.  
  124.  
  125. #ifdef _WIN32
  126. OnSAMPQuery_O = (onsampquery_t)DetourFunction((PBYTE)OnQUERYCallBack, (PBYTE)OnSAMPQuery);
  127. #else
  128. HookFunction((PBYTE)OnQUERYCallBack,(PBYTE)OnSAMPQuery);
  129. #endif
  130.  
  131. logprintf("  - Anti Query flood by Josstaa 1.1 loaded \n");
  132.    return true;
  133. }
  134.  
  135.  
  136. PLUGIN_EXPORT void PLUGIN_CALL Unload()
  137. {
  138.    logprintf("  - Anti Query flood by Josstaa 1.1 unloaded");
  139. }
  140.  
  141. AMX_NATIVE_INFO PluginNatives[] =
  142. {
  143.    {0, 0}
  144. };
  145.  
  146. PLUGIN_EXPORT int PLUGIN_CALL AmxLoad( AMX *amx )
  147. {
  148.    return amx_Register(amx, PluginNatives, -1);
  149. }
  150.  
  151.  
  152. PLUGIN_EXPORT int PLUGIN_CALL AmxUnload( AMX *amx )
  153. {
  154.    return AMX_ERR_NONE;
  155. }
  156.  
  157.  

Por cierto, las direcciones las saque con IDA Pro, aquí están
Código
  1. #ifdef _WIN32
  2.  
  3. #include <windows.h>
  4. #include <process.h>
  5.  
  6.  
  7. #pragma comment(lib, "detours/detours.lib")
  8. #include "detours/detours.h"
  9. #define SAMP_03x 0x487DD0
  10. #define SAMP_03xR12 0x488060
  11. #define SAMP_03xR2 0x488140
  12.  
  13. #define ADDR_03x 0x490850
  14. #define ADDR_03xR12 0x490B10
  15. #define ADDR_03xR2 0x490C30
  16. #else
  17. #define SAMP_03x 0x80B0410
  18. #define SAMP_03xR12 0x80B07C0
  19. #define SAMP_03xR2 0x80B0840
  20.  
  21. #define ADDR_03x 0x80C6EF0
  22. #define ADDR_03xR12 0x80C72B0
  23. #define ADDR_03xR2 0x80C73A0
  24.  
  25. typedef int SOCKET;
  26. typedef unsigned long DWORD;
  27. typedef unsigned char BYTE;
  28. typedef BYTE * PBYTE;
  29.  
  30. #include <unistd.h>
  31. #include <sys/socket.h>
  32. #include <netinet/in.h>
  33. #include <arpa/inet.h>
  34. #include <sys/mman.h>
  35. #include <string.h>
  36. #include <pthread.h>
  37.  
  38. #endif
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines