Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. int SocketClient::readHandschake() {
  2. printf("Handschaking (Socket 5)\n");
  3. char* buffer;
  4.  
  5. char* handschake = SocketStreamUtils::readBytes(client, 3);
  6. if (handschake[0] != 0x05) {
  7. printf("Unsuported Protocoll: %02x\n", handschake[0]); //TODO close
  8. return 1;
  9. }
  10. int alowedMethods = handschake[1];
  11. for (int i = 0; i < alowedMethods; i++) {
  12. if (SocketStreamUtils::readBytes(client, 1)[0] == 0x02) { //Username/Password
  13. alowedMethods = -1;
  14. }
  15. }
  16. if (alowedMethods != -1) {
  17. printf("Unsuported Methods!\n"); //TODO close
  18. return 2;
  19. }
  20. buffer = new char[2];
  21. buffer[0] = 0x05;
  22. buffer[1] = 0x02;
  23. SocketStreamUtils::writeBytes(client, buffer, 2);
  24.  
  25. if (SocketStreamUtils::readBytes(client, 1)[0] != 1) {
  26. printf("Auth fail (Wrong packet header)!\n"); //TODO close
  27. return 3;
  28. }
  29. char* username = SocketStreamUtils::readBytes(client,
  30. SocketStreamUtils::readBytes(client, 1)[0]);
  31. char* passwort;
  32. int length = SocketStreamUtils::readBytes(client, 1)[0];
  33. if (length != 0)
  34. passwort = SocketStreamUtils::readBytes(client, length);
  35. printf("Auth username: \"%s\" Auth password: \"%s\"\n", username, passwort);
  36. if (strcmp(username, properties->user)) {
  37. printf("Username wrong. Must be: \"%s\"\n", properties->user);
  38. buffer = new char[2];
  39. buffer[0] = 0x05; //Underknown
  40. buffer[1] = 0x01; //Username Fail
  41. return 4;
  42. }
  43. if (strcmp(passwort, properties->password)) {
  44. printf("Password wrong. Must be: \"%s\"\n", properties->password);
  45. buffer = new char[2];
  46. buffer[0] = 0x05; //Underknown
  47. buffer[1] = 0x02; //Passwort Fail
  48. return 5;
  49. }
  50. buffer = new char[2];
  51. buffer[0] = 0x05; //Underknown
  52. buffer[1] = 0x00; //Login sucesss
  53. SocketStreamUtils::writeBytes(client, buffer, 2);
  54. return 0;
  55. }
  56. int SocketClient::readSOCKS5Target() {
  57. printf("Connected (Select target)\n");
  58. char* commands = SocketStreamUtils::readBytes(client, 4);
  59. if (commands[1] != 0x01) {
  60. printf("Unsuported Operaion: %02x" + commands[1]); //TODO close
  61. writeCommandState(0x07);
  62. return 1;
  63. }
  64. int addrLength = -1;
  65. if (commands[3] == 0x01)
  66. addrLength = 4;
  67. else if (commands[3] == 0x03)
  68. addrLength = SocketStreamUtils::readBytes(client, 1)[0];
  69. //else if (commands[3] == 0x04)
  70. // addrLength = 16;
  71. else
  72. {
  73. writeCommandState(0x08);
  74. }
  75. printf("Address length: %d\n", addrLength);
  76. char* addr = SocketStreamUtils::readBytes(client, addrLength);
  77. if (commands[3] == 0x03) {
  78. remote_host = new char[strlen(addr)];
  79. snprintf(remote_host, strlen(addr) + 1, "%s", addr);
  80. remote_host =
  81. inet_ntoa(
  82. *((struct in_addr *) gethostbyname(remote_host)->h_addr_list[0]));
  83. } else if (commands[3] == 0x01) {
  84. remote_host = new char[15];
  85. int i = *((int*) addr);
  86. sprintf(remote_host, "%i.%i.%i.%i", (i >> 24) & 0xFF, (i >> 16) & 0xFF,
  87. (i >> 8) & 0xFF, i & 0xFF);
  88. } else {
  89. printf("Cant use Ipv6");
  90. writeCommandState(0x08);
  91. return 2;
  92. }
  93. remote_port = new char[12];
  94. snprintf(remote_port, 12, "%d", SocketStreamUtils::readShort(client));
  95.  
  96. writeCommandState(0x00);
  97. //PROT VERSION
  98. //1 = CONNECT
  99. //0
  100. printf("Target: %s:%s (Socket: 0x%016llX)\n", remote_host, remote_port,
  101. (long) &client);
  102.  
  103. /* Get the address info */
  104. memset(&hints, 0, sizeof hints);
  105. if (commands[3] == 0x03) {
  106. struct hostent *he;
  107. struct in_addr **addr_list;
  108. int i;
  109.  
  110. if ((he = gethostbyname(remote_host)) == NULL) {
  111. herror("gethostbyname");
  112. return 4; //TODO close
  113. }
  114. addr_list = (struct in_addr **) he->h_addr_list;
  115. for (i = 0; addr_list[i] != NULL; i++) {
  116. remote_host = inet_ntoa(*addr_list[i]);
  117. break;
  118. }
  119. hints.sin_family = AF_INET;
  120. hints.sin_addr.s_addr = inet_addr(remote_host);
  121. hints.sin_port = htons(atoi(remote_port));
  122. printf("Resolved: %s\n", remote_host);
  123. } else if (commands[3] == 0x01) {
  124. hints.sin_family = AF_INET;
  125. hints.sin_addr.s_addr = inet_addr("85.114.141.243");
  126. hints.sin_port = htons(25565);
  127. }
  128. return 0;
  129. }
  130. void SocketClient::writeCommandState(int state) {
  131. char* buffer = new char[10];
  132. buffer[0] = 0x05; //VERSION
  133. buffer[1] = state; //SUCESS 0x07
  134. buffer[2] = 0x00; //NEEDET UNUSED
  135. buffer[3] = 0x01; //IP-Type
  136. buffer[4] = 0x00; //IP[1]
  137. buffer[5] = 0x00; //IP[2]
  138. buffer[6] = 0x00; //IP[3]
  139. buffer[7] = 0x00; //IP[4]
  140. buffer[8] = 0x00; //PORT[1]
  141. buffer[9] = 0x00; //PORT[2]
  142. SocketStreamUtils::writeBytes(client, buffer, 10);
  143. }
  144. unsigned int SocketClient::transfer(int from, int to) {
  145. char buf[BUF_SIZE];
  146. unsigned int disconnected = 0;
  147. size_t bytes_read, bytes_written;
  148. bytes_read = read(from, buf, BUF_SIZE);
  149. if (bytes_read == 0) {
  150. disconnected = 1;
  151. } else {
  152. bytes_written = write(to, buf, bytes_read);
  153. if (bytes_written == -1) {
  154. disconnected = 1;
  155. }
  156. }
  157. return disconnected;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement