THCsmbgetOS.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * This is a little smb OS-detection tool which gets workgroup, smbserver and OS
  3. * works for all tested samba versions on different platforms
  4. * like: macosx,aix,solaris,linux,bsd and all Windows platforms !
  5. * below you can see some sample outputs:
  6. *
  7. * Windows 2003 gives me:
  8. * Remote OS:
  9. * ----------
  10. * WINDOMAIN1
  11. * Windows Server 2003 5.2
  12. * Windows Server 2003 3790
  13. *
  14. * Windows NT gives me:
  15. * Remote OS:
  16. * ----------
  17. * WINDOMAIN2
  18. * NT LAN Manager 4.0
  19. * Windows NT 4.0
  20. *
  21. * Windows 2k gives me:
  22. * Remote OS:
  23. * ----------
  24. * WINDOMAIN3
  25. * Windows 2000 LAN Manager
  26. * Windows 5.0
  27. *
  28. * Windows XP gives me:
  29. * Remote OS:
  30. * ----------
  31. * WINDOMAIN4
  32. * Windows 2000 LAN Manager
  33. * Windows 5.1
  34. *
  35. * Samba gives me:
  36. * Remote OS:
  37. * ----------
  38. * SAMBADOMAIN1
  39. * Samba 2.0.7
  40. * Unix
  41. *
  42. * COMPILE:
  43. * cl THCsmbgetOS.c
  44. *
  45. * RUN:
  46. * C:\ccode\THCsmbgetOS>THCsmbgetOS.exe gnpctx01
  47. *
  48. * -------------------------------------------------------
  49. * THCsmbgetOS v0.1 - gets group, server and os via SMB
  50. * by Johnny Cyberpunk (jcyberpunk@thc.org)
  51. * -------------------------------------------------------
  52. *
  53. * [*] Connecting Port 139....
  54. * [*] Sending session request....
  55. * [*] Sending negotiation request....
  56. * [*] Sending setup account request....
  57. * [*] Successful....
  58. *
  59. * Remote OS:
  60. * ----------
  61. * MYNTDOMAIN
  62. * Windows Server 2003 5.2
  63. * Windows Server 2003 3790
  64. *
  65. * Enjoy,
  66. *
  67. * http://www.thc.org
  68. */
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #include <winsock2.h>
  73. #pragma comment(lib, "ws2_32.lib")
  74. char sessionrequest[] =
  75. "\x81\x00\x00\x44\x20\x43\x4b\x46\x44\x45\x4e\x45\x43\x46\x44\x45"
  76. "\x46\x46\x43\x46\x47\x45\x46\x46\x43\x43\x41\x43\x41\x43\x41\x43"
  77. "\x41\x43\x41\x43\x41\x00\x20\x45\x4b\x45\x44\x46\x45\x45\x49\x45"
  78. "\x44\x43\x41\x43\x41\x43\x41\x43\x41\x43\x41\x43\x41\x43\x41\x43"
  79. "\x41\x43\x41\x43\x41\x41\x41\x00";
  80. char negotiate[] =
  81. "\x00\x00\x00\x2f\xff\x53\x4d\x42\x72\x00\x00\x00\x00\x00\x00\x00"
  82. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x5c\x02"
  83. "\x00\x00\x00\x00\x00\x0c\x00\x02\x4e\x54\x20\x4c\x4d\x20\x30\x2e"
  84. "\x31\x32\x00";
  85. char setupaccount[] =
  86. "\x00\x00\x00\x48\xff\x53\x4d\x42\x73\x00\x00\x00\x00\x00\x00\x00"
  87. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x5c\x02"
  88. "\x00\x00\x00\x00\x0d\xff\x00\x00\x00\xff\xff\x02\x00\x5c\x02\x00"
  89. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x0b"
  90. "\x00\x00\x00\x4a\x43\00\x41\x54\x54\x48\x43\x00";
  91. int main(int argc, char *argv[])
  92. {
  93. unsigned short smbport=139;
  94. unsigned char *infobuf;
  95. unsigned int sock,addr,i;
  96. int rc;
  97. struct sockaddr_in smbtcp;
  98. struct hostent * hp;
  99. WSADATA wsaData;
  100. unsigned int zeroc=0;
  101. printf("\n-------------------------------------------------------\n");
  102. printf(" THCsmbgetOS v0.1 - gets group, server and os via SMB\n");
  103. printf(" by Johnny Cyberpunk (jcyberpunk@thc.org)\n");
  104. printf("-------------------------------------------------------\n");
  105. if(argc<2)
  106. {
  107. printf("gimme host or ip\n");
  108. exit(-1);
  109. }
  110. if (WSAStartup(MAKEWORD(2,1),&wsaData) != 0)
  111. {
  112. printf("WSAStartup failed !\n");
  113. exit(-1);
  114. }
  115. hp = gethostbyname(argv[1]);
  116. if (!hp){
  117. addr = inet_addr(argv[1]);
  118. }
  119. if ((!hp) && (addr == INADDR_NONE) )
  120. {
  121. printf("Unable to resolve %s\n",argv[1]);
  122. exit(-1);
  123. }
  124. sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  125. if (!sock)
  126. {
  127. printf("socket() error...\n");
  128. exit(-1);
  129. }
  130. if (hp != NULL)
  131. memcpy(&(smbtcp.sin_addr),hp->h_addr,hp->h_length);
  132. else
  133. smbtcp.sin_addr.s_addr = addr;
  134. if (hp)
  135. smbtcp.sin_family = hp->h_addrtype;
  136. else
  137. smbtcp.sin_family = AF_INET;
  138. smbtcp.sin_port=htons(smbport);
  139. infobuf=malloc(256);
  140. memset(infobuf,0,256);
  141. printf("\n[*] Connecting Port 139....\n");
  142. rc=connect(sock, (struct sockaddr *) &smbtcp, sizeof (struct sockaddr_in));
  143. if(rc==0)
  144. {
  145. printf("[*] Sending session request....\n");
  146. send(sock,sessionrequest,sizeof(sessionrequest)-1,0);
  147. Sleep(500);
  148. rc=recv(sock,infobuf,256,0);
  149. if(rc<0)
  150. {
  151. printf("error = %d (rc=%u)\n\n",WSAGetLastError(),rc);
  152. return (-1);
  153. }
  154. memset(infobuf,0,256);
  155. printf("[*] Sending negotiation request....\n");
  156. send(sock,negotiate,sizeof(negotiate)-1,0);
  157. Sleep(500);
  158. rc=recv(sock,infobuf,256,0);
  159. if(rc<0)
  160. {
  161. printf("error = %d (rc=%u)\n\n",WSAGetLastError(),rc);
  162. return (-2);
  163. }
  164. memset(infobuf,0,256);
  165. printf("[*] Sending setup account request....\n");
  166. send(sock,setupaccount,sizeof(setupaccount)-1,0);
  167. Sleep(500);
  168. rc=recv(sock,infobuf,256,0);
  169. if(rc<0)
  170. {
  171. printf("error = %d (rc=%u)\n\n",WSAGetLastError(),rc);
  172. return (-3);
  173. }
  174. else if (rc==0)
  175. {
  176. printf("[*] Successful....\n");
  177. printf("\nRemote OS:\n");
  178. printf("----------");
  179. printf("\nI got back a null buffer ! WINXP sometimes does it\n");
  180. }
  181. else
  182. {
  183. printf("[*] Successful....\n");
  184. printf("\nRemote OS:\n");
  185. printf("----------");
  186. i=rc;
  187. while ((--i>0)&&(zeroc<4))
  188. {
  189. if (infobuf[i]==0x00)
  190. {
  191. printf("%s\n",(char *)&(infobuf[i+1]));
  192. zeroc++;
  193. }
  194. }
  195. }
  196. printf("\n\n");
  197. }
  198. else
  199. printf("can't connect to smb port 139!\n");
  200. shutdown(sock,1);
  201. closesocket(sock);
  202. free(infobuf);
  203. exit(0);
  204. }