8

我正在尝试使用 c# 发送 IP 数据包。

    destAddress = IPAddress.Parse("192.168.0.198"),
    destPort = 80;

    // Create a raw socket to send this packet
    rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

    // Bind the socket to the interface specified
    IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.140"),0);
    rawSocket.Bind(iep);

    // Set the HeaderIncluded option since we include the IP header
    rawSocket.SetSocketOption( socketLevel, SocketOptionName.HeaderIncluded, 1 );

    // Send the packet!
    int rc = rawSocket.SendTo(builtPacket, new IPEndPoint(destAddress, destPort));
    Console.WriteLine("sent {0} bytes to {1}", rc, destAddress.ToString());

builtPacket 的内容如下所示。它是一个包含 TCP SYN 数据包的 IP 数据包(这就是我认为我创建的)。

45 00 00 28 00 00 00 00 02 06 36 6E C0 A8 00 8C

C0 A8 00 C6 14 1E 00 50 00 00 00 00 00 00 00 00

05 02 FF FF E6 4F 00 00

输出是:

sent 40 bytes to 192.168.0.198

问题是我在 Wireshark 跟踪中看不到任何内容。就像数据在堆栈中的距离不够远,Wireshark 无法看到它?如果我使用浏览器连接到 192.168.0.198,Wireshark 会显示所有数据包,但当我尝试使用上述代码和数据发送数据包时,什么也不显示。

我的配置:

  • 我以管理员身份运行,所以这不是权限问题。

  • Windows7(不在虚拟机中运行)

  • Wireless connection only (IP config reports its IP as 192.168.0.140)

What am I doing wrong?

I'm sure Occam's Razor applies here, but I've been looking at this for hours and can't figure out what's wrong.

4

2 回答 2

3

This question, backed up by MSDN, claims that Windows no longer (XP SP 2 through 7) allows transmission of TCP data using raw sockets.

于 2012-05-11T10:50:31.720 回答
0

My guess is that either Wireshark is not looking at the right network interface, or that the destination ip address somehow resolves to the local machine, in which case it will routed inside of the OS and be invisible to the 'Shark.

于 2012-05-11T10:19:25.300 回答