Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. // changed parts in common.h:
  2. ...
  3. int mystdin[2]; // pipes to child process
  4. int mystdout[2];
  5.  
  6. void print_unencrypted_data(char *buf, size_t len) {
  7. printf("from SSL to bash: %d bytes", (int)len);
  8. write(mystdin[1],buf,(int)len); // write to child's stdin instead of stdout
  9. }
  10. ...
  11. void do_stdin_read()
  12. {
  13. char buf[DEFAULT_BUF_SIZE];
  14. //ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
  15. ssize_t n = read(mystdout[0], buf, sizeof(buf)); // read from child's stdout instead of stdin
  16. //printf("from shell: %.*s", (int)n, buf);
  17. if (n>0)
  18. send_unencrypted_bytes(buf, (size_t)n);
  19. }
  20. ...
  21.  
  22. // changed part in main:
  23. int main(int argc, char **argv)
  24. {
  25. ...
  26. if (connect(sockfd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
  27. die("connect()");
  28.  
  29. // added code:
  30.  
  31. pipe(mystdin);
  32. pipe(mystdout);
  33.  
  34. int pid = fork();
  35. if (pid==(pid_t)0) // child: simple echo
  36. {
  37. close(mystdin[1]);
  38. close(mystdout[0]);
  39. char buf;
  40. int i;
  41. while(1)
  42. {
  43. read(mystdin[0],&buf,1);
  44. write(mystdout[1],&buf,1);
  45. // replace "a" by a very large output:
  46. if (buf=='a')
  47. for (i=0; i<160000; i++) // 160000 might have to be increased depending on system
  48. write(mystdout[1],&buf,1);
  49. }
  50. return 0;
  51. }
  52.  
  53. // parent
  54. close(mystdin[0]);
  55. close(mystdout[1]);
  56.  
  57. if (pid>(pid_t)0)
  58. {
  59. struct pollfd fdset[2];
  60. memset(&fdset, 0, sizeof(fdset));
  61.  
  62. fdset[0].fd = mystdout[0]; //STDIN_FILENO; // stdout[0];
  63. fdset[0].events = POLLIN;
  64. // original code from here
  65. }
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement