Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C++;原始套接字嗅探器无法捕获Windows 10上传入的TCP数据包_C#_Python_C++_Sockets_Raw Sockets - Fatal编程技术网

C# C++;原始套接字嗅探器无法捕获Windows 10上传入的TCP数据包

C# C++;原始套接字嗅探器无法捕获Windows 10上传入的TCP数据包,c#,python,c++,sockets,raw-sockets,C#,Python,C++,Sockets,Raw Sockets,代码的核心部分如下所示。 实际上,代码是从 该代码可以捕获UDP传入/传出数据包和TCP传出数据包,但无法捕获Windows10上的任何TCP传入数据包。 然而,它在WindowsXP上运行得相当好 sniffer = socket(AF_INET, SOCK_RAW, IPPROTO_IP); if (sniffer == INVALID_SOCKET) { printf("Failed to create raw socket.\n"); return 1; } memse

代码的核心部分如下所示。 实际上,代码是从

该代码可以捕获UDP传入/传出数据包和TCP传出数据包,但无法捕获Windows10上的任何TCP传入数据包。 然而,它在WindowsXP上运行得相当好

sniffer = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
if (sniffer == INVALID_SOCKET) {
    printf("Failed to create raw socket.\n");
    return 1;
}

memset(&dest, 0, sizeof(dest));
memcpy(&dest.sin_addr.s_addr, local->h_addr_list[in], sizeof(dest.sin_addr.s_addr));
dest.sin_family = AF_INET;
dest.sin_port = 0;

printf("\nBinding socket to local system and port 0 ...");
if (bind(sniffer, (struct sockaddr *)&dest, sizeof(dest)) == SOCKET_ERROR) {
    printf("bind(%s) failed.\n", inet_ntoa(addr));
    return 1;
}
printf("Binding successful");
//Enable this socket with the power to sniff : SIO_RCVALL is the key Receive ALL ;)

j = 1;
printf("\nSetting socket to sniff...");
if (WSAIoctl(sniffer, SIO_RCVALL, &j, sizeof(j), 0, 0, (LPDWORD)&in, 0, 0) == SOCKET_ERROR) {
    printf("WSAIoctl() failed.\n");
    perror("Error:");
    return 1;
}
printf("Socket set.");

//Begin
printf("\nStarted Sniffing\n");
printf("Packet Capture Statistics...\n");
StartSniffing(sniffer); //Happy Sniffing

//End
closesocket(sniffer);
WSACleanup();

然后,我尝试用C语言编写,与C++代码的结果相同。p> 最后,我尝试了python中的原始套接字,它在Win10上运行得非常好

import socket
from struct import *

host = '10.0.0.18'
# create a raw socket and bind it to the public interface
socket_protocol = socket.IPPROTO_IP
sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
sniffer.bind((host, 0))
# we want the IP headers included in the capture

#sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

while True:
    packet = sniffer.recvfrom(65565)

    #packet string from tuple
    packet = packet[0]

    #take first 20 characters for the ip header
    ip_header = packet[0:20]

    #now unpack them :)
    iph = unpack('!BBHHHBBH4s4s' , ip_header)

    version_ihl = iph[0]
    version = version_ihl >> 4
    ihl = version_ihl & 0xF

    iph_length = ihl * 4
    total_len = iph[2]
    ttl = iph[5]
    protocol = iph[6]
    s_addr = socket.inet_ntoa(iph[8]);
    d_addr = socket.inet_ntoa(iph[9]);
    if protocol == 6:
         print 'Length : ' + str(total_len) + ' Source Address : ' + str(s_addr) + ' Destination Address : ' + str(d_addr)

很奇怪,我对C++代码有什么错误吗?< /p>愚蠢的问题——你试过用高架命令行吗?@ PSOLT是什么意思?不能在命令行中运行C++,对吗?请仔细检查问题。我是说编译代码后运行程序。请理解这些评论。您可以使用管理员权限运行它(使用提升的命令行或使用管理员权限运行VS),也可以不使用。嗯,我确实使用管理员权限运行过它。而Python代码可以工作,C++代码不工作,C不工作。这很奇怪。他们使用不同的API,所以一点也不奇怪。在我看来,系统或防火墙可能会阻止你的程序。愚蠢的问题-你试过在提升的命令行中运行它吗?@pSoLT你是什么意思?不能在命令行中运行C++,对吗?请仔细检查问题。我是说编译代码后运行程序。请理解这些评论。您可以使用管理员权限运行它(使用提升的命令行或使用管理员权限运行VS),也可以不使用。嗯,我确实使用管理员权限运行过它。而Python代码可以工作,C++代码不工作,C不工作。这很奇怪。他们使用不同的API,所以一点也不奇怪。在我看来,系统或防火墙可能会阻止您的程序。