【IT168技术文档】
引言
目前,已经有不少的Sniff工具软件,如Windows环境下,最富盛名的工具是Netxray和Sniffer pro,用它们在 Windows环境下抓包来分析,非常方便。在UNIX环境下如Sniffit,Snoop,Tcpdump,Dsniff 等都是比较常见的。这里介绍一个用C语言和网络数据包和分析开发工具libpcap及winpcap实现的简易网络Sniffer。
网络嗅探器程序框图
首先给出流程如图1所示。
网络嗅探器程序实现
在c环境下编程,源码如下:
/* June 2nd,2002 * Project for graduation qualification By Bby Team 19 */ #include <stdio.h> #include <conio.h> //必须加路径,必须把头文件packet32.h包含进去 #include "..\..\Include\packet32.h" #include "..\..\Include\ntddndis.h" #define Max_Num_Adapter 10 // Prototypes原形 //发包 void PrintPackets(LPPACKET lpPacket); //设备列表 char AdapterList[Max_Num_Adapter][1024]; // 主程序开始 int main() { //define a pointer to an ADAPTER structure设备指针 LPADAPTER lpAdapter = 0; //define a pointer to a PACKET structure包指针 LPPACKET lpPacket; int i; DWORD dwErrorCode; DWORD dwVersion; DWORD dwWindowsMajorVersion; //Unicode strings (WinNT) WCHAR AdapterName[8192]; //网络适配器设备列表 WCHAR *temp,*temp1; //ASCII strings (Win9x) char AdapterNamea[8192]; //网络适配器设备列表 char *tempa,*temp1a; int AdapterNum=0,Open; ULONG AdapterLength; char buffer[256000]; // 容纳来自驱动器的数据的缓冲区 struct bpf_stat stat; // 获得本机网卡名 AdapterLength=4096; printf("Packet.dll test application. Library version:%s\n", PacketGetVersion()); printf("Adapters installed:\n"); i=0;