【问题标题】:Libpcap ARP packet struct not mapping correctlyLibpcap ARP 数据包结构未正确映射
【发布时间】:2017-12-30 12:04:48
【问题描述】:

每次检测到数据包时都会运行此代码,但 ARP IP 地址与应有的不匹配,源 IP 地址甚至不是本地的。我添加了一个测试打印输出来尝试找出问题,当我运行 ARP 扫描(在 192.168.1.* 上)时,我得到了这样的输出:

Message: [2054] Src IP: 18.0.255.255 (28:cf:e9:18:db:29) - Trg IP: 192.168.1.42 (ff:ff:ff:ff:28:cf)
Message: [2054] Src IP: 18.0.255.255 (28:cf:e9:18:db:29) - Trg IP: 192.168.1.43 (ff:ff:ff:ff:28:cf)
Message: [2054] Src IP: 18.0.255.255 (28:cf:e9:18:db:29) - Trg IP: 192.168.1.44 (ff:ff:ff:ff:28:cf)
Message: [2054] Src IP: 18.0.255.255 (28:cf:e9:18:db:29) - Trg IP: 192.168.1.45 (ff:ff:ff:ff:28:cf)
Message: [2054] Src IP: 18.0.255.255 (28:cf:e9:18:db:29) - Trg IP: 192.168.1.46 (ff:ff:ff:ff:28:cf)

什么会导致这种类型的行为,即目标 IP(结构中的最后一个字段)被正确读取但其余部分没有?

   const struct pkt_ethernet *ethernet = (struct pkt_ethernet*)(packet);

    char ether_src[48];
    char ether_dst[48];
    char ether_typ[8];
    int  ether_typ_dec;

    snprintf(ether_src, 48,  "%s", ether_ntoa(ethernet->ether_src));
    snprintf(ether_dst, 48,  "%s", ether_ntoa(ethernet->ether_dst));
    snprintf(ether_typ,  8,  "%d", ntohs(ethernet->ether_type));
    ether_typ_dec = ntohs(ethernet->ether_type);

    switch (ether_typ_dec)
    {
        case 2054: // ARP Packet
        {
            const struct pkt_arp *arp = (struct pkt_arp*)(packet + SIZE_ETHERNET);

            char arp_srcIP[INET_ADDRSTRLEN]; // ARP Source IP
            char arp_trgIP[INET_ADDRSTRLEN]; // ARP Target IP
            char arp_srcHW[48];
            char arp_trgHW[48];

            inet_ntop(AF_INET, &arp->srcIP, arp_srcIP, INET_ADDRSTRLEN);
            inet_ntop(AF_INET, &arp->trgIP, arp_trgIP, INET_ADDRSTRLEN);
            snprintf(arp_srcHW, 48,  "%s", ether_ntoa(arp->srcHw));
            snprintf(arp_trgHW, 48,  "%s", ether_ntoa(arp->trgHW));

            char test[300];
            snprintf(test, 300,  "[%d] Src IP: %s (%s) - Trg IP: %s (%s)", ether_typ_dec, arp_srcIP, arp_srcHW, arp_trgIP, arp_trgHW);

            capMessage(test);

            break;
        }
    }

ARP 结构:

struct pkt_arp
{
    u_int16_t htype;                              /* Hardware Type           */
    u_int16_t ptype;                              /* Protocol Type           */
    u_char hlen;                                  /* Hardware Address Length */
    u_char plen;                                  /* Protocol Address Length */
    u_int16_t oper;                               /* Operation Code          */
    struct ether_addr srcHw[ETHER_ADDR_LEN];       /* Sender hardware address */
    struct  in_addr srcIP;                        /* Sender IP address       */
    struct ether_addr trgHW[ETHER_ADDR_LEN];      /* Target hardware address */
    struct  in_addr trgIP;                        /* Target IP address       */
} __attribute__ ((__packed__));

【问题讨论】:

  • 刚开始看...不建议使用十进制数字来查找数据包中的值,例如以太类型.. 0x0800、0x0806 等...这些是网络人员知道要查找的内容以及如何编写 RFC。转换为十进制会导致大量的头发拉扯。
  • 我可能累了,但&arp-> 不适合我。它已经是一个指针了.. 我觉得我会尝试作为一个没有 & 的 arp->,但它可能只是我昏昏欲睡的大脑。
  • 目标硬件地址也很可疑......根据我的记忆,在 arp 请求中应该是 0x0000000。最后的 0x28cf 看起来很奇怪
  • @DavidHoelzer 如果我删除 & 我得到一个错误:error: passing 'const struct in_addr' to parameter of incompatible type 'const void *' 但手册页说:The function inet_ntop() converts an address *src from network format (usually a struct in_addr

标签: c struct libpcap packet-capture arp


【解决方案1】:

所以问题出在结构中:

struct pkt_arp
{
    u_int16_t htype;                              
    u_int16_t ptype;                              
    u_char hlen;                                  
    u_char plen;                                  
    u_int16_t oper;                               
    struct ether_addr srcHw[ETHER_ADDR_LEN]; <---- HERE  
    struct  in_addr srcIP;                       
    struct ether_addr trgHW[ETHER_ADDR_LEN]; <---- and HERE      
    struct  in_addr trgIP;                       
} __attribute__ ((__packed__));

这应该是struct ether_addr srcHw;,并且在代码中应该使用 ether_ntoa(&ethernet->ether_dst) 引用,包括与号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    相关资源
    最近更新 更多