Advertisement
krot

TCP Connect portscanner

Aug 16th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. /*
  2.  TCP Connect portscanner with winsock
  3. */
  4.  
  5. #include<stdio.h>    
  6. #include<winsock2.h>
  7. #pragma comment(lib, "ws2_32.lib"); //To link the winsock library  
  8.  
  9. int main(int argc, char **argv)  
  10. {
  11.  WSADATA firstsock;  
  12.  SOCKET s;
  13.  struct hostent *host;
  14.  int err,i, startport , endport;
  15.  struct sockaddr_in sa; //this stores the destination address
  16.  char hostname[100];
  17.  
  18.  strncpy((char *)&sa,"",sizeof sa);  
  19.  sa.sin_family = AF_INET; //this line must be like this coz internet
  20.  
  21.  //Initialise winsock
  22.  if (WSAStartup(MAKEWORD(2,0),&firstsock) != 0)  //CHECKS FOR WINSOCK VERSION 2.0
  23.  {
  24.   fprintf(stderr,"WSAStartup() failed"); //print formatted data specify stream and options
  25.   exit(EXIT_FAILURE);        //or exit(1);
  26.  }
  27.  
  28.  printf("Enter hostname or ip to scan : ");
  29.  gets(hostname);
  30.  
  31.  printf("Enter starting port : ");
  32.  scanf("%d" , &startport);
  33.  
  34.  printf("Enter ending port : ");
  35.  scanf("%d" , &endport);
  36.  
  37.  if(isdigit(hostname[0]))
  38.  {  
  39.   printf("Doing inet_addr...");
  40.   sa.sin_addr.s_addr = inet_addr(hostname); //get ip into s_addr
  41.   printf("Done\n");
  42.  }        
  43.  else if( (host=gethostbyname(hostname)) != 0)
  44.  {
  45.   printf("Doing gethostbyname()...");
  46.   strncpy((char *)&sa.sin_addr , (char *)host->h_addr_list[0] , sizeof sa.sin_addr);
  47.   printf("Done\n");
  48.  }
  49.  else
  50.  {
  51.     printf("Error resolving hostname");
  52.        exit(EXIT_FAILURE);
  53.  }
  54.  
  55.  
  56.  //Start the portscan loop
  57.  printf("Starting the scan loop...\n");
  58.  for(i = startport ; i<= endport ; i++)
  59.  {
  60.    
  61.   s = socket(AF_INET , SOCK_STREAM , 0); //make net a valid socket handle
  62.   if(s < 0)  //if not a socket
  63.   {
  64.    perror("\nSocket creation failed");  // perror function prints an error message to stderr
  65.    exit(EXIT_FAILURE);       //or exit(0);
  66.   }
  67.    
  68.   sa.sin_port = htons(i);
  69.   //connect to the server with that socket
  70.   err = connect(s , (struct sockaddr *)&sa , sizeof sa);
  71.  
  72.   if(err == SOCKET_ERROR) //connection not accepted
  73.   {
  74.    printf("%s %-5d Winsock Error Code : %d\n" , hostname , i , WSAGetLastError());
  75.    fflush(stdout);
  76.   }
  77.   else  //connection accepted
  78.   {
  79.    printf("%s %-5d accepted            \n" , hostname , i);
  80.    if( shutdown( s ,SD_BOTH ) == SOCKET_ERROR )
  81.    {
  82.     perror("\nshutdown");// perror function prints an error message to stderr
  83.     exit(EXIT_FAILURE);  
  84.    }
  85.   }
  86.   closesocket(s);   //closes the net socket
  87.  }
  88.  
  89.  fflush(stdout); //clears the contents of a buffer or flushes a stream
  90.  return(0);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement