网络攻防对抗实验设计报告学院:计算机科学与技术学院班级:信安1204姓名:马灿学号:20123252实验名称:局域网mac地址扫描工具实验日期:2015/12/18-2015.12.22目录1、设计题目:局域网mac地址扫描工具2、设计方案:3、模块说明:4、代码分析及程序优化5、数据文件名及说明6、系统运行测试结果分析1、设计题目a)局域网mac地址扫描工具2、设计方案a)该工具能自动扫描本机网络适配器信息,计算该局域网子网范围,确定要扫描的主机是否在线,获取在线主机mac地址等功能3、模块说明a)系统功能模块b)函数以及调用关系i.intGetAdaptersInfo()//获取本地网络适配器信息的函数1.ii.intPing(stringIp)//实现ping的功能1.iii.intip_to_mac(stringip)//实现发送arp请求获取mac地址和记录文件1.4、代码分析及程序优化a)主要函数有intGetAdaptersInfo();intPing(stringIp);intip_to_mac(stringip);5、数据文件名及说明a)程序目录下mac.txt用于保存能够ping通的主机的ip和mac信息6、系统运行测试结果分析a)程序全自动运行截图b)文本保存有效的结果c)源程序//macan.cpp:Definestheentrypointfortheconsoleapplication.////#includestdafx.h#includewinsock2.h#includewindows.h#includestdio.h#includeiphlpapi.h#pragmacomment(lib,iphlpapi.lib)#pragmacomment(lib,ws2_32.lib)#includestdlib.h#includemath.h#includefstream#includestringusingnamespacestd;IP_ADDR_STRINGIpAddressList;intGetAdaptersInfo()//获取本地网络适配器信息的函数{//变量声明IP_ADAPTER_INFO*pAdapterInfo;//指定获取到的网络信息结构体链表的指针ULONGulOutBufLen;//获取到网络信息结构体链表的长度DWORDdwRetVal;//返回调用编码PIP_ADAPTER_INFOpAdapter;//获取本地网络适配器的信息//为pAdapterINfo分配空间pAdapterInfo=(IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));ulOutBufLen=sizeof(IP_ADAPTER_INFO);//需要两次调用GetAdaptersInfo()函数//第1次调用GetAdaptersInfo(),获取返回结果的大小保存到ulOutBufLen中//因为网络信息结构体链表的默认长度是不知道的if(ERROR_SUCCESS!=GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)){free(pAdapterInfo);pAdapterInfo=(IP_ADAPTER_INFO*)malloc(ulOutBufLen);}//第2次调用GetAdaptersInfo(),获取本地网络信息保存到结构体pAdapterInfo中if(ERROR_SUCCESS!=(dwRetVal=GetAdaptersInfo(pAdapterInfo,&ulOutBufLen))){printf(GetAdaptersInfoError!&d\n,dwRetVal);exit(1);}//显示本地网络适配器信息,从pAdapterInfo获取并显示本地网络信息pAdapter=pAdapterInfo;while(pAdapter){//printf(IP地址:\t\t%s\n,pAdapter-IpAddressList.IpAddress.String);//printf(子网掩码:\t\t%s\n,pAdapter-IpAddressList.IpMask.String);//处理下一个网络适配器IpAddressList=pAdapter-IpAddressList;pAdapter=pAdapter-Next;}//释放资源if(pAdapterInfo){free(pAdapterInfo);}printf(\n\n);//system(pause);return0;}intip_to_mac(stringip){FILE*out;out=fopen(mac.txt,a+);fprintf(out,%s的mac地址:,ip.c_str());//ip写入文件HRESULThr;ULONGpulMac[2];ULONGulLen;charstrMacAddr[100]={0};memset(pulMac,0xff,sizeof(pulMac));ulLen=6;ULONGip_new=inet_addr(ip.c_str());hr=SendARP(ip_new,0,pulMac,&ulLen);if(hr!=NO_ERROR){printf(NothingFind!\n\n\n);return1;}unsignedchar*mac_addr=(unsignedchar*)pulMac;sprintf(strMacAddr,%s的mac地址为:%.2x-%.2x-%.2x-%.2x-%.2x-%.2x\n\n\n,ip.c_str(),mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5]);printf(strMacAddr);inti;fprintf(out,%.2x,mac_addr[0]);for(i=1;i6;i++){fprintf(out,-%.2x,mac_addr[i]);}fprintf(out,\n);fclose(out);return0;}intcount(ULONGIpMask){inty=0;while(IpMask){if(IpMask&1)y++;IpMask=1;}returny;}/*structsockaddr_in{shortsin_family;u_shortsin_port;structin_addrsin_addr;charsin_zero[8];};structin_addr{in_addr_ts_addr;};*/typedefstruct_ICMP_HEADER{BYTEnType;BYTEnCode;USHORTnCheckSum;USHORTnId;USHORTnSequence;UINTnTimeStamp;}ICMP_HEADER,*PICMP_HEADER;//PingSock.cpp:定义控制台应用程序的入口点。//定义默认缓冲区长度#defineDEF_BUF_SIZE1024#defineIP_HEADER_SIZE20#defineICMP_HEADER_SIZE(sizeof(ICMP_HEADER))#defineICMP_DATA_SIZE32#defineICMP_PACK_SIZE(ICMP_HEADER_SIZE+ICMP_DATA_SIZE)charszInfo[DEF_BUF_SIZE]={0};USHORTGetCheckSum(LPBYTElpBuf,DWORDdwSize){DWORDdwCheckSum=0;USHORT*lpWord=(USHORT*)lpBuf;while(dwSize1){dwCheckSum+=*lpWord++;dwSize-=2;}if(1==dwSize)dwCheckSum+=*((USHORT*)lpBuf);dwCheckSum=(dwCheckSum16)+(dwCheckSum&0xffff);return(USHORT)(~dwCheckSum);}intPing(stringIp){SOCKADDR_INDestAddr;DestAddr.sin_family=AF_INET;DestAddr.sin_addr.S_un.S_addr=inet_addr(Ip.c_str());DestAddr.sin_port=htons(0);//创建ICMP请求包charICMPPack[ICMP_PACK_SIZE]={0};PICMP_HEADERpICMPHeader=(PICMP_HEADER)ICMPPack;pICMPHeader-nType=8;pICMPHeader-nCode=0;pICMPHeader-nId=(USHORT)::GetCurrentProcessId();pICMPHeader-nCheckSum=0;pICMPHeader-nTimeStamp=0;memset(&(ICMPPack[ICMP_HEADER_SIZE]),'E',ICMP_DATA_SIZE);//初始化WinSockWORDwVersionRequested=MAKEWORD(2,2);WSADATAwsaData;if(WSAStartup(wVersionRequested,&wsaData)!=0){return1;}//创建初始套接字SOCKETRawSock=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);if(INVALID_SOCKET==RawSock){printf(createrawsocketerror\n);return1;}intnTime=1000;intnRet=::setsockopt(RawSock,SOL_SOCKET,SO_RCVTIMEO,(char*)(&nTime),sizeof(nTime));charszRecvBuf[DEF_BUF_SIZE];SOCKADDR_INSourSockAddr;intt=0;for(inti=0;i4;i++){pICMPHeader-nCheckSum=0;pICMPHeader-nTimeStamp=::GetTickCount();pICMPHeader-nSequence=i;pICMPHeader-nCheckSum=GetCheckSum((LPBYTE)ICMPPack,ICMP_PACK_SIZE);intnRet=::sendto(RawSock,ICMPPack,ICMP_PACK_SIZE,0,(SOCKADDR*)&DestAddr,sizeof(DestAddr));if(nRet==SOCKET_ERROR){printf(sendtoerror!\n);return1;}//接收ICMP响应intnLen=sizeof(SourSockAddr);nRet=::recvfrom(RawSock,szRecvBuf,DEF_BUF_SIZE,0,(SOCKADDR*)&SourSockAddr,&nLen);if(nRet==SOCKET_ERROR){if(::WSAGetLastError()==WSAETIMEDOUT){printf(RequestTimeout\n);t++;continue;}else{printf(recvfromerror!\n);return1;}}intnTime=::GetTickCount()-pICMPHeader-nTimeStamp;intnRealSize=nRet-IP_HEADER_SIZE-ICMP_HEADER_SIZE;if(nRealSize0){printf(Tolessrecv