TCP Port Scanner Coded By ME

Discussion in 'Programming' started by billy786, Jun 19, 2008.

  1. #1
    Well i dont know if this is relevant to this website:D, but hell its the programming section, so i thought might aswell:). Its a simple TCP port scanner using the connect method with a if statement to determine whether the port is open or closed. It also uses a for loop for the port range. BTW this is for the beginners who starting to program in C using sockets so no flaming for how crap my code is lol :D.

    /* 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
    }

    }
     
    billy786, Jun 19, 2008 IP
  2. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #2
    netstat -a does the same thing , is it your project assignment ?
     
    it career, Jun 20, 2008 IP
  3. DnHype

    DnHype Active Member

    Messages:
    1,010
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    80
    #3


    Nice copy past from other site :)
    but this is not from you, or at least hide this line :p
     
    DnHype, Jun 20, 2008 IP
  4. Skullborg

    Skullborg Guest

    Messages:
    757
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Bah.. I know almost everyone from h4ck-y0u.org.. Tell me your username there. no offense, but.. I highly doubt you leeched this source code from h4ck-y0u.org.
     
    Skullborg, Jun 20, 2008 IP
  5. billy786

    billy786 Peon

    Messages:
    323
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    and78 on hack-y0u the site admin changed my post go look on the site
     
    billy786, Jun 20, 2008 IP
  6. billy786

    billy786 Peon

    Messages:
    323
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    netstat -a shows the open ports on the local machine?

    this can scan any machine boss
    :D
     
    billy786, Jun 21, 2008 IP