是否可以从 C# 中的选定进程读取传出数据包?如果是,我应该使用什么 api? 提前致谢。
最佳答案
我假设您正在尝试执行类似于 WireShark 或 Winsock Packet Editor 的操作。
简短的回答是否。绝对没有具有内置功能的命名空间或程序集。
长的答案是是的,但你必须亲自动手。你很可能必须制作一个 C++ DLL 来注入(inject)“监视”它的过程。但是,您可以通过 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 导入。
查看 Inside the Windows PE Format (Part 2)和 C++ Code Example .
完成所有这些后,您必须将制作的 DLL 注入(inject)到目标进程中。这是用于执行此操作的 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# 监控程序由您决定。您可以使用一些进程间通信,例如Named Pipes , NamedPipeClientStream
在 C# 中。
但是,这样做就可以了,而且美妙的部分是它几乎适用于任何程序。同样的技术可以应用于任何类型的嗅探,而不仅仅是 Winsock。
关于c# - 从选定进程读取传出数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14309393/