/* Created by: Giorgos Kappes */ /* Year of first version: 2008 */ /* For any question mail to: g_kappes@hotmail.com */ /* This is open source software */ /****************************************************************/ /* Compile: gcc tcpscan.c -o tcpscan */ /****************************************************************/ #include /* perror() and errno */ #include /* required by getprotobyname() */ #include #include /* both required by socket() */ #include /* define sockaddr_in */ #include /* memset() */ #include #include #include #include #define PORT_MAX 65535 /* Max port number */ #define TIMEOUT_TIME 2 /* Timeout time */ extern int errno; extern int h_errno; void PrintHelp() { printf("-------------------------------------------------------------\n"); printf(" TCPSCAN VERSION: 1.0\n"); printf("-------------------------------------------------------------\n"); printf("Created by: Giorgos Kappes\n"); printf("Year of first version: 2008\n"); printf("For any question mail to: g_kappes@hotmail.com\n"); printf("This is open source software\n"); printf("-------------------------------------------------------------\n"); printf(" SOME HELP\n"); printf("-------------------------------------------------------------\n"); printf("SYNTAX\n--> tcpscan [host name/ ip address]\n"); printf("EXAMPLES\ntcpscan 85.167.12.10 \n"); printf("tcpscan scylla.cs.uoi.gr \n"); printf("-------------------------------------------------------------\n"); } main(int argc, char *argv[]) { /* VARIABLES */ int sd; /* our socket descriptor */ int rval; /* our connect descriptor */ int port; /* the connection port */ int flags; fd_set myset; struct timeval tv; socklen_t lon; int valopt; int lp,hp,ans,open=0,t_time=TIMEOUT_TIME; /* Structure to store pointer to protocol name in /etc/protocol*/ struct protoent *protocol; /* structure to store ip address and port for connection */ struct sockaddr_in socketaddr; /* structure to setup the address of the remote host */ struct hostent *hostaddr; /* ----------------------------------------------------------- */ /* CHECK PROGRAM RUN */ /* Check The arguments */ if (argc<2) { /* No arguments given. We must print program */ PrintHelp(); /* information */ exit(0); } else if (argc<2) { printf("ERROR - Missing argument.\n"); exit(0); } else if (argc>2) { printf("ERROR - Too much arguments.\n"); exit(0); } /* ----------------------------------------------------------- */ /* INITIALIZE PROTOCOL - SOCKET AND CONNECT */ /* initialize the protocol pointer by calling getprotobyname */ protocol = getprotobyname( "tcp" ); if ( !protocol ) { perror( "getprotobyname()" ); return (errno); } printf("Ports to scan: 0 - %d\n",PORT_MAX); lp = 0; hp = PORT_MAX; ans = 2; while (ans!=1) { printf("\n1. Contiune\n2. Specify new port range\n3. Scan a single port\n4. Set Timeout time\n\n> "); scanf("%d",&ans); while(ans<1 || ans > 4) { printf("\nWrong answer! Try Again!\n\n"); printf("\n1. Contiune\n2. Specify new port range\n3. Scan a single port\n4. Set Timeout time\n\n> "); scanf("%d",&ans); } if (ans == 2) { printf("Lower port: "); scanf("%d",&lp); printf("Highest port: "); scanf("%d",&hp); } else if (ans == 3) { printf("Port: "); scanf("%d",&hp); lp=hp; } else if (ans == 4){ printf("Timeout time: The default is 2 sec. For lower time the program will be faster, but for higher time the results may be more accurate\n"); printf("Timeout time [1-10]: "); scanf("%d",&t_time); while (t_time<1 || t_time > 10) { printf("Wrong time. Try again.\n"); printf("Timeout time [1-10]: "); scanf("%d",&t_time); } printf("\nTimeout time set to: %d sec\n",t_time); } } printf("\nPorts to scan: %d - %d\n",lp,hp); printf("\nPort Scanning: %s. . .\n\n",argv[1]); printf("-------------------------------------------\n"); printf(" RESULTS\n"); printf("-------------------------------------------\n"); for (port=lp;port<=hp;port++) { /* build our socket */ sd = socket( PF_INET, SOCK_STREAM, protocol->p_proto ); if ( sd == -1 ) { perror( "socket()" ); return (errno); } /* make our socket non blocking to let us handle timeout */ fcntl(sd, F_SETFL, (flags = fcntl(sd,F_GETFL)) | O_NONBLOCK ); /* At this point, all we have done is created a means for * * communicating on the network, but we are not connected to * * anyone at this point. Before we can do that, we must decide * * who it is we want to talk to and establish a connection. */ /* Initialize socketaddr */ memset( &socketaddr, 0, sizeof(socketaddr)); /* initialize it */ socketaddr.sin_family = AF_INET; /* set the fam type to Internet */ socketaddr.sin_port = htons( port ); /* set port to argv[2] */ hostaddr = gethostbyname(argv[1]); if ( !hostaddr ) { fprintf( stderr, "gethostbyname(): %s\n", hstrerror(h_errno) ); return (h_errno); } /* Now we'll use memcpy() to place it in our sockaddr_in struct: */ memcpy( &socketaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length ); /* At this point, we're ready to connect to the remote host: */ rval = connect( sd, (struct sockaddr *) &socketaddr, sizeof(socketaddr) ); if (rval<0) { if (errno == EINPROGRESS) { tv.tv_sec = t_time; tv.tv_usec = 0; FD_ZERO(&myset); FD_SET(sd, &myset); if (select(sd+1, NULL, &myset, NULL, &tv) > 0) { lon = sizeof(int); getsockopt(sd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon); if (!valopt) { printf("--- Found OPEN Port: %d [TCP]\n",port); open++; } } else { printf("------ TIMEOUT on port: %d\n",port); } } else { printf("error\n"); exit(0); } } close(sd); } printf("-------------------------------------------\n"); printf("Open TCP ports: %d\n",open); printf("-------------------------------------------\n"); printf("\nPort scanning on %s completed!\n\n",argv[1]); return(0); }