ScanOneIP

本文介绍了一个使用C++编写的简单NBTSTAT网络扫描程序。该程序通过发送特定的数据包来获取远程主机的NetBIOS信息,如主机名、组名、用户名及MAC地址等,并展示如何解析接收到的数据。程序包括了主线程、数据接收处理以及循环发送请求的线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// ScanOneIP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ScanOneIP.h"

#include <AFXSOCK.H>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;


#define destPORT 137  //nbtstat name port
#define myPORT 4321
#define IDP_SOCKETS_INIT_FAILED         103
/////////////////////////////////////////////////////////////////////////////
//--------------------------------var-------------------------------
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};
unsigned char B1[4],B2[4];
HANDLE wait_handle;
int i;

class CUDP : public CSocket
{	
public:
	virtual void OnReceive(int nErrorCode);
};

CUDP m_UDPSocket;

void CUDP::OnReceive(int nErrorCode)
{
	// TODO: Add your specialized code here and/or call the base class
	BYTE Buf[500];
	CString str,strIP,strHost,strHex,strMac,Host,Group,User;
	UINT dport;
	m_UDPSocket.ReceiveFrom(Buf,500,strIP,dport,0);//接收数据
	//如果接收到的ip为空或者与原来接收到的ip相同,则返回
	//if(strIP==(char)NULL||strIP==strOldIP)return;
	//strOldIP=strIP;
	//int index=m_ListView.InsertItem(0,strIP);//将ip插入ListView
	printf("strIP:%s\n",(LPCTSTR)strIP);
	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(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);
				//m_ListView.SetItem(index,4,LVIF_TEXT,strMac, 0, 0, 0,0);
				printf("strMac:%s\n",(LPCTSTR)strMac);
				break;
			}
			
			
			if(num==0&&strHost!="")
			{
				//m_ListView.SetItem(index,2,LVIF_TEXT,strHost, 0, 0, 0,0);
				printf("strHost:%s\n",(LPCTSTR)strHost);
				Host=strHost;
				num++;
			}
			else
			{
				if(Host!=strHost&&num==1&&strHost!="")
				{
					//m_ListView.SetItem(index,1,LVIF_TEXT,strHost, 0, 0, 0,0);
					printf("strHost:%s\n",(LPCTSTR)strHost);
					Group=strHost;
					num++;
				}
				else 
				{
					if(strHost!=Host&&strHost!=Group&&num==2&&strHost!="")
					{
						User=strHost;
						if(User!="__MSBROWSE__")
						{
							//m_ListView.SetItem(index,3,LVIF_TEXT,User, 0, 0, 0,0);
							printf("User:%s\n",(LPCTSTR)User);
							num++;
						}
					}
				}
				
			}
			
			strMac=strHex;
			strHost="";
			strHex="";
			
		}
		
	}

	//触发事件,导致线程函数的继续执行
	SetEvent(wait_handle);
	CSocket::OnReceive(nErrorCode);

}

UINT NbtstatThread(LPVOID param);
CString	m_strIP;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		nRetCode = 2;
	}
	else
	{
		if(!m_UDPSocket.Create(myPORT,SOCK_DGRAM))
		{
			AfxMessageBox("Failed Create Socket");
		}
		wait_handle=CreateEvent(NULL,true,false,"receive data");//创建处于非触发状态的事件。类型为手动

		//启动线程
		CWinThread* pThread =AfxBeginThread(NbtstatThread,NULL,THREAD_PRIORITY_NORMAL);
		WaitForSingleObject(pThread->m_hThread, INFINITE);
		MessageBox(NULL,"wait","delay",MB_OK);
		// TODO: code your application's behavior here.
		CString strHello;
		strHello.LoadString(IDS_HELLO);
		cout << (LPCTSTR)strHello << endl;
	}

	return nRetCode;
}


//--------------------------nbtstat线程----------------------
UINT NbtstatThread(LPVOID param)
{
	//循环对要查询的ip发数据
	do
	{
		B1[0]=172,B1[1]=19,B1[2]=1,B1[3]=43;
		m_strIP.Format("%d.%d.%d.%d",B1[0],B1[1],B1[2],B1[3]);//得到ip
		//m_strIP.Format("%d.%d.%d.%d",172,19,1,43);
		//pDlg->m_ListBox.InsertString(0,pDlg->m_strIP);//将该ip插入ListView的ip字段
		if(B1[3]!=0&&B1[2]!=0)
			m_UDPSocket.SendTo((void*)bs,50,destPORT,m_strIP,0);//向指定的ip发数据报
        int nWait=100;//pDlg->m_spin.GetPos();//设置超时
		printf("before WaitForSingleObject\n");
		WaitForSingleObject(
			wait_handle,        // 等待事件的句柄
			//INFINITE //
			nWait   // 超时
			);
		printf("after WaitForSingleObject\n");
		ResetEvent(wait_handle);//将事件重新置回非触发状态
		//=============================================		
	}while(0);
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值