网络安全监管实验.docx

上传人:b****5 文档编号:4062090 上传时间:2022-11-27 格式:DOCX 页数:12 大小:19.33KB
下载 相关 举报
网络安全监管实验.docx_第1页
第1页 / 共12页
网络安全监管实验.docx_第2页
第2页 / 共12页
网络安全监管实验.docx_第3页
第3页 / 共12页
网络安全监管实验.docx_第4页
第4页 / 共12页
网络安全监管实验.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

网络安全监管实验.docx

《网络安全监管实验.docx》由会员分享,可在线阅读,更多相关《网络安全监管实验.docx(12页珍藏版)》请在冰豆网上搜索。

网络安全监管实验.docx

网络安全监管实验

使用WinPcap进行网络数据包编程

一、实验目的

通过实验创建一个使用wpcap.dll的应用程序,学习如何使用WinPcap进行编程。

二、实验环境

操作系统环境:

Windows2000/NT/XP;

编程环境:

Visualc++;Turboc

三、实验任务

任务性质:

设计性实验

任务描述:

首先要完全理解winpcap编程的实现过程,然后用C或C++编写程序。

实验步骤:

1.首先运行winpcap的安装程序WinPcap_3_1.exe,然后把winpcap开发包WpdPack_3_1.zip解压缩到某个目录下,这里假设把winpcap开发包解压缩到C:

\wpdpack文件夹下。

在C:

\wpdpack文件夹下有子文件夹”include”和”lib”,分别把它们添加到VC的头文件路径和静态链接库路径下面。

具体方法如下:

打开VC,选择Tools菜单的子菜单options,打开对话框Options,选择Directory页面,然后在”Includefiles”中添加路径”C:

\wpdpack\include”,在”Libraryfiles”中添加路径”C:

\wpdpack\lib”。

某些应用程序可能使用到PlatformSDK,此时应该安装psdk,然后将其include和lib目录添加到VC的头文件路径和静态链接库路径下面。

在具体开发一个基于winpcap的工程时,还需要把winpcap的静态链接库wpcap.lib添加进来。

选择菜单Project的子菜单Settings,打开对话框ProjectSettings,选择页面Link,在项目Object/librarymodules中添加wpcap.lib。

由于在Windows中要使用其他网络函数操作,如ntohs(),必须使用套接字函数,所以还应该添加静态链接库wsock32.lib。

有些控制台程序需要运行参数,可以在VC中这样设置:

打开VC菜单Project的子菜单Setting中的对话框ProjectSettings,然后在页面Debug的选项Programarguments中填入参数。

2.创建工程

打开VC,选择菜单FileNew,在弹出的对话框中选择Projects页,再选择Win32ConsoleApplication,通过Location下面的编辑框旁边的按钮选择存放工程的路径,在Projectname:

下的编辑框中输入工程名Proj_1,单击OK按钮。

在弹出的对话框中选择Anemptyproject,单击Finish按钮。

在接下来弹出的对话框中单击OK按钮。

选择菜单FileNew,在弹出的对话框中选择Files页,再选择C++SourceFile,将Addtoproject左边的选择框选中,确定Addtoproject下面的编辑框中出现的是Proj_1。

在File下的编辑框中输入文件名ex1,单击OK按钮。

单击SaveAll工具条,再单击Build(F7)工具条,输出窗口显示以下信息。

单击工具条上红色的!

号运行程序。

3.PacketDump

这个程序会依据命令行参数,从网络适配器,或是从文件来读取数据包。

如果没有提供源,那么程序会显示出所有可用的适配器,你可以选其中一个。

当捕获过程开始,程序会打印数据包的时间戳,长度,原始内容。

一旦被编译了,那么它将能运行于所有的Win32平台,当然,它也可以被编译成Unix平台的程序。

 

--------------------------------------------------------------------------------

 

