Winpcap简单问题-如何发送数据包到指定的ip/端口?

发布于 2024-08-02 10:47:37 字数 211 浏览 12 评论 0原文

我阅读了教程等,但我不明白。它确实可以让您发送数据包,但是您如何告诉 Winpcap 将这些数据包发送到哪里呢?我是否应该在数据包上添加任何标头,以便它知道将其转发到哪个 ip/端口?我是说。假设我想向我的 MSN 发送一些数据,就好像我给名单上的某个人写了一些东西一样。我可以使用 sendpacket(),但它只会将数据包/字节数组作为参数,而不指定发送到哪个应用程序/ip/端口。

谢谢

I read the tutorials and so, but I am not getting it. It does let you send packets, but how can you tell Winpcap where to send those packets? Is there any header I should put on the packets so it will know to which ip/port's to forward it? I mean. Let's imagine I want to send some data to my MSN, as if I had wrote something to someone on my list. I can use the sendpacket(), but it will only take the packet/byte array as argument, not specifing to which app/ip/port so send it.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硬不硬你别怂 2024-08-09 10:47:37

您不告诉 Winpcap 将数据包发送到哪里。你告诉它把一个数据包放在电线上。网络交换机会将数据包发送到正确的目的地。接收端的 TCP 堆栈会将数据包发送到正确的应用程序/服务。显然,这意味着路由信息必须位于数据包本身中。

以您的示例为例,您需要将相应 MSN 服务器的 IP 地址和 TCP 端口放入数据包中。如果不这样做,您的网络硬件将丢弃该数据包或错误路由该数据包。

You don't tell Winpcap where to send packets. You tell it to put a packet on the wire. The network switch will send the packet to the right destination. The TCP stack on the receiving end will send the packet to the right application/service. Obviously this means the routing information has to be in the packet itself.

To take your example, you'd need to put the IP address and TCP port of the appropriate MSN server in the packet. If you don't, your networking hardware will discard or misroute that packet.

哀由 2024-08-09 10:47:37

这就是我通过线路发送 ARP 请求的方式。
1. 定义协议的结构,即如果您想发送 ARP 数据包,您将需要一个包含数据链路层(以太网标头)和网络层(ARP 标头)的结构。相应地,如果您想通过 IP 发送 tcp 数据包,您将需要以太网标头、ip 标头和 tcp 标头的数据结构。

  1. 定义结构后,用您想要的值初始化结构的实例,即如果您希望数据包发送到网络中的所有计算机,请将以太网标头的目标 mac 值设置为 ff:ff:ff:ff :ff:ff 如果要将数据包发送到机器 X(IP 地址 192.168.0.88),则将 ip 层中的目标地址设置为该值。

  2. 完成后,您将需要声明一个 char* 数组,并将所有结构复制到 char* 数组以创建字节序列并通过线路发送。

// 只是为了向您展示我定义结构的含义, // 与代码片段的其余部分无关

typedef struct IP_header 
{
    u_char      VersionNInternetHeaderLength;       // Version (4 bits) + Internet header length (4 bits)
    /*u_char        version:4;
    u_char      HeaderLength:4;*/
    u_char      Type;                               // Type of service 
    u_short     TotalLength;                        // Total length 
    u_short     Identification;                     // Identification

    u_char      rsv     : 1;
    u_char      df      : 1;
    u_char      mf      : 1;

    u_char      FragmentOffset1 : 5;
    u_char      FragmentOffset2;
    //u_short       Flags_fo;                           // Flags (3 bits) + Fragment offset (13 bits)

    u_char      TimeToLive;                         // Time to live
    u_char      Protocol;                           // Next level Protocol of the encapsulated payload
    u_short     Checksum;                           // Header checksum
    IP_address  SourceAddress;                      // Source address
    IP_address  DestinationAddress;                 // Destination address
    u_int       OptionNPadding;                     // Option + Padding

    IP_header()
    {
        mf = 0;
        rsv = 0;
        df = 0;
        FragmentOffset1 = 0;
        FragmentOffset2 = 0;

        TimeToLive = 128;

        TotalLength = sizeof(IP_header);
        Identification = 0xABCD;
        Checksum = 0xABCD;
        OptionNPadding = 0;
    }
}IP_header;


