2014-06-20 35 views
0

我打了一些弯路,功能挂钩,我有下面的代码一个奇怪的问题发送():功能仅挂接钩子的recv()和不使用迂回

基本上是,无论发生了什么DetourTransactionCommit()是成功的,但实际上只有recv()函数被挂钩,而发送不是,因为OutputDebugStringA(“Sent packet!”);

从未触发

#include "stdafx.h" 
#include "stdio.h" 
#include "WinInet.h" 
#include "tchar.h" 
#include "windows.h" 
#include "detours.h" 
#include <Winsock2.h> 
#include <WS2tcpip.h> 
#include <crtdbg.h> 

#pragma comment(lib, "detours.lib") 
#pragma comment(lib, "WinInet.lib") 
#pragma comment(lib, "ws2_32.lib") 

int (WINAPI *pSend)(SOCKET s, const char* buf, int len, int flags) = send; 
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags); 

int (WINAPI *pRecv)(SOCKET s, char* buf, int len, int flags) = recv; 
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags); 


BOOL APIENTRY DllMain(HMODULE hModule, 
         DWORD ul_reason_for_call, 
         LPVOID lpReserved 
        ) 
{ 
    LONG errore; 

    switch (ul_reason_for_call) 
    { 
    case DLL_PROCESS_ATTACH: 

     DetourTransactionBegin(); 
     DetourUpdateThread(GetCurrentThread()); 
     DetourAttach(&(PVOID&)pSend, MySend); 
     if (DetourTransactionCommit() == NO_ERROR) { 
      OutputDebugStringA("Send function hooked successfully"); 
     } 
     else{ 
      OutputDebugStringA("Failed to hook Send function"); 
     } 

     DetourTransactionBegin(); 
     DetourUpdateThread(GetCurrentThread()); 
     DetourAttach(&(PVOID&)pRecv, MyRecv); 
     if (DetourTransactionCommit() == NO_ERROR) { 
      OutputDebugStringA("Recv function hooked successfully"); 
     } 
     else{ 
      OutputDebugStringA("Failed to hook Recv function"); 
     } 

    case DLL_THREAD_ATTACH: 
    case DLL_THREAD_DETACH: 
    case DLL_PROCESS_DETACH: 
     break; 
    } 

    return TRUE; 
} 

int WINAPI MySend(SOCKET s, const char* buf, int len, int flags) { 
    OutputDebugStringA("Sent packet!"); 
    return pSend(s, buf, len, flags); 
} 

int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags) { 
    OutputDebugStringA("Received packet!"); 
    return pRecv(s, buf, len, flags); 
} 

UPDATE: Appearently与功能问题是关系到过程中,我试图注入DLL到。 它看起来像试图挂钩发送()在Internet Explorer 11 x86失败的原因,我仍然要弄清楚。 我尝试使用winsock2(putty)将完全相同的DLL注入到另一个程序中,并且该函数正确连接。

也许有人知道发生这种情况的原因?

+0

也许钩从未运行,因为'发送()'不会被调用?还有'WSASend','WSASendMsg' ......多种写入套接字的方式。 –

+0

不管'WSASend *'函数是否在引擎盖下调用'send'? –

+0

很高兴看到你的绕行功能。 –

回答

0

正如Ben Voigt指出的那样,显然send()并不是在Internet Explorer 11中调用的。 我试过用钩子WSASend()代替它,它工作。

编辑的工作snipplet如下:

#include "stdafx.h" 
    #include "stdio.h" 
    #include "WinInet.h" 
    #include "tchar.h" 
    #include "windows.h" 
    #include "detours.h" 
    #include <Winsock2.h> 
    #include <WS2tcpip.h> 
    #include <crtdbg.h> 

    #pragma comment(lib, "detours.lib") 
    #pragma comment(lib, "WinInet.lib") 
    #pragma comment(lib, "ws2_32.lib") 

int(WINAPI *pWSASend) (
    SOCKET s, 
    LPWSABUF lpBuffers, 
    DWORD dwBufferCount, 
    LPDWORD lpNumberOfBytesSent, 
    DWORD dwFlags, 
    LPWSAOVERLAPPED lpOverlapped, 
    LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine) = WSASend; 

int WINAPI MyWSASend(
    SOCKET s, 
    LPWSABUF lpBuffers, 
    DWORD dwBufferCount, 
    LPDWORD lpNumberOfBytesSent, 
    DWORD dwFlags, 
    LPWSAOVERLAPPED lpOverlapped, 
    LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine); 


int (WINAPI *pRecv)(SOCKET s, char* buf, int len, int flags) = recv; 
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags); 


BOOL APIENTRY DllMain(HMODULE hModule, 
         DWORD ul_reason_for_call, 
         LPVOID lpReserved 
        ) 
{ 
    LONG errore; 

    switch (ul_reason_for_call) 
    { 
    case DLL_PROCESS_ATTACH: 

     DetourTransactionBegin(); 
     DetourUpdateThread(GetCurrentThread()); 
     DetourAttach(&(PVOID&)pWSASend, MyWSASend); 
     if (DetourTransactionCommit() == NO_ERROR) { 
      OutputDebugStringA("Send function hooked successfully"); 
     } 
     else{ 
      OutputDebugStringA("Failed to hook Send function"); 
     } 

     DetourTransactionBegin(); 
     DetourUpdateThread(GetCurrentThread()); 
     DetourAttach(&(PVOID&)pRecv, MyRecv); 
     if (DetourTransactionCommit() == NO_ERROR) { 
      OutputDebugStringA("Recv function hooked successfully"); 
     } 
     else{ 
      OutputDebugStringA("Failed to hook Recv function"); 
     } 

    case DLL_THREAD_ATTACH: 
    case DLL_THREAD_DETACH: 
    case DLL_PROCESS_DETACH: 
     break; 
    } 

    return TRUE; 
} 

int WINAPI MyWSASend(
    SOCKET s, 
    LPWSABUF lpBuffers, 
    DWORD dwBufferCount, 
    LPDWORD lpNumberOfBytesSent, 
    DWORD dwFlags, 
    LPWSAOVERLAPPED lpOverlapped, 
    LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine) 
{ 
    OutputDebugStringA("Packet Sent!"); 
    return (pWSASend)(s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags, lpOverlapped, lpCompletionRoutine); 
}; 
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags) { 
    OutputDebugStringA("Received packet!"); 
    return pRecv(s, buf, len, flags); 
}