博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

端口扫描(转载)

Posted on 2012-03-18 19:23  ccmfc  阅读(258)  评论(0编辑  收藏  举报

#include <stdio.h>
#include <winsock2.h>
#include <time.h>
#pragma comment(lib,"ws2_32.lib")

char *plays[4]=
{
        " | ",
                " / ",
                " - ",
                " \\ ",
};

char *host;
int threadnum;
int startport,endport,nowport;

CRITICAL_SECTION for_w;

struct timeval timeout;
#define TIME_WAIT 100000

void display(void)  // 定义状态提示函数
{
        static int play=0;
        printf("=%s= %5d threads %d%s Completed. \r", plays[play],threadnum,((nowport-startport+1)*100)/(endport-startport),"%");
        ++play %= 4;
       
}

void WaitThreadEnd(void)
{
        while(threadnum>0)
        {
                Sleep(4);       
        }       
}

//分程
DWORD WINAPI ThreadFunc( LPVOID lp )

        int port = (int)lp;
        //申请套接字
        SOCKET sockfd;
        //申请地址变量
        struct sockaddr_in addr;
        FD_SET mask;
        unsigned long value;
        addr.sin_family =AF_INET;
        //取用全局级变量中的任务目标IP地址
        addr.sin_addr.s_addr =inet_addr(host);
        int i;       
        for(i=0;i<10;i++)
        {
                value=1;
                //建立Soket
                sockfd = socket(AF_INET, SOCK_STREAM,0);
                if(sockfd==INVALID_SOCKET)
                { 
                        EnterCriticalSection(&for_w);
                        printf("Socket error in port: %d!\n",port+i);
                        LeaveCriticalSection(&for_w);
                        continue;
                }
                ioctlsocket(sockfd,FIONBIO,&value);//设置sockfd为非阻塞模式
                addr.sin_port = htons(port+i);//转换至大尾构造
                connect(sockfd,(struct sockaddr *) &addr, sizeof(addr));//请求目标
                FD_ZERO(&mask);//FD_SET类select非阻塞模式编程体系宏操作(此为清零操作)
                FD_SET(sockfd,&mask);//加入监视套接字
                value=select(0,NULL,&mask,NULL,&timeout);//非阻塞
                closesocket(sockfd);
                if(value!=0 && value!=-1)//判定
                {
                        EnterCriticalSection(&for_w);
                        printf("\t\t锁定目标 ->%s:%d<- ...\r\n",host,port+i);
                        LeaveCriticalSection(&for_w);
                }
        }
        InterlockedExchangeAdd((long *)&threadnum,-1);
       
        EnterCriticalSection(&for_w);
        display();
        LeaveCriticalSection(&for_w);
       
        return 0;
}

int main( int argc,char **argv )
{
        WSADATA ws;
        WSAStartup(MAKEWORD(2,2), &ws);
       
        host="192.168.1.200";
       
        clock_t start,end;//程序运行的起始和结束时间
        float costtime;//程序耗时
        HANDLE cdd;//过程内存中介量
       
        //SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS);
       
        timeout.tv_usec=TIME_WAIT;
        timeout.tv_sec=0;
       
        //临界区初始化
        InitializeCriticalSection(&for_w);
       
        start=clock();//开始计时
       
        startport=1;
        endport=63550;
       
       
        for(nowport=startport;nowport<endport;nowport+=10)
        {
                while(threadnum>2000)
                {
                        Sleep(10);
                }
                InterlockedExchangeAdd((long *)&threadnum,1);
                cdd = CreateThread( NULL,0,ThreadFunc,(void *)nowport, 0,NULL);
                //对扫描线程设置CPU最高优先
                SetThreadPriority(cdd, THREAD_PRIORITY_TIME_CRITICAL);
                CloseHandle(cdd);
                //安全阀值
               
        }
        //等待线程清空
        WaitThreadEnd();
        Sleep(10);
        end=clock();//计时结束
        costtime= (float)(end - start) / CLOCKS_PER_SEC;  //转换时间格式
        printf("\nCost time:%f Sec",costtime);//显示耗时
        printf("\n\n");
        //释放Soket
        WSACleanup();
        //清除临界区
        DeleteCriticalSection(&for_w);
        return 0;
}