Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Simple C++/SFML port scanner - C++ Code Bank


Simple C++/SFML port scanner
A simple CLI port scanner written in C++ using the SFML library.
                #include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<SFML/Network.hpp>
#include<SFML/System.hpp>

std::ostream* out=NULL;
std::string ip;
unsigned short min=1, max=65535;
bool portOpen=false, verbose=false;
sf::Mutex GlobalMutex;
unsigned short step=1;

int usage(){

	std::cout << "Usage:\n\tportscan [options] <ip>" << std::endl
	<< "Options:" << std::endl
	<< "\tv\t\tList unopened ports as closed.\n"
	<< "\ti\t\tSet the first port to scan to the following argument.\n"
	<< "\te\t\tSet the last port to scan to the following argument.\n"
	<< "\to\t\tOutput to file specified in following argument." << std::endl;

	return 0;

}

void scanOdd(){

	for(unsigned short port=(min+1);port<max;port+=2){
	
		sf::TcpSocket sock;

		if(sock.Connect(ip,port)==sf::Socket::Done){

		sf::Lock l(GlobalMutex);
		(*out) << "Port open: " << port << std::endl;

		}
		else if(verbose){

		sf::Lock l(GlobalMutex);
		(*out) << "Port closed: " << port << std::endl;

		}

	}

}

int main(int argc, char** argv){

	sf::Thread t(&scanOdd);
	
	if(argc<2){

		return usage();

	}
	for(int i=1;i<argc;i++){

		if(strcmp(argv[i],"-v")==0)
			verbose=true;
		else if(strcmp(argv[i],"-i")==0){
			if(!(argc>=(i+2))) return usage();

			std::string temp;
			std::stringstream s;
			temp=std::string(argv[++i]);
			s << temp << std::endl;
			s >> min;

		}
		else if(strcmp(argv[i],"-e")==0){
			if(!(argc>=(i+2))) return usage();

			std::string temp;
			std::stringstream s;
			temp=std::string(argv[++i]);
			s << temp << std::endl;
			s >> max;

		}
		else if(strcmp(argv[i],"-o")==0){
			if(!(argc>=(i+2))) return usage();

			out=new std::ofstream(argv[++i]);

		}
		else if(ip=="")
			ip=argv[i];
		else return usage();

	}

	if(ip=="") return usage();

	if(max<min) return usage();
	if(out==NULL) out=&std::cout;

	if(max-min>1000){
		
		step=2;
		t.Launch();

	}
	(*out) << "Scanning ports " << min << " through " << max
		  << " at " << ip << std::endl;
	sf::Clock c;
	c.Reset();
	for(unsigned short port=min;port<max;port+=step){

		sf::TcpSocket sock;
		if(sock.Connect(ip, port)==sf::Socket::Done){

			sf::Lock l(GlobalMutex);
			portOpen=true;
			(*out) << "Port open: " << port << std::endl;		
		}
		else if(verbose){

			sf::Lock l(GlobalMutex);
			(*out) << "Port closed: " << port << std::endl;

		}
		
	}

	if(!portOpen&&min==max)
	(*out) << "Port " << min << " is not open." << std::endl;
	else if(!portOpen)
	(*out) << "No open ports, or target is not connected." << std::endl;

	(*out) << "Time elapsed: " << c.GetElapsedTime() << std::endl;
			
}


            
Comments
Sorry but there are no comments to display