Advertisement
zergon321

R-SCAN

Aug 27th, 2017
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.54 KB | None | 0 0
  1. #include "winsock_error.h"
  2. #include "io_extended.h"
  3. #include <WS2tcpip.h>
  4.  
  5. #pragma comment(lib, "ws2_32.lib")
  6. #pragma warning(disable : 4996)
  7.  
  8. int main()
  9. {
  10.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
  11.  
  12.     std::cout << "R-Scan v2.0 by NightGhost" << std::endl
  13.         << "the port scanner" << std::endl
  14.         << "supports only IPv4 addresses" << std::endl << std::endl;
  15.  
  16.     WSADATA filler;
  17.  
  18.     if (WSAStartup(WSA_VERSION, &filler))
  19.     {
  20.         output_error("Couldn't initialize WSA");
  21.         return EXIT_FAILURE;
  22.     }
  23.  
  24.     std::cout << "R-Scan is ready. You must enter a remote host name and number of ports (65535 to scan all of them)" << std::endl
  25.         << "or the range of ports, for example: 49152-65535" << std::endl
  26.         << "to scan only one port, you must type its number with prefix \">\", for example: >80" << std::endl
  27.         << "after this you must press SPACE and specify a waiting time in seconds, for example: 1024 1 - to scan ports from 1 to 1024 with 1 sec for every address" << std::endl
  28.         << "waiting time is a time interval allotted for scanning every single address for paricular port" << std::endl
  29.         << "for a most complete result you need to perform scanning of the same range with the different time intervals" << std::endl
  30.         << "it's preferable to choose time intervals from 1 to 4 secs, but all depends on many factors, so this decision is left at your discretion" << std::endl
  31.         << std::endl;
  32.  
  33.     char host_name[MAXGETHOSTSTRUCT];
  34.  
  35.     enter_message("Please enter a host name: ", host_name);
  36.     std::cout << std::endl;
  37.  
  38.     HOSTENT* remote_host_info = gethostbyname(host_name);
  39.  
  40.     if (remote_host_info == NULL)
  41.     {
  42.         output_error("Couldn't get a host info");
  43.         finalize();
  44.  
  45.         return EXIT_FAILURE;
  46.     }
  47.  
  48.     if (remote_host_info->h_addrtype == AF_INET6)
  49.     {
  50.         output_error("Sorry, but R-Scan can't perform scanning of ipv6-hosts");
  51.         finalize();
  52.  
  53.         return EXIT_SUCCESS;
  54.     }
  55.  
  56.     //ALIASES SHOWING
  57.     if (remote_host_info->h_aliases[0])
  58.     {
  59.         std::cout << "Server name aliases: " << std::endl;
  60.  
  61.         for (int i = 0; remote_host_info->h_aliases[i]; i++)
  62.             std::cout << remote_host_info->h_aliases[i] << std::endl;
  63.  
  64.         std::cout << std::endl;
  65.     }
  66.     else
  67.         std::cout << "Host has no aliases" << std::endl << std::endl;
  68.  
  69.     in_addr tmp;
  70.  
  71.     //ADDRESSES SHOWING
  72.     if (remote_host_info->h_addr_list[1])
  73.     {
  74.         std::cout << "Server addresses: " << std::endl;
  75.  
  76.         for (int i = 0; remote_host_info->h_addr_list[i]; i++)
  77.         {
  78.             tmp.s_addr = *(u_long*)remote_host_info->h_addr_list[i];
  79.             std::cout << inet_ntoa(tmp) << std::endl;
  80.         }
  81.  
  82.         std::cout << std::endl;
  83.     }
  84.     else
  85.     {
  86.         tmp.s_addr = *(u_long*)remote_host_info->h_addr;
  87.         std::cout << inet_ntoa(tmp) << std::endl << std::endl;
  88.     }
  89.  
  90.     port_range range;
  91.     int waiting_time;
  92.  
  93.     do
  94.     {
  95.         std::cout << "Please enter a number of ports to scan: ";
  96.         parse_input(std::cin, range, waiting_time);
  97.  
  98.         if (range.error_flag)
  99.             output_error(range.error_message);
  100.     } while (range.error_flag);
  101.  
  102.     con_list cons;
  103.     sockaddr_in server_data;
  104.     u_long cmd = 1; //command for ioctlsocket()
  105.     fd_set write;
  106.     timeval wait_time = { 0, 0 };
  107.     USHORT port = range.from;
  108.  
  109.     server_data.sin_family = AF_INET;
  110.     tmp.s_addr = *(u_long*)remote_host_info->h_addr;
  111.     inet_pton(AF_INET, (PCSTR)inet_ntoa(tmp), &server_data.sin_addr);
  112.  
  113.     while (true)
  114.     {
  115.         FD_ZERO(&write);
  116.         delete_invalid_cons(cons);
  117.  
  118.         //CONNECTION PHASE
  119.         if (port <= range.to && cons.size() < FD_SETSIZE)
  120.         {
  121.             SOCKET sock = TCP_SOCKET;
  122.             server_data.sin_port = htons(port);
  123.  
  124.             if (sock == INVALID_SOCKET)
  125.             {
  126.                 output_error("Couldn't create scan socket");
  127.                 finalize();
  128.  
  129.                 return EXIT_FAILURE;
  130.             }
  131.  
  132.             if (ioctlsocket(sock, FIONBIO, &cmd) == SOCKET_ERROR)
  133.             {
  134.                 error_to_close_cons("Couldn't put the socket in asynchronous mode", cons);
  135.                 error_to_close_socket("", sock);
  136.                 finalize();
  137.  
  138.                 return EXIT_FAILURE;
  139.             }
  140.  
  141.             cons.push_back({ sock, port, 0, clock() });
  142.  
  143.             if (connect(sock, (SOCKADDR*)&server_data, sizeof(server_data)) == SOCKET_ERROR)
  144.                 if (WSAGetLastError() != WSAEWOULDBLOCK)
  145.                 {
  146.                     error_to_close_cons("Couldn't connect", cons);
  147.                     error_to_close_socket("", sock);
  148.                     finalize();
  149.  
  150.                     return EXIT_FAILURE;
  151.                 }
  152.            
  153.             port++;
  154.         }
  155.  
  156.         //SCAN PHASE
  157.  
  158.         if (cons.empty()) //it must be here because connection initializing and connection confirming are performed in the same loop,
  159.             break; //so if not the all connections are initialized but all the rest are confirmed then this insruction, being placed in the beginning or in the end of loop,
  160.                    //could complete scanning too early
  161.        
  162.         for (u_int i = 0; i < cons.size(); i++)
  163.             FD_SET(cons[i].sock, &write);
  164.  
  165.         if (select(0, NULL, &write, NULL, &wait_time) == SOCKET_ERROR)
  166.         {
  167.             error_to_close_cons("Couldn't perform client loop", cons);
  168.             finalize();
  169.  
  170.             return EXIT_FAILURE;
  171.         }
  172.  
  173.         for (u_int i = 0; i < cons.size(); i++)
  174.             if (FD_ISSET(cons[i].sock, &write))
  175.             {
  176.                 tmp.s_addr = *(u_long*)remote_host_info->h_addr_list[cons[i].address_num];
  177.                 std::cout << "Connection established on " << inet_ntoa(tmp) << ':' << cons[i].con_port << " in " << (clock() - cons[i].con_time) / CLOCKS_PER_SEC << " seconds" << std::endl;
  178.  
  179.                 close_socket(cons[i].sock);
  180.                 cons[i].sock = INVALID_SOCKET;
  181.             }
  182.             else if (clock() > cons[i].con_time + waiting_time * CLOCKS_PER_SEC) //if connection time exceeded
  183.             {
  184.                 cons[i].address_num++;
  185.  
  186.                 if (remote_host_info->h_addr_list[cons[i].address_num]) //if there's unscanned addr - change addr
  187.                 {
  188.                     server_data.sin_port = htons(port);
  189.                     tmp.s_addr = *(u_long*)remote_host_info->h_addr_list[cons[i].address_num];
  190.                     inet_pton(AF_INET, (PCSTR)inet_ntoa(tmp), &server_data.sin_addr);
  191.  
  192.                     close_socket(cons[i].sock);
  193.                     cons[i].sock = TCP_SOCKET;
  194.  
  195.                     if (ioctlsocket(cons[i].sock, FIONBIO, &cmd) == SOCKET_ERROR)
  196.                     {
  197.                         error_to_close_cons("Couldn't put the socket in asynchronous mode", cons);
  198.                         error_to_close_socket("", cons[i].sock);
  199.                         finalize();
  200.  
  201.                         return EXIT_FAILURE;
  202.                     }
  203.  
  204.                     if (connect(cons[i].sock, (SOCKADDR*)&server_data, sizeof(server_data)) == SOCKET_ERROR)
  205.                         if (WSAGetLastError() != WSAEWOULDBLOCK)
  206.                         {
  207.                             error_to_close_cons("Couldn't connect", cons);
  208.                             error_to_close_socket("", cons[i].sock);
  209.                             finalize();
  210.  
  211.                             return EXIT_FAILURE;
  212.                         }
  213.                 }
  214.                 else //else there's no application is using this port
  215.                 {
  216.                     close_socket(cons[i].sock);
  217.                     cons[i].sock = INVALID_SOCKET;
  218.                 }
  219.             }
  220.     }
  221.  
  222.     std::cout << "Scan complete" << std::endl;
  223.     finalize();
  224.  
  225.     return EXIT_SUCCESS;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement