北斋143 2023-06-24 22:24 采纳率: 61.5%
浏览 24

代码执行到这个模块之间出现越界还是什么问题不能继续运行

代码执行到这个模块之间出现越界还是什么问题不能继续运行

img

img


//获取自己主机的MAC地址
int GetSelfMac(pcap_t* adhandle, const char* ip_addr, unsigned char* ip_mac)
{
    unsigned char sendbuf[42];
    int i = -1;
    int res;
    struct ethernet_head eh;
    struct arp_head ah;
    struct pcap_pkthdr* pkt_header;
    const u_char* pkt_data;
    memset(eh.dst_mac_add, 0xff, 6); //目的地址全为广播地址
    memset(eh.src_mac_add, 0x0f, 6);
    memset(ah.src_mac_add, 0x0f, 6);
    memset(ah.dst_mac_add, 0x00, 6);
    eh.type = htons(ETH_ARP);
    ah.hardware_type = htons(ARP_HARDWARE);
    ah.protocol_type = htons(ETH_IP);
    ah.hardware_add_len = 6;
    ah.hardware_add_len = 4;
    ah.src_ip_add = inet_addr("100.100.100.100"); //随便设一个请求方ip
    ah.operation_field = htons(ARP_REQUEST);
    ah.dst_ip_add = inet_addr(ip_addr);
    memset(sendbuf, 0, sizeof(sendbuf));
    memcpy(sendbuf, &eh, sizeof(eh));
    memcpy(sendbuf + sizeof(eh), &ah, sizeof(ah));
    if (pcap_sendpacket(adhandle, sendbuf, 42) == 0) {
        printf("\nPacketSend succeed\n");
    }
    else {
        printf("PacketSendPacket in getmine Error: %d\n",(int) GetLastError());
        return 0;
    }

    while ((res = pcap_next_ex(adhandle, &pkt_header, &pkt_data)) >= 0) {
        if (*(unsigned short *)(pkt_data + 12) == htons(ETH_ARP)
            && *(unsigned short*)(pkt_data + 20) == htons(ARP_REPLY)
            && *(unsigned long*)(pkt_data + 38)
            == inet_addr("100.100.100.100")) {
            for (i = 0; i < 6; i++) {
                ip_mac[i] = *(unsigned char *)(pkt_data + 22 + i);
            }
            printf("获取自己主机的MAC地址成功!\n");
            break;
        }
    }
    if (i == 6) {
        return 1;
    }
    else {
        return 0;
    }
}

