C實(shí)現(xiàn)的非阻塞方式命令行端口掃描器源碼
該實(shí)例是一個(gè)C實(shí)現(xiàn)的基于命令行模式端口掃描代碼,并且是以非阻塞方式來實(shí)現(xiàn)對(duì)IP和端口的連接測(cè)試。為了大家使用和學(xué)習(xí)方便,已在代碼中盡可能多的地方加入了注釋,相信對(duì)于幫助大家理解C端口掃描有很大幫助。
具體功能代碼如下:
#include <afxext.h> #include <winsock.h> // 編譯時(shí)需使用的庫 #pragma comment(lib,"wsock32.lib") // select()成員定義 #define ZERO (fd_set *)0 // 變量定義 int maxth, scanok, scannum; int portip, hoststart, hoststop, startport, endport; //定義了開始I和結(jié)束P地址,開始和結(jié)束端口 long searchnum, searched; void usage(char *); // 定義顯示使用方法函數(shù) void playx(int); // 定義狀態(tài)提示函數(shù) void setip2(char *); // 定義設(shè)置IP函數(shù) void customport(char *, char *, char *); // 定義自定義掃描端口函數(shù) void portscannow(int); // 定義端口掃描掃描 int main(int argc, char *argv[]) { WSADATA wsadata; // 清屏 system("cls.exe"); // 顯示版本信息 printf("\r\n============== 命令行端口掃描器 PortScanner V1.0 =============="); // 檢查輸入 if ((argc < 3) || (argc > 4)) { // 顯示幫助提示 usage(argv[0]); return -1; } // 檢測(cè)是否為port掃描 if(!(stricmp(strlwr(argv[1]), "-p") == 0)) { usage(argv[0]); return -1; } // 程序初始化 if (WSAStartup(MAKEWORD(1,1), &wsadata) != 0) //如果初始化錯(cuò)誤 { printf("\r\nWsatartup error"); //出錯(cuò)信息 return -1; } // 端口掃描參數(shù)轉(zhuǎn)換 // 如果參數(shù)為三個(gè) if (argc == 3) { // 直接設(shè)置IP setip2(argv[2]); } // 如果參數(shù)為四個(gè) else if (argc == 4) { // 進(jìn)入定制端口掃描處理 customport(argv[0], argv[2], argv[3]); } // 參數(shù)過多顯示幫助 else { usage(argv[0]); return -1; } // 掃描端口開始 portscannow(argc); WSACleanup(); return 0; } // 幫助提示函數(shù) void usage(char * prog) { printf("Usage: %s <Option>", prog); printf("\r\n\n <Option>:"); printf("\r\n -p [ Port|StartPort-EndPort ] < HostName|IP|StartIP-EndIP >"); printf("\r\n\n Example: "); printf("\r\n %s -p 192.168.0.1", prog); printf("\r\n %s -p 192.168.0.1-192.168.0.254", prog); printf("\r\n %s -p 21-80 192.168.0.1", prog); printf("\r\n %s -p 21-80 192.168.0.1-192.168.0.254\r\n", prog); return; } // 進(jìn)度提示 void playx(int play = 0) { // 進(jìn)度條 char *plays[12]= { " | ", " / ", " - ", " \\ ", " | ", " / ", " - ", " \\ ", " | ", " / ", " - ", " \\ ", }; if (searchnum != 0) { for (int i = 0 ; i <= 3; i ++) { printf(" =%s= %d%s Completed. \r", plays , searched * 100 / (searchnum + 1), "%"); Sleep(5); } } else { printf(" =%s=\r", plays[play]); //顯示進(jìn)度 Sleep(10); } } // 設(shè)置IP void setip2(char *cp) { int host; struct hostent *testhost; char *startip = "", *endip = ""; // 判斷是否為 192.168.0.1-192.168.0.254 形式的輸入 if (strstr(cp, "-") && strlen(cp) > 15 && strlen(cp) < 32) { // 提取出結(jié)束IP endip = strchr(cp, '-') + 1; // 提取出開始IP strncpy(startip, cp, strlen(cp) - strlen(strchr(cp, '-'))); // 給控制要掃描IP段的變量賦值 hoststart = ntohl(inet_addr(startip)); hoststop = ntohl(inet_addr(endip)); } else { // 取得輸入的主機(jī)地址 testhost = gethostbyname(startip); // 如果地址不存在 if(!testhost) { WSACleanup( ); printf("\r\nCan't get ip of: %s", cp); exit(-1); } // 給控制要掃描IP段的變量賦值 memcpy(&host, testhost->h_addr, 4); hoststop = hoststart = ntohl(host); } } // 測(cè)試線程是否已滿 void TestThread(int thread = 200) { for (;;) { playx(); // 測(cè)試線程是否已滿 if (maxth > thread) Sleep(100); else break; } return; } // 等待線程結(jié)束函數(shù) void WaitThreadEnd() { // 延時(shí) Sleep(6000); // 顯示等待提示 printf("\r \r\n"); printf(" Wait ( %d )Thread end...\r\n", maxth); for(;;) { // 判斷所有線程是否已經(jīng)結(jié)束 if (maxth > 0) { // 延時(shí)等待線程序結(jié)束 Sleep(100); playx(); continue; } else break; } printf("\r\n"); return; } // 定制端口掃描參數(shù) void customport(char *cp, char *cp2, char *cp3) { int intport; char *checker; // 處理要掃描的端口 // 掃描開始端口變量賦值 startport = atoi(cp2); // 掃描結(jié)束端口變量賦值 endport = atoi(cp2); // 判斷是否 21-80 形式 if (strstr(cp2,"-")) { intport = atoi(checker = strchr(cp2, '-') + 1); if (intport > 0 && intport < 65536) // 掃描結(jié)束端口變量賦值 endport = intport; } // 端口大小判斷 if (startport < 0 || startport > 65536 || endport < 0 || endport > 65535) { usage(cp); exit(-1); } // 處理ip地址 setip2(cp3); } // 端口掃描函數(shù) UINT portscan(LPVOID port) { int addr = portip; // 取得要掃描的地址 int sock; struct fd_set mask; struct timeval timeout; struct sockaddr_in server; unsigned long flag = 1; // 創(chuàng)建一個(gè)sock sock = socket(AF_INET, SOCK_STREAM, 0); // 創(chuàng)建sock失敗處理 if (sock == INVALID_SOCKET) { printf("\r\nSock Error:%s", WSAGetLastError()); maxth --; return -1; } // 給sock成員賦值 server.sin_family=AF_INET; server.sin_addr.s_addr = htonl(addr); // 要掃描的地址 server.sin_port = htons(short(port)); // 要掃描的端口 // 顯示進(jìn)度 playx(); // 調(diào)用ioctlsocket()設(shè)置套接字為非阻塞模式 if (ioctlsocket(sock, FIONBIO, &flag) != 0) { // 設(shè)置失敗處理 printf("\r\nSock Error:%s", WSAGetLastError()); closesocket(sock); maxth --; return -1; } // 調(diào)用connect()連接遠(yuǎn)程主機(jī)端口 connect(sock, (struct sockaddr*)&server, sizeof(server)); timeout.tv_sec = 18; // 超時(shí)限制為18秒 timeout.tv_usec = 0; FD_ZERO(&mask); // 清空集合mask FD_SET(sock, &mask); // 將sock放入集合mask中 // 用select() 處理掃描結(jié)果 switch(select(sock + 1, ZERO, &mask, ZERO, &timeout)) { case -1: { printf("\r\nSelect() error"); maxth --; return -1; } // sock超時(shí)處理 case 0: { maxth --; closesocket(sock); return -1; } default: if(FD_ISSET(sock, &mask)) { // 禁止sock發(fā)送和接受數(shù)據(jù) shutdown(sock, 0); // 設(shè)置輸出結(jié)果格式 printf(" [Found:] %s Port: %d open.\r\n", inet_ntoa(server.sin_addr), ntohs(server.sin_port)); // 關(guān)閉sock closesocket(sock); scanok ++; maxth --; return 1; } } return 0; } // 掃描開始主函數(shù) void portscannow(int xp) { int sport; char *timenow, timebuf[32]; // 默認(rèn)掃描的端口 char *ports[32]={ "21", "22", "23", "25", "53", "79", "80", "110", "111", "113", "123", "135", "139", "143", "443", "512", "513", "514", "515", "540", "1080", "1433", "1521", "1524", "3306", "3389", "5631", "6000", "6112", "8000", "8080", "12345"http://這里你也可以自己要掃描的端口 }; // 顯示掃描開始的時(shí)間 timenow = _strtime(timebuf); printf("\r\nPortScan Start Time: %s\r\n\n",timenow); // 計(jì)數(shù)器初始化. maxth = 0; scanok = 0; scannum = 0; searched = 0; // 計(jì)算要掃描的端口數(shù)量 searchnum = hoststop - hoststart +1; if(xp == 3) searchnum = searchnum * 32; if(xp == 4) searchnum = searchnum * (endport - startport +1); // 端口掃描開始 for (portip = hoststart; portip <= hoststop; portip ++, scannum ++) { // *.*.*.0和*.*.*.255 地址處理 if ((portip % 256) == 0 || (portip % 256) == 255) { if(xp == 3) searchnum = searchnum - 32; if(xp == 4) searchnum = searchnum - (endport - startport +1); scannum --; playx(); continue; } if(i > 11) i = 0; // 默認(rèn)端口掃描 // scan 192.168.0.1 // scan 192.168.0.1-192.168.0.254 if (xp == 3) { for (sport = 0; sport < 32; sport ++, maxth ++, searched ++) { // 測(cè)試當(dāng)前線程是否大于180 TestThread(180); // 產(chǎn)生新的線程處理端口掃描 CWinThread * pthread = AfxBeginThread(portscan,LPVOID(atoi((char*)ports[sport]))); //延時(shí) Sleep(120); } } // 自端口掃描 // scan -p 21 192.168.0.1 // scan -p 21-80 192.168.0.1-192.168.0.254 if (xp == 4) { // 計(jì)算要掃描的端口 sport = endport - startport; if(sport > 500 ) { // 掃描自的端口 for(sport = startport; sport <= endport; sport ++, maxth ++, searched ++) { TestThread(2000); // 產(chǎn)生新的線程處理端口掃描 CWinThread * pthread = AfxBeginThread(portscan, LPVOID(sport)); // 延時(shí) Sleep(10); } } else { // 掃描自的端口 for(sport = startport; sport <= endport; sport ++, maxth ++, searched ++) { // 測(cè)試當(dāng)前線程是否大于250 TestThread(250); // 產(chǎn)生新的線程處理端口掃描 CWinThread * pthread = AfxBeginThread(portscan, LPVOID(sport)); // 延時(shí) Sleep(100); playx(); } } } } // 等待所有的線程結(jié)束 WaitThreadEnd(); // 顯示端口掃描結(jié)束時(shí)間 timenow = _strtime(timebuf); printf("\r\nPortScan End Time: %s", timenow); printf("\r\nScan %d Hosts completed. Open %d Ports!\r\n", scannum, scanok); }
為了測(cè)試該端口掃描器,可以使用如下連接測(cè)試代碼進(jìn)行測(cè)試,源碼如下:
/*此函數(shù)實(shí)現(xiàn)判斷m_server的m_port端口是否可以連上,超時(shí)限制為nTimeOut秒*/ BOOL ConnectTest(char * m_server,int m_port) { struct hostent* host = NULL; struct sockaddr_in saddr; unsigned int s = 0; BOOL ret; time_t start; int error; host = gethostbyname (m_server); if (host==NULL)return FALSE; saddr.sin_family = AF_INET; saddr.sin_port = htons(m_port); saddr.sin_addr = *((struct in_addr*)host->h_addr); if( (s=socket(AF_INET, SOCK_STREAM, 0))<0){ return FALSE; } fcntl(s,F_SETFL, O_NONBLOCK); if(connect(s,(struct sockaddr*)&saddr, sizeof(saddr)) == -1) { if (errno == EINPROGRESS){// it is in the connect process struct timeval tv; fd_set writefds; tv.tv_sec = m_nTimeOut; tv.tv_usec = 0; FD_ZERO(&writefds); FD_SET(s, &writefds); if(select(s+1,NULL,&writefds,NULL,&tv)>0){ int len=sizeof(int); //下面的一句一定要,主要針對(duì)防火墻 getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &len); if(error==0) ret=TRUE; else ret=FALSE; }else ret=FALSE;//timeout or error happen }else ret=FALSE; } else ret=TRUE; close(s); return ret; }
上一篇:VC小技巧匯總之控件技巧
欄 目:C語言
下一篇:C語言實(shí)現(xiàn)排序算法之歸并排序詳解
本文標(biāo)題:C實(shí)現(xiàn)的非阻塞方式命令行端口掃描器源碼
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3587.html
您可能感興趣的文章
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10c語言求1+2+...+n的解決方法
- 01-10求子數(shù)組最大和的解決方法詳解
- 01-10深入理解約瑟夫環(huán)的數(shù)學(xué)優(yōu)化方法
- 01-10深入二叉樹兩個(gè)結(jié)點(diǎn)的最低共同父結(jié)點(diǎn)的詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)- 解析最少換車次數(shù)的問題詳解
- 01-10c語言 跳臺(tái)階問題的解決方法
- 01-10如何判斷一個(gè)數(shù)是否為2的冪次方?若是,并判斷出來是多少次方


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改