/*

*Copyright(c)1999-2005NetGroup,PolitecnicodiTorino(Italy)

*Copyright(c)2005-2006CACETechnologies,Davis(California)

*Allrightsreserved.

*

*Redistributionanduseinsourceandbinaryforms,withorwithout

*modification,arepermittedprovidedthatthefollowingconditions

*aremet:

*

*1.Redistributionsofsourcecodemustretaintheabovecopyright

*notice,thislistofconditionsandthefollowingdisclaimer.

*2.Redistributionsinbinaryformmustreproducetheabovecopyright

*notice,thislistofconditionsandthefollowingdisclaimerinthe

*documentationand/orothermaterialsprovidedwiththedistribution.

*3.NeitherthenameofthePolitecnicodiTorino,CACETechnologies

*northenamesofitscontributorsmaybeusedtoendorseorpromote

*productsderivedfromthissoftwarewithoutspecificpriorwritten

*permission.

*

*THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS

*"ASIS"ANDANYEXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOT

*LIMITEDTO,THEIMPLIEDWARRANTIESOFMERCHANTABILITYANDFITNESSFOR

*APARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALLTHECOPYRIGHT

*OWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,

*SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOT

*LIMITEDTO,PROCUREMENTOFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,

*DATA,ORPROFITS;ORBUSINESSINTERRUPTION)HOWEVERCAUSEDANDONANY

*THEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,ORTORT

*(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSE

*OFTHISSOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.

*

*/

 

#include

#include

//

//NOTE:

remembertoincludeWPCAPandHAVE_REMOTEamongyour

//preprocessordefinitions.

//

#include

#defineLINE_LEN16

main(intargc,char**argv)

