Information:
So, I have been bored for a while and I was flipping through 'My Threads' and saw the Simple port scanner thread I made a while ago; It wasn't the best because it had limitations. Limitations such as one port at a time. So I decided to crank it up to make it multiple ports at a time. Also, I would like to mention that I am still learning Network Programming, Sockets, and all that good stuff, so please use constructive criticism instead of flaming. :)
How to use it
-You have two options.
-Option 1 gives you the ability to scan list of ports. For example: 1337 - 1600 scans all the numbers between 1337 to 1600.
-Option 2 lets you choose specific ports; This is done by using Dynamic Memory Allocation.
Source: http://pastebin.com/T0FBHaxs
#include#include #pragma comment(lib, "ws2_32.lib"); int main () { int option; int numOfPorts; int *portsArr; sockaddr_in sin; std::cout << "1. List(ex: x - y)\n"; std::cout << "2. Specific\n"; std::cout << "Option: "; std::cin >> option; if(option == 1) { WSADATA wsData; if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0) { std::cout << "Error: Cannot startup\n"; std::cin.get(); std::cin.get(); return 0; } int firstPort, secondPort; std::cout << "First port: "; std::cin >> firstPort; std::cout << "Second port: "; std::cin >> secondPort; for(int i = firstPort; i <= secondPort; i++) { SOCKET pSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(pSocket == INVALID_SOCKET) { std::cout << "Error: Invalid Socket\n"; std::cin.get(); std::cin.get(); return 0; } ZeroMemory(&sin, sizeof(sin)); sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); sin.sin_family = AF_INET; sin.sin_port = htons(i); if(connect(pSocket, (sockaddr*)&sin, sizeof(sin)) != SOCKET_ERROR) { std::cout << "Port: " << i << " is open!\n"; } else { std::cout << "Port: " << i << " is closed!\n"; } closesocket(pSocket); } } else if(option == 2) { WSADATA wsData; if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0) { std::cout << "Error: Cannot startup\n"; std::cin.get(); std::cin.get(); return 0; } std::cout << "How many ports would you like to scan?:"; std::cin >> numOfPorts; if(numOfPorts <= 0) { std::cout << "Ports must be greater than 0"; } else { portsArr = new int[numOfPorts]; for(int i = 0; i < numOfPorts; i++) { std::cout << "Port[" << i << "]: "; std::cin >> portsArr[i]; } for(int i = 0; i < numOfPorts; i++) { SOCKET pSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(pSocket == INVALID_SOCKET) { std::cout << "Error: Invalid Socket\n"; std::cin.get(); std::cin.get(); return 0; } ZeroMemory(&sin, sizeof(sin)); sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); sin.sin_family = AF_INET; sin.sin_port = htons(portsArr[i]); if(connect(pSocket, (sockaddr*)&sin, sizeof(sin)) != SOCKET_ERROR) { std::cout << "Port: " << portsArr[i] << " is open!\n"; } else { std::cout << "Port: " << portsArr[i] << " is closed!\n"; } closesocket(pSocket); } } delete [] portsArr; } else { std::cout << "Enter a valid option!\n"; std::cin.get(); std::cin.get(); return 0; } std::cin.get(); std::cin.get(); return 0; }
Thanks,
Sri Krishna
P.S. I know the code isn't that great; So, please give me some way to optimize my code.
No comments:
Post a Comment