Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15183092
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类: 网络与安全

2008-05-31 15:51:12

本文介绍一个用C语言和网络数据包分析开发工具实现的简易网络Sniffer。目前,已经有不少的Sniff工具软件,如Windows环境下,最富盛名的工具是Netxray和Sniffer pro,用它们在 Windows环境下抓包来分析,非常方便。在UNIX环境下如Sniffit,Snoop,Tcpdump,Dsniff 等都是比较常见的。这里介绍一个用C语言和网络数据包和分析开发工具libpcap及winpcap实现的简易网络Sniffer。

网络嗅探器程序框图

首先给出流程如图1所示。



图1 流程图

 网络嗅探器程序实现

在c环境下编程,源码如下:

/* June 2nd,2002

* Project for graduation qualification By Bby Team 19 */

#include <stdio.h>

#include <conio.h>

//必须加路径,必须把头文件packet32.h包含进去

#include "....Includepacket32.h"

#include "....Includentddndis.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:%sn", PacketGetVersion());

 printf("Adapters installed:n");

 i=0;


下面这段代码是用来在不同版本下得到网络适配器名:

Win9x 和WinNT中的网卡名称是分别用ASCII和UNICODE实现的,

所以首先要得到本地操作系统

的版本号:

dwVersion=GetVersion();

dwWindowsMajorVersion= (DWORD)(LOBYTE(LOWORD(dwVersion)));


这里首先用到的Packet.dll函数是PacketGetAdapterNames(PTSTR pStr,

PULONG BufferSize,通常它是与驱动程序通信并被调用的第一个函数,

它将返回的用户本地系统中安装

的网络适配器的名字放在

缓冲区pStr中;BufferSize是缓冲区的长度:

if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4))

{

  //是Windows NT

  // 找不到设备列表

  if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){

printf("Unable to retrieve the list of the adapters!n");

return -1;

  }

  // 找到设备列表

  temp=AdapterName;

  temp1=AdapterName;

  while ((*temp!='')||(*(temp-1)!=''))

  {

if (*temp=='')

{

  memcpy(AdapterList[i],temp1,(temp-temp1)*2);

  temp1=temp+1;

  i++;

}

temp++;

  }

  // 显示适配器列表

  AdapterNum=i;

  for (i=0;i<AdapterNum;i++)

wprintf(L"n%d- %sn",i+1,AdapterList[i]);

printf("n");

  }

  else //否则就是windows 9x,获取适配器名的方法同WinNT下

  {

if(PacketGetAdapterNames(AdapterNamea,&AdapterLength)==FALSE){

  printf("Unable to retrieve the list of the adapters!n");

  return -1;

  }

  tempa=AdapterNamea;

  temp1a=AdapterNamea;

  while ((*tempa!='')||(*(tempa-1)!=''))

  {

if (*tempa=='')

{

  memcpy(AdapterList[i],temp1a,tempa-temp1a);

  temp1a=tempa+1;

  i++;

}

tempa++;

  }

  AdapterNum=i;

  for (i=0;i<AdapterNum;i++)

printf("n%d- %sn",i+1,AdapterList[i]);

printf("n");

}
阅读(327) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~