portscanner code problem...
Results 1 to 6 of 6

Thread: portscanner code problem...

  1. #1
    Member
    Join Date
    Sep 2001
    Posts
    56

    Unhappy portscanner code problem...

    okay, I'm trying to write a little portscanner, and all it can do right now is scan one port at a time. I am trying to use command-line arguments, but when I try to get a range of ports to scan, I have trouble getting the port number arguments (i.e., 1-1024). I can get the 1 just fine. Here is the code I have so far; hopefully someone can help me figure out the code to make this happen.

    later on I'm going to make this scanner able to scan multiple IP addresses as well as multiple ports. anyway, the code:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <winsock2.h>
    using namespace std;
    
    void usage (char *progname)
    {
       cerr << "\nUsage:\n\t" << progname << " -h <host> -p <port>\n\n";
       cerr << "Where:\n\t<host> is the host you want to scan.\n\t<port> is the port you want to scan.\n";
       cerr << "\t<port> can be a range as well as a single number (i.e., -p 1-1024).\n\n";
    
       WSACleanup();
       exit (1);
    }
    
    int main (int argc, char *argv[])
    {
       char *host_name = "localhost";
       u_short start_port;
       u_short end_port;
       int retval;
       u_int host_addr;
       struct sockaddr_in host;
       struct hostent *hp;
       WSADATA wsaData;
       SOCKET scan_sock;
    
       if (argc > 1)
       {
          for (int i = 1; i < argc; i++)
          {
             if ((argv[i][0] == '-') || (argv[i][0] == '/'))
             {
                switch (tolower (argv[i][1]))
                {
                   case 'h':
                      host_name = argv[++i];
                      break;
    
                   case 'p':
                      start_port = atoi (argv[++i]);
                      /* what the f**k do I put here?! */
    
                      break;
    
                   default:
                      usage (argv[0]);
                      break;
                }
             }
             else
                usage (argv[0]);
          }
       }
    
       if (WSAStartup (0x0202, &wsaData) == SOCKET_ERROR)
       {
          cerr << "\nWSAStartup() failed with error: " << WSAGetLastError() << endl;
             WSACleanup();
             return 0;
       }
    
       if (((start_port <= 0) || (start_port > 65535)) || ((end_port <= 0) || (start_port > 65535)))
          usage (argv[0]);
    
       if (isalpha (host_name[0]))
          hp = gethostbyname (host_name);
       else
       {
          host_addr = inet_addr (host_name);
          hp = gethostbyaddr ((char*)&host_addr, 4, AF_INET);
       }
    
       if (hp == NULL)
       {
          cerr << "\nCannot resolve address [" << host_name << "]: ";
          cerr << "Error: " << WSAGetLastError() << endl;
          WSACleanup();
          return 0;
       }
    
       scan_sock = socket (AF_INET, SOCK_STREAM, 0);
    
       if (scan_sock < 0)
       {
          cerr << "\nError opening socket: " << WSAGetLastError() << endl;
          WSACleanup();
          return 0;
       }
    
       cout << "\nScanning [" << host_name << "] for open ports...\n\n";
       cout << "\nPORT\tSTATUS\n\n";
    
       if ((start_port > 0) || (start_port < 65536))
       {
          /* copy resolved host information to struct sockaddr_in */
          memset (&host, 0, sizeof (host));
          memcpy (&(host.sin_addr), hp->h_addr_list, hp->h_length);
          host.sin_family = AF_INET;
          host.sin_port = htons (start_port);
          host.sin_addr.S_un.S_addr = host_addr;
    
          retval = connect (scan_sock, (struct sockaddr*)&host, sizeof (host));
    
          if (retval == 0)
          {
             cout << start_port << "\tOpen\n\n";
             closesocket (scan_sock);
             WSACleanup();
             return 0;
          }
    
          else
          {
             cout << start_port << "\tClosed\n\n";
             closesocket (scan_sock);
             WSACleanup();
             return 0;
          }
       }
    
       else if (((start_port > 0) && (start_port < 65536)) && ((end_port > 0) && (end_port < 65536)))
       {
          /* copy resolved host information to struct sockaddr_in */
          memset (&host, 0, sizeof (host));
          memcpy (&(host.sin_addr), hp->h_addr_list, hp->h_length);
          host.sin_family = AF_INET;
          host.sin_addr.S_un.S_addr = host_addr;
    
          for (int i = start_port; i <= end_port; i++)
          {
             host.sin_port = htons (i);
    
             retval = connect (scan_sock, (struct sockaddr *)&host, sizeof (host));
    
             if (retval == 0)
             {
                cout << i << "\tOpen\n\n";
                closesocket (scan_sock);
                WSACleanup();
                return 0;
             }
    
             else
             {
                cout << i << "\tClosed\n\n";
                closesocket (scan_sock);
                WSACleanup();
                return 0;
             }
          }
       }
    
       return 0;
    }

  2. #2
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    Try something like

    Code:
    sscanf(argv[i++],"%d-%d",&start_port,&end_port);
    Note this is the "C" way of doing it, I don't know the C++ way.

    I knocked up a test prog, it worked. Don't know what above code will do if there is no hyphen, maybe you should check for that first.

  3. #3
    Senior Member tampabay420's Avatar
    Join Date
    Aug 2002
    Posts
    953
    where did you get that winsock library? (winsock.h)???

    edit**
    i guess winsock.h is only for Visual C++? Are there any GNU Socket Libraries?
    yeah, I\'m gonna need that by friday...

  4. #4
    Member
    Join Date
    Sep 2001
    Posts
    56
    I got most of my libraries from Visual C++ 6.0; but the <winsock.h> and <winsock2.h> should come with other windows compilers. also, if you use <winsock.h>, you need to link your programs with wsock32.lib and if you use <winsock2.h>, you need to link with ws2_32.lib

    Thanks for the info slarty...I could just write the code in C instead of C++; it's not like I'm making use of C++'s features anyway. so I think I'll port it to C for now and figure out a way to do it in C++.

    I also downloaded the source code for nmap, so that should help me out alot.

    -Nitro-

  5. #5
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    Nitro: you do know right, that if you write it in C++ you can still use all* the features of C, right? You can certainly call any C library function like sscanf

    However for cleanliness purposes, I would normally imagine that you only want to use *either* the C or C++ API for a particular purpose. As you're using iostream which is a C++ library, it might be nice to use a C++ library for strings, however I don't know how to do that in C++

    There is a possibility of things going wrong if you mix the C and C++ IO library calls for the same file (usually stdout mixing cout and printf etc)

    You can of course write the prog in C++ but use C libraries only; lots of people do.

    *Actually not quite all of them, but so nearly it doesn't matter

  6. #6
    Member
    Join Date
    Sep 2001
    Posts
    56
    yeah, I know that you can mix C and C++; but I just meant that if I was going to use sscanf and all the other stdio.h functions, I would use C instead of C++....thanks for the info though.

    -Nitro-

Posting Permissions

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