NetBIOS是网络基本输入输出系统,通过netbios协议,可以得到局域网内的主机信息。
在windows下可以使用nbtstat命令查看netbios信息
1 |
nbtstat -A 192.168.0.103 |
向指内网指定地址发送netbios协议数据包,可以得到局域网内机器的计算机名,工作组,MAC地址
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
#include <stdio.h> #include <afx.h> #include <WinSock2.h> #pragma comment(lib,"ws2_32.lib") //数据包 BYTE bs[50]={0x0,0x00,0x0,0x10,0x0,0x1,0x0,0x0,0x0,0x0, 0x0,0x0,0x20,0x43,0x4b,0x41,0x41,0x41,0x41,0x41, 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41, 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41, 0x41,0x41,0x41,0x41,0x41,0x0,0x0,0x21,0x0,0x1}; WSADATA wsadata; SOCKET m_Socket; SOCKADDR_IN serveraddr; int port=137; //netbios协议端口 } void main() { //初始化winsock版本 WORD ver=MAKEWORD(2,2); WSAStartup(ver,&wsadata); //初始socket地址信息 char strIP[20] = {"192.168.153.1"}; m_Socket = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP); serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(port); serveraddr.sin_addr.S_un.S_addr = inet_addr(strIP); //发送UDP数据包 DWORD len = sendto(m_Socket,(char*)bs,sizeof(bs),0,(sockaddr*)&serveraddr,sizeof(serveraddr)); if (len < 0) { printf("send udp error"); } //接受返回的数据包 BYTE Buf[512] = {0}; recvfrom(m_Socket,(char*)Buf,sizeof(Buf),0,NULL,NULL); printf("IP is %s\n",strIP); //关闭socket连接 closesocket(m_Socket); WSACleanup(); //处理数据包里的信息 CString str,strHost,strHex,strMac,Host,Group,User; int i; strHost=""; //机器名字 strHex="";//MAC地址 User="?";// Host="\\"; int tem=0,num=0; bool bAdd=true; //根据数据报规则取出相应的信息 for(i=57;i<500;i++) //57-72 { // if(Buf[i]==0xcc) // break; if (num>3) break; if(Buf[i]==0x20) bAdd=false; if(bAdd) { str.Format("%c",Buf[i]); if(Buf[i]>=' ') strHost+=str; str.Format("%02x.",Buf[i]); strHex+=str; } if((++tem)%18==0) { bAdd=true; strHost.TrimRight((char)NULL); if(strHost=="") { strMac.Delete(17,strMac.GetLength()-17); printf("MAC is %s\n",strMac); num++; break; } if(num==0&&strHost!="") { printf("Host is %s\n",strHost); Host=strHost; num++; } else { if(Host!=strHost && num==1&& strHost!="") { printf("Host is %s\n",strHost); Group=strHost; num++; } else { if(strHost!=Host&&strHost!=Group&&num==2&&strHost!="") { User=strHost; if(User!="__MSBROWSE__") { printf("user is %s\n",User); num++; } } } } strMac=strHex; strHost=""; strHex=""; } } } |
转载请注明:exchen's blog » netbios协议探测主机信息