Junan的天堂

天堂和地狱的距离

portsScan.c

#include <stdio.h>
#include <conio.h>
#include <winsock.h>

int intThreadNum=0;
int intTimeOut;
FILE *fp;
struct THREADINFO
{    char strHost[20];
    int intCurPort;
};
DWORD doCheck(LPVOID pParam)
{
    SOCKET sockfd;
    SOCKADDR_IN addr;
    TIMEVAL timeout;
    FD_SET    mask;
    u_long value;
    struct THREADINFO* thdCur=(struct THREADINFO*)pParam;

    value=wherey();
    printf("Scan %s:%d/n",thdCur->strHost,thdCur->intCurPort);
    gotoxy(1,value);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd==INVALID_SOCKET)
    {    printf("Socket error!/n");
        intThreadNum--;
        return(0);
    }
    value=1;
    ioctlsocket(sockfd,FIONBIO,&value);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(thdCur->intCurPort);
    addr.sin_addr.s_addr = inet_addr(thdCur->strHost);
    connect(sockfd,(struct sockaddr *) &addr, sizeof(addr));

    timeout.tv_sec=intTimeOut;
    timeout.tv_usec=0;
    FD_ZERO(&mask);
    FD_SET(sockfd,&mask);
    value=select(sockfd+1,NULL,&mask,NULL,&timeout);
    if(value==0 || value==-1)
    {    closesocket(sockfd);
        intThreadNum--;
        return(0);
    }
    printf("Found %s:%d           /n",thdCur->strHost,thdCur->intCurPort);
    fprintf(fp,"%s:%d/n",thdCur->strHost,thdCur->intCurPort);
    fflush(fp);
    intThreadNum--;
    closesocket(sockfd);
    return(1);
}

int main(int argc, char *argv[])
{
    char *p;
    u_long    startIP,endIP;
    WSADATA wsaData;
    IN_ADDR    serverIP;
    DWORD dwThreadID;
    struct THREADINFO thdINFO[300];
    int i,j,intMAXThread,startPort,endPort,intPort[100],intPortCount=-1;

    printf("Dotpot Port Scanner Ver 0.92(Test Version,Not contain ping fuction)/nPowered by Dotpot Workstudio,2001. Copyleft,No rights reserved./nPlease visit Armament Shop(http://arm.533.net) for last version./n/n");
    if(argc<3 || argc>5)
    {    printf("Usage:  %s <startIP>[endIP] <port1-port2|port1,port2,...> [thread] [timeout]/nNotes:  [thread] must be a integer between 5 to 300,default is 60./n/t[timeout] must be a integer between 1 to 4,default is 1s./n/tAll arguments(if in use) must be in order./n/tAll available results will append to DPPS.TXT./n  eg.:  %s 127.0.0.1 1-65535 100/n/t%s 127.0.0.1 21,23,25,80,135,3389/n/t%s 192.168.0.1-192.168.10.254 1-65535/n/t%s 192.168.0.1-192.168.10.1 21,23,25,79,80,135/n",
            argv[0],argv[0],argv[0],argv[0],argv[0]);
        return(1);
    }

    if(strchr(argv[1],'-'))
    {for(p=argv[1];p;p++)
        if(*p=='-')
        {
            *p++='/0';
            endIP=ntohl(inet_addr(p));
            break;
        }
    }
    else    endIP=ntohl(inet_addr(argv[1]));
    startIP=ntohl(inet_addr(argv[1]));
    if((startIP==INADDR_NONE)&&(endIP==INADDR_NONE))
    {    printf("IP address Error!/n");
        return(2);
    }
    if(startIP>endIP)
    {    i=startIP;
        startIP=endIP;
        endIP=i;
    }


    p=argv[2];
    if(strstr(argv[2],"-"))
    {    startPort=atoi(argv[2]);
        for(;*p;)
            if(*(p++)=='-')break;
        endPort=atoi(p);
        if(startPort>endPort)
        {    j=startPort;
            startPort=endPort;
            endPort=startPort;
        }
        if(startPort<1 || endPort>65535)
        {    printf("Port Error!/n");
            return(3);
        }

    }
    else
    {    intPort[0]=atoi(p);
        intPortCount++;
        for(i=0;*p && (i<100);)
        {    if(*(p++)==',')
            {    if(intPort[i]<1 || intPort[i]>65535)
                {    printf("Port Error!/n");
                    return(3);
                }
                i++;
                intPort[i]=atoi(p);
                intPortCount++;
            }
        }
    }
    if(argc==4)
    {    intMAXThread=atoi(argv[3]);
        if(intMAXThread<5)    intTimeOut=intMAXThread;
    }
    if(argc==5)
    {    intMAXThread=atoi(argv[3]);
        intTimeOut=atoi(argv[4]);
    }
    if(intMAXThread<5 || intMAXThread>300)    intMAXThread=60;
    if(intTimeOut<1 || intTimeOut>4)    intTimeOut=1;

    /*thdINFO=calloc(intMAXThread,sizeof(struct THREADINFO *));
    if(thdINFO==NULL)
    {    printf("Could not allocate memory!/n");
        return(4);
    }*/

    if(!(fp=fopen("DPPS.TXT","a")))
    {    printf("Could not create log file: DPPS.TXT!/n");
        return(5);
    }

    if (WSAStartup(MAKEWORD(1, 1), &wsaData) < 0)
    {    printf("Could not init Windows Socket!/n");
        return(6);
    }

    serverIP.s_addr=htonl(startIP);
    printf("Scan form %s to ",inet_ntoa(serverIP));
    serverIP.s_addr=htonl(endIP);
    printf("%s/tPort: ",inet_ntoa(serverIP));

    if(intPortCount!=-1)
    {    for(i=0;i<=intPortCount;i++)printf("%d ",intPort[i]);
        j=intPort[0];
    }
    else
    {    j=startPort;
        printf("%d to %d",startPort,endPort);
    }
    printf("/n----------------------------------------------------------/n");


    i=startIP;
    int k=0;
    while(i<=endIP)
    {
        if(((i % 256)==0)||((i % 256)==255)){i++;continue;}
        if(intThreadNum<intMAXThread)
        {
            serverIP.s_addr=htonl(i);
            strcpy(thdINFO[intThreadNum].strHost,inet_ntoa(serverIP));
            thdINFO[intThreadNum].intCurPort=j;
            CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)doCheck,(LPVOID)&thdINFO[intThreadNum],0,&dwThreadID);
            //doCheck((LPVOID)&thdINFO[intThreadNum]);
            intThreadNum++;

            if(intPortCount!=-1)
            {    if(j==intPort[intPortCount])
                {
                    j=intPort[0];
                    i++;
                    k=0;
                }
                else    j=intPort[++k];
            }
            else
            {    if(j==endPort){
                    j=startPort;
                    i++;
                }
                else
                {
                    j++;
                }
            }
        }
    }

    for(i=0;i<100;i++)
    {    if(!intThreadNum)break;
        Sleep(100);
    }

    printf("/n----------------------------------------------------------/nPort scan complete.");
    fclose(fp);
    WSACleanup();

    if(intThreadNum)
        printf("/nBut there are %d threads faild./nPlease decrease thread number next time./n",intThreadNum);
    else
        printf(" All threads exit successfully./n");
    return(0);
}

阅读更多
上一篇SQLExec.c
下一篇乱码算法大全
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

portsScan.c

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