nmbnamex.c
上传用户:puhui2008
上传日期:2007-01-07
资源大小:295k
文件大小:4k
源码类别:

系统/网络安全

开发平台:

Unix_Linux

  1. /* Note i have include a little utility pinched from ADMtoolz
  2.  for get the netbios name
  3.   --------------------------------------------------------------------------
  4. ------------------------------[ADMnmbname.c]----------------------------------
  5.   --------------------------------------------------------------------------  */
  6. #define DEFAULT_OFFSET 3500
  7. #define DEFAULT_BUFFER_SIZE 3081
  8. #define NOP 0x90
  9. #define NMBHDRSIZE 13
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <sys/wait.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/stat.h>
  19. #include <netdb.h>
  20. #include <netinet/in.h>
  21. #include <netinet/ip.h>
  22. #include <netinet/ip_icmp.h>
  23. #include <netinet/ip_tcp.h>
  24. struct nmbhdr {
  25. unsigned short int id;
  26. unsigned char  R:1;
  27. unsigned char  opcode:4;
  28. unsigned char  AA:1;
  29. unsigned char  TC:1;
  30. unsigned char  RD:1;
  31. unsigned char  RA:1;
  32. unsigned char  unless:2;
  33. unsigned char  B:1;
  34. unsigned char  RCODE:4;
  35. unsigned short int que_num;
  36. unsigned short int rep_num;
  37. unsigned short int num_rr;
  38. unsigned short int num_rrsup;
  39. unsigned char namelen;
  40. };
  41. struct typez{
  42. u_int type;
  43. u_int type2;
  44. };
  45. unsigned int host2ip(char *serv)
  46. {
  47. struct sockaddr_in sin;
  48. struct hostent *hent;
  49. hent=gethostbyname(serv);
  50. if(hent == NULL) return 0;
  51. bzero((char *)&sin, sizeof(sin));
  52. bcopy(hent->h_addr, (char *)&sin.sin_addr, hent->h_length);
  53. return sin.sin_addr.s_addr;
  54. }
  55. main( int argc, char  **argv)
  56. {
  57. struct sockaddr_in  sin_me , sin_dst;
  58. struct nmbhdr *nmb,*nmb2;
  59. struct iphdr *ipz;
  60. struct typez  *typz;
  61. struct hostent *hent;
  62. int socket_client,sr,num,i=1,bha,timeout=0,try=0,GO=0;
  63. int longueur=sizeof(struct sockaddr_in);
  64. char  *data;
  65. char  *dataz;
  66. char   buffer[1024];
  67. char   buffer2[1024];
  68. char   namezz[1024];
  69. char   name[64]="CKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  70. char   c;
  71. if(argc <2) {
  72.         printf("usage: ADMnmbname <ip of the victim>n");
  73.         exit (0);
  74.         }
  75. socket_client=socket(AF_INET,SOCK_DGRAM,17);
  76. sr=socket(AF_INET,SOCK_RAW,17);
  77. ioctl(sr,FIONBIO,&i);
  78. sin_me.sin_family=AF_INET;
  79. sin_me.sin_addr.s_addr=htonl(INADDR_ANY);
  80. sin_me.sin_port=htons(2600);
  81. sin_dst.sin_family=AF_INET;
  82. sin_dst.sin_port=htons(137);
  83. sin_dst.sin_addr.s_addr = host2ip(argv[1]);
  84. nmb = (struct nmbhdr *)  buffer;
  85. data = (char *)(buffer+NMBHDRSIZE);
  86. typz = (struct typez *)(buffer+NMBHDRSIZE+33);
  87. nmb2 = (struct nmbhdr *)(buffer2+20+8);
  88. ipz   = (struct iphdr *)buffer2;
  89. dataz = (char *)(buffer2+50+7+20+8);
  90. memset(buffer,0,1024);
  91. memset(buffer2,0,1024);
  92. memset(namezz,0,1024);
  93. memcpy(data,name,33);
  94.            /* play with the netbios query format :) */
  95. nmb->id=0x003;
  96. nmb->R=0;                  /* 0 for question 1 for response */
  97. nmb->opcode=0;             /* 0 = query */
  98. nmb->que_num=htons(1);     /* i have only 1 question :) */
  99. nmb->namelen=0x20;
  100. typz->type=0x2100;
  101. typz->type2=0x1000;
  102. sendto(socket_client,buffer,50,0,(struct sockaddr *)&sin_dst,longueur);
  103.   for(timeout=0;timeout<90;timeout++ )
  104.   {
  105.            usleep(100000);
  106.            buffer2[0]='0';
  107.            recvfrom(sr,buffer2,800,0,(struct sockaddr *)&sin_dst,&(int)longueur);
  108.         if(buffer2[0]!='0')
  109.                 {
  110.                           if(nmb2->rep_num!=0)
  111.                             {
  112.                             bha=0;
  113.                                      for(;;)
  114.                                      {
  115.                                         c=*(dataz+bha);
  116.                                         if(c!='x20')
  117.                                                         {
  118.                                                         namezz[bha]=c;
  119.                                                         bha++;
  120.                                                          }
  121.                                         if(c=='x20')break;
  122.                                    }
  123. printf("%sn",namezz);
  124.                                 printf("netbios name of %s is %sn",argv[1],namezz);
  125.                                 try =4;
  126.                                 GO = 4;
  127.                                 break;
  128.                               }
  129.                 }
  130.      }
  131. memset(buffer,0,1024);
  132. memset(buffer2,0,1024);
  133. }