/* * * * * * * * * * * * * * * * * *
 *
 * propecia2.c - a fast class A port scanner
 *  
 * usage: ./propecia2 <x> <port>
 * 
 * * * *
 * 
 * This is a hack of propecia.c to include class A domain scanning 
 * and banner grabbing. Originally, propecia.c was coded with only
 * class C domain scanning.
 *
 * by John Martinelli
 *    john@martinelli.com
 *    john-martinelli.com
 *
 * * * * *
 *
 * April 15, 2007
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <netinet/in.h>

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

	int sockfd, result, counter1, counter2, counter3;
	char host[15], banner[512];
	char *classa;
	int port;

	struct sockaddr_in address;

	if (argc < 3)
	{
		printf ("Usage: %s <x> <port>\n", argv[0]);
		exit (1);
	}

	port = atoi(argv[2]);
	classa = argv[1];
	sprintf(host, "%s.255.255.255", classa);
	printf("\n");
	if(!inet_aton(host, &address.sin_addr))
	{
		printf("Invalid Class A Address\n");
		exit(1);
	}

	for (counter1 = 1; counter1 <= 255; counter1++)
	{
	counter2 = 1;
 	  for (counter2 = 1; counter2 <= 255; counter2++)
	  {
	    counter3 = 1;
	    for (counter3 = 1; counter3 <= 255; counter3++)
	    {
		sprintf (host, "%s.%d.%d.%d\n", classa, counter1, counter2, counter3);
		if ((fork ()) == 0)
		{
		  address.sin_family = AF_INET;
		  address.sin_port = htons (port);
 		  address.sin_addr.s_addr = inet_addr (host);
 
		  sockfd = socket (AF_INET, SOCK_STREAM, 0);

		  if (sockfd < 0)
		  {
		    perror ("Socket");
		    exit (2);
		  }

		  alarm (3);
		  result = connect (sockfd, (struct sockaddr *) &address, sizeof(address));

		  if (result == 0)
  		  {
	  	    memset(banner, 0, sizeof(banner));
		    read(sockfd, &banner, 512);
		    printf ("[$] Port %s open on %s    Banner: %s\n", argv[2], host, banner);
		    close (sockfd);
		    exit (0);
		  } } } }

	sleep(1);
	close (sockfd);
	exit (0);
} }
