Closed Thread
Results 1 to 3 of 3

Thread: Simple TCP port scanner for beginners

  1. #1
    billy786 is offline Newbie
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Talking Simple TCP port scanner for beginners

    This is a simple port scanner created by me when i was learning socket programming in C. I thought it might be usefull to someone and decide to post it hope you enjoy it.

    The code is using tcp connect with an if statement to determine whether the port is opened or closes within a for loop which is for the port range.
    Code:
    /* A TCP port scanner created by billy
    www.softhardware.co.uk*/
    
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdlib.h>
    #include <errno.h>
    
    
    /* Main programs starts*/
    int main(int argc, char **argv)
    {
       int   sd;         //socket descriptor
       int    port;         //port number
       int   start;         //start port
       int    end;         //end port
       int    rval;         //socket descriptor for connect   
       char    responce[1024];      //to receive data
       char   *message="shell";       //data to send
       struct hostent *hostaddr;   //To be used for IPaddress
       struct sockaddr_in servaddr;   //socket structure
       
       if (argc < 4 )
       {
          printf("------Created By www.h4ck-y0u.org-----------\n");
          printf("--------------------------------------------------\n");
          printf("Usage: ./tscan <IPaddress> <Start Port> <End Port>\n");
          printf("--------------------------------------------------\n");
          return (EINVAL);
       }
       start = atoi(argv[2]);
       end   = atoi(argv[3]);
       for (port=start; port<=end; port++)
       {
    
             //portno is ascii to int second argument      
    
       sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket
       if (sd == -1)
       {
         perror("Socket()\n");
         return (errno);
       }   
    
       memset( &servaddr, 0, sizeof(servaddr));
    
       servaddr.sin_family = AF_INET;
       servaddr.sin_port = htons(port); //set the portno
       
       hostaddr = gethostbyname( argv[1] ); //get the ip 1st argument
       
       memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
          
       //below connects to the specified ip in hostaddr
       
     
    
       rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
       if (rval == -1)
       {
       printf("Port %d is closed\n", port);
       close(sd);
       }
       else
       printf("Port %d is open\n",port);
       
       close(sd);         //socket descriptor
       }
       
    }
    Last edited by WingedPanther; 06-20-2008 at 09:10 AM. Reason: add code tags

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    torleif is offline Newbie
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Re: Simple TCP port scanner for beginners

    Nice and simple, also I see another fan of the unix like #include <sys/types.h>. I only learnt about this recently and was using a complicated cross platform library

  4. #3
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Simple TCP port scanner for beginners

    You should post this in the Tutorials section. Very nice. +rep given.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PHP Port Scanner
    By TkTech in forum Classes and Code Snippets
    Replies: 41
    Last Post: 02-16-2010, 08:58 AM
  2. [Java] Port Scanner
    By Impulser in forum Classes and Code Snippets
    Replies: 0
    Last Post: 01-13-2010, 01:30 PM
  3. [PHP] Port Scanner with Form
    By Impulser in forum Classes and Code Snippets
    Replies: 0
    Last Post: 08-28-2009, 12:01 AM
  4. Simple Port Scanner in PHP
    By robertiptv in forum Classes and Code Snippets
    Replies: 4
    Last Post: 01-09-2009, 12:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts