Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. //This thread method reads the network socket, replacing the process's stdin with a pipe
  2. //that it feeds network output to.
  3. void* snet::inthread(void* args)
  4. {
  5.     //Create and direct the pipe
  6.     int inpipe[2];
  7.     pipe(inpipe);
  8.     dup2(inpipe[READ_END], STDIN_FILENO);
  9.  
  10.     //set up input
  11.     int len;
  12.     char input[4096] = {0};
  13.  
  14.     //send network input to shell input
  15.     while(true)
  16.     {
  17.         while((len = read(snet::sock, input, 4096)) >0)
  18.         {
  19.             write(inpipe[WRITE_END], input, len);
  20.         }
  21.     }
  22. }
  23.  
  24. //This thread replaces the process's stdout with a pipe that sends its output to the client.
  25. void* snet::outthread(void* args)
  26. {
  27.     //create and direct the pipe
  28.     int oldin = dup(STDOUT_FILENO); //save old stdin for verbose
  29.     int outpipe[2];
  30.     pipe(outpipe);
  31.     dup2(outpipe[WRITE_END], STDOUT_FILENO);
  32.  
  33.     //set up output
  34.     int len;
  35.     char output[4096] = {0};
  36.  
  37.     while(true)
  38.     {
  39.         while((len = read(outpipe[READ_END], output, 4096)) >0)
  40.         {
  41.             if(snet::verbose) {write(oldin, output, len);}
  42.             write(snet::sock, output, len);
  43.         }
  44.     }
  45. }
  46.  
  47. //Creates an open TCP connection and waits for a client. Once connected, accepts commands
  48. //and becomes a remote shell.
  49. void snet::slisten(vector<string>& cmds)
  50. {
  51.     //get args, port--if no port, return with error
  52.     snet::verbose = false;
  53.     bool isPort = false;
  54.     int port = -1;
  55.     for(unsigned int i = 0; i < cmds.size(); i++)
  56.     {
  57.         if(cmds[i].at(0) == '-') //grab argument if it starts with -
  58.         {
  59.             if(cmds[i] == "-v")
  60.             {
  61.                 snet::verbose = true;
  62.             }
  63.         }
  64.  
  65.         if(snet::isNumber(cmds[i])) //if it was a number, then we assign it to port
  66.         {
  67.             port = atoi(cmds[i].c_str());
  68.         }
  69.     }
  70.  
  71.     //If the user didn't specify a port, return.
  72.     if(port == -1)
  73.     {
  74.         cout << "Error: No port specified. Please specify a port." << endl;
  75.         exit(0);
  76.     }
  77.  
  78.     //set up socket
  79.     int shellsocket = socket(AF_INET, SOCK_STREAM, 0);
  80.     struct sockaddr_in listenaddr;
  81.     memset(&listenaddr, 0, sizeof(sockaddr_in));
  82.     listenaddr.sin_family = AF_INET;
  83.     listenaddr.sin_port = htons(port);
  84.     inet_aton("127.0.0.1", &listenaddr.sin_addr);
  85.  
  86.     bind(shellsocket, (struct sockaddr*) &listenaddr, sizeof(listenaddr));
  87.  
  88.     //listen for incoming connection
  89.     struct sockaddr_in remoteaddr;
  90.     socklen_t remoteaddrlen = sizeof(remoteaddr);
  91.     listen(shellsocket, 5);
  92.     snet::sock = accept(shellsocket, (struct sockaddr*) &remoteaddr, &remoteaddrlen);
  93.     close(shellsocket);
  94.  
  95.     //the socket has served it's purpose
  96.     close(shellsocket);
  97.  
  98.     //set up threads for monitoring and writing
  99.     pthread_t in, out;
  100.  
  101.     pthread_create(&in, NULL, inthread, NULL);
  102.     pthread_create(&out, NULL, outthread, NULL);
  103.     pthread_detach(in);
  104.     pthread_detach(out);
  105.  
  106.     //return to main--this continues until the server does ctrl + c or client exits
  107.     return;
  108. }
  109.  
  110. //Reads from the socket's output and prints to stdout
  111. void* snet::clinthread(void* args)
  112. {
  113.     char input[4096];
  114.     int inlen = 0;
  115.     while(true)
  116.     {
  117.         while((inlen = read(snet::sock, input, 4096)) >0)
  118.         {
  119.             write(STDOUT_FILENO, input, inlen);
  120.         }
  121.     }
  122. }
  123.  
  124. //Connects to a given IP and port (probably another shell of this type)
  125. //to send commands and receive output.
  126. void snet::sconnect(vector<string>& cmds)
  127. {
  128.     //return if no args or if no/invalid port
  129.     if(cmds.size() < 3)
  130.     {
  131.         cout << "Please specify both an IP address and port." << endl;
  132.         exit(0);
  133.     }
  134.  
  135.     if(!snet::isNumber(cmds[2]))
  136.     {
  137.         cout << "That port is invalid. Please specify a port." << endl;
  138.         exit(0);
  139.     }
  140.  
  141.     //obtain args
  142.     string ip = cmds[1];
  143.     int port = atoi(cmds[2].c_str());
  144.  
  145.     //set up connection socket and connection address
  146.     snet::sock = socket(AF_INET, SOCK_STREAM, 0);
  147.     struct sockaddr_in connectaddr;
  148.     memset(&connectaddr, 0, sizeof(sockaddr_in));
  149.     connectaddr.sin_family = AF_INET;
  150.     connectaddr.sin_port = htons(port);
  151.     inet_aton(ip.c_str(), &connectaddr.sin_addr);
  152.  
  153.     //connect, return if fail or show if succeed
  154.     socklen_t len = sizeof(connectaddr);
  155.     if((connect(snet::sock,(sockaddr *) &connectaddr, len)) == -1)
  156.     {
  157.         cout << "Connection failed." << endl;
  158.         exit(0);
  159.     }
  160.     cout << "Connection established. Exit to return to your own shell." << endl;
  161.  
  162.     //set up in/out loop
  163.     //set up threads for monitoring and writing
  164.     pthread_t serverout;
  165.     pthread_create(&serverout, NULL, clinthread, NULL);
  166.     pthread_detach(serverout);
  167.     string cmd = "";
  168.     cin.clear();
  169.     while(cmd != "exit")
  170.     {
  171.         //get user cmd, send it over the socket
  172.         getline(cin, cmd);
  173.         cin.clear();
  174.         cmd += "\n";
  175.         write(snet::sock, cmd.c_str(), cmd.size());
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement