pcap文件格式是bpf保存原始数据包的格式,很多软件都在使用,比如tcpdump、wireshark等等,了解pcap格式可以加深对原始数据包的了解,自己也可以手工构造任意的数据包进行测试。

pcap文件的格式为:
  文件头    24字节
  数据包头 + 数据包  数据包头为16字节,后面紧跟数据包
  数据包头 + 数据包  ……
 
pcap.h里定义了文件头的格式

  1. struct pcap_file_header {
  2. bpf_u_int32 magic;
  3. u_short version_major;
  4. u_short version_minor;
  5. bpf_int32 thiszone;     /* gmt to local correction */
  6. bpf_u_int32 sigfigs;    /* accuracy of timestamps */
  7. bpf_u_int32 snaplen;    /* max length saved portion of each pkt */
  8. bpf_u_int32 linktype;   /* data link type (LINKTYPE_*) */
  9. };

看一下各字段的含义:
magic:   4字节 pcap文件标识 目前为“d4 c3 b2 a1”
major:   2字节 主版本号     #define PCAP_VERSION_MAJOR 2
minor:   2字节 次版本号     #define PCAP_VERSION_MINOR 4
thiszone:4字节 时区修正     并未使用,目前全为0
sigfigs: 4字节 精确时间戳   并未使用,目前全为0
snaplen: 4字节 抓包最大长度 如果要抓全,设为0x0000ffff(65535),
               tcpdump -s 0就是设置这个参数,缺省为68字节
linktype:4字节 链路类型    一般都是1:ethernet
常用链路类型:
      0           BSD loopback devices, except for later OpenBSD
       1           Ethernet, and Linux loopback devices
       6           802.5 Token Ring
       7           ARCnet
       8           SLIP
       9           PPP
       10          FDDI
       100         LLC/SNAP-encapsulated ATM
       101         "raw IP", with no link
       102         BSD/OS SLIP
       103         BSD/OS PPP
       104         Cisco HDLC
       105         802.11
       108         later OpenBSD loopback devices (with the AF_value in network byte order)
       113         special Linux "cooked" capture
       114         LocalTalk

=======================================================================================
|    magic    |major  | minor |   thiszone  |   sigfigs   |   snaplen   |  linktype  
| d4 c3 b2 a1 | 02 00 | 04 00 | 00 00 00 00 | 00 00 00 00 | ff ff 00 00 | 01 00 00 00
=======================================================================================
 
 
数据包头的格式

  1. struct pcap_pkthdr {
  2. struct timeval ts;      /* time stamp */
  3. bpf_u_int32 caplen;     /* length of portion present */
  4. bpf_u_int32 len;        /* length this packet (off wire) */
  5. };
  6. struct timeval {
  7. long            tv_sec;         /* seconds (XXX should be time_t) */
  8. suseconds_t     tv_usec;        /* and microseconds */
  9. };

ts:    8字节 抓包时间 4字节表示秒数,4字节表示微秒数
caplen:4字节 保存下来的包长度(最多是snaplen,比如68字节)
len:   4字节 数据包的真实长度,如果文件中保存的不是完整数据包,可能比caplen大

了解了pcap文件格式,就可以自己手工构造任意数据包了,可以以录好的包为基础,

构建pcap文件示例:

