Winsock remote data reading (CMD)

Asked

Viewed 146 times

0

I have a server (remote) where redirects your shell to the client (remote), the client side is using netcat or telnet for communication. I am developing the client side for use with Windows, but I am having some problems when it comes to receiving the data from the server, they are coming incomplete, and also I can’t send commands more than 2 times to the server.

the application must send remote commands to the shell (send), and receive the data (recv) until the client wishes to quit. the problem is to receive the complete data from the server...

what should be done to improve this application?

while (1)
{
    //READ
    memset(pbuf, 0, sizeof(pbuf));
    iResult = recv(client[iD].socket, pbuf, sizeof(pbuf) - 1, 0);
    if (iResult < 0)
    {
        if (WSAGetLastError() == WSAEWOULDBLOCK)
        {
            continue;
        }

        printf("recv error: %d\n", WSAGetLastError());
        return SOCKET_ERROR;
    }
    else if (iResult == 0)
    {
        printf("disconnected\n");
        return 0;
    }
    else
    {
        for (int i = 0; i < iResult; i++)
        {
            std::cout << pbuf[i];
        }
        memset(pbuf, 0, sizeof(pbuf));

        //SEND
        memset(key_buffer, 0, sizeof(key_buffer));
        fgets(key_buffer, sizeof(key_buffer) - 1, stdin);
        if (!strcmp(key_buffer, "exit"))
            break;
        iResult = send(client[iD].socket, key_buffer, sizeof(key_buffer) - 1, 0);
        if (iResult == SOCKET_ERROR) {
            printf("send failed with error: %d\n", WSAGetLastError());
            CLEAN_UP();
            return 1;
        }

    }
}

1 answer

0


"Receive the complete data from the server", probably the problem should be in this buffer that you are using "pbuf" It must be packed, or else it may not be being handled properly during the process. tries to create a function to receive and send data to this Shell.

Example:

while (1)
{
    if (ReadShell(client[iD].socket) == 3)
        break;
}

tries to use this for data reading:

char chunk[1024] = "";

    memset(chunk, 0, sizeof(chunk));
    if ((iResult = recv(client[iD].socket, chunk, 1024, 0)) < 0)
    {   
        std::cout << "Falha ao receber dados do servidor" << std::endl;
        getchar();
        return 3;
    }
    else
    {
        for (int i = 0; i < iResult; i++)
        {
            if (chunk[i] != NULL)
                printf("%c", chunk[i]);
        }
    }

And this to send the data:

  char message[DEFAULT_BUFLEN] = "";
        Sleep(500);
        memset(message, 0, sizeof(message));
        fgets(message, sizeof(message), stdin);
        if (send(client[iD].socket, message, strlen(message), 0) < 0)
        {
            puts("Falha ao enviar dados.");
            getchar();
            return 3;
        }

        if (!strcmp(message, "exit\n"))
        {   
            std::cout << "\nFinalizando Shell..." << std::endl;
            Sleep(1000);
            return 3;
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.