C完成的非壅塞方法敕令行端口掃描器源碼。本站提示廣大學習愛好者:(C完成的非壅塞方法敕令行端口掃描器源碼)文章只能為提供參考,不一定能成為您想要的結果。以下是C完成的非壅塞方法敕令行端口掃描器源碼正文
該實例是一個C完成的基於敕令行形式端口掃描代碼,而且是以非壅塞方法來完成對IP和端口的銜接測試。為了年夜家應用和進修便利,已在代碼中盡量多的處所參加了正文,信任關於贊助年夜家懂得C端口掃描有很年夜贊助。
詳細功效代碼以下:
#include <afxext.h> #include <winsock.h> // 編譯時需應用的庫 #pragma comment(lib,"wsock32.lib") // select()成員界說 #define ZERO (fd_set *)0 // 變量界說 int maxth, scanok, scannum; int portip, hoststart, hoststop, startport, endport; //界說了開端I和停止P地址,開端和停止端口 long searchnum, searched; void usage(char *); // 界說顯示應用辦法函數 void playx(int); // 界說狀況提醒函數 void setip2(char *); // 界說設置IP函數 void customport(char *, char *, char *); // 界說自界說掃描端口函數 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; } // 檢測能否為port掃描 if(!(stricmp(strlwr(argv[1]), "-p") == 0)) { usage(argv[0]); return -1; } // 法式初始化 if (WSAStartup(MAKEWORD(1,1), &wsadata) != 0) //假如初始化毛病 { printf("\r\nWsatartup error"); //失足信息 return -1; } // 端口掃描參數轉換 // 假如參數為三個 if (argc == 3) { // 直接設置IP setip2(argv[2]); } // 假如參數為四個 else if (argc == 4) { // 進入定制端口掃描處置 customport(argv[0], argv[2], argv[3]); } // 參數過量顯示贊助 else { usage(argv[0]); return -1; } // 掃描端口開端 portscannow(argc); WSACleanup(); return 0; } // 贊助提醒函數 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; } // 進度提醒 void playx(int play = 0) { // 進度條 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]); //顯示進度 Sleep(10); } } // 設置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) { // 提掏出停止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 { // 獲得輸出的主機地址 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); } } // 測試線程能否已滿 void TestThread(int thread = 200) { for (;;) { playx(); // 測試線程能否已滿 if (maxth > thread) Sleep(100); else break; } return; } // 期待線程停止函數 void WaitThreadEnd() { // 延時 Sleep(6000); // 顯示期待提醒 printf("\r \r\n"); printf(" Wait ( %d )Thread end...\r\n", maxth); for(;;) { // 斷定一切線程能否曾經停止 if (maxth > 0) { // 延時期待線法式停止 Sleep(100); playx(); continue; } else break; } printf("\r\n"); return; } // 定制端口掃描參數 void customport(char *cp, char *cp2, char *cp3) { int intport; char *checker; // 處置要掃描的端口 // 掃描開端端口變量賦值 startport = atoi(cp2); // 掃描停止端口變量賦值 endport = atoi(cp2); // 斷定能否 21-80 情勢 if (strstr(cp2,"-")) { intport = atoi(checker = strchr(cp2, '-') + 1); if (intport > 0 && intport < 65536) // 掃描停止端口變量賦值 endport = intport; } // 端口年夜小斷定 if (startport < 0 || startport > 65536 || endport < 0 || endport > 65535) { usage(cp); exit(-1); } // 處置ip地址 setip2(cp3); } // 端口掃描函數 UINT portscan(LPVOID port) { int addr = portip; // 獲得要掃描的地址 int sock; struct fd_set mask; struct timeval timeout; struct sockaddr_in server; unsigned long flag = 1; // 創立一個sock sock = socket(AF_INET, SOCK_STREAM, 0); // 創立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)); // 要掃描的端口 // 顯示進度 playx(); // 挪用ioctlsocket()設置套接字為非壅塞形式 if (ioctlsocket(sock, FIONBIO, &flag) != 0) { // 設置掉敗處置 printf("\r\nSock Error:%s", WSAGetLastError()); closesocket(sock); maxth --; return -1; } // 挪用connect()銜接長途主機端口 connect(sock, (struct sockaddr*)&server, sizeof(server)); timeout.tv_sec = 18; // 超時限制為18秒 timeout.tv_usec = 0; FD_ZERO(&mask); // 清空聚集mask FD_SET(sock, &mask); // 將sock放入聚集mask中 // 用select() 處置掃描成果 switch(select(sock + 1, ZERO, &mask, ZERO, &timeout)) { case -1: { printf("\r\nSelect() error"); maxth --; return -1; } // sock超時處置 case 0: { maxth --; closesocket(sock); return -1; } default: if(FD_ISSET(sock, &mask)) { // 制止sock發送和接收數據 shutdown(sock, 0); // 設置輸入成果格局 printf(" [Found:] %s Port: %d open.\r\n", inet_ntoa(server.sin_addr), ntohs(server.sin_port)); // 封閉sock closesocket(sock); scanok ++; maxth --; return 1; } } return 0; } // 掃描開端主函數 void portscannow(int xp) { int sport; char *timenow, timebuf[32]; // 默許掃描的端口 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"//這裡你也能夠本身要掃描的端口 }; // 顯示掃描開端的時光 timenow = _strtime(timebuf); printf("\r\nPortScan Start Time: %s\r\n\n",timenow); // 計數器初始化. maxth = 0; scanok = 0; scannum = 0; searched = 0; // 盤算要掃描的端口數目 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; // 默許端口掃描 // 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 ++) { // 測試以後線程能否年夜於180 TestThread(180); // 發生新的線程處置端口掃描 CWinThread * pthread = AfxBeginThread(portscan,LPVOID(atoi((char*)ports[sport]))); //延時 Sleep(120); } } // 自端口掃描 // scan -p 21 192.168.0.1 // scan -p 21-80 192.168.0.1-192.168.0.254 if (xp == 4) { // 盤算要掃描的端口 sport = endport - startport; if(sport > 500 ) { // 掃描自的端口 for(sport = startport; sport <= endport; sport ++, maxth ++, searched ++) { TestThread(2000); // 發生新的線程處置端口掃描 CWinThread * pthread = AfxBeginThread(portscan, LPVOID(sport)); // 延時 Sleep(10); } } else { // 掃描自的端口 for(sport = startport; sport <= endport; sport ++, maxth ++, searched ++) { // 測試以後線程能否年夜於250 TestThread(250); // 發生新的線程處置端口掃描 CWinThread * pthread = AfxBeginThread(portscan, LPVOID(sport)); // 延時 Sleep(100); playx(); } } } } // 期待一切的線程停止 WaitThreadEnd(); // 顯示端口掃描停止時光 timenow = _strtime(timebuf); printf("\r\nPortScan End Time: %s", timenow); printf("\r\nScan %d Hosts completed. Open %d Ports!\r\n", scannum, scanok); }
為了測試該端口掃描器,可使用以下銜接測試代碼停止測試,源碼以下:
/*此函數完成斷定m_server的m_port端口能否可以連上,超時限制為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); //上面的一句必定要,重要針對防火牆 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; }