Skip to content

Instantly share code, notes, and snippets.

@LuaxY
Created July 25, 2014 19:00
Show Gist options
  • Save LuaxY/93a95f85cbe930a667a4 to your computer and use it in GitHub Desktop.
Save LuaxY/93a95f85cbe930a667a4 to your computer and use it in GitHub Desktop.
Hook connect function with MiniHook
#include <Windows.h>
#include "MinHook.h"
#if defined _M_X64
#pragma comment(lib, "libMinHook.x64.lib")
#elif defined _M_IX86
#pragma comment(lib, "libMinHook.x86.lib")
#endif
#pragma comment(lib, "Ws2_32.lib")
typedef int (WINAPI *CONNECT)(SOCKET, const struct sockaddr*, int);
// futur pointeur sur la vraie fonction
CONNECT realConnect = NULL;
// notre fonction détourné
int WINAPI fakeConnect(SOCKET s, const struct sockaddr* name, int len)
{
// on récupère l'ip de connexion
char* ip;
ip = inet_ntoa(((struct sockaddr_in*)name)->sin_addr);
// comparaison de l'ip de connexion avec l'ip officel (remplacez 0.0.0.0)
if (strcmp(ip, "0.0.0.0") == 0)
{
struct sockaddr_in sa;
// l'ip de connexion est bien celle que l'on souhaite modifier
// on la remplace donc dans la structure sockaddr_in
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
sa.sin_family = AF_INET;
sa.sin_port = htons(1337);
// puis on appel la vraie fonction connect
return realConnect(s, (sockaddr*)&sa, sizeof(sa));
}
// et dans le cas contraire, on fait simplement suivre la connexion
return realConnect(s, name, len);
}
BOOL WINAPI DllMain(HINSTANCE, DWORD dwReason, LPVOID)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH: // lors du chargement de la DLL
MH_Initialize(); // on initialise MiniHook
// création du détournement de la fonction ici, avec la fonction original et la nouvelle
MH_CreateHook(&connect, &fakeConnect, reinterpret_cast<void**>(&realConnect));
// puis en l'active
MH_EnableHook(&connect);
break;
case DLL_PROCESS_DETACH: // lors du déchargement de la DLL
MH_DisableHook(&connect); // on désactive le détournement
MH_Uninitialize(); // et on décharge proprement MiniHook
break;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment