ADMscan3.c
上传用户:szwxkjdz
上传日期:2007-01-05
资源大小:4k
文件大小:6k
源码类别:

扫描程序

开发平台:

Unix_Linux

  1. #define IPHDRSIZE sizeof(struct iphdr)
  2. #define ICMPHDRSIZE sizeof(struct icmphdr)
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <signal.h>
  6. #include <sys/ioctl.h>
  7. //#include <sys/filio.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. //#include "mytypes.h"
  11. #include "ip.h"
  12. #include "ip_icmp.h"
  13. #include <netinet/in.h>
  14. #include <netdb.h>
  15. extern char *inet_ntoa(struct in_addr);
  16. extern int atoi(const char *);
  17. extern char *strncpy(char *, const char *, size_t );
  18. extern void *memset(void *, int , size_t );
  19. int raw_send,raw_icmp;
  20. unsigned char packet[IPHDRSIZE +ICMPHDRSIZE+1] =  //datasize is always 0
  21. "x45x00"
  22. "x00x1c" //htons(IPHDRSIZE +ICMPHDRSIZE)
  23. "x00x00x00x00xffx01x00x00x00x00x00x00x00x00x00x00" //end of IP
  24. "x08x00";
  25. /*
  26.  * in_cksum --
  27.  *  Checksum routine for Internet Protocol family headers (C Version)
  28.  */
  29. unsigned short in_cksum(addr, len)
  30.     u_short *addr;
  31.     int len;
  32. {
  33.     register int nleft = len;
  34.     register u_short *w = addr;
  35.     register int sum = 0;
  36.     u_short answer = 0;
  37.  
  38.     /*
  39.      * Our algorithm is simple, using a 32 bit accumulator (sum), we add
  40.      * sequential 16 bit words to it, and at the end, fold back all the
  41.      * carry bits from the top 16 bits into the lower 16 bits.
  42.      */
  43.     while (nleft > 1)  {
  44.         sum += *w++;
  45.         nleft -= 2;
  46.     }
  47.  
  48.     /* mop up an odd byte, if necessary */
  49.     if (nleft == 1) {
  50.         *(u_char *)(&answer) = *(u_char *)w ;
  51.         sum += answer;
  52.     }
  53.  
  54.     /* add back carry outs from top 16 bits to low 16 bits */
  55.     sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  56.     sum += (sum >> 16);         /* add carry */
  57.     answer = ~sum;              /* truncate to 16 bits */
  58.     return(answer);
  59. }
  60. unsigned long int host2ip(char *host)
  61. {
  62. struct sockaddr_in sin;
  63. struct hostent *hent;
  64.       
  65. if ((hent=gethostbyname(host)) == NULL){
  66. perror("gethostbyname");
  67. return -1;
  68. }
  69. memcpy((char *)&sin.sin_addr,hent->h_addr, hent->h_length);
  70. return sin.sin_addr.s_addr;
  71. }
  72. char *inet_nt0a(u_long source)
  73. {
  74. struct in_addr koko;
  75. koko.s_addr = source;
  76. return(inet_ntoa(koko));
  77. }
  78. void icmp_echo(unsigned long int dest, int id, int seq) 
  79. {
  80. struct sockaddr_in sin_dst;
  81. ((struct iphdr *)packet)->daddr    = dest;
  82. ((struct iphdr *)packet)->check    = 0;
  83. ((struct iphdr *)packet)->check    = in_cksum(packet, IPHDRSIZE);
  84. ((struct icmphdr *)(packet+IPHDRSIZE))->un.echo.id  = id;
  85. ((struct icmphdr *)(packet+IPHDRSIZE))->un.echo.sequence = seq;
  86. ((struct icmphdr *)(packet+IPHDRSIZE))->checksum = 0;
  87. ((struct icmphdr *)(packet+IPHDRSIZE))->checksum = in_cksum(
  88. packet+IPHDRSIZE,ICMPHDRSIZE);
  89. sin_dst.sin_addr.s_addr = dest;
  90. sin_dst.sin_family = AF_INET;
  91. if (sendto(raw_send,packet,IPHDRSIZE+ICMPHDRSIZE,0,
  92. (struct sockaddr*)&sin_dst,sizeof(struct sockaddr))==-1)
  93. puts("sendto error");
  94. }
  95. void usage(char *progname)
  96. {
  97. printf("%s [-hVvb] [-u ms] [-t tries] [-s sec] [-f filename] targethostn",progname);
  98. printf(" -u: delay in ms between two icmp echoes (default 10000)n");
  99. printf(" -b: use this if u want to scan AAA.BBB.xxx.xxx ie 256*256 hostsn");
  100. printf(" -t: tries (default 1)n");
  101. printf(" -s: time in sec to wait for replies after last echo send (default 10)n");
  102. printf(" -f: file to save the results (default "IP")n");
  103. printf(" -v: verbosen");
  104. printf(" -V: more verbosen");
  105. printf(" -h: this messagen");
  106. }
  107.                      
  108. int main(int argc, char **argv)
  109. {
  110. struct sockaddr_in recv_dst;
  111. unsigned long da_ip;
  112. char buffer[4096]="IP"; //default filename
  113. char reply[256*256];
  114. int b=1;
  115. int on=1,verbose=0,verbose2=0;
  116. int i,j,n,len,timez=10,max_try=1;
  117. FILE *log;
  118. pid_t pid;
  119. unsigned long int nip;
  120. struct iphdr *ip=(struct iphdr*)buffer;
  121. struct icmphdr *icmp=(struct icmphdr*)(buffer+IPHDRSIZE);
  122. int o; //for getopt stuff
  123. u_long usec=10000;
  124. char *targethost;
  125. char *progname = argv[0];
  126.  printf("3lite-subnet scanner 3.0 n");
  127.  printf("ADM 0wnz U... n");
  128.  printf("GreetzZ : ALL ADM & [[- HeiKe -]] & bheu ;)) n");
  129. if (argc <2){usage(progname); exit(0);}
  130. while ((o = getopt(argc, argv, "u:f:t:s:vbVh")) != EOF)
  131. switch((char) o){
  132. case 'u':
  133. if (optarg)
  134. usec = atoi(optarg);
  135. break;
  136. case 'b':
  137. b=256;
  138. break;
  139. case 't':
  140. if (optarg)
  141. max_try = atoi(optarg);
  142. break;
  143. case 's':
  144. if (optarg)
  145. timez = atoi(optarg);
  146. break;
  147. case 'f':
  148. if (optarg)
  149. strcpy(buffer,optarg);
  150. break;
  151. case 'v':
  152. verbose2 = 1;
  153. break;
  154. case 'V':
  155. verbose = 1;
  156. verbose2 = 1;
  157. break;
  158. case 'h':
  159. usage(progname);
  160. exit(0);
  161. }
  162. if (optind >= argc) {
  163. usage(progname);
  164. exit(1);
  165. }
  166. targethost = argv[optind];
  167. if ((log=fopen(buffer,"w")) == NULL){
  168. perror("fopen");
  169. exit(1);
  170. }
  171. if ((raw_send=socket(AF_INET, SOCK_RAW, 255)) == -1){
  172. printf("cant open raw socket for sendingn");
  173. exit(1);
  174. }
  175. if ((raw_icmp=socket(AF_INET ,SOCK_RAW ,1)) == -1){
  176. printf("can't open raw socket for receivingn");
  177. exit(1);
  178. }
  179. ioctl(raw_icmp,FIONBIO,&on);
  180. if (!(nip=host2ip(targethost))) exit(1);
  181.   da_ip=ntohl(nip);
  182. if (b==1)
  183. da_ip = da_ip & 0xFFFFFF00; 
  184. else
  185. da_ip=da_ip & 0xFFFF0000;
  186. switch (pid=fork()) {
  187.        case -1:
  188. perror("fork");
  189. exit(1);
  190.     case 0:
  191. memset(reply,0,sizeof(reply));
  192. while (1) {
  193.   len=sizeof(struct sockaddr);
  194.   n=recvfrom(raw_icmp,buffer,sizeof(buffer),0,
  195.    (struct sockaddr*)&recv_dst,&len);
  196.                   if ((n!=-1) && !(icmp->type)) {
  197. if (verbose) printf("-->reply from %s (id=%i,seq=%i)n",
  198. inet_nt0a(ip->saddr),icmp->un.echo.id,
  199. icmp->un.echo.sequence);
  200. if (!reply[icmp->un.echo.sequence]) {
  201.   fprintf(log,"%sn",inet_nt0a(ip->saddr));
  202.   fflush(log);
  203.   reply[icmp->un.echo.sequence]=1;
  204. }
  205.   }
  206. }
  207. break;
  208.     default:
  209. for (j=0;j<max_try;j++) 
  210. for (i=0;i<(256*b);i++) {
  211. if (verbose2) printf("send echo to: %s (id=%i, seq=%i)n",
  212. inet_nt0a(htonl(da_ip+i)),j,i);
  213. icmp_echo(htonl(da_ip+i),j,i);
  214. usleep(usec);
  215. }
  216. sleep(timez);
  217. kill(pid,SIGTERM);
  218. }
  219. exit(0);
  220.