//向局域网内所有可能的IP地址发送ARP请求包线程
DWORD WINAPI SendArpPacket(LPVOID lpParameter)
{
    struct sparam* spara = (struct sparam*)lpParameter;
    pcap_t* adhandle = spara->adhandle;
    char* ip = spara->ip;
    unsigned char* mac = spara->mac;
    char* netmask = spara->netmask;
    printf("ip_mac:%02x-%02x-%02x-%02x-%02x-%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    printf("自身的IP地址为:%s\n", ip);
    printf("地址掩码NETMASK为:%s\n", netmask);
    printf("\n");
    unsigned char sendbuf[42];
    struct ethernet_head eh;
    struct arp_head ah;
    memset(eh.dst_mac_add, 0xff, 6);
    memcpy(eh.src_mac_add, mac, 6);
    memcpy(ah.src_mac_add, mac, 6);
    memset(ah.dst_mac_add, 0xff, 6);
    eh.type = htons(ETH_ARP);
    ah.hardware_type = htons(ARP_HARDWARE);
    ah.protocol_type = htons(ETH_IP);
    ah.hardware_add_len = 6;
    ah.protocol_add_len = 4;
    ah.src_ip_add = inet_addr(ip);
    ah.operation_field = htons(ARP_REQUEST);

    //向局域网内广播发arp包  
    unsigned long myip = inet_addr(ip);
    unsigned long mynetmask = inet_addr(netmask);
    unsigned long hisip = htonl((myip & mynetmask));
    for (int i = 0; i < HOSTNUM; i++)
    {
        ah.dst_ip_add = htonl(hisip + i);
        memset(sendbuf, 0, sizeof(sendbuf));
        memcpy(sendbuf, &eh, sizeof(sendbuf));
        memcpy(sendbuf + sizeof(eh), &ah, sizeof(ah));
        if (pcap_sendpacket(adhandle, sendbuf, 42) == 0)
        {
            printf("\nPacketSend succeed\n");
            printf("\nPacketSend succeed\n");
        }
        else
        {
            printf("PacketSendPacket in getmine Error: %d\n", (int)GetLastError());
        }
        Sleep(50);
    }
    Sleep(1000);
    flag = 1;
    return 0;
}

//分析截留的数据包获取活动的主机IP地址
DWORD WINAPI GetLivePC(LPVOID lpParameter)
{
    struct gparam* gparam = (struct gparam*)lpParameter;
    pcap_t* adhandle = gparam->adhandle;
    int res;
    unsigned char Mac[6];
    struct pcap_pkthdr* pkt_header;
    const char* pkt_data;
    while (1)
    {
        if (flag)
        {
            printf("扫描完毕,按任意键退出!\n");
            break;
        }
        if ((res = pcap_next_ex(adhandle, &pkt_header, &pkt_data)) >= 0)
        {
            if (*(unsigned short*)(pkt_data + 12) == htons(ETH_ARP))
            {
                struct arp_packet* recv = (struct arp_packet*)pkt_data;
                if (*(unsigned short*)(pkt_data + 20) == htons(ARP_REPLY))
                {
                    printf("------------------------------\n");
                    printf("IP地址:%d.%d.%d.%d  MAC地址:", recv->ah.src_ip_add & 255, recv->ah.src_ip_add >> 8 & 255,
                        recv->ah.src_ip_add >> 16 & 255, recv->ah.src_ip_add >> 24 & 255);
                    for (int i = 0; i < 6; i++)
                    {
                        Mac[i] = *(unsigned char*)(pkt_data + 22 + i);
                        printf("%02x", Mac[i]);
                    }
                    printf("\n");
                }
            }
        }
        Sleep(10);
    }
    return 0;
}

  • 写回答

2条回答 默认 最新

  • 泡沫o0 2023年度博客之星上海赛道TOP 1 2023-06-25 01:51
    关注

    你的代码中存在一些可能的问题,可能导致程序无法正常运行。以下是一些可能的问题和解决方案:

    1. 内存越界:在你的SendArpPacket函数中,你使用memcpy函数将ehah结构体复制到sendbuf中。但是,你使用sizeof(sendbuf)作为memcpy的长度参数,这可能会导致内存越界,因为sendbuf的大小可能小于ehah的总大小。你应该使用sizeof(eh)sizeof(ah)作为memcpy的长度参数。

    2. 权限问题:如果你的程序在运行时需要管理员权限,但没有得到足够的权限,那么它可能无法正常运行。你可以尝试以管理员权限运行你的程序。

    3. 网络问题:如果你的网络设置或网络设备有问题,那么你的程序可能无法正常运行。你应该检查你的网络设置和网络设备,确保它们能够正常工作。

    4. 错误的IP地址或MAC地址:如果你提供的IP地址或MAC地址是错误的,那么你的程序可能无法正常运行。你应该检查你的IP地址和MAC地址,确保它们是正确的。

    如果以上的建议都无法解决你的问题,你可能需要使用调试工具来调试你的程序,找出导致程序无法正常运行的具体原因。

    评论

报告相同问题?

问题事件

  • 创建了问题 6月24日

悬赏问题

  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 更换了一个新的win10系统,再下载VS时碰到的问题,是C++组件的?
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线
  • ¥20 无法创建新的堆栈防护界面
  • ¥15 sessionStorage在vue中的用法
  • ¥15 wordpress更换域名后用户图片头像不显示