是否可以从 C# 中的选定进程读取传出数据包?如果是,我应该使用什么 api?提前致谢。
3 回答
我假设您正在尝试做类似于WireShark或Winsock Packet Editor的事情。
简短的回答是否定的。绝对没有内置功能的命名空间或程序集。
很长的答案是肯定的,但你必须弄脏你的手。您很可能不得不制作一个C++ DLL注入到进程中以“窥探”它。但是,您可以通过C#接口此DLL并使您的接口全部在.NET中。
您的第一步是创建C++ DLL,它只需要一些导出:
bool InitialzeHook()
{
// TODO: Patch the Import Address Table (IAT) to overwrite
// the address of Winsock's send/recv functions
// with your SpySend/SpyRecv ones instead.
}
bool UninitializeHook()
{
// TODO: Restore the Import Address Table (IAT) to the way you found it.
}
// This function will be called instead of Winsock's recv function once hooked.
int SpySend(SOCKET s, const char *buf, int len, int flags)
{
// TODO: Do something with the data to be sent, like logging it.
// Call the real Winsock send function.
int numberOfBytesSent = send(s, buf, len, flags);
// Return back to the calling process.
return numberOfBytesSent;
}
// This function will be called instead of Winsock's recv function once hooked.
int SpyRecv(SOCKET s, char *buf, int len, int flags)
{
// Call the real Winsock recv function to get the data.
int numberOfBytesReceived = recv(s, buf, len, flags);
// TODO: Do something with the received data, like logging it.
// Return back to the calling process.
return numberOfBytesReceived;
}
这一切中最困难的部分是修补导入地址表(IAT)的功能。关于如何遍历它并在其中查找函数导入有各种资源。提示:您必须按序号而不是名称来修补Winsock导入。
查看深入了解 Windows PE 格式(第 2 部分)和C++ 代码示例。
完成所有这些后,您必须将您制作的DLL注入目标进程。这是执行此操作的C++伪代码(在我脑海中):
// Get the target window handle (if you don't have the process ID handy).
HWND hWnd = FindWindowA(NULL, "Your Target Window Name");
// Get the process ID from the target window handle.
DWORD processId = 0;
DWORD threadId = GetWindowThreadProcessId(hWnd, &processId);
// Open the process for reading/writing memory.
DWORD accessFlags = PROCESS_VM_OPERATION |
PROCESS_VM_READ |
PROCESS_VM_WRITE |
PROCESS_QUERY_INFORMATION;
HANDLE hProcess = OpenProcess(accessFlags, false, processId);
// Get the base address for Kernel32.dll (always the same for each process).
HMODULE hKernel32 = GetModuleHandleA("kernel32");
// Get the address of LoadLibraryA (always the same for each process).
DWORD loadLibraryAddr = GetProcAddress(hKernel32, "LoadLibraryA");
// Allocate some space in the remote process and write the library string to it.
LPVOID libraryNameBuffer = VirtualAllocEx(hProcess, NULL, 256,
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE);
LPCSTR libraryName = L"MySpyLibrary.dll\0";
DWORD numberOfBytesWritten = 0;
BOOL writeResult = WriteProcessMemory(hProcess, libraryNameBuffer,
(LPCVOID)libraryName,
strlen(libraryName) + 1,
&numberOfBytesWritten);
// Create a thread in the remote process, using LoadLibraryA as the procedure,
// and the parameter is the library name we just wrote to the remote process.
DWORD remoteThreadId = 0;
HANDLE hRemoteThread = CreateRemoteThread(hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)loadLibraryAddr,
libraryNameBuffer,
0, &remotThreadId);
// Wait for our thread to complete and get the exit code (which is the return value).
DWORD loadedLibraryAddr = 0;
BOOL waitResult = WaitForSingleObject(hRemoteThread, INFINITE);
BOOL exitResult = GetExitCodeThread(hRemoteThread, &loadedLibraryAddr);
// TODO: Check that it was loaded properly
// if(lodadedLibraryAddr == NULL) { ... }
// Cleanup our loose ends here.
VirtualFreeEx(hProcess, libraryNameBuffer, 256, MEM_RELEASE);
CloseHandle(hRemoteThread);
CloseHandle(hProcess);
不过,您可以通过C# 平台调用(pInvoke)执行相同的操作。如何记录数据并将数据传输回C#监控程序取决于您。您可以在C#中使用一些进程间通信,例如命名管道。NamedPipeClientStream
但是,这会做到这一点,而且美妙的部分是它几乎适用于任何程序。同样的技术可以应用于任何类型的嗅探,而不仅仅是Winsock。
当然你可以......但前提是该进程对它的侦听器有一个“公共钩子”。否则,您将不得不创建一个嗅探器:调试可执行文件,找到套接字发送缓冲区的偏移量并将读取器连接到它。通过类似防火墙的应用程序更容易做到这一点。
您可以使用 TPL 数据流来做到这一点。