Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "winsock_error.h"
- #include "io_extended.h"
- #include <WS2tcpip.h>
- #pragma comment(lib, "ws2_32.lib")
- #pragma warning(disable : 4996)
- int main()
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
- std::cout << "R-Scan v2.0 by NightGhost" << std::endl
- << "the port scanner" << std::endl
- << "supports only IPv4 addresses" << std::endl << std::endl;
- WSADATA filler;
- if (WSAStartup(WSA_VERSION, &filler))
- {
- output_error("Couldn't initialize WSA");
- return EXIT_FAILURE;
- }
- 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
- << "or the range of ports, for example: 49152-65535" << std::endl
- << "to scan only one port, you must type its number with prefix \">\", for example: >80" << std::endl
- << "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
- << "waiting time is a time interval allotted for scanning every single address for paricular port" << std::endl
- << "for a most complete result you need to perform scanning of the same range with the different time intervals" << std::endl
- << "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
- << std::endl;
- char host_name[MAXGETHOSTSTRUCT];
- enter_message("Please enter a host name: ", host_name);
- std::cout << std::endl;
- HOSTENT* remote_host_info = gethostbyname(host_name);
- if (remote_host_info == NULL)
- {
- output_error("Couldn't get a host info");
- finalize();
- return EXIT_FAILURE;
- }
- if (remote_host_info->h_addrtype == AF_INET6)
- {
- output_error("Sorry, but R-Scan can't perform scanning of ipv6-hosts");
- finalize();
- return EXIT_SUCCESS;
- }
- //ALIASES SHOWING
- if (remote_host_info->h_aliases[0])
- {
- std::cout << "Server name aliases: " << std::endl;
- for (int i = 0; remote_host_info->h_aliases[i]; i++)
- std::cout << remote_host_info->h_aliases[i] << std::endl;
- std::cout << std::endl;
- }
- else
- std::cout << "Host has no aliases" << std::endl << std::endl;
- in_addr tmp;
- //ADDRESSES SHOWING
- if (remote_host_info->h_addr_list[1])
- {
- std::cout << "Server addresses: " << std::endl;
- for (int i = 0; remote_host_info->h_addr_list[i]; i++)
- {
- tmp.s_addr = *(u_long*)remote_host_info->h_addr_list[i];
- std::cout << inet_ntoa(tmp) << std::endl;
- }
- std::cout << std::endl;
- }
- else
- {
- tmp.s_addr = *(u_long*)remote_host_info->h_addr;
- std::cout << inet_ntoa(tmp) << std::endl << std::endl;
- }
- port_range range;
- int waiting_time;
- do
- {
- std::cout << "Please enter a number of ports to scan: ";
- parse_input(std::cin, range, waiting_time);
- if (range.error_flag)
- output_error(range.error_message);
- } while (range.error_flag);
- con_list cons;
- sockaddr_in server_data;
- u_long cmd = 1; //command for ioctlsocket()
- fd_set write;
- timeval wait_time = { 0, 0 };
- USHORT port = range.from;
- server_data.sin_family = AF_INET;
- tmp.s_addr = *(u_long*)remote_host_info->h_addr;
- inet_pton(AF_INET, (PCSTR)inet_ntoa(tmp), &server_data.sin_addr);
- while (true)
- {
- FD_ZERO(&write);
- delete_invalid_cons(cons);
- //CONNECTION PHASE
- if (port <= range.to && cons.size() < FD_SETSIZE)
- {
- SOCKET sock = TCP_SOCKET;
- server_data.sin_port = htons(port);
- if (sock == INVALID_SOCKET)
- {
- output_error("Couldn't create scan socket");
- finalize();
- return EXIT_FAILURE;
- }
- if (ioctlsocket(sock, FIONBIO, &cmd) == SOCKET_ERROR)
- {
- error_to_close_cons("Couldn't put the socket in asynchronous mode", cons);
- error_to_close_socket("", sock);
- finalize();
- return EXIT_FAILURE;
- }
- cons.push_back({ sock, port, 0, clock() });
- if (connect(sock, (SOCKADDR*)&server_data, sizeof(server_data)) == SOCKET_ERROR)
- if (WSAGetLastError() != WSAEWOULDBLOCK)
- {
- error_to_close_cons("Couldn't connect", cons);
- error_to_close_socket("", sock);
- finalize();
- return EXIT_FAILURE;
- }
- port++;
- }
- //SCAN PHASE
- if (cons.empty()) //it must be here because connection initializing and connection confirming are performed in the same loop,
- 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,
- //could complete scanning too early
- for (u_int i = 0; i < cons.size(); i++)
- FD_SET(cons[i].sock, &write);
- if (select(0, NULL, &write, NULL, &wait_time) == SOCKET_ERROR)
- {
- error_to_close_cons("Couldn't perform client loop", cons);
- finalize();
- return EXIT_FAILURE;
- }
- for (u_int i = 0; i < cons.size(); i++)
- if (FD_ISSET(cons[i].sock, &write))
- {
- tmp.s_addr = *(u_long*)remote_host_info->h_addr_list[cons[i].address_num];
- std::cout << "Connection established on " << inet_ntoa(tmp) << ':' << cons[i].con_port << " in " << (clock() - cons[i].con_time) / CLOCKS_PER_SEC << " seconds" << std::endl;
- close_socket(cons[i].sock);
- cons[i].sock = INVALID_SOCKET;
- }
- else if (clock() > cons[i].con_time + waiting_time * CLOCKS_PER_SEC) //if connection time exceeded
- {
- cons[i].address_num++;
- if (remote_host_info->h_addr_list[cons[i].address_num]) //if there's unscanned addr - change addr
- {
- server_data.sin_port = htons(port);
- tmp.s_addr = *(u_long*)remote_host_info->h_addr_list[cons[i].address_num];
- inet_pton(AF_INET, (PCSTR)inet_ntoa(tmp), &server_data.sin_addr);
- close_socket(cons[i].sock);
- cons[i].sock = TCP_SOCKET;
- if (ioctlsocket(cons[i].sock, FIONBIO, &cmd) == SOCKET_ERROR)
- {
- error_to_close_cons("Couldn't put the socket in asynchronous mode", cons);
- error_to_close_socket("", cons[i].sock);
- finalize();
- return EXIT_FAILURE;
- }
- if (connect(cons[i].sock, (SOCKADDR*)&server_data, sizeof(server_data)) == SOCKET_ERROR)
- if (WSAGetLastError() != WSAEWOULDBLOCK)
- {
- error_to_close_cons("Couldn't connect", cons);
- error_to_close_socket("", cons[i].sock);
- finalize();
- return EXIT_FAILURE;
- }
- }
- else //else there's no application is using this port
- {
- close_socket(cons[i].sock);
- cons[i].sock = INVALID_SOCKET;
- }
- }
- }
- std::cout << "Scan complete" << std::endl;
- finalize();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement