linux多线程扫描器

linux多线程扫描器


刺猬@http://blog.csdn.net/littlehedgehog






那个深入浅出驱动程序系列文章后面作者似乎有些心不在焉,文章草草了事,还是自己来看看相关的书吧。



再来说说这个scanner,速度还是过得去的,在我测试的环境中,跟nmap速度不相上下,不过郁闷的是扫描主机比较慢。说来其实没多大实用价值,仅供学习参观之用。代码就在下面,有兴趣自己可以改改用。


  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <regex.h>
  6. #include <pthread.h>
  7. #include <sys/types.h>
  8. #include <sys/time.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #define DEBUG
  12. #ifdef DEBUG
  13. #define Debug   printf
  14. #else
  15. #define Debug   //
  16. #endif
  17. #define PORT_LIMIT  32
  18. #define IP_LIMIT    32
  19. #define VERSION     0.1
  20. #define swap(a,b,type)  do{type temp=a;a=b;b=temp;}while(0)
  21. #define oops(msg,x) do{perror(msg);exit(x);}while(0)
  22. struct scan_range
  23. {
  24.     u_long  s_ip;
  25.     u_long  e_ip;
  26.     u_short s_port;
  27.     u_short e_port;
  28. };
  29. ///
  30. void usage(char *);
  31. int scan_port_range(u_long,u_short,u_short);
  32. int scan_ip_range(u_long,u_long,u_short);
  33. int scan_thread(struct scan_range *s_range);
  34. int scan_port_array(u_long ip,const u_short *port);
  35. int scan_ip_array(const u_long *ip_array,u_short port);
  36. int read_port(char *filename);
  37. int read_ip(char *filename);
  38. int do_connect(u_long,u_short);
  39. /
  40. extern char *optarg;
  41. pthread_t   thread;
  42. u_short port_array[PORT_LIMIT];     //为ip、port文件定义的数组
  43. u_long  ip_array[IP_LIMIT];
  44. static char err=1,errlen=1;
  45. struct scan_range s_range;
  46. ///
  47. int main(int argc,char *argv[])
  48. {
  49.     char ch;
  50.     char *port_file=NULL;
  51.     char *ip_file=NULL;
  52.     char *delimiter=NULL;
  53.     u_long s_ip=0,e_ip=0;
  54.     u_short s_port=0,e_port=0;
  55.        
  56.     memset(port_array,0,sizeof(port_array));
  57.     memset(ip_array,0,sizeof(ip_array));
  58.      
  59.     while ((ch=getopt(argc,argv,"i:p:h"))!=-1)
  60.     {
  61.         switch (ch)
  62.         {
  63.         case 'i':
  64.         case 'I':
  65.             if ((delimiter=strchr(optarg,'-'))!=NULL)   //ip范围
  66.             {
  67.                 *delimiter++=0;
  68.                 s_ip=ntohl(inet_addr(optarg));
  69.                 e_ip=ntohl(inet_addr(delimiter));
  70.                 *(--delimiter)='-';
  71.                 if(s_ip>e_ip)
  72.                     swap(s_ip,e_ip,u_long);
  73.             }
  74.             else
  75.             {
  76.                 if (!~inet_addr(optarg))                //说明是ip文件
  77.                 {
  78.                     ip_file=optarg;
  79.                 }
  80.                 else                                    //单个ip
  81.                     s_ip=ntohl(inet_addr(optarg)),e_ip=s_ip;
  82.             }
  83.             break;
  84.         case 'p':
  85.         case 'P':
  86.             if ((delimiter=strchr(optarg,'-'))!=NULL)   //port范围
  87.             {
  88.                 s_port=atoi(optarg);
  89.                 e_port=atoi(++delimiter);
  90.                 if(s_port>e_port)
  91.                     swap(s_port,e_port,u_short);
  92.                 s_port=s_port<1?1:s_port;
  93.                 e_port=e_port>34464?34464:e_port;
  94.             }
  95.             else
  96.             {
  97.                 if (strstr(optarg,".txt")!=NULL)        //port文件
  98.                 {
  99.                     port_file=optarg;
  100.                 }
  101.                 else                                //单个port
  102.                 {
  103.                     s_port=atoi(optarg);
  104.                     e_port=s_port;
  105.                 }
  106.             }
  107.             break;
  108.         case 'h':
  109.         case 'H':
  110.             usage(argv[0]);
  111.             return 0;
  112.         default:
  113.             printf("[+]What do you want?  -h for help/n");
  114.             return 0;
  115.         }
  116.     }
  117.     if (s_ip&&e_ip)
  118.     {
  119.         if (s_ip==e_ip)             //单个ip扫描
  120.         {
  121.             if (port_file)
  122.             {
  123.                 read_port(port_file);
  124.                 printf("[+]Now scan the target according to the port file %s/n",port_file);
  125.                 scan_port_array(s_ip,port_array);
  126.             }
  127.             else
  128.             {
  129.                 printf("[+]Now scan the taget %s from port %d to %d/n",argv[2],s_port,e_port);
  130.                 scan_port_range(s_ip,s_port,e_port);
  131.             }
  132.         }
  133.         else                        //多个ip扫描
  134.         {
  135.             if (s_port==e_port)
  136.             {
  137.                 printf("[+]Note! this function may take more time BE PATIENT! /nNow scan the targets %s /n",argv[2]);
  138.                 scan_ip_range(s_ip,e_ip,s_port);
  139.             }
  140.             else if (s_port!=e_port)
  141.             {
  142.                 printf("[+]Sorry,this function hasn't been implemented/n");
  143.             }
  144.         }
  145.     }
  146.     else if (ip_file)
  147.     {
  148.         read_ip(ip_file);
  149.         if (s_port==e_port)
  150.         {
  151.             printf("[+]Now scan these targets in %s /n",ip_file);
  152.             scan_ip_array(ip_array,s_port);
  153.         }
  154.         else
  155.             printf("[+]Sorry,this function hasn't been implemented/n");
  156.     }
  157.     else
  158.     {
  159.         printf("[+]What do you want?/t-h for help?/n");
  160.         return -1;
  161.     }
  162.     printf("[+] Mission completed!/n");
  163.     return 0;
  164. }
  165. void usage(char *scanner)
  166. {
  167.     printf("/n/t/t/t For you  WLL/n");
  168.     printf("/n/n[+] linux multi-thread  scanner 0.1 (c) 刺猬 /n/n");
  169.     printf("usage : %s -i <ip|ip range|ipfile> -p <port|port range|port file>/n/n",scanner);
  170.     printf("NOTE: the file MUST be extended .txt/n/n");
  171.     printf("example:/n");
  172.     printf("%s -i 192.168.0.1 -p 1-10000/n",scanner);
  173.     printf("%s -i 192.168.0.1-192.168.0.254 -p 80/n",scanner);
  174.     printf("%s -i ip.txt -p 3389/n",scanner);
  175.     printf("/nHave fun! by the way some function hasn't been implemented... /n/n/n");
  176.     exit(0);
  177. }
  178. /*
  179.  * 扫描port范围  从start_port 到 end_port
  180.  */
  181. int scan_port_range(u_long ip,u_short start_port,u_short end_port)
  182. {
  183.     int sum=end_port-start_port;
  184.     int granularity=sum>>4;
  185.     int i=start_port;
  186.     if (!granularity)
  187.         granularity=sum;
  188.     s_range.s_ip=ip;
  189.     s_range.e_ip=ip;
  190.     do
  191.     {
  192.         s_range.s_port=i;
  193.         s_range.e_port=(i+granularity)>end_port?end_port+1:i+granularity;
  194.         if (pthread_create(&thread,NULL,(void *)scan_thread,(void *)&s_range)!=0)
  195.         {
  196.             warn("Create thread failed!");
  197.             continue;
  198.         }
  199.         pthread_join(thread,NULL);
  200.     }
  201.     while ((i=s_range.e_port)<end_port);
  202. }
  203. /*
  204.  * 扫描ip范围 从s_ip 到 e_ip
  205.  */
  206. int scan_ip_range(u_long s_ip,u_long e_ip,u_short port)
  207. {
  208.     int sum=e_ip-s_ip;
  209.     int granularity=sum>>3;
  210.     int i=s_ip;
  211.     if (!granularity)
  212.         granularity=sum;
  213.     s_range.s_port=port;
  214.     s_range.e_port=port;
  215.     do
  216.     {
  217.         s_range.s_ip=i;
  218.         s_range.e_ip=(i+granularity)>e_ip?e_ip+1:i+granularity;
  219.         if (pthread_create(&thread,NULL,(void *)scan_thread,(void *)&s_range)!=0)
  220.         {
  221.             warn("Create thread failed!");
  222.             continue;
  223.         }
  224.         pthread_join(thread,NULL);
  225.     }
  226.     while ((i=s_range.e_ip)<e_ip);
  227. }
  228. /*
  229.  * 扫描线程  注意 我没有实现多ip多port扫描
  230.  */
  231. int scan_thread(struct scan_range *s_range)
  232. {
  233.     int start,end,temp;
  234.     if (s_range->s_ip==s_range->e_ip)       //扫描端口
  235.     {
  236.         temp=htonl(s_range->s_ip);
  237.         start=s_range->s_port;
  238.         end=s_range->e_port;
  239.         while (start<end)
  240.         {
  241.             if (do_connect(temp,htons(start))==1)
  242.             {
  243.                 printf("%d/n",start);
  244.                 fflush(stdout);
  245.             }
  246.             start++;
  247.         }
  248.     }
  249.     else                                    //扫描ip
  250.     {
  251.         temp=htons(s_range->s_port);
  252.         start=s_range->s_ip;
  253.         end=s_range->e_ip;
  254.         while (start<end)
  255.         {
  256.             if (do_connect(htonl(start),temp)==1)
  257.             {
  258.                 printf("%s/n",inet_ntoa(htonl(start)));
  259.                 fflush(stdout);
  260.             }
  261.             start++;
  262.         }
  263.     }
  264.     return (0);
  265. }
  266. /*
  267.  * 扫描端口数组 
  268.  */
  269. int scan_port_array(u_long ip,const u_short *port)
  270. {
  271.     u_short *pos=port;
  272.     while (*pos)
  273.     {
  274.         if (do_connect(htonl(ip),htons(*pos))==1)
  275.             printf("%d/n",*pos);
  276.         pos++;
  277.     }
  278. }
  279. /*
  280.  * 扫描ip数组
  281.  */
  282. int scan_ip_array(const u_long *ip_array,u_short port)
  283. {
  284.     u_long *pos=ip_array;
  285.     while (*pos)
  286.     {
  287.         if (do_connect(*pos,htons(port))==1)
  288.             printf("%s/n",inet_ntoa(*pos));
  289.         pos++;
  290.     }
  291. }
  292. /* 
  293.  * 读取port文件和ip文件
  294.  */
  295. int read_port(char *filename)
  296. {
  297.     int count=0,buffer;
  298.     FILE *fp;
  299.     if ((fp=fopen(filename,"r"))==NULL)
  300.         oops("Cannot open the port file",-1);
  301.     while (!feof(fp)&&count<PORT_LIMIT)
  302.     {
  303.         fscanf(fp,"%d",&port_array[count++]);
  304.     }
  305.     return 0;
  306. }
  307. int read_ip(char *filename)
  308. {
  309.     int     count=0;
  310.     char    buffer[20];
  311.     FILE *fp;
  312.     if ((fp=fopen(filename,"r"))==NULL)
  313.         oops("Cannot open the ip file",-1);
  314.     while (!feof(fp)&&count<IP_LIMIT)
  315.     {
  316.         fscanf(fp,"%s",buffer);
  317.         ip_array[count++]=(inet_addr(buffer));
  318.     }
  319.     ip_array[--count]=0;
  320.     return 0;
  321. }
  322. /*
  323.  * 如果连接成功 表示该端口打开
  324.  */
  325. int do_connect(u_long addr,u_short port)
  326. {
  327.     u_long sock_fd;
  328.     fd_set mask;
  329.     struct timeval timeout;
  330.     struct sockaddr_in host;
  331.     host.sin_family=AF_INET;
  332.     host.sin_addr.s_addr=addr;
  333.     host.sin_port=port;
  334.     if ((sock_fd=socket(AF_INET,SOCK_STREAM,0))<0)
  335.     {
  336.         perror("1:");
  337.         return(-1);
  338.     }
  339.     if (fcntl(sock_fd,F_SETFL,O_NONBLOCK)<0)
  340.     {
  341.         close(sock_fd);
  342.         perror("2:");
  343.         return(-1);
  344.     }
  345.     connect(sock_fd,(struct sockaddr *)&host,sizeof(host));
  346.     timeout.tv_sec=1;
  347.     timeout.tv_usec=0;
  348.     FD_ZERO(&mask);
  349.     FD_SET(sock_fd,&mask);
  350.     switch (select(sock_fd+1,NULL,&mask,NULL,&timeout))
  351.     {
  352.     case -1:
  353.         close(sock_fd);
  354.         perror("3:");
  355.         return(-1);
  356.     case 0:
  357.         close(sock_fd);
  358.         break;
  359.     default:
  360.         if (FD_ISSET(sock_fd,&mask))
  361.         {
  362.             err=1;
  363.             getsockopt(sock_fd,SOL_SOCKET,SO_ERROR,(char *)&err,(void *)&errlen);
  364.             if (err==0)
  365.             {
  366.                 close(sock_fd);
  367.                 return 1;
  368.             }
  369.         }
  370.         close(sock_fd);
  371.         break;
  372.     }
  373.     return 0;
  374. }
  375. int scan_test(struct scan_range *s_range)
  376. {
  377.     int start=s_range->s_port;
  378.     int end=s_range->e_port;
  379.     printf("%d/t%d/t%d/n",s_range->s_ip,(start),(end));
  380.     sleep(1);
  381.     return (0);
  382. }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值