-
TCP/IP详解卷1 - 系列文
-
引言
在初学TCP/IP协议时,会觉得协议是一种很抽象的东西,通过wireshark抓包分析各种协议,可以让抽象变得具体。
-
wireshark抓包
wireshar抓包的教程很多,这里就不再重复。参考下图:
![](http://upload-images.jianshu.io/upload_images/2118438-6ba341ead3e15ffe.gif?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
-
wireshark快速get对应filter
我们知道wireshark抓到的内容很多,要过滤我们需要的信息就需要设置对应的filter,例如:想要过滤ICMP协议相关的log,直接在Filter里面输入协议名称"icmp",就能过滤出ICMP的log。
这是一种最简单的过滤条件了。实际上设置filter的语句有很多,如果想要过滤ICMP中type为3(目的地不可到达)的数据报,但又不知道filter应该怎么写,那应该怎么办呢?
这个时候,我们就可以直接利用wireshark提供的As Filter功能,快速get到想要设置的filter。
如下图所示,要过滤ARP数据报中的ARP请求数据报,直接选中Opcode: request(1),右键 ->Copy->As Filter,就能得到对应filter。
![](http://upload-images.jianshu.io/upload_images/2118438-ef6f96506d26c837.gif?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
-
IP数据报
-
IPv4报文首部
要分析IP数据报的内容,首先得知道它的数据报的数据结构。
![](http://upload-images.jianshu.io/upload_images/2118438-ce5dc813306180c0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
-
wireshark过滤IP数据报
然后过滤IP数据报,从下面这个图可以看出,IP数据报被封装到以太网帧里面,当以太网帧的type值为0x0800时,那么以太网帧封装的就是IP数据报了。
![](http://upload-images.jianshu.io/upload_images/2118438-e274c3272fcb53a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
因此,设置eth.type == 0x800就能过滤出IP数据报。根据下图,再对比IPv4报文首部的数据结构,就可以更加清晰地理解IP数据报了。
![](http://upload-images.jianshu.io/upload_images/2118438-78278d2fb820e532.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
-
ARP
-
ARP报文格式
![](http://upload-images.jianshu.io/upload_images/2118438-e3c4ec0a2719c8b3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
-
wireshark过滤ARP数据报
和IP数据报一样,ARP数据报被封装到以太网帧里面,当以太网帧的type值为0x0806,设置
eth.type == 0x806就能过滤ARP数据报。
ARP数据报又分为请求数据报和应答数据报。
首先,查看我本机的MAC地址,在wireshark中过滤ARP请求数据报的MAC地址等于我本机地址的一条log。如下图,本机请求IP地址为10.120.1.91的主机的MAC的ARP请求。
![](http://upload-images.jianshu.io/upload_images/2118438-19a85d7857a165ec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
通过上面的ARP数据报,我又可以得到ARP请求中Sender IP address的Filter格式,我只需要过滤
Sender IP address为10.120.1.91,最后过滤出来的结果就只有2条。
查看其中Target MAC address为我本机MAC地址,并且Opcode为2,即是10.120.1.91返回的APR应答,其中Sender MAC address,就是ARP请求得到的MAC地址。
![](http://upload-images.jianshu.io/upload_images/2118438-5dcc34e492e81a8d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
-
ICMP
-
ICMP数据报格式
![](http://upload-images.jianshu.io/upload_images/2118438-45175e1c06368f9e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/696)
-
wireshark过滤ICMP数据报
ICMP报头格式虽然简单,但是对应类型很多,分析方法也是类似的,选中你想要设置为过滤条件的参数,然后copy为filter
![](http://upload-images.jianshu.io/upload_images/2118438-d3e88e2d4c780bd9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](http://upload-images.jianshu.io/upload_images/2118438-699b72ea24c0d93f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](http://upload-images.jianshu.io/upload_images/2118438-c08c16e110a0a066.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](http://upload-images.jianshu.io/upload_images/2118438-2005b529b1098b79.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)