当前位置:编程学习 > 网站相关 >>

netbios协议探测主机信息

NetBIOS是网络基本输入输出系统,通过netbios协议,可以得到局域网内的主机信息

在windows下可以使用nbtstat命令查看netbios信息

nbtstat -A 192.168.0.103

向指内网指定地址发送netbios协议数据包,可以得到局域网内机器的计算机名,工作组,MAC地址

代码如下:

[cpp
#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  
           &n

补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,