首页 > 代码库 > mac/unix系统:C++实现一个端口扫描器
mac/unix系统:C++实现一个端口扫描器
在比较早以前,我用过S扫描器, 以及大名鼎鼎的nmap扫描器, 可以快速扫描某个主机开放的端口, 今天使用C实现这样一个软件,
编译环境为Mac, 系统版本10.11.6:
#include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <unistd.h>#include <time.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h>#include <arpa/inet.h>void msg() { printf("EP:scan ip startport endport\nEP:scan ip 127.0.0.1 20 2009\n"); } int main(int argc,char** argv) { char *ip; int startport,endport,sockfd,i; struct sockaddr_in to; float costtime; clock_t start,end; if(4!=argc) { msg(); return 0; } ip=argv[1]; startport=atoi(argv[2]); endport=atoi(argv[3]); if(startport<1 || endport>65535 || endport<startport) { printf("端口范围出错/n"); return 0; } else{ printf("IP:%s %d-%d\n",ip,startport,endport); } to.sin_family=AF_INET; to.sin_addr.s_addr=inet_addr(ip); start=clock(); for(i=startport;i<=endport;i++) { sockfd=socket(AF_INET,SOCK_STREAM,0); to.sin_port=htons(i); if(connect(sockfd,(struct sockaddr *)&to,sizeof(struct sockaddr)) == 0) { printf("%s %d\n",ip,i); close(sockfd); }; } end=clock(); costtime=(float)(end-start)/CLOCKS_PER_SEC; printf("用时:%f秒\n",costtime); return 0; }
亲测可行:
以上的代码只能检测固定的ip, 通过更改源码, 软件可以支持区域ip端口的检测, 多加一个循环:
#include <stdio.h>#include <stdlib.h>#include <sys/socket.h>#include <unistd.h>#include <time.h>#include <sys/types.h>#include <netinet/in.h>#include <netdb.h>#include <arpa/inet.h>#include <string.h>void msg(){ printf( "EP:scan ip startport endport\nEP:scan ip 127.0.0.1 20 2009\n" ); printf( "EP:scan ip endip startport endport\nEP:scan ip 127.0.0. 250 20 2009\n" );}int main( int argc, char** argv ){ char * ip; char * endip; int startport, endport, sockfd, i; struct sockaddr_in to; float costtime; clock_t start, end; if ( 4 == argc ) { ip = argv[1]; startport = atoi( argv[2] ); endport = atoi( argv[3] ); if ( startport < 1 || endport > 65535 || endport < startport ) { printf( "端口范围出错/n" ); return(0); }else { printf( "IP:%s %d-%d\n", ip, startport, endport ); } to.sin_family = AF_INET; to.sin_addr.s_addr = inet_addr( ip ); start = clock(); for ( i = startport; i <= endport; i++ ) { sockfd = socket( AF_INET, SOCK_STREAM, 0 ); to.sin_port = htons( i ); if ( connect( sockfd, (struct sockaddr *) &to, sizeof(struct sockaddr) ) == 0 ) { printf( "%s %d\n", ip, i ); close( sockfd ); } ; } end = clock(); costtime = (float) (end - start) / CLOCKS_PER_SEC; printf( "用时:%f秒\n", costtime ); return(0); }else if ( 5 == argc ) { ip = argv[1]; endip = argv[2]; startport = atoi( argv[3] ); endport = atoi( argv[4] ); char *tempip; if ( startport < 1 || endport > 65535 || endport < startport ) { printf( "端口范围出错/n" ); return(0); }else { /* 循环ip地址 */ char *ipval; start = clock(); for ( int i = 1; i < atoi( endip ); i++ ) { sprintf( ipval, "%s%d", ip, i ); printf( "IP:%s\n", ipval ); to.sin_family = AF_INET; to.sin_addr.s_addr = inet_addr( ipval ); for ( i = startport; i <= endport; i++ ) { printf("%s => %d\n", ipval , i); sockfd = socket( AF_INET, SOCK_STREAM, 0 ); to.sin_port = htons( i ); if ( connect( sockfd, (struct sockaddr *) &to, sizeof(struct sockaddr) ) == 0 ) { printf( "%s %d\n", ip, i ); close( sockfd ); } printf("end\n"); } } end = clock(); costtime = (float) (end - start) / CLOCKS_PER_SEC; printf( "用时:%f秒\n", costtime ); } return(0); } msg(); return(0);}
参考链接:
Linux C语言写的超级简单端口扫描器 http://blog.csdn.net/kongjiajie/article/details/4799986
Linux的SOCKET编程详解 http://blog.csdn.net/hguisu/article/details/7445768/
EOF
mac/unix系统:C++实现一个端口扫描器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。