{

pcap_if_t*alldevs,*d;

pcap_t*fp;

u_intinum,i=0;

charerrbuf[PCAP_ERRBUF_SIZE];

intres;

structpcap_pkthdr*header;

constu_char*pkt_data;

printf("pktdump_ex:

printsthepacketsofthenetworkusingWinPcap.n");

printf("Usage:

pktdump_ex[-ssource]nn"

"Examples:

n"

"pktdump_ex-sfile:

//c:

/temp/file.acpn"

"pktdump_ex-srpcap:

//\Device\NPF_{C8736017-F3C3-4373-94AC-9A34B7DAD998}nn");

if(argc<3)

{

printf("nNoadapterselected:

printingthedevicelist:

n");

/*Theuserdidn'tprovideapacketsource:

Retrievethelocaldevicelist*/

if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)==-1)

{

fprintf(stderr,"Errorinpcap_findalldevs_ex:

%sn",errbuf);

return-1;

}

/*Printthelist*/

for(d=alldevs;d;d=d->next)

{

printf("%d.%sn",++i,d->name);

if(d->description)

printf("(%s)n",d->description);

else

printf("(Nodescriptionavailable)n");

}

if(i==0)

{

fprintf(stderr,"Nointerfacesfound!

Exiting.n");

return-1;

}

printf("Entertheinterfacenumber(1-%d):

",i);

scanf("%d",&inum);

if(inum<1||inum>i)

{

printf("nInterfacenumberoutofrange.n");

/*Freethedevicelist*/

pcap_freealldevs(alldevs);

return-1;

}

/*Jumptotheselectedadapter*/

for(d=alldevs,i=0;inext,i++);

/*Openthedevice*/

if((fp=pcap_open(d->name,

100/*snaplen*/,

PCAP_OPENFLAG_PROMISCUOUS/*flags*/,

20/*readtimeout*/,

NULL/*remoteauthentication*/,

errbuf)

)==NULL)

{

fprintf(stderr,"nErroropeningadaptern");

return-1;

}

}

else

{

//Donotcheckfortheswitchtype('-s')

if((fp=pcap_open(argv[2],

100/*snaplen*/,

PCAP_OPENFLAG_PROMISCUOUS/*flags*/,

20/*readtimeout*/,

NULL/*remoteauthentication*/,

errbuf)

)==NULL)

{

fprintf(stderr,"nErroropeningsource:

%sn",errbuf);

return-1;

}

}

/*Readthepackets*/

while((res=pcap_next_ex(fp,&header,&pkt_data))>=0)

{

if(res==0)

/*Timeoutelapsed*/

continue;

/*printpkttimestampandpktlen*/

printf("%ld:

%ld(%ld)n",header->ts.tv_sec,header->ts.tv_usec,header->len);

/*Printthepacket*/

for(i=1;(icaplen+1);i++)

{

printf("%.2x",pkt_data[i-1]);

if((i%LINE_LEN)==0)printf("n");

}

printf("nn");

}

if(res==-1)

{

fprintf(stderr,"Errorreadingthepackets:

%sn",pcap_geterr(fp));

return-1;

}

return0;

}

 

数据包过滤器

这是一个更加完整的使用libpcap的范例程序,它显示了如何创建和设置过滤器,如何把捕获保存到磁盘。

这个程序在Win32和Unix平台下都能编译。

Pcap_filter(pf.exe)是一个通用的数据包过滤程序:

它的输入参数有数据包的源(可以是物理接口,或是一个文件),过滤器和一个输出文件。

它会从源获取数据包,并对它们进行过滤,如果它们符合过滤器的要求,就把它们保存到输出文件,直到按下Ctrl+C,或者整个文件处理完毕。

Pcap_filter不但可以根据一个特定的过滤器,来堆处理网络中的数据,而且可以从已经保存过的文件中提取数据包。

输入和输出文件的格式都是libpcap兼容的格式,比如,WinDump,tcpdump和其他许多网络工具。

/*

*Copyright(c)1999-2005NetGroup,PolitecnicodiTorino(Italy)

*Copyright(c)2005-2006CACETechnologies,Davis(California)

*Allrightsreserved.

*

*Redistributionanduseinsourceandbinaryforms,withorwithout

*modification,arepermittedprovidedthatthefollowingconditions

*aremet:

*

*1.Redistributionsofsourcecodemustretaintheabovecopyright

*notice,thislistofconditionsandthefollowingdisclaimer.

*2.Redistributionsinbinaryformmustreproducetheabovecopyright

*notice,thislistofconditionsandthefollowingdisclaimerinthe

*documentationand/orothermaterialsprovidedwiththedistribution.

*3.NeitherthenameofthePolitecnicodiTorino,CACETechnologies

*northenamesofitscontributorsmaybeusedtoendorseorpromote

*productsderivedfromthissoftwarewithoutspecificpriorwritten

*permission.

*

*THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS

*"ASIS"ANDANYEXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOT

*LIMITEDTO,THEIMPLIEDWARRANTIESOFMERCHANTABILITYANDFITNESSFOR

*APARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALLTHECOPYRIGHT

*OWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,

*SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOT

*LIMITEDTO,PROCUREMENTOFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,

*DATA,ORPROFITS;ORBUSINESSINTERRUPTION)HOWEVERCAUSEDANDONANY

*THEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,ORTORT

*(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSE

*OFTHISSOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.

*

*/

 

#include

#include

#include

#defineMAX_PRINT80

#defineMAX_LINE16

 

voidusage();

 

voidmain(intargc,char**argv)

{

pcap_t*fp;

charerrbuf[PCAP_ERRBUF_SIZE];

char*source=NULL;

char*ofilename=NULL;

char*filter=NULL;

inti;

pcap_dumper_t*dumpfile;

structbpf_programfcode;

bpf_u_int32NetMask;

intres;

structpcap_pkthdr*header;

constu_char*pkt_data;

if(argc==1)

{

usage();

return;

}

for(i=1;i

{

switch(argv[i][1])

{

case's':

{

source=argv[i+1];

};

break;

case'o':

{

ofilename=argv[i+1];

};

break;

case'f':

{

filter=argv[i+1];

};

break;

}

}

//openacapturefromthenetwork

if(source!

=NULL)

{

if((fp=pcap_open(source,

1514/*snaplen*/,

PCAP_OPENFLAG_PROMISCUOUS/*flags*/,

20/*readtimeout*/,

NULL/*remoteauthentication*/,

errbuf)

)==NULL)

{

fprintf(stderr,"nUnabletoopentheadapter.n");

return;

}

}

elseusage();

if(filter!

=NULL)

{

//Weshouldloopthroughtheadaptersreturnedbythepcap_findalldevs_ex()

//inordertolocatethecorrectone.

//

//Let'sdothingssimpler:

wesupposetobeinaCclassnetwork;-)

NetMask=0xffffff;

//compilethefilter

if(pcap_compile(fp,&fcode,filter,1,NetMask)<0)

{

fprintf(stderr,"nErrorcompilingfilter:

wrongsyntax.n");

return;

}

//setthefilter

if(pcap_setfilter(fp,&fcode)<0)

{

fprintf(stderr,"nErrorsettingthefiltern");

return;

}

}

//openthedumpfile

if(ofilename!

=NULL)

{

dumpfile=pcap_dump_open(fp,ofilename);

if(dumpfile==NULL)

{

fprintf(stderr,"nErroropeningoutputfilen");

return;

}

}

elseusage();

//startthecapture

while((res=pcap_next_ex(fp,&header,&pkt_data))>=0)

{

if(res==0)

/*Timeoutelapsed*/

continue;

//savethepacketont

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1