tcpdump でパケットキャプチャ

tcpdump で「ローカルホストの 80 番ポート ( 送受信 ) のパケットをキャプチャする」コマンドメモ。

tcpdump -i lo -X dst port 80 or src port 80

指定しているオプションは、
*「-i」でインターフェイスを指定 ( *1 )
*「-X」でパケットのデータ部分を 16 進数+ASCII で表示
*「dst port 80 or src port 80」で送信先ポート 80 番、送信元ポート 80 番のパケットをキャプチャするようにフィルタリング

という感じです。

( *1 ) ifconfig の結果。ローカルホストはインターフェイスは lo なので、こいつを指定。

eth0      Link encap:Ethernet  HWaddr 08:00:27:58:5D:B1  
          inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0
 - 略 -

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
 - 略 -


実行結果 ( curl -v http://localhost/test.html とかで適当にパケット発生させる )

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
12:39:38.032688 IP6 localhost.58157 > localhost.http: Flags [S], seq 1522441353, win 32752, options [mss 16376,sackOK,TS val 1203780 ecr 0,nop,wscale 6], length 0
        0x0000:  6000 0000 0028 0640 0000 0000 0000 0000  `....(.@........
        0x0010:  0000 0000 0000 0001 0000 0000 0000 0000  ................
        0x0020:  0000 0000 0000 0001 e32d 0050 5abe 9c89  .........-.PZ...
        0x0030:  0000 0000 a002 7ff0 54af 0000 0204 3ff8  ........T.....?.
        0x0040:  0402 080a 0012 5e44 0000 0000 0103 0306  ......^D........
12:39:38.032703 IP6 localhost.http > localhost.58157: Flags [S.], seq 2473444858, ack 1522441354, win 32728, options [mss 16376,sackOK,TS val 1203780 ecr 1203780,nop,wscale 6], length 0
        0x0000:  6000 0000 0028 0640 0000 0000 0000 0000  `....(.@........
        0x0010:  0000 0000 0000 0001 0000 0000 0000 0000  ................
        0x0020:  0000 0000 0000 0001 0050 e32d 936d c5fa  .........P.-.m..
        0x0030:  5abe 9c8a a012 7fd8 9cf7 0000 0204 3ff8  Z.............?.
        0x0040:  0402 080a 0012 5e44 0012 5e44 0103 0306  ......^D..^D....
12:39:38.032713 IP6 localhost.58157 > localhost.http: Flags [.], ack 1, win 512, options [nop,nop,TS val 1203780 ecr 1203780], length 0
        0x0000:  6000 0000 0020 0640 0000 0000 0000 0000  `......@........
        0x0010:  0000 0000 0000 0001 0000 0000 0000 0000  ................
        0x0020:  0000 0000 0000 0001 e32d 0050 5abe 9c8a  .........-.PZ...
        0x0030:  936d c5fb 8010 0200 83df 0000 0101 080a  .m..............
        0x0040:  0012 5e44 0012 5e44                      ..^D..^D
12:39:38.034488 IP6 localhost.58157 > localhost.http: Flags [P.], seq 1:168, ack 1, win 512, options [nop,nop,TS val 1203782 ecr 1203780], length 167
        0x0000:  6000 0000 00c7 0640 0000 0000 0000 0000  `......@........
        0x0010:  0000 0000 0000 0001 0000 0000 0000 0000  ................
        0x0020:  0000 0000 0000 0001 e32d 0050 5abe 9c8a  .........-.PZ...
        0x0030:  936d c5fb 8018 0200 00cf 0000 0101 080a  .m..............
        0x0040:  0012 5e46 0012 5e44 4745 5420 2f74 6573  ..^F..^DGET./tes
        0x0050:  742e 6874 6d6c 2048 5454 502f 312e 310d  t.html.HTTP/1.1.
        0x0060:  0a55 7365 722d 4167 656e 743a 2063 7572  .User-Agent:.cur
        0x0070:  6c2f 372e 3139 2e37 2028 6936 3836 2d70  l/7.19.7.(i686-p
        0x0080:  632d 6c69 6e75 782d 676e 7529 206c 6962  c-linux-gnu).lib
        0x0090:  6375 726c 2f37 2e31 392e 3720 4e53 532f  curl/7.19.7.NSS/
        0x00a0:  332e 3132 2e37 2e30 207a 6c69 622f 312e  3.12.7.0.zlib/1.
        0x00b0:  322e 3320 6c69 6269 646e 2f31 2e31 3820  2.3.libidn/1.18.
        0x00c0:  6c69 6273 7368 322f 312e 322e 320d 0a48  libssh2/1.2.2..H
        0x00d0:  6f73 743a 206c 6f63 616c 686f 7374 0d0a  ost:.localhost..
        0x00e0:  4163 6365 7074 3a20 2a2f 2a0d 0a0d 0a    Accept:.*/*....
12:39:38.034509 IP6 localhost.http > localhost.58157: Flags [.], ack 168, win 529, options [nop,nop,TS val 1203782 ecr 1203782], length 0
        0x0000:  6000 0000 0020 0640 0000 0000 0000 0000  `......@........
        0x0010:  0000 0000 0000 0001 0000 0000 0000 0000  ................
        0x0020:  0000 0000 0000 0001 0050 e32d 936d c5fb  .........P.-.m..
        0x0030:  5abe 9d31 8010 0211 8323 0000 0101 080a  Z..1.....#......
        0x0040:  0012 5e46 0012 5e46                      ..^F..^F
12:39:38.035025 IP6 localhost.http > localhost.58157: Flags [P.], seq 1:331, ack 168, win 529, options [nop,nop,TS val 1203783 ecr 1203782], length 330
        0x0000:  6000 0000 016a 0640 0000 0000 0000 0000  `....j.@........
        0x0010:  0000 0000 0000 0001 0000 0000 0000 0000  ................
        0x0020:  0000 0000 0000 0001 0050 e32d 936d c5fb  .........P.-.m..
        0x0030:  5abe 9d31 8018 0211 0172 0000 0101 080a  Z..1.....r......
        0x0040:  0012 5e47 0012 5e46 4854 5450 2f31 2e31  ..^G..^FHTTP/1.1
        0x0050:  2032 3030 204f 4b0d 0a44 6174 653a 2053  .200.OK..Date:.S
        0x0060:  6174 2c20 3235 204a 756c 2032 3031 3520  at,.25.Jul.2015.
        0x0070:  3033 3a33 393a 3338 2047 4d54 0d0a 5365  03:39:38.GMT..Se
        0x0080:  7276 6572 3a20 4170 6163 6865 2f32 2e32  rver:.Apache/2.2
        0x0090:  2e31 3520 2843 656e 744f 5329 0d0a 4c61  .15.(CentOS)..La
        0x00a0:  7374 2d4d 6f64 6966 6965 643a 2057 6564  st-Modified:.Wed
        0x00b0:  2c20 3038 204a 756c 2032 3031 3520 3135  ,.08.Jul.2015.15
        0x00c0:  3a34 343a 3331 2047 4d54 0d0a 4554 6167  :44:31.GMT..ETag
        0x00d0:  3a20 2232 3065 3238 2d33 662d 3531 6135  :."20e28-3f-51a5
        0x00e0:  6630 3363 6130 3637 3522 0d0a 4163 6365  f03ca0675"..Acce
        0x00f0:  7074 2d52 616e 6765 733a 2062 7974 6573  pt-Ranges:.bytes
        0x0100:  0d0a 436f 6e74 656e 742d 4c65 6e67 7468  ..Content-Length
        0x0110:  3a20 3633 0d0a 436f 6e6e 6563 7469 6f6e  :.63..Connection
        0x0120:  3a20 636c 6f73 650d 0a43 6f6e 7465 6e74  :.close..Content
        0x0130:  2d54 7970 653a 2074 6578 742f 6874 6d6c  -Type:.text/html
        0x0140:  3b20 6368 6172 7365 743d 5554 462d 380d  ;.charset=UTF-8.
        0x0150:  0a0d 0a3c 2144 4f43 5459 5045 2068 746d  ...<!DOCTYPE.htm
        0x0160:  6c3e 0a3c 6874 6d6c 3e0a 3c62 6f64 793e  l>.<html>.<body>
        0x0170:  0a3c 6831 3e74 6573 7420 2121 3c2f 6831  .<h1>test.!!</h1
        0x0180:  3e0a 3c2f 626f 6479 3e0a 3c2f 6874 6d6c  >.</body>.</html
        0x0190:  3e0a                                     >.
12:39:38.035129 IP6 localhost.http > localhost.58157: Flags [F.], seq 331, ack 168, win 529, options [nop,nop,TS val 1203783 ecr 1203782], length 0
        0x0000:  6000 0000 0020 0640 0000 0000 0000 0000  `......@........
        0x0010:  0000 0000 0000 0001 0000 0000 0000 0000  ................
        0x0020:  0000 0000 0000 0001 0050 e32d 936d c745  .........P.-.m.E
        0x0030:  5abe 9d31 8011 0211 81d7 0000 0101 080a  Z..1............
        0x0040:  0012 5e47 0012 5e46                      ..^G..^F
12:39:38.035860 IP6 localhost.58157 > localhost.http: Flags [.], ack 331, win 529, options [nop,nop,TS val 1203784 ecr 1203783], length 0
        0x0000:  6000 0000 0020 0640 0000 0000 0000 0000  `......@........
        0x0010:  0000 0000 0000 0001 0000 0000 0000 0000  ................
        0x0020:  0000 0000 0000 0001 e32d 0050 5abe 9d31  .........-.PZ..1
        0x0030:  936d c745 8010 0211 81d6 0000 0101 080a  .m.E............
        0x0040:  0012 5e48 0012 5e47                      ..^H..^G
12:39:38.036152 IP6 localhost.58157 > localhost.http: Flags [F.], seq 168, ack 332, win 529, options [nop,nop,TS val 1203784 ecr 1203783], length 0
        0x0000:  6000 0000 0020 0640 0000 0000 0000 0000  `......@........
        0x0010:  0000 0000 0000 0001 0000 0000 0000 0000  ................
        0x0020:  0000 0000 0000 0001 e32d 0050 5abe 9d31  .........-.PZ..1
        0x0030:  936d c746 8011 0211 81d4 0000 0101 080a  .m.F............
        0x0040:  0012 5e48 0012 5e47                      ..^H..^G
12:39:38.036163 IP6 localhost.http > localhost.58157: Flags [.], ack 169, win 529, options [nop,nop,TS val 1203784 ecr 1203784], length 0
        0x0000:  6000 0000 0020 0640 0000 0000 0000 0000  `......@........
        0x0010:  0000 0000 0000 0001 0000 0000 0000 0000  ................
        0x0020:  0000 0000 0000 0001 0050 e32d 936d c746  .........P.-.m.F
        0x0030:  5abe 9d32 8010 0211 81d3 0000 0101 080a  Z..2............
        0x0040:  0012 5e48 0012 5e48                      ..^H..^H

localhost.58157 > localhost.http」、「localhost.http > localhost.58157」とあるので、80 番ポートの送受信パケットが取れております。


ファイルに保存して Wireshark なりでゆっくり見たいという場合は「-w」を指定してやれば OK。

tcpdump -i lo -X dst port 80 or src port 80 -w test.pcap


Manpage、指定可能なフィルタリングの情報は以下にございます。

・Manpage of TCPDUMP
http://www.tcpdump.org/manpages/tcpdump.1.html
TCPDUMP filters
http://www.cs.ucr.edu/~marios/ethereal-tcpdump.pdf


以上です。


[ 環境情報 ]
CentOS 6.2