-1

我正在尝试创建一个魔术包以在某台 PC 上应用 WOL,我尝试以与 Wireshark 捕获中的数据包外观完全相同的格式执行此操作,但 PC 无法将接收数据包识别为魔术包

这是代码:

UINT8*  CTestCase::CreateMagicPacket(IN OUT UINT8* p_buffer, IN int  size, IN MACAddress src, IN MACAddress dst)
{

    UINT8 *my_buff_to_send = new UINT8[size]{ 0 }; //create new buffer with the size we want


    for (int i = 0; i < 6; i++) //broadcast address
    {
        my_buff_to_send[i] = 0xFF;
        m_pLog->AddMessage(" |");
        m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
    }

    int m = 5;
    for (size_t i = 6; i < 12; i++) //insert source mac adress to the new buffer
    {

            my_buff_to_send[i] = src.Get(m);
            m_pLog->AddMessage(" |");
            m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
            m--;
    }


    my_buff_to_send[12] = 0x08;
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[12]));
    my_buff_to_send[13] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[13]));
    my_buff_to_send[14] =  0x45;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[14]));
    my_buff_to_send[15] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[15]));
    my_buff_to_send[16] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[16]));
    my_buff_to_send[17] =  0x90;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[17]));
    my_buff_to_send[18] = 0x48;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[18]));
    my_buff_to_send[19] = 0x91;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[19]));
    my_buff_to_send[20] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[20]));
    my_buff_to_send[21] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[21]));
    my_buff_to_send[22] = 0x80;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[22]));
    my_buff_to_send[23] = 0x11;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[23]));
    my_buff_to_send[24] = 0x00;  //insert permanent type to the buffer OR dd
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[24]));
    my_buff_to_send[25] = 0x00;  //insert permanent type to the buffer OR bd
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[25]));
    my_buff_to_send[26] = 0x0a;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[26]));
    my_buff_to_send[27] = 0x0a;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[27]));
    my_buff_to_send[28] = 0x0a;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[28]));
    my_buff_to_send[29] = 0x05;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[29]));
    my_buff_to_send[30] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[30]));
    my_buff_to_send[31] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[31]));
    my_buff_to_send[32] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[32]));
    my_buff_to_send[33] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[33]));
    my_buff_to_send[34] = 0xd0;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[34]));
    my_buff_to_send[35] = 0x28;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[35]));
    my_buff_to_send[36] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[36]));
    my_buff_to_send[37] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[37]));
    my_buff_to_send[38] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[38]));
    my_buff_to_send[39] = 0x7c;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[39]));
    my_buff_to_send[40] = 0x14;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[40]));
    my_buff_to_send[41] = 0x9c;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[41]));



    for (int i = 42; i < 48; i++)
    {
        my_buff_to_send[i] = 0xFF;
        m_pLog->AddMessage(" |");
        m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
    }

    int r = 5;
    for (size_t i = 48; i < 54; i++) //insert source mac adress to the new buffer
    {
        my_buff_to_send[i] = dst.Get(r);
        m_pLog->AddMessage(" |");
        m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
        r--;
    }

    for (int i = 54; i < 60; i++)
    {
        my_buff_to_send[i] = 0xFF;
        m_pLog->AddMessage(" |");
        m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
    }


    for (size_t j = 0; j < 16; j++)
    {

        for (int k = 5; k >= 0; k--) //insert destination mac adress to the buffer
        {
            int i = 60;
            my_buff_to_send[i] = dst.Get(k);

            m_pLog->AddMessage(" |");
            m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
            i++;
        }

    }


            return my_buff_to_send;




};
4

1 回答 1

2

您真的需要自己创建原始以太网数据包吗?

完成此操作后,我刚刚将魔术数据包嵌入到 UDP 数据包之类的东西中,然后进行广播。例如:

#include "sock2/sock2.h"

int main() { 
    std::string MAC_address("\x00\x00\x00\x00\x00\x00", 6);
    std::string buffer("\xff\xff\xff\xff\xff\xff");

    for (int i=0; i<16; i++)
        buffer.append(MAC_address);

    IP::UDP::socket s;
    s.setopt(SO_BROADCAST);

    IP::address addr(192, 168, 0, 255, 5);
    s.send(buffer, addr);
}

这是非常简单的(例如,硬编码地址,几乎没有错误检查),但是为 MAC 地址使用适当的字节就可以了。我没有尝试详细分析您的所有代码,但它似乎包含的内容比我预期的通常需要的要多得多。

于 2018-06-11T08:28:00.967 回答