该实例是一个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){// 直接设置IPsetip2(argv[2]);}// 如果参数为四个elseif (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);}}// 设置IPvoid 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){// 提取出结束IPendip = strchr(cp, '-') + 1;// 提取出开始IPstrncpy(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;// 创建一个socksock = 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); // 清空集合maskFD_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.254if (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.254if (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; }
新闻热点
疑难解答