EvilZone

Programming and Scripting => C - C++ => : x86_64 September 06, 2012, 11:23:17 PM

: My Simple PortScanner [*nix]
: x86_64 September 06, 2012, 11:23:17 PM

This is just a basic TCP Port scanner that returns a list of the ports and whether or not they are open. It should run on any *nix system. Due to adding the import "sys/types.h", it will also run on legacy BSD systems which don't include the needed definitions in "sys/socket.h". It takes the IP to be scanned as the only argument when run from the command line.


:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>


int main(int argc, const char * argv[])
{
    int s, port;
    struct hostent *hostaddr;
    struct sockaddr_in servaddr;


    servaddr.sin_family = AF_INET;
    hostaddr = gethostbyname(argv[1]);


    for (port=0; port<=65535; port++)
    {
  s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  servaddr.sin_port = htons(port);


  if(connect(s, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1)
    printf("Port %d is closed\n", port);
  else
    printf("Port %d is open\n",port);
    close(s);
    }


    return 0;
}


I compiled it with gcc 4.2.1 and tested it on a mac.
: Re: My Simple PortScanner [*nix]
: Xires September 06, 2012, 11:39:35 PM
Not too bad for a very basic TCP connect scanner.  I might suggest changing 'port' to a 'register unsigned int' type and then using "++port" as increment in the 'for' loop.  If you're feeling daring(and looking for a challenge), you could try making it multithreaded.
: Re: My Simple PortScanner [*nix]
: jeefo12 September 12, 2012, 06:17:16 PM
Hi
I'm start learning C / C++
I tried this program
But it's super duper slow why ?
./output localhost
waiting almost 1 minut and Port 1 is closed
i changed 3 things
#include <unistd.h> // because i was cannot compile. close function does not declared
unsigned int port;
for (port=1; port<=65535; ++port)
: Re: My Simple PortScanner [*nix]
: x86_64 September 23, 2012, 10:18:53 PM
Was tested on mac, one with real specs. I didn't bother with optimisation as it just gets the job done.