lunedì 13 settembre 2010

simple port scanner

pscan
Some years ago I wrote a simple c language port-scanner to understand the basic of unix system service.

Download unix executable: pscan
Here the code:  pscan.c
/*
 * author: Alessandro Muratore < muratore.ale@gmail.com >
 *            example of a simple port scanner
 *         writen in c for *nix system
 *
 */
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main()
{
        struct sockaddr_in sck_addr;

        char ip[15];

        int port;
        int portS;
        int portE;

        int sck;
        int sck_con;

        printf("insert the host ip: ");
        scanf("%s",ip);
        printf("insert the first port of the range: ");
        scanf("%d", &portS);
        printf("insert the last port of the range: ");
        scanf("%d", &portE);

        for (port=portS; port<=portE; port++)
        {
                if ((sck=socket(AF_INET, SOCK_STREAM, 0))==-1)
                {
                        perror("socket error\n");
                }
                else
                {
                        sck_addr.sin_family=AF_INET;
                        sck_addr.sin_addr.s_addr=inet_addr(ip);
                        sck_addr.sin_port=htons(port);

                        if (sck_con=connect(sck,(struct sockaddr*) &sck_addr, sizeof(sck_addr))==0)
                        {
                                printf("\nport: %d - open\n\n", port);
                        }
                }
                close (sck);
        }
}

Nessun commento:

Posta un commento