title: Hooking Send () recv()
body: <p>Ok i been playing around with some hooking again. I recently moved from studying php to c/c++ for several months now .
  so i decided to do something about Hooking looking up some facts from msdn and some other things on here .</p>
  
  <p>I try to inject this DLL into Firefox, when i do, i do not see it open the hookFile.txt and save the buffer to the .txt file, am i getting something wrong ?
  my source code goes like this</p>
  
  <p>"DLLHookSendRecv.h"</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  
  void ApiHook(LPSTR Module,LPCSTR OldFunc,LPVOID NewFunc,unsigned char *backup);
  extern "C" _declspec(dllexport) void newSend(SOCKET s, const char* buf, int len, int flags);
  extern "C" _declspec(dllexport) void newRecv(SOCKET s, const char* buf, int len, int flags);
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;Winsock.h&gt;
  #include "DLLHookSendRecv.h"
  
  #pragma comment (lib,"ws2_32")
  
  typedef int (*WINAPI oldsend)(SOCKET s,const char* buf,int len,int flags);
  typedef int (*WINAPI oldrecv)(SOCKET s,const char* buf,int len,int flags);
  
  BYTE hook[6];
  
  void ApiHook(LPSTR Module,LPCSTR OldFunc,LPVOID NewFunc,unsigned char *backup)
  {
    DWORD dwProtect;
    HINSTANCE hLib = LoadLibraryA(Module);
    DWORD OldFuncAddr = (DWORD)GetProcAddress(hLib,OldFunc);
    DWORD NewFuncAddr = (DWORD)NewFunc;
  
    BYTE jmp[6] ={0xE9,0x00,0x00,0x00,0x00,0xC3};
    DWORD  jmpAddr = (NewFuncAddr - OldFuncAddr) -5;
    memcpy(&amp;jmp[1],&amp;jmpAddr,4);
  
    VirtualProtect((LPVOID) OldFuncAddr,6,PAGE_EXECUTE_READWRITE,&amp;dwProtect);
    WriteProcessMemory(GetCurrentProcess(),(LPVOID)OldFuncAddr,jmp,6,0);
    VirtualProtect((LPVOID)OldFuncAddr,6,dwProtect,&amp;dwProtect);
  }
  
  extern "C" _declspec(dllexport) void newSend(SOCKET s, const char* buf, int len, int flags)
  {
     FILE* logFile;
     logFile = fopen("hookFile.txt","w");
     fprintf(logFile,"%s",buf);
     fclose(logFile);
    return send(s,buf,len,flags);
  }
  
  extern "C" _declspec(dllexport) void newRecv(SOCKET s, const char* buf, int len, int flags)
  {
     FILE* logFile;
     logFile = fopen("hookFile.txt","w");
     fprintf(logFile,buf);
     fclose(logFile);
    return recv(s,buf,len,flags);
  }
  
  BOOL APIENTRY DllMain(HINSTANCE hInstDLL,DWORD ul_reason_for_call,LPVOID lpReserved)
  {
    switch(ul_reason_for_call)
    {
      case DLL_PROCESS_ATTACH:
        ApiHook("ws2_32.dll","send",newSend,hook);
        ApiHook("ws2_32.dll","recv",newRecv,hook);
      break;
      case DLL_PROCESS_DETACH:
      break;
      case DLL_THREAD_ATTACH:
        ApiHook("ws2_32.dll","send",newSend,hook);
        ApiHook("ws2_32.dll","recv",newRecv,hook);
      break;
      case DLL_THREAD_DETACH:
          break;
    }
      return TRUE;
  }
  </code></pre>