在比较早以前,我用过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
时间: 2024-10-13 22:24:27