memcpy()没有按预期工作

时间:2015-06-01 21:08:32

标签: c++ linux buffer-overflow heartbleed-bug network-security

我试图通过Linux在C / C ++中简单实现Heartbleed Bug(在vmplayer上使用ElementaryOS)。根据我对heartbleed bug的理解,它涉及客户端向服务器发送心跳请求,指定包含的有效负载的大小大于有效负载的实际大小,这导致服务器包括将被布局的内存中的内容在本地有效负载缓冲区之后,在响应中。

到目前为止我所拥有的是客户端/服务器应用程序。客户端连接到服务器并发送一条敏感信息,在这种情况下是密码。接下来,客户端向服务器发送心跳请求,指定有效负载的错误大小。我声明我的局部变量如下:

        char password[30];// = new char[30];    //Some sensitve data, that will be accessed using the Heartbleed Bug
        char temp[15];// = new char[15];
        char payload[45];// = new char[45];

我从心跳请求中检索内容,如下所示:

        int payloadSize = atoi(strtok(buffer, " "));
        temp = strtok(NULL, "\n");

在心跳请求中,有效负载是"有效负载",大小为45.然后我就这样调用memcpy():

        memcpy(payload, temp, payloadSize /* 45 in this case */);

我希望有效载荷变量保持值"有效载荷",然后是密码变量中的内容。然而,有效载荷保持值"有效载荷"只要。

        cout<<payload<<endl; //prints payload

有谁可以指出我做错了什么?

所有代码:

int main()
{
    ofstream log("ServerLog.txt");

    int listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
    sockaddr_in serverAddress, clientAddress;
    socklen_t clientLen;

    serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = 54321;

    if(bind(listeningSocket, (sockaddr *) &serverAddress, sizeof(serverAddress)) < 0)
    {
        cout<<strerror(errno)<<endl;
    }

    else
    {
        cout<<"Socket bound to port 12345"<<endl;

        if(listen(listeningSocket, 5) < 0)
        {
            cout<<strerror(errno)<<endl;
        }

        else
        {
            log<<"Listening for connections now..."<<endl;
            int commSocket = accept(listeningSocket, (sockaddr *) &clientAddress, &clientLen);

            log<<"Now connected to a client..."<<endl;

            int N = 0;
            char buffer[100];// = new char[100];
            char password[30];// = new char[30];    //Some sensitve data, that will be accessed using the Heartbleed Bug
            char *temp = new char[15];
            char payload[45];// = new char[45];

            N = recv(commSocket, password, 100, 0);     //Receive sensitive data, in this case, a password

            if(N == 0)          //Check for remote socket close
            {
                cout<<"The remote connection has been closed."<<endl;
            }

            else if(N < 0)      //In case there is an error
            {
                cout<<strerror(errno)<<endl;
            }

            else        //All good, move on
            {
                //cout<<N<<" "<<password<<endl;
                log<<"Password received from client: "<<password<<" of length: "<<N<<endl;

                N = recv(commSocket, buffer, 100, 0);      //recv heartbeat request

                if(N == 0)          //Check for remote socket close
                {
                    cout<<"The remote connection has been closed."<<endl;
                }

                else if(N < 0)      //In case there is an error
                {
                    cout<<strerror(errno)<<endl;
                }

                else
                {
                    log<<"Heartbeat request received from client: "<<buffer<<endl;

                    int payloadSize = atoi(strtok(buffer, " "));
                    temp = strtok(NULL, "\n");

                    memcpy(payload, temp, 45);

                    if(N < 0)
                    {
                        cout<<strerror(errno)<<endl;
                    }
                }
            }
        }
    }

    return 0;
}

2 个答案:

答案 0 :(得分:2)

C样式字符串以空终止符(\0)终止,cout不知道它们存储的缓冲区长度。当您使用memcpy时,它会复制空终止符以及字符串之外的所有内容。缓冲区实际上保存了字符串后面的所有内容,但它位于空终止符之后,因此它不被视为字符串的一部分,这意味着它不会被cout打印。

缓冲区有效负载可能如下所示

{ 'p', 'a', 'y', 'l', 'o', 'a', 'd', '\0', 'g', 'a', 'r', 'b', 'a', 'g', 'e' }
                                     ^^^^ String ends here

因此,当您使用cout进行打印时,它只会打印到\0字符。这意味着它将打印payload而不打印任何其他内容。

如果要打印缓冲区中的所有值,则必须逐个打印。你可以这样做:

for(int i = 0; i < 45; i++)
{
    cout << payload[i];
}
cout << endl;

将打印缓冲区的所有单个字符打印到控制台。

由于打印特殊字符可能会产生奇怪的输出,您可能需要打印出字符的数值。要打印字节的数值,您可以使用printf执行@MattMcNabb建议的操作。然后代码看起来像这样

for(int i = 0; i < 45; i++)
{
    printf("%02X", (unsigned char)payload[i]);
}

答案 1 :(得分:0)

您使用strtok的方式是错误的。如果int payloadSize = atoi(strtok(buffer, " "));为您提供payloadSize,那么当您执行temp时,您将不会在temp = strtok(NULL, "\n");中拥有任何内容,因为您已将缓冲区指针移至strtok的上下文中{1}}。

在memcpy

之前检查temp中的内容