我想编写一个“中间人”程序来帮助我调试正在开发的网络应用程序。经过一番Google搜寻后(我根本不知道该怎么做),我想到了必须创建一个钩子(Hook)(以前我用过其他语言的钩子(Hook),但是它们要简单得多),拦截WinSock的send
。根据不同的文章,我必须修改函数的前5个字节才能跳转到我的函数,做一些工作,然后跳回。我在网上找到this code,并将其重写在控制台应用程序中。这是我想出的:
#include "stdafx.h"
#include <iostream>
#include <WinSock2.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#define JMP(frm, to) (int)(((int)to - (int)frm) - 5);
#define DEF_PORT 27015
using namespace std;
bool HookSend();
void UnhookSend();
DWORD SendOriginal = 0;
DWORD SendReturn = 0;
DWORD *SendHookFunc = 0;
DWORD OldProtection = 0;
char* send_buffer;
int send_sizeofdata = 0;
SOCKET send_s;
int send_flags = 0;
HINSTANCE hWinSock = 0;
void __declspec(naked) __stdcall SendHook()
{
__asm
{
mov edi,edi
push ebp
mov ebp, esp
mov eax, [ebp+0x08] /* Param 1 : Socket */
mov send_s, eax
mov eax, [ebp+0x0C] /* Param 2 : buffer */
mov [send_buffer], eax
mov eax, [ebp+0x10] /*Param 3 : Size*/
mov send_sizeofdata, eax
mov eax, [ebp+0x14] /*Param 4 : flags*/
mov send_flags, eax
jmp SendReturn
}
}
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
SOCKET soket;
sockaddr_in soketDesc;
char buf[256];
cout << "Packet Reader by Timothy Volpe" << endl;
cout << "Program will read packets sent" << endl << endl;
cout << "Loading WinSock" << endl;
if((hWinSock = LoadLibrary("ws2_32.dll")) == NULL)
{
cout << "Failed to load WinSock library" << endl;
return -1;
}
cout << "Starting WinSock API" << endl;
if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
{
cout << "Failed to load WinSock API (Error:" << WSAGetLastError() << ")" << endl;
cout << "Press enter to exit" << endl;
cin.get();
return -1;
}
cout << "Hooking send()" << endl;
if(!HookSend())
{
cout << "Press enter to exit" << endl;
cin.get();
return -1;
}
cout << "Hooked successfully!" << endl;
cout << "Creating socket" << endl;
soket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(soket == INVALID_SOCKET)
{
cout << "Failed to create socket (Error:" << WSAGetLastError() << ")" << endl;
UnhookSend();
cout << "Press enter to exit" << endl;
cin.get();
return -1;
}
cout << "Connecting socket to port " << DEF_PORT << endl;
soketDesc.sin_family = AF_INET;
soketDesc.sin_addr.s_addr = inet_addr("127.0.0.1");
soketDesc.sin_port = htons(DEF_PORT);
if(connect(soket, (SOCKADDR*)&soketDesc, sizeof(soketDesc)) == SOCKET_ERROR)
{
cout << "Failed to connect socket (Error:" << WSAGetLastError() << ")" << endl;
UnhookSend();
cout << "Press enter to exit" << endl;
cin.get();
return -1;
}
cout << "Enter some data to send (MAX 256):" << endl;
cin.get(buf, 256); //Read 256 chars
cout << "Sending \"" << buf << "\"" << endl;
//Send the data
send(soket, buf, (int)sizeof(buf), 0);
//Close
closesocket(soket);
WSACleanup();
UnhookSend();
cout << endl << "Press enter to exit" << endl;
cin.clear();
cin.ignore();
cin.get();
return 0;
}
bool HookSend()
{
//Get the new function's address
SendHookFunc = (DWORD*)SendHook;
//Get the send() function's address
SendOriginal = (DWORD)GetProcAddress(hWinSock, "send");
//Get the return address, which is 5 bytes past the start
SendReturn = SendOriginal + 5;
printf("SEND\tStart: %x\t Return: %x\n", SendOriginal, SendReturn);
//Change protection for the first 5 bytes
VirtualProtect((void*)SendOriginal, 0x05, PAGE_READWRITE, &OldProtection);
*(BYTE*)(SendOriginal) = 0xe9;
*(int*)(SendOriginal+1) = JMP(SendOriginal, SendHookFunc);
return true;
}
void UnhookSend()
{
cout << "Unhooking..." << endl;
//Restore the old stuff
*(WORD*)SendOriginal = 0xFF8B;
*(BYTE*)(SendOriginal+1) = 0x55;
*(WORD*)(SendOriginal+3) = 0xEC8B;
//Restore protection
VirtualProtect((void*)SendOriginal, 0x05, OldProtection, &OldProtection);
}
但是,它不起作用,因为我在
socket()
上遇到访问冲突。如果我删除对HookSend
和UnhookSend
的调用,它将运行良好。如果我改变了调用顺序(例如将WSAStartup
放在HookSend
之后),它将在第一个WinSock函数上崩溃。所以我想我很喜欢破坏整个库或某些东西,我真的不太确定。老实说,我今天才学习汇编语言(嗯,这是Visual Studio的版本)。握起来非常容易,我相信我理解这段代码中的程序集。我的代码几乎与网上的代码相同,所以我不知道出了什么问题。我想知道这是操作系统冲突还是项目设置问题,还是完全不同的事情。我在这里很茫然,所以任何朝着正确方向指示的指针都很棒!另外,如果我要解决这个完全错误的问题(例如有更好的方法),请随时告诉我。而且我确实了解Microsoft's Detours,但是我不希望将它用于这种简单的事情(而且我也不喜欢非商业使用许可证的想法idea绕在我的头上,即使它可能永远不会发布) 。
最佳答案
很少需要以这种蛮力方式调试实际的API调用。您可以仅使用Wireshark之类的数据包嗅探器来捕获和分析正在传输的网络流量。但是,如果必须调试API调用,则实际上是在尝试手动实现Detour,因此,您实际上应该使用Microsoft的Detours库正确地实现真正的Detour。更好的选择是使用WinSock的内置调试功能,而不完全挂钩API函数。让操作系统为您处理 call 和参数的挂接和记录。