VB icon

A Simple but effective port scanner

Email
Submitted on: 1/7/2015 5:04:00 PM
By: zer0python (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: C, UNIX C++
Views: 2084
 
     This port scanner is pretty simple, it just fork()'s each connect() call, and reads the return value.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: A Simple but effective port scanner
// Description:This port scanner is pretty simple, it just fork()'s each connect() call, and reads the return value.
// By: zer0python (from psc cd)
//**************************************

/* simple connect port scanner.. -- very fast .. very detectable... */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <netdb.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/poll.h>
static int verbose = 0;
enum port_e {
	P_ERROR = 0,
	P_CLOSED = 1,
	P_OPEN = 2,
};
enum port_e chkport(struct sockaddr_in addr);
//int v_printf(const char *fmt, ...);	/* verbose printf */
#define v_printf(x)	if(verbose) printf x
int main(int argc, char *argv[])
{
	int index = 1, i;
	struct sockaddr_in addr;
	struct hostent *hp;
	if(argc < 2) {
		fprintf(stderr, "Usage:\n\t%s [-v] <host>\n", argv[0]);
		return 0;
	}
	if((argv[1][0] == '-') && argv[1][1] == 'v')
		verbose = index++;
	if(index != 1 && argc == 2) {
		fprintf(stderr, "missing host\n");
		return 0;
	}
	hp = gethostbyname(argv[index]);
	if(!hp) {
		fprintf(stderr, "could not lookup host\n");
		return 0;
	}
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = PF_INET;
	memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
	printf("Scanning Host %s\n", argv[index]);
	clock_t st = clock();
	for(i = 1; i <= 65535; i++) {
		addr.sin_port = htons(i);
		if(!fork()) {
			enum port_e p = chkport(addr);
			switch(p) {
				case P_OPEN: printf("%-4d OPEN\n", i); break;
				case P_CLOSED: if(verbose) printf("%-4d CLOSED\n", i); break;
				case P_ERROR: if(verbose) printf("%-4d ERROR\n", i); break;
			}
			exit(0);
		}
	}
	printf("Done in %.2lf seconds.\n", (float) (clock() - st) / CLOCKS_PER_SEC);
	return 0;
}
enum port_e chkport(struct sockaddr_in addr)
{
	int sd = socket(PF_INET, SOCK_STREAM, 0);
	enum port_e prtst = P_OPEN;
	if(sd < 0)
		return P_ERROR;
/*
	if(fcntl(sd, F_SETFL, O_NONBLOCK) < 0) {
		close(sd);
		return P_ERROR;
	}
*/
	if(connect(sd, (struct sockaddr*) &addr, sizeof(addr)))
		return P_CLOSED;
	shutdown(sd, 2);
	close(sd);
	return prtst;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.