2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

mergecapコマンドの使い方

Last updated at Posted at 2017-09-04

最新版は以下に記載しました。
https://hana-shin.hatenablog.com/entry/2023/04/02/212417

#1 mergcapコマンドとは?
pcapファイルを結合するためのコマンドです。

#2 環境
VMware Workstation 12 Player上のゲストマシンを使っています。

[root@admin ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

[root@admin ~]# uname -r
3.10.0-514.el7.x86_64

#3 インストール

[root@server ~]# yum -y install wireshark
[root@server ~]# mergecap -h
Mergecap 1.10.14 (Git Rev Unknown from unknown)
-以下、略-

#4 オプション一覧

[root@server ~]# mergecap -h
Mergecap 1.10.14 (Git Rev Unknown from unknown)
Merge two or more capture files into one.
See http://www.wireshark.org for more information.

Usage: mergecap [options] -w <outfile>|- <infile> [<infile> ...]

Output:
  -a                concatenate rather than merge files.
                    default is to merge based on frame timestamps.
  -s <snaplen>      truncate packets to <snaplen> bytes of data.
  -w <outfile>|-    set the output filename to <outfile> or '-' for stdout.
  -F <capture type> set the output file type; default is pcapng.
                    an empty "-F" option will list the file types.
  -T <encap type>   set the output file encapsulation type;
                    default is the same as the first input file.
                    an empty "-T" option will list the encapsulation types.

Miscellaneous:
  -h                display this help and exit.
  -v                verbose output.

#5 事前準備
##5.1 テスト用pcapファイルの作成
テスト用のpcapファイルを3つ作成します。
それぞれのファイルは、ICMPパケット、DNSパケット、ARPパケットだけを含みます。

ICMPパケット採取の準備をする。
[root@server ~]# tcpdump -i eth0 icmp -w 1.cap

別ターミナルを開く。デフォルトGWに対してpingを実行して、ICMPパケットを採取する。
[root@server ~]# ping -c 1 192.168.0.1

DNSパケット採取の準備をする。
[root@server ~]# tcpdump -i eth0 udp port 53 -w 2.cap

別ターミナルを開く。digコマンドを実行して、DNSパケットを採取する。
[root@server ~]# dig ntp.nict.jp +short
133.243.238.244
133.243.238.164
133.243.238.243
133.243.238.163

ARPパケット採取の準備をする。
[root@server ~]# tcpdump -i eth0 arp -w 3.cap

別ターミナルを開く。arping コマンドを実行して、ARPパケットを採取する。
[root@server ~]# arping -c 1 -I eth0 192.168.0.1

##5.2 pcapファイルの中身確認

pcapファイルの中身を確認する。ICMPパケットが採取されていることがわかる。
[root@server ~]# tshark -r 1.cap -ta
Running as user "root" and group "root". This could be dangerous.
  1   20:00:58 192.168.0.100 -> 192.168.0.1  ICMP 98 Echo (ping) request  id=0x03fa, seq=1/256, ttl=64
  2   20:00:59  192.168.0.1 -> 192.168.0.100 ICMP 98 Echo (ping) reply    id=0x03fa, seq=1/256, ttl=255 (request in 1)

pcapファイルの中身を確認する。DNSパケットが採取されていることがわかる。
[root@server ~]# tshark -r 2.cap -ta
Running as user "root" and group "root". This could be dangerous.
  1   20:01:41 192.168.0.100 -> 192.168.3.1  DNS 82 Standard query 0x0462  A ntp.nict.jp
  2   20:01:41  192.168.3.1 -> 192.168.0.100 DNS 249 Standard query response 0x0462  A 133.243.238.244 A 133.243.238.164 A 133.243.238.243 A 133.243.238.163

pcapファイルの中身を確認する。ARPパケットが採取されていることがわかる。
[root@server ~]# tshark -r 3.cap -ta
Running as user "root" and group "root". This could be dangerous.
  1   20:04:57 Vmware_9b:e6:7d -> Broadcast    ARP 42 Who has 192.168.0.1?  Tell 192.168.0.100
  2   20:04:57 NecPlatf_1e:da:07 -> Vmware_9b:e6:7d ARP 60 192.168.0.1 is at 00:3a:9d:1e:da:07

#6 pcapファイル結合がどのような順番で行われるかの確認
##6.1 mergecapへの引数を時刻の新しい順(3.cap,2cap,1,cap)に指定した場合

時刻の新しい順にpcapファイルを指定する。
[root@server ~]# mergecap 3.cap 2.cap 1.cap -w test1.cap

pcapファイルの中身を確認する。時刻の古い順になっていることがわかる。
[root@server ~]# tshark -r test1.cap -ta
Running as user "root" and group "root". This could be dangerous.
  1 20:00:58.977028 192.168.0.100 -> 192.168.0.1  ICMP 98 Echo (ping) request  id=0x03fa, seq=1/256, ttl=64
  2 20:00:59.000393  192.168.0.1 -> 192.168.0.100 ICMP 98 Echo (ping) reply    id=0x03fa, seq=1/256, ttl=255 (request in 1)
  3 20:01:41.305125 192.168.0.100 -> 192.168.3.1  DNS 82 Standard query 0x0462  A ntp.nict.jp
  4 20:01:41.307593  192.168.3.1 -> 192.168.0.100 DNS 249 Standard query response 0x0462  A 133.243.238.244 A 133.243.238.164 A 133.243.238.243 A 133.243.238.163
  5 20:04:57.341151 Vmware_9b:e6:7d -> Broadcast    ARP 42 Who has 192.168.0.1?  Tell 192.168.0.100
  6 20:04:57.346023 NecPlatf_1e:da:07 -> Vmware_9b:e6:7d ARP 60 192.168.0.1 is at 00:3a:9d:1e:da:07

##6.2 mergecapへの引数を時刻の古い順(1.cap,2cap,3,cap)に指定した場合

時刻の古い順にpcapファイルを指定する。
[root@server ~]# mergecap 1.cap 2.cap 3.cap -w test2.cap

pcapファイルの中身を確認する。時刻の古い順になっていることがわかる。
[root@server ~]# tshark -r test2.cap -ta
Running as user "root" and group "root". This could be dangerous.
  1 20:00:58.977028 192.168.0.100 -> 192.168.0.1  ICMP 98 Echo (ping) request  id=0x03fa, seq=1/256, ttl=64
  2 20:00:59.000393  192.168.0.1 -> 192.168.0.100 ICMP 98 Echo (ping) reply    id=0x03fa, seq=1/256, ttl=255 (request in 1)
  3 20:01:41.305125 192.168.0.100 -> 192.168.3.1  DNS 82 Standard query 0x0462  A ntp.nict.jp
  4 20:01:41.307593  192.168.3.1 -> 192.168.0.100 DNS 249 Standard query response 0x0462  A 133.243.238.244 A 133.243.238.164 A 133.243.238.243 A 133.243.238.163
  5 20:04:57.341151 Vmware_9b:e6:7d -> Broadcast    ARP 42 Who has 192.168.0.1?  Tell 192.168.0.100
  6 20:04:57.346023 NecPlatf_1e:da:07 -> Vmware_9b:e6:7d ARP 60 192.168.0.1 is at 00:3a:9d:1e:da:07

##6.3 まとめ
mergecapが各pcapファイルの時刻を判定して、古い順にpcapを結合する。(manの説明通り)

#7 pcapファイル結合(-a)
-aを使うと、mergecap の引数に指定した順にpcapファイルが結合されます。

時刻の新しい順にpcapファイルを結合する。
[root@server ~]# mergecap -a 3.cap 2.cap 1.cap -w test3.cap

結合したpcapファイルの中身を確認する。時刻の新しい順にpcapファイルが結合されたことがわかる。
[root@server ~]# tshark -r test3.cap -ta
Running as user "root" and group "root". This could be dangerous.
  1 20:04:57.341151 Vmware_9b:e6:7d -> Broadcast    ARP 42 Who has 192.168.0.1?  Tell 192.168.0.100
  2 20:04:57.346023 NecPlatf_1e:da:07 -> Vmware_9b:e6:7d ARP 60 192.168.0.1 is at 00:3a:9d:1e:da:07
  3 20:01:41.305125 192.168.0.100 -> 192.168.3.1  DNS 82 Standard query 0x0462  A ntp.nict.jp
  4 20:01:41.307593  192.168.3.1 -> 192.168.0.100 DNS 249 Standard query response 0x0462  A 133.243.238.244 A 133.243.238.164 A 133.243.238.243 A 133.243.238.163
  5 20:00:58.977028 192.168.0.100 -> 192.168.0.1  ICMP 98 Echo (ping) request  id=0x03fa, seq=1/256, ttl=64
  6 20:00:59.000393  192.168.0.1 -> 192.168.0.100 ICMP 98 Echo (ping) reply    id=0x03fa, seq=1/256, ttl=255 (request in 5)

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?