Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. // License : MIT
  2. // Yaseen Mohamed Twati
  3.  
  4. #ifdef _WIN32           // Windows
  5. #include <winsock2.h>
  6. #include <ws2tcpip.h>
  7. #define MSG_NOSIGNAL 0
  8. #else                   // Linuc + Max
  9.  
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <arpa/inet.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <netdb.h>
  17. #include <errno.h>
  18. #include <err.h>
  19.  
  20. #define SOCKET_ERROR    -1
  21. #define INVALID_SOCKET  -1
  22.  
  23. typedef int         SOCKET;
  24. typedef sockaddr    SOCKADDR;
  25. typedef sockaddr_in SOCKADDR_IN;
  26.  
  27. #define closesocket close
  28.  
  29. #ifdef __APPLE__
  30.  
  31. #define MSG_NOSIGNAL 0
  32.  
  33. #endif
  34.  
  35. #endif
  36.  
  37. #include <iostream>
  38.  
  39. // todo: Replace with http://stackoverflow.com/questions/24611215/one-liner-for-raii-on-non-pointer ?
  40. // From http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
  41. struct WndSocketDeleter
  42. {
  43.     typedef SOCKET pointer;
  44.  
  45.     void operator()(SOCKET h)
  46.     {
  47.         ::closesocket(h);
  48.     }
  49. };
  50.  
  51. typedef std::unique_ptr<SOCKET, WndSocketDeleter> unique_socket;
  52. bool operator==(const unique_socket& lhs, const SOCKET &rhs);
  53. bool operator!=(const unique_socket& lhs, const SOCKET &rhs);
  54.  
  55.  
  56. SOCKET setup_tor(const char* host, const unsigned short port)
  57. {
  58.     const unsigned short TOR_PORT = 9050;
  59.     const char* TOR_IP = "127.0.0.1";
  60.    
  61.     SOCKET      sock;
  62.     if((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  63.         return INVALID_SOCKET;
  64.        
  65.     SOCKADDR_IN sockAddr;
  66.     sockAddr.sin_family       = AF_INET;
  67.     sockAddr.sin_port         = htons(TOR_PORT);
  68.     sockAddr.sin_addr.s_addr  = inet_addr(TOR_IP);
  69.  
  70.     if(SOCKET_ERROR == connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR_IN)))
  71.         return INVALID_SOCKET;
  72.    
  73.     const char AUTH_PACKET[3] =
  74.     {
  75.         0x05, // SOCKS 5
  76.         0x01, // One Authentication Method
  77.         0x00  // No AUthentication
  78.     };
  79.     send(sock, AUTH_PACKET, sizeof(AUTH_PACKET), MSG_NOSIGNAL); //! todo error handling
  80.    
  81.     char Resp1[2];
  82.     recv(sock, Resp1, 2, 0);  //! todo error handling
  83.     if(Resp1[1] != 0x00)
  84.         return INVALID_SOCKET;
  85.    
  86.     //! todo IPv6
  87.     // Is it an IP?
  88.     if(INADDR_NONE != inet_addr(host))
  89.     {
  90.         const char IP_CONNECTION[10] = {
  91.              0x05, // SOCKS5
  92.              0x01, // CONNECT
  93.              0x00, // RESERVED
  94.              0x01, // IPV4
  95.             };
  96.            
  97.         unsigned long ip = inet_addr(host);
  98.         memcpy(IP_CONNECTION+4, &ip, sizeof(ip));
  99.         memcpy(IP_CONNECTION+4+sizeof(ip), &Port, 2);
  100.         send(sock, IP_CONNECTION, sizeof(IP_CONNECTION), 0); //! todo error handling
  101.     }
  102.     else
  103.     {
  104.         const char DOMAIN_CONNECTION[4] = {
  105.           0x05, // SOCKS5
  106.           0x01, // CONNECT
  107.           0x00, // RESERVED
  108.           0x03, // DOMAIN
  109.         };
  110.         const unsigned char domainLen = strlen(host);
  111.        
  112.        
  113.         /*! todo error handling */
  114.         send(sock, DOMAIN_CONNECTION, sizeof(DOMAIN_CONNECTION), 0);
  115.         send(sock, &domainLen, 1, 0);
  116.         send(sock, host, domainLen, 0);
  117.         send(sock, &port, sizeof(port), MSG_NOSIGNAL);
  118.     }
  119.    
  120.     char Resp2[10];
  121.     recv(sock, Resp2, 10, 0);
  122.     if(Resp2[1] != 0x00)
  123.     {    
  124.         return INVALID_SOCKET;
  125.     }
  126.  
  127.     return sock;
  128. }
  129.  
  130. int main()
  131. {
  132.     std::cout << " [*] C++ Tor Exmaple " << std::endl;
  133.     std::cout << " [*] By Yaseen Eltii " << std::endl;
  134.     std::cout << " [*] Connecting " << std::endl;
  135.  
  136.    
  137.     SOCKET sock = setup_tor("104.236.192.175", 80);
  138.     if(sock == INVALID_SOCKET)
  139.         return 1;
  140.  
  141.  
  142.     std::cout << " [*] Connected " << std::endl;
  143.  
  144.     // Here you can normally use send and recv
  145.     // Testing With a HTTP GET Request
  146.     std::cout << " [*] Testing with GET Request \n" << std::endl;
  147.     send(sock, "GET / \n\r\n\r", strlen("GET / \n\r\n\r"), MSG_NOSIGNAL);
  148.     char RecvBuffer[2048];
  149.     size_t Rcved = recv(sock, RecvBuffer, 2048, 0);
  150.     std::cout.write(RecvBuffer, Rcved);
  151.  
  152.     std::cout << std::endl;
  153.  
  154.     return(0);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement