- 论坛徽章:
- 0
|
以下是自己以前写的一个端口扫描程序,但是
1。扫描结果很不稳定 有的时候同一IP每次扫描出来的结果都不一样(以前问过,但是没有解决)
2。如果目的主机没有防火墙,很有可能显示对方开放所有端口。
另外,如何通过扫描区别网关?
- #include <stdio.h>;
- #include <sys/time.h>;
- #include <sys/socket.h>;
- #include <sys/types.h>;
- #include <netinet/in.h>;
- #include <unistd.h>;
- #include <fcntl.h>;
- #include <netdb.h>;
- #include <string.h>;
- #ifndef MAXNAMELEN
- #define MAXNAMELEN 256
- #endif
- #define MAXPORT 256
- u_long startip,endip;
- int port[MAXPORT]={21,23,25,53,79,80,88,109,110,135,139,1080,1434,3389,7626,8080};
- int portcount=16;
- struct timeval time={5,0};
- fd_set fdset;
- void usage();
- void setiphost();
- void resetport(char* newport);
- void setip(char* start,char* end);
- void scan(u_long ip);
- main(int argc,char *argv[])
- {
-
- int i=1;
- u_long ip;
- printf("%u\n",argc);
- printf("%u\n",inet_addr(argv[1]));
- switch(argc)
- {
- case 1:
- setiphost();
- break;
- case 2:
- printf("argc:2\n");
- if(argv[1]=="h")
- {
- usage();
- }
- else if(strchr(argv[1],',')!=0)
- {
- setip(argv[1],argv[1]);
- }
- else
- {
- resetport(argv[1]);
- }
- break;
- case 3:
- printf("argc:3\n");
- setip(argv[1],argv[2]);
- break;
- case 4:
- printf("argc:4\n");
- setip(argv[1],argv[2]);
- resetport(argv[3]);
- break;
- default:
- usage();
- }
- printf("we are going to scan %u\t%u\n",startip,endip);
- printf("%s\t%s\n",argv[1],argv[2]);
- for(ip=startip;ip<=endip;ip++)
- {
- scan(ip);
- printf("\n");
- }
- }
- void usage()
- {
- printf("scan startip endip port\n");
- printf("for example:scan 11.11.11.11 11.11.11.60 21,23,80\n");
- printf("if there is no port,the program will scan port21,23,25,53,79,80,88,109,110,135,139,1080,1434,3389,7626,8080.\n");
- printf("if there is no startip and endip,the program will scan the host.\n");
- exit(0);
- return;
- }
- void setip(char* start,char* end)
- {
- u_long change;
- startip=ntohl(inet_addr(start));
-
- printf("start\t%s\t%u\n",start,startip);
- endip=ntohl(inet_addr(end));
- printf("%d\n",inet_addr(end));
- printf("end\t%s\t%u\n",end,endip);
- if(startip>;endip)
- {
- change=startip;
- startip=endip;
- endip=change;
- }
- return;
- }
- void setiphost()
- {
- struct hostent *hst;
- char name[MAXNAMELEN];
- size_t size;
- if(gethostname(name,size)!=0)
- {
- printf("gethostname() failed.\n");
- exit(-1);
- }
- if(hst=gethostbyname(name))
- {
- printf("local ip address:%s",hst->;h_addr_list[0]);
- setip(hst->;h_addr_list[0],hst->;h_addr_list[0]);
- }
- else
- {
- printf("gethostbyname() failed.\n");
- usage();
- }
- return;
- }
- void resetport(char* newport)
- {
- char *p;
- char *douhao=",";
- portcount=0;
- if(p=strtok(newport,douhao))
- {
- port[0]=atoi(p);
- printf("you are going to scan port:%d\t",port[0]);
- portcount=1;
- }
- else
- {
- printf("resetport() failed.\n");
- usage();
- }
- while(p=strtok(NULL,douhao))
- {
- port[portcount]=atoi(p);
- printf("%d\t",port[portcount]);
- portcount++;
- }
- }
- void scan(u_long ip)
- {
- struct sockaddr_in sckddr;
- int skt,i,j,m;
- int sasize=sizeof(sckddr);
- sckddr.sin_family=AF_INET;
- sckddr.sin_addr.s_addr=htonl(ip);
-
- for(i=0;i<portcount;i++)
- {
-
- sckddr.sin_port=htons(port[i]);
- if((skt=socket(AF_INET,SOCK_STREAM,0))<0)
- {
- printf("socket() failed.\n");
- exit(-1);
- }
- if(fcntl(skt,F_SETFL,O_NDELAY)<0)
- {
- printf("fcntl() failed.\n");
- exit(-1);
- }
- connect(skt,(struct sockaddr *)&sckddr,sasize);
- FD_ZERO(&fdset);
- FD_SET(skt,&fdset);
- j=select(skt+1,(fd_set *)0,&fdset,(fd_set *)0,&time);
- switch (j)
- {
- case -1:
- printf("select() failed.\n");
- exit(-1);
- case 0:
- close(skt);
- break;
- default:
- if(FD_ISSET(skt,&fdset))
- {
- printf("%s\topen\t%u\n",inet_ntoa(ntohl(ip)),port[i]);
- }
-
- }
- }
- }
复制代码 |
|