-- 作者:binaryluo
-- 发布时间:12/30/2005 11:43:00 PM
-- [转帖]sniffer技术原理及应用,包括编程方法和工具使用(二)
winpcap也是用的NDIS,将自己注册为一个协议处理驱动。(在原代码的driverentry里面能看到)又:上面这个drvipflt这个代码的过滤部分不知道大家是不是看起来很熟悉,是的,是抄的那个numege的驱动开发包里面的一个包过滤程序里的,看来老外也是喜欢到处抄的。 ruike: 读研的时候专门搞过nids,因此对winpcap可以说是情有独钟,这个东东确实好用,但也确实很烦人,它有一个致命的缺陷就是只适用于共享式以太网络,对于交换式网络下的数据则无能为力,我专门做过测试,在使用交换机连接的局域网下,只能监听到本网段内的数据,而对于来自其他网段的数据则无法监听,除非你把probe接到交换机之前或者接到交换机的console口上,不过那样的弊端是显而易见的。 所以,winpcap的应用还是很有局限性的! kingzai: 实现交换网络的嗅探也有不少方法的 1.将你的抓包程序放在网关或代理服务器上,这样抓到整个局域网的包。 2.对交换机实行端口映射,将该端口的数据包全部映射到某个监控机器上。 3.在交换机和路由器之间连接一个HUB,这样数据将以广播的方式发送。 4.实行ARP欺骗,即在你的机器上实现整个包的转发,不过会降低整个局域网的效率。 warton: 嗅探对策: 光说嗅探了,我说说反嗅探吧:) 1.检查网内的主机上是否将网卡设置为混合模式(有很多工具可以做到,AntiSniff,Promiscan,Sentinel等) 2.对EtterCap这样的交换网络嗅探器(进行ARP欺骗),可以采用防止ARP欺骗的方法来对待 3.SSH加密通道 4.SSL 5.VPN 6.PGP等 目前这用利用网卡混合模式来进行sniffer的软件看来作用不太大了,所以应该多考虑交换网络的可行办法: MAC Flooding,MAC Duplicating,ARP欺骗等等 这些方法实现起来就不怎么容易了,欢迎有兴趣的朋友提供相关的资料,呵呵! netsys: 难道没人用过RAW SOCKET 吗? 虽然WINPCP功能很大,但RAW SOCKET可以让你直接了SOCKET的原生机制。 实际上我提的那两个问题是很容易解决的。。 netsys2: 对于一些混合模式的SNIFFER,大多采用发送特殊ARP包的方式,正确的网卡不会响应,而处于混合模式的网卡则会响应。当然,ARP与IP处于同层,因此你不能用RAW SOCKET完成,你需WinPcap支持工作。 下面是部分代码 AnsiString msgStatus; extern TArpFuncParam wParams; int BuildARPPacket(PArpPacket ArpPacket, unsigned char *dst_etheraddr, unsigned char *src_etheraddr, int ar_op, unsigned char *ar_sha, unsigned char *ar_sip, unsigned char *ar_tha, unsigned char *ar_tip,unsigned short int ar_hw) { memcpy(&(ArpPacket->eth_dst_addr), dst_etheraddr, ETH_ADD_LEN); memcpy(&(ArpPacket->eth_src_addr), src_etheraddr, ETH_ADD_LEN); ArpPacket->eth_type = htons(ETH_TYPE_ARP); ArpPacket->ar_hrd = htons(ar_hw); ArpPacket->ar_pro = htons(ARP_PRO_IP); ArpPacket->ar_hln = ARP_ETH_ADD_SPACE; ArpPacket->ar_pln = ARP_IP_ADD_SPACE; ArpPacket->ar_op = htons(ar_op); memcpy(&(ArpPacket->ar_sha), ar_sha, ARP_ETH_ADD_SPACE); memcpy(&(ArpPacket->ar_spa), ar_sip, ARP_IP_ADD_SPACE); memcpy(&(ArpPacket->ar_tha), ar_tha, ARP_ETH_ADD_SPACE); memcpy(&(ArpPacket->ar_tpa), ar_tip, ARP_IP_ADD_SPACE); memset(ArpPacket->eth_pad, 32, ETH_PADDING_ARP); return(EXIT_SUCCESS); } int OpenAdapter(LPADAPTER *lpAdapter) { *lpAdapter = PacketOpenAdapter(wParams.AdapterList[wParams.SelectedAdapter]); if(!(*lpAdapter) || ((*lpAdapter)->hFile == INVALID_HANDLE_VALUE)) { msgStatus = "Error : unable to open the driver."; SHOWSTAT(msgStatus); return(EXIT_FAILURE); } return(EXIT_SUCCESS); } void CloseAdapter(LPADAPTER lpAdapter) { PacketCloseAdapter(lpAdapter); } void GetLocalMAC(LPADAPTER lpAdapter, unsigned char *ether_addr) { ULONG IoCtlBufferLength = (sizeof(PACKET_OID_DATA) + sizeof(ULONG) - 1); PPACKET_OID_DATA OidData; OidData = (struct _PACKET_OID_DATA *)malloc(IoCtlBufferLength); OidData->Oid = OID_802_3_CURRENT_ADDRESS; OidData->Length = 6; if(PacketRequest(lpAdapter, FALSE, OidData) == FALSE) memcpy(ether_addr, 0, 6); else memcpy(ether_addr, OidData->Data, 6); free(OidData); } int GetARPReply(LPPACKET lpPacket, unsigned char *iptarget, unsigned char *result) { unsigned short int ether_type; unsigned char ipsender[4]; unsigned int off=0; unsigned int tlen; struct bpf_hdr *hdr; char *pChar; char *buf; buf = (char *)lpPacket->Buffer; hdr = (struct bpf_hdr *)(buf + off); tlen = hdr->bh_caplen; off += hdr->bh_hdrlen; pChar = (char*)(buf + off); off = Packet_WORDALIGN(off + tlen); memcpy(ðer_type, pChar + 12, 2); ether_type = ntohs(ether_type); if(ether_type == ETH_TYPE_ARP) { memcpy(ipsender, pChar + 28, 4); if((iptarget[0] == ipsender[0])&&(iptarget[1] == ipsender[1])&& (iptarget[2] == ipsender[2])&&(iptarget[3] == ipsender[3])) memcpy(result, pChar + 22, 6); else return(EXIT_FAILURE); } else return(EXIT_FAILURE); return(EXIT_SUCCESS); } int CheckPROMode(LPADAPTER lpAdapter, unsigned char *iptarget, unsigned char *remotemac) { LPPACKET lpPacketRequest; LPPACKET lpPacketReply; char buffer[256000]; TArpPacket ArpPacket; unsigned char magicpack[ETH_ADD_LEN]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFE}; unsigned char mactarget[ARP_ETH_ADD_SPACE]; DWORD timestamp = 0; int numPacks = 0; /* Init fields */ memset(mactarget, 0, 6); /* Allocate PACKET structure for ARP Request packet */ if((lpPacketRequest = PacketAllocatePacket()) == NULL) { msgStatus = "Error : failed to allocate the LPPACKET structure.."; SHOWSTAT(msgStatus); return(EXIT_FAILURE); } /* Init packet structure */ memset(&ArpPacket, 0, sizeof(TArpPacket)); /* Build ARP Request packet */ BuildARPPacket(&ArpPacket, magicpack, wParams.srcMAC, ARP_OP_REQUEST, wParams.srcMAC, wParams.srcIPAdd, mactarget, iptarget,wParams.ar_hw); /* Init ARP Request packet */ PacketInitPacket(lpPacketRequest, &ArpPacket, sizeof(ArpPacket)); /* Set number of ARP Request packets to send */ if(PacketSetNumWrites(lpAdapter, 1) == FALSE) { msgStatus = "Warning : unable to send more than one packet in a single write.."; SHOWSTAT(msgStatus); } /* Set hardware filter to directed mode */ if(PacketSetHwFilter(lpAdapter, NDIS_PACKET_TYPE_DIRECTED) == FALSE) { msgStatus ="Warning: unable to set directed mode.."; SHOWSTAT(msgStatus); } /* Set a 512K buffer in the driver */ if(PacketSetBuff(lpAdapter, 512000) == FALSE) { msgStatus = "Error: unable to set the kernel buffer.."; SHOWSTAT(msgStatus); PacketFreePacket(lpPacketRequest); return(EXIT_FAILURE); } /* Set a 1 second read timeout */ if(PacketSetReadTimeout(lpAdapter, -1) == FALSE) { msgStatus = "Warning: unable to set the read tiemout.."; SHOWSTAT(msgStatus); } /* Allocate PACKET structure for ARP Reply packet */ if((lpPacketReply = PacketAllocatePacket()) == NULL) { msgStatus = "Error: failed to allocate the LPPACKET structure.."; SHOWSTAT(msgStatus); PacketFreePacket(lpPacketRequest); return(EXIT_FAILURE); } /* Init ARP Reply packet */ PacketInitPacket(lpPacketReply, (char*)buffer, 256000); /* Allocate memory for remote MAC address */ timestamp = GetTickCount(); /* Main capture loop */ for(;;) { if(numPacks < wParams.numPacks) { /* Send packet */ if(PacketSendPacket(lpAdapter, lpPacketRequest, TRUE) == FALSE) { msgStatus ="Error : unable to send the packets.."; SHOWSTAT(msgStatus); PacketFreePacket(lpPacketRequest); PacketFreePacket(lpPacketReply); return(EXIT_FAILURE); } /* Free packet */ PacketFreePacket(lpPacketRequest); numPacks += 1; } /* Capture the packets */ if(PacketReceivePacket(lpAdapter, lpPacketReply, TRUE) == FALSE) { msgStatus = "Error: PacketReceivePacket failed.."; SHOWSTAT(msgStatus); PacketFreePacket(lpPacketReply); return(EXIT_FAILURE); } if(lpPacketReply->ulBytesReceived > 0) if(GetARPReply(lpPacketReply, iptarget, remotemac) == EXIT_SUCCESS) break; if((GetTickCount() - timestamp) > wParams.delay) { PacketFreePacket(lpPacketReply); return(EXIT_FAILURE); } } /* Free packet */ PacketFreePacket(lpPacketReply); return(EXIT_SUCCESS); }
|