gpt4 book ai didi

C++ 套接字 : Enabling Promiscuous Mode in Windows

转载 作者:可可西里 更新时间:2023-11-01 11:43:01 34 4
gpt4 key购买 nike

我正在尝试修改我当前的套接字程序以捕获混杂的数据包。我目前所拥有的是能够正常抓包的。我已经看到关于如何在 Linux 上执行此操作的其他答案,但我需要找到一种在 Windows 上完成此操作的方法。

这是我的代码:

int main(int argc, char const *argv[])
{
SOCKET s; //The bound socket
struct sockaddr_in server;
int recv_len; //Size of received data
char udpbuf[BUFLEN]; //A buffer for the incoming data.
float data; //The data in the packet

//Create a socket
if ((s = socket(AF_INET, SOCK_RAW, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
}
printf("Socket created.\n");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(ADDR);
server.sin_port = htons(PORT);

//Bind socket to address
if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done\n");

while (true)
{
//Block statment. Code will wait until it detect packets.
if ((recv_len = recvfrom(s, udpbuf, BUFLEN, 0, 0, 0)) == SOCKET_ERROR)
{
printf("recvfrom() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}

return 0;
}

最佳答案

要在 Windows 上将套接字置于混杂模式,您需要调用 WSAIoCtl()发出SIO_RCVALL control code到 socket 。

int main(int argc, char const *argv[])
{
WSADATA wsa;
SOCKET s; //The bound socket
struct sockaddr_in server;
int recv_len; //Size of received data
char udpbuf[BUFLEN]; //A buffer for the incoming data.

//Initialize Winsock
int err = WSAStartup(MAKEWORD(2,0), &wsa);
if (err != 0)
{
printf("Could not initialize Winsock : %d", err);
exit(EXIT_FAILURE);
}

//Create a socket
if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_IP)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}
printf("Socket created.\n");

//Prepare the sockaddr_in structure
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(ADDR);
server.sin_port = htons(PORT);

//Bind socket to address
if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
closesocket(s);
WSACleanup();
exit(EXIT_FAILURE);
}
puts("Bind done\n");

// enable promiscuous mode
DWORD dwValue = RCVALL_ON;
DWORD dwBytesReturned = 0;
if (WSAIoctl(s, SIO_RCVALL, &dwValue, sizeof(dwValue), NULL, 0, &dwBytesReturned, NULL, NULL) == SOCKET_ERROR)
{
printf("Ioctl failed with error code : %d", WSAGetLastError());
closesocket(s);
WSACleanup();
exit(EXIT_FAILURE);
}
puts("Ioctl done\n");

while (true)
{
//Block statment. Code will wait until it detect packets.
if ((recv_len = recvfrom(s, udpbuf, BUFLEN, 0, 0, 0)) == SOCKET_ERROR)
{
printf("recvfrom() failed with error code : %d", WSAGetLastError());
closesocket(s);
WSACleanup();
exit(EXIT_FAILURE);
}
}

closesocket(s);
WSACleanup();

return 0;
}

或者,Microsoft 还有一个单独的 Network Monitor API允许您在根本不使用 Winsock 的情况下监视和捕获网络流量。

关于C++ 套接字 : Enabling Promiscuous Mode in Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53160861/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com