曲径通幽论坛

标题: 使用wireshark及tcpdump观察三次握手 [打印本页]

作者: beyes    时间: 2011-12-19 11:09
标题: 使用wireshark及tcpdump观察三次握手
所有基于 TCP 的连接在开始时都必须经过一个称为三次握手(three-way handshake)的连接确认过程。三次握手的过程如下图所示:
[attach]114[/attach]

上图中,SYN 表示在
在连接建立时对需要进行同步的序号,ACK 表示
应答字段标志。关于这两个概念可参考:
http://www.groad.net/bbs/read.php?tid-5621.html

下面使用 wireshark 和 tcpdump 两种工具来分析三次握手的过程,wireshark 是一种图形化工具,tcpdump 基于命令行。

这里假设服务器 IP 是 192.168.1.104 ,提供 HTTP 服务,客户机的 IP 为 192.168.1.100 ,通过浏览器连接服务器上的 HTTP 服务。在启动 wireshark 前,我们可以在 wireshark 的过滤选项里填入 port 80 表示只对 80 端口的数据进行监视与捕捉。

第一次握手 wireshark 捕捉到的数据如下图所示:
[attach]115[/attach]

第一次握手由客户端向服务器发起,其中会设置 SYN 标志以及发送一个随机序列号。

第二次握手由服务器向客户端回应,捕捉到的数据如下图所示:
[attach]116[/attach]

在第二次握手中,服务器通发送的包头中设置了 SYN 和 ACK 标志,同时将客户端发来的序列号加一作为对客户端的应答,另外还包含了自己的初始化序列号。

第三次握手为客户端响应服务器的应答,如下图所示:
[attach]117[/attach]

在最后一次握手中,客户端设置了应答服务器标志 ACK,注意此时不再设置 SYN 标志,最后还对在第二次握手里服务器发来的序列号加 1 表示对服务器进行了应答。

下面是用 tcpdump 工具的使用,原理一样,只不过 tcpdump 基于命令,同样,比如基于 22 端口(SSH服务)的连接,下面只是三次握手的数据流:
linux-xh53:~ # tcpdump -i eth0 port 22 -S -vv
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
09:54:32.927057 IP (tos 0x0, ttl 64, id 18765, offset 0, flags [DF], proto TCP (6), length 52)
    192.168.1.100.ecsqdmn > 192.168.1.104.ssh: Flags [S], cksum 0xc57d (correct), seq 3731693470, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
09:54:32.927090 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52)

    192.168.1.104.ssh > 192.168.1.100.ecsqdmn: Flags [S.], cksum 0x8443 (incorrect -> 0x00f6), seq 4262702424, ack 3731693471, win 14600, options [mss 1460,nop,nop,sackOK,nop,wscale 4], length 0
09:54:32.927468 IP (tos 0x0, ttl 64, id 18766, offset 0, flags [DF], proto TCP (6), length 40)

    192.168.1.100.ecsqdmn > 192.168.1.104.ssh: Flags [.], cksum 0x69b1 (correct), seq 3731693471, ack 4262702425, win 4380, length 0
09:54:32.949513 IP (tos 0x0, ttl 64, id 34270, offset 0, flags [DF], proto TCP (6), length 61)
上面命令中,-S 选项是查看绝对序号,也就是会在 seq 或 ack 后列出序号具体的值,如果不用该选项,那么只显示相对序号。




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2