Ethernet_header EthernetHeader;// = (Ethernet_header*)malloc(sizeof(Ethernet_header));
        ARP_header ARPHeader ;//= (ARP_header*)malloc(sizeof(ARP_header));

        ARPHeader.HardwareType = htons(1);
        ARPHeader.ProtocolType = htons(0x800);
        ARPHeader.OPCODE = htons(1);

        ARPHeader.HeaderLength = 6;
        ARPHeader.ProtocolLength = 4;

        ARPHeader.SenderMAC = MY_FAKE_MAC;
        ARPHeader.SenderIP = MY_IP;

        ARPHeader.TargetMAC = MAC_address();
        ARPHeader.TargetIP = Whose;

        EthernetHeader.DestinationMAC = BROADCASTMAC;
        EthernetHeader.SourceMAC = MY_FAKE_MAC;
        EthernetHeader.EtherType = htons(0x806);

        u_char* packet = (u_char*)malloc(sizeof(EthernetHeader) + sizeof(ARPHeader));

        memcpy(packet, &EthernetHeader, sizeof(EthernetHeader));
        memcpy(packet + sizeof(EthernetHeader), &ARPHeader, sizeof(ARPHeader));

        SendPacket(packet);

This is how i sent an ARP request over the wire.
1. Define structures for the protocols i.e if you want to send ARP packets you will need a structure that will contain the data link layer(Ethernet header) and network layer (ARP Header). Correspondingly if you want to send a tcp packet over IP you will need a data structure for the ethernet header, ip header and tcp header.

  1. once you defined the structures, initialize an instance of the structure with values you want i.e. if you want the packet to go to all machines in the network set the destination mac value of the Ethernet header to ff:ff:ff:ff:ff:ff if you want to send the packet to machine X with(IP address 192.168.0.88) then set the destination address in the ip layer to that value.

  2. Once done you will need to declare a char* array and copy all the structures to the char* array to create a byte sequence and send it over the wire.

//Just to show you what i mean by defining structures doesn't //relate to the rest of the code snippet

typedef struct IP_header 
{
    u_char      VersionNInternetHeaderLength;       // Version (4 bits) + Internet header length (4 bits)
    /*u_char        version:4;
    u_char      HeaderLength:4;*/
    u_char      Type;                               // Type of service 
    u_short     TotalLength;                        // Total length 
    u_short     Identification;                     // Identification

    u_char      rsv     : 1;
    u_char      df      : 1;
    u_char      mf      : 1;

    u_char      FragmentOffset1 : 5;
    u_char      FragmentOffset2;
    //u_short       Flags_fo;                           // Flags (3 bits) + Fragment offset (13 bits)

    u_char      TimeToLive;                         // Time to live
    u_char      Protocol;                           // Next level Protocol of the encapsulated payload
    u_short     Checksum;                           // Header checksum
    IP_address  SourceAddress;                      // Source address
    IP_address  DestinationAddress;                 // Destination address
    u_int       OptionNPadding;                     // Option + Padding

    IP_header()
    {
        mf = 0;
        rsv = 0;
        df = 0;
        FragmentOffset1 = 0;
        FragmentOffset2 = 0;

        TimeToLive = 128;

        TotalLength = sizeof(IP_header);
        Identification = 0xABCD;
        Checksum = 0xABCD;
        OptionNPadding = 0;
    }
}IP_header;


Ethernet_header EthernetHeader;// = (Ethernet_header*)malloc(sizeof(Ethernet_header));
        ARP_header ARPHeader ;//= (ARP_header*)malloc(sizeof(ARP_header));

        ARPHeader.HardwareType = htons(1);
        ARPHeader.ProtocolType = htons(0x800);
        ARPHeader.OPCODE = htons(1);

        ARPHeader.HeaderLength = 6;
        ARPHeader.ProtocolLength = 4;

        ARPHeader.SenderMAC = MY_FAKE_MAC;
        ARPHeader.SenderIP = MY_IP;

        ARPHeader.TargetMAC = MAC_address();
        ARPHeader.TargetIP = Whose;

        EthernetHeader.DestinationMAC = BROADCASTMAC;
        EthernetHeader.SourceMAC = MY_FAKE_MAC;
        EthernetHeader.EtherType = htons(0x806);

        u_char* packet = (u_char*)malloc(sizeof(EthernetHeader) + sizeof(ARPHeader));

        memcpy(packet, &EthernetHeader, sizeof(EthernetHeader));
        memcpy(packet + sizeof(EthernetHeader), &ARPHeader, sizeof(ARPHeader));

        SendPacket(packet);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文