本程序的主要目标是展示如何解析所捕获的数据包的协议首部。

这个程序可以称为UDPdump,打印一些网络上传输的UDP数据的信息。

   1:  #include"pcap.h"
   2:  typedef struct ip_address{
   3:      u_char byte1,byte2,byte3,byte4;
   4:  }ip_address;
   5:  typedef struct ip_header{
   6:      u_char ver_ihl;      // 版本 (4 bits) + 首部长度 (4 bits)
   7:      u_char tos;          // 服务类型(Type of service) 
   8:      u_short tlen;        // 总长(Total length) 
   9:      u_short idenfication;
  10:      u_short flags_fo;
  11:      u_char ttl;
  12:      u_char proto;
  13:      u_short crc;
  14:      ip_address saddr;
  15:      ip_address daddr;
  16:      u_int op_pad;
  17:  }ip_header;
  18:   
  19:  //UDP head
  20:  typedef struct udp_header{
  21:      u_short sport;       // 源端口(Source port)
  22:      u_short dport;       // 目的端口(Destination port)
  23:      u_short len;         // UDP数据包长度(Datagram length)
  24:      u_short crc;         // 校验和(Checksum)    
  25:  }udp_header;
  26:  //每次捕获到数据包时,libpcap都会自动调用这个回调函数
  27:  void packet_handler(u_char *param,const pcap_pkthdr*header,const u_char *ptk_data){
  28:   
  29:      char timestr[16];
  30:      //将时间戳转换成可识别的格式
  31:      time_t local_tv_sec=header->ts.tv_sec;   
  32:      tm *ltime=localtime(&local_tv_sec);
  33:      strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
  34:      printf("%s,%.6d len:%d\n",timestr,header->ts.tv_usec,header->len);
  35:   
  36:      ip_header *iphead=(ip_header*)(ptk_data+14);
  37:      u_int ip_len=(iphead->ver_ihl&0xf)*4;
  38:      udp_header *udphead=(udp_header*)((u_char*)iphead+ip_len);
  39:      u_short sport=ntohs(udphead->sport);
  40:      u_short dport=ntohs(udphead->dport);
  41:      printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
  42:          iphead->saddr.byte1,
  43:          iphead->saddr.byte2,
  44:          iphead->saddr.byte3,
  45:          iphead->saddr.byte4,
  46:          sport,
  47:          iphead->daddr.byte1,
  48:          iphead->daddr.byte2,
  49:          iphead->daddr.byte3,
  50:          iphead->daddr.byte4,
  51:          dport);
  52:   
  53:  }
  54:  int main(){
  55:      pcap_if_t *alldevs;
  56:      pcap_t *adhandle;
  57:      char errbuf[PCAP_ERRBUF_SIZE];
  58:      u_int netmask=0;
  59:      bpf_program fcode;
  60:      char packet_filter[]="ip and udp";
  61:   
  62:      if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)==-1){
  63:          fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
  64:          exit(1);
  65:      }
  66:   
  67:      //print device list
  68:      int count=1;
  69:      for(pcap_if_t *d=alldevs;d!=NULL;d=d->next){
  70:          printf("%d. %s",count++,d->name);
  71:          printf("(%s)\n",d->description);
  72:      }
  73:      if(count==1) {
  74:          printf("No interface found! Make sure WinPcap is isntalled\n");
  75:          return -1;
  76:      }
  77:      int num;
  78:      printf("Enter the interface number:(1-%d): ",count);
  79:      scanf("%d",&num);
  80:      if(num<1||num>count){
  81:          printf("Out Of Range\n");
  82:          pcap_freealldevs(alldevs);
  83:          return -1;
  84:      }
  85:      pcap_if_t *d=alldevs;
  86:      // 跳转到选中的适配器 
  87:      for(int i=0;i<num-1;i++,d=d->next);
  88:      // 设备名
  89:      // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
  90:      // 混杂模式
  91:      // 读取超时时间
  92:      // 远程机器验证
  93:      // 错误缓冲池
  94:      adhandle=pcap_open(d->name,65536,PCAP_OPENFLAG_PROMISCUOUS,1000,NULL,errbuf);
  95:      if(adhandle==NULL){
  96:          fprintf(stderr,"Unable to open the adapter %s",d->name);
  97:          return -1;
  98:      }
  99:      if (pcap_datalink(adhandle)!=DLT_EN10MB){
 100:          fprintf(stderr,"This program works only on Ethernet network.\n");
 101:          pcap_freealldevs(alldevs);
 102:          return -1;
 103:      }
 104:   
 105:   
 106:      if(d->addresses!=NULL)
 107:          netmask=((sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
 108:      else
 109:          netmask=0xffffff;
 110:      printf("debug: %d\n",netmask);
 111:      if(pcap_compile(adhandle,&fcode,packet_filter,1,netmask)<0){
 112:          fprintf(stderr,"Error setting thr filter\n");
 113:          pcap_freealldevs(alldevs);
 114:          return -1;
 115:      }
 116:      printf("Listening on %s...\n",d->description);
 117:   
 118:      pcap_freealldevs(alldevs);
 119:      //开始捕获 
 120:      pcap_loop(adhandle,0,packet_handler,NULL);
 121:   
 122:   
 123:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

试验结果:

ARPSpoofing教程(四) - 捕获并分析数据包的更多相关文章

  1. Wireshark数据抓包教程之认识捕获分析数据包

    Wireshark数据抓包教程之认识捕获分析数据包 认识Wireshark捕获数据包 当我们对Wireshark主窗口各部分作用了解了,学会捕获数据了,接下来就该去认识这些捕获的数据包了.Wiresh ...

  2. 使用winpcap多线程抓包,以及简单的分析数据包

    刚开始使用winpcap数据包的时候,我在抓包的时候使用了 pcap_loop(adhandle, 0, packet_handler, NULL); 这个回调函数进行抓包.同时在回调函数中分析IP地 ...

  3. Wireshark 如何捕获网络流量数据包

    转自:http://www.4hou.com/web/7465.html?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutia ...

  4. #WEB安全基础 : HTTP协议 | 0x7 学会使用wireshark分析数据包

    wireshark是开源,免费,跨平台的抓包分析工具 我们可以通过wireshark学习HTTP报文和进行抓包分析,在CTF中的流量分析需要用到抓包 1.下载和安装 这是wireshark的官网 ht ...

  5. 使用wireshark在windows平台下捕获HTTP协议数据包中的帐号密码信息

    1.打开wireshark软件,从Interface List中选择相应的网卡,例如我的PC机上是“本地连接”,然后选择”Start”启动抓包程序. 2.打开学校主页,输入账号和密码登录校内邮箱. 3 ...

  6. wireshark1.8捕获无线网卡的数据包——找不到无线网卡!

    问题说明:奇怪的是,我线网卡明明有的,是interl的型号,可是wireshark总是找不到,如下: 奇了怪了,没有!原来是如下的: 实际上这块无线网卡是存在的,只不过由于兼容或驱动的原因无法显示型号 ...

  7. winPcap编程之打开适配器并捕获数据包(四 转)

    在贴源码之前先介绍一个将要用到的很重要的函数--pcap_open(),下面是pcap_open()在remote-ex.h中的声明: pcap_t *pcap_open(const char *so ...

  8. wireshark数据包分析实战 第三、四章

    1,wireshark支持的协议上千种,开源的. 2,wireshark需要winpcap驱动支持.winpcap驱动的作用通过操作系统捕捉原始数据包.应用过滤器.将网卡切换为混杂模式. 3,捕获文件 ...

  9. 详解DHCP工作方法,并用wireshark对DHCP四个数据包抓包分析

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

随机推荐

  1. 代码重构之 —— 一堆if、esle 逻辑的处理

    这几天,接手一个同事的代码,关于微信接口开发的,那一堆的 if,看得哥蛋痛了,这个毛病也是很多新手容易犯的,所以特地把这次重构写出来. 下面来我们看看这个代码的问题所在,if else 里面的代码块逻 ...

  2. Orchard创建全局应用

    Orchard的本地化管理托管于一个外部服务(Crowdin),这个项目是公开的且欢迎大家做贡献. Orchard支持两种类型的本地: Orchard应用程序以及已安装模块中的文本字符串的本地化(其实 ...

  3. suggest插件实现下拉选择筛选

    实现的效果展示: 代码如下: 1.需要引入jquery,bootstrap-suggest.js,bootstrap.min.css 2.html页面代码: <!DOCTYPE html> ...

  4. NPOI2.0学习(一)

    引用空间 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; 创建工作簿(workbook)和sheet HSSFWorkbook wk = new ...

  5. CSS3——让最后一行显示省略号

    代码如下: <!DOCTYPE html> <html> <head> <title></title> <meta charset=& ...

  6. memcache 安装

    1 下载两个文件 wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz wget http://www.monkey.org/ ...

  7. Change Eclipse Tooltip's Color in Ubuntu

    这个问题十分高级,随着Ubuntu版本的变迁这个问题的解决方案也在不断变化 最开始,SystemSettings里面可以设置工具条背景色,后来这个选项在新版本Ubuntu中消失了 我用过Ubuntu1 ...

  8. A+B

    Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to e ...

  9. WPS显示无法创建对象,请确认对象已在系统注册表中注册

    第一种方法:在系统的开始--所有程序找到WPS--WPS office工具--配置工具--高级--兼容设置,在这里勾选兼容第三方系统和软件. 第二种方法: xp/win7系统:拷贝packager.e ...

  10. 在Ubuntu 14.04中安装最新版Eclipse

    1.下载eclipse从官网http://www.eclipse.org/downloads/下载Eclipse IDE for Java EE Developers的Linux版本eclipse-S ...