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

这个程序可以称为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. requirejs:性能优化-及早并行加载

    为了提高页面的性能,通常情况下,我们希望资源尽可能地早地并行加载.这里有两个要点,首先是尽早,其次是并行. 通过data-main方式加载要尽可能地避免,因为它让requirejs.业务代码不必要地串 ...

  2. js的原型链和constructor

    转载:http://www.108js.com/article/article1/10201.html?id=1092 请先瞻仰上边的这篇文章. 对象的原型链: box.__proto__.__pro ...

  3. Linq连接查询之左连接、右连接、内连接、全连接、交叉连接、Union合并、Concat连接、Intersect相交、Except与非查询

    内连接查询 内连接与SqL中inner join一样,即找出两个序列的交集 Model1Container model = new Model1Container(); //内连接 var query ...

  4. C程序中对时间的处理——time库函数详解

    包含文件:<sys/time.h> <time.h> 一.在C语言中有time_t, tm, timeval等几种类型的时间 1.time_t time_t实际上是长整数类型, ...

  5. 曼慧尼特u检验(两个样本数据间有无差异)

    曼-惠特尼U检验(Mann-Whitney检验) How the Mann-Whitney test works Mann-Whitney检验又叫做秩和检验,是比较没有配对的两个独立样本的非参数检验. ...

  6. 很简单的多线程访问python嘿嘿嘿

    import urllib import socket from threading import * url = "http://www.baidu.com/s?ie=UTF-8& ...

  7. oracle数据匹配merge into

    来源于:http://blog.csdn.net/vtopqx/article/details/50633865 前言: 很久之前,估计在2010年左右在使用Oralce,当时有个需求就是需要对两个表 ...

  8. Ubuntu 14.04 安装最新稳定版Nginx 1.6.0

    如果已经安装,请先卸载sudo apt-get remove nginx最新的稳定版Nginx 1.6.0在ubuntuupdates ppa库中提供,网址http://www.ubuntuupdat ...

  9. [转]Servlet 3.0 新特性详解

    原文地址:http://blog.csdn.net/xiazdong/article/details/7208316 Servlet 3.0 新特性概览 1.Servlet.Filter.Listen ...

  10. Android数据格式解析对象JSON用法(转)

    地址:http://www.cnblogs.com/devinzhang/archive/2012/01/09/2317315.html 里面的重点: JSON解析案例     (1)解析Object ...