含有两套构建方法, 因起先不知道可以通过pcap_open_dead & pcap_dump_open 构建文件头, 所以开始是自己创建一个文件并写入文件头.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <stdint.h>
  6. #include <errno.h>
  7. #include <pcap.h>
  8. #include "common.h"
  9. #define TCPDUMP_MAGIC       0xa1b2c3d4
  10. #ifndef PCAP_VERSION_MAJOR
  11. #define PCAP_VERSION_MAJOR 2
  12. #define
  13. #define PCAP_VERSION_MINOR
  14. #define PCAP_VERSION_MINOR 4
  15. #endif
  16. #define LINKTYPE_NULL       DLT_NULL
  17. #define LINKTYPE_ETHERNET   DLT_EN10MB  /* also for 100Mb and up */
  18. #define LINKTYPE_EXP_ETHERNET   DLT_EN3MB   /* 3Mb experimental Ethernet */
  19. #define LINKTYPE_AX25       DLT_AX25
  20. #define LINKTYPE_PRONET     DLT_PRONET
  21. #define LINKTYPE_CHAOS      DLT_CHAOS
  22. #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */
  23. #define LINKTYPE_ARCNET     DLT_ARCNET  /* BSD-style headers */
  24. #define LINKTYPE_SLIP       DLT_SLIP
  25. #define LINKTYPE_PPP        DLT_PPP
  26. #define LINKTYPE_FDDI       DLT_FDDI
  27. static int
  28. pcap_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
  29. {
  30. struct pcap_file_header hdr;
  31. hdr.magic = TCPDUMP_MAGIC;
  32. hdr.version_major = PCAP_VERSION_MAJOR;
  33. hdr.version_minor = PCAP_VERSION_MINOR;
  34. hdr.thiszone = thiszone;
  35. hdr.snaplen = snaplen;
  36. hdr.sigfigs = 0;
  37. hdr.linktype = linktype;
  38. if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
  39. return (-1);
  40. return (0);
  41. }
  42. #define FILE_SAVE "pcap_write.pcap"
  43. uint8_t l2_data[] = {
  44. 0x00, 0x0c, 0x29, 0x99, 0xfc, 0xa6, 0x00, 0x0c, 0x29, 0xd7, 0xc1, 0xf2, 0x08, 0x00, 0x45, 0x00,
  45. 0x00, 0x46, 0x87, 0x8a, 0x00, 0x00, 0x40, 0x11, 0x6e, 0xa5, 0xc0, 0xa8, 0x01, 0x31, 0xc0, 0xa8,
  46. 0x01, 0xf6, 0x7e, 0x75, 0x00, 0x35, 0x00, 0x32, 0x89, 0x42, 0x0a, 0x5d, 0x00, 0x00, 0x00, 0x01,
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x6e, 0x73, 0x31, 0x05, 0x67, 0x75, 0x61, 0x72, 0x64,
  48. 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x29, 0x10, 0x00, 0x00, 0x00,
  49. 0x80, 0x00, 0x00, 0x00
  50. };
  51. int main(int argc, char **argv)
  52. {
  53. #if 0
  54. FILE *fp = NULL;
  55. struct pcap_pkthdr h;
  56. fp = fopen(FILE_SAVE, "wb");
  57. if (!fp){
  58. fprintf(stderr, "fopen %s for write failed. errno=%d desc=%s\n",
  59. FILE_SAVE, errno, strerror(errno));
  60. return 1;
  61. }
  62. pcap_write_header(fp, LINKTYPE_ETHERNET, 0x0, 0x0000ffff);
  63. gettimeofday(&h.ts, NULL);
  64. h.caplen = sizeof(l2_data);
  65. h.len    = sizeof(l2_data);
  66. pcap_dump((uint8_t *)fp, &h, l2_data);
  67. fflush(fp);
  68. fclose(fp);
  69. #else
  70. pcap_t *p = NULL;
  71. pcap_dumper_t *fp = NULL;
  72. struct pcap_pkthdr h;
  73. p = pcap_open_dead(LINKTYPE_ETHERNET, 0x0000ffff);
  74. if (NULL == p){
  75. fprintf(stderr, "pcap_open_dead failed.\n");
  76. return 1;
  77. }
  78. fp = pcap_dump_open(p, FILE_SAVE);
  79. if (NULL == fp){
  80. fprintf(stderr, "pcap_dump_open failed.\n");
  81. return 1;
  82. }
  83. gettimeofday(&h.ts, NULL);
  84. h.caplen = sizeof(l2_data);
  85. h.len    = sizeof(l2_data);
  86. pcap_dump((uint8_t *)fp, &h, l2_data);
  87. pcap_dump_close(fp);
  88. #endif
  89. return 0;
  90. }

编译 & 链接

# gcc pcap_write.c -o pcap_write -lpcap

自主创建tcpdump/wireshark pcap文件的更多相关文章

  1. 构建tcpdump/wireshark pcap文件

      pcap文件格式是bpf保存原始数据包的格式,很多软件都在使用,比如tcpdump.wireshark等等,了解pcap格式可以加深对原始数据包的了解,自己也可以手工构造任意的数据包进行测试. p ...

  2. 基于TcpDump和pcap文件分析的Android平台网络抓包程序设计与实现【随便】

    一.考虑使用Tcpdump,将抓到的包保存到cap文件中,然后手动分析.参考资料:1. http://www.cnblogs.com/tt-0411/archive/2012/09/23/269936 ...

  3. text2pcap: 将hex转储文本转换为Wireshark可打开的pcap文件

    简介 Text2pcap是一个读取ASCII hex转储的程序,它将描述的数据写入pcap或pcapng文件.text2pcap可以读取包含多个数据包的hexdumps,并构建多个数据包的捕获文件.t ...

  4. pcap文件生成metadata——使用tshark解析tcpdump的pcap包

    pcap文件生成metadata #!/usr/bin/env python # -*- coding: utf-8 -*- import os import time, datetime impor ...

  5. 使用tcpdump+Wireshark(或Fiddler)做linux服务器的网络请求分析

    我们的服务器上,一般都没有窗口界面,这时候要抓包,用tcpdump是最方便的.而分析网络请求时,wireshark又是相当方便的,这时候我们就需要把它们两个一起来使用了. tcpdump 抓取数据 命 ...

  6. JSunpack-n模拟WireShark拦截文件传输

    前言: 在前面的实验里我们进行了JSunpack-n的安装及其简单使用.JSunpack-n还有另外一些功能须要进行測试试验,由于本人也是刚接触这些东西.本文就当中一个"功能点"进 ...

  7. tcpdump wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容) 实例介绍

    tcpdump wireshark 实用过滤表达式(针对ip.协议.端口.长度和内容) 实例介绍 标签: 网络tcpdst工具windowslinux 2012-05-15 18:12 3777人阅读 ...

  8. python dpkt 解析 pcap 文件

    dpkt Tutorial #2: Parsing a PCAP File 原文链接:https://jon.oberheide.org/blog/2008/10/15/dpkt-tutorial-2 ...

  9. 无法在“EntityFramework”已存在的情况下创建影像复制该文件的解决方案

    问题产生的原因:你项目正在生成中你就打开浏览器预览了,导致这个问题解决方案:右击重新生成项目,等生成后再打开 “/”应用程序中的服务器错误. 无法在“EntityFramework”已存在的情况下创建 ...

随机推荐

  1. C语言程序设计做题笔记之C语言基础知识(上)

    C语言是一种功能强大.简洁的计算机语言,通过它可以编写程序,指挥计算机完成指定的任务.我们可以利用C语言创建程序(即一组指令),并让计算机依指令行事.并且C是相当灵活的,用于执行计算机程序能完成的几乎 ...

  2. Stanford CoreNLP--Named Entities Recognizer(NER)

    Standford Named Entities Recognizer(NER),命名实体识别是信息提取(Information Extraction)的一个子任务,它把文字的原子元素(Atomic ...

  3. 算法练习之:Doubles

    Doubles Time Limit: 1000MS Memory Limit: 65536KB Problem Description As part of an arithmetic compet ...

  4. [wikioi]数的划分

    http://wikioi.com/problem/1039/ 划分型DP.最终的思路是,F[i][j]表示i分成j份,如果分出来的有1,那么去掉1,就是F[i-1][j-1]:如果没有1,那就都减1 ...

  5. JavaScript 判断对象是否为空

    /** **判断是否null *@param data */ function isNull(data) {     return (data == "" || data == u ...

  6. 深入Android开发之--理解View#onTouchEvent

    一:前言 View是Android中最基本的UI单元. 当一个View接收到了触碰事件时,会调用其onTouchEvent方法.方法声明如下: ? 1 2 3 4 5 6 7 /**  * Imple ...

  7. nohup.out

    nohup.out 文件的产生 linux的nohup命令的用法 不输出nohup.out nohup node app.js > /dev/null 2>&1 &

  8. C# Read/Write another Process' Memory ZZ

    Today's tutorial is about...processes' memory! In this article I'll show you how to read/write a pro ...

  9. HDU-2952 Counting Sheep (DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  10. HDU-2561 第二小整数

    http://acm.hdu.edu.cn/showproblem.php?pid=2561 第二小整数 Time Limit: 3000/1000 MS (Java/Others)    Memor ...