现在的位置: 首页 > 综合 > 正文

OSI七层模型及对应的数据包格式

2019年05月26日 ⁄ 综合 ⁄ 共 10016字 ⁄ 字号 评论关闭

我接触网络协议也比较久了,不过一直都只懂个皮毛,最近比较深入研究之后终于有点豁然开朗的感觉。也因为网络上各种协议的资料太多但是都比较分散杂乱,所以在这里做点总结,给大家提供一些资料也备自己以后查阅。 

鉴于有些朋友没有耐心完全看完整篇文章,所以我先给大家一个相当权威网站,该详细提供了各种网络协议及分析(有这么一个网站足矣)

我下面列举的材料也基本来于此 

RFC Sourcebook

http://www.networksorcery.com/enp/default1101.htm 


 首先让我们从OSI七层结构 说起

OSI/RM即Open System Interconnection Reference Model开放系统互连基本参考模型。开放,是指非垄断的。系统是指现实的系统中与互联有关的各部分。 

层次

名称

数据格式及行为

功能

典型设备

TCP/IP常见协议

第七层

应用层Application

 

 

 

 

数据Data

网络服务与使用者应用程序间的一个接口

 

NNTP,SIP,SSI,DNS,FTP,Gopher,HTTP,NFS,NTP,DHCP,SMPP,SMTP,SNMP,Telnet,RIP,BGP

第六层

表示层Presentation

应用程序和网络间的翻译官:对数据加密、解密,图片、文件的编码、解码等

 

MIME,SSL,TLS,XDR

第五层

会话层Session

维持网络上两节点之间建立、维持和终止会话

 

Sockets.Session establishment in TCP,SIP,RTP

第四层

传输层Transport

数据组织成数据段Segment

在机器内部端口间传输数据包

 

TCP,UDP,SCTP,DCCP

第三层

网络层Network

分割和重新组合数据包Packet

IP地址封装解封装(IPMAC地址的转换)

路由器

IP,IPsec,ICMP,IGMP,OSPF

第二层

数据链路层DataLink

将比特信息封装成数据帧Frame/及解封装

对数据纠错/封装解封装MAC地址

网桥、交换机

PPP,SLIP,PPTP,L2TP

第一层

物理层Physical

传输比特(bit)流

建立、维护和取消物理连接

网卡、中继器和集线器

 

 

Ethernet: 以太网协议
下面介绍一下各个帧格式  

  • Ethernet II 
           就是DIX以太网联盟推出的,它由6个字节的目的MAC地址,6个字节的源MAC地址,2个字节的类型域(用于标示封装在这个Frame、里面数据的类型)以上为Frame Header,接下来是46--1500 字节的数据,和4字节的帧校验)
  • Novell Ethernet 
           它的帧头与Ethernet有所不同其中EthernetII帧头中的类型域变成了长度域,后面接着的两个字节为0xFFFF 
           用于标示这个帧是Novell Ether类型的Frame 由于前面的0xFFFF站掉了两个字节所以数据域缩小为44-1498个字节,帧校验不变。
  • IEEE 802.3/802.2 
           802.3的Frame Header和Ethernet II的帧头有所不同EthernetII类型域变成了长度域。其中又引入802.2协议(LLC)在802.3帧头后面添加了一个LLC首部,由DSAP(Destination Service Access Point) 
           1 byte,SSAP(Source SAP),一个控制域--1 byte! 
           SAP用于标示帧的上层协议
  • Ethernet SNAP 
           SNAP Frame与802.3/802.2 Frame的最大区别是增加了一个5 Bytes的SNAP ID其中前面3个byte通常与源mac地址的前三个bytes相同为厂商代码!有时也可设为0,后2 bytes 与Ethernet II的类型域相同。。。

  如何区分不同的帧格式    
       Ethernet中存在这四种Frame那些网络设备又是如何识别的呢? 如何区分EthernetII与其他三种格式的Frame 如果帧头跟随source mac地址的2 bytes的值大于1500 则此Frame为EthernetII格式的。 
       接着比较紧接着的两bytes如果为0xFFFF则为Novell Ether 类型的Frame 
       如果为0xAAAA则为Ethernet SNAP格式的Frame ,如果都不是则为Ethernet 802.3/802.2格式的帧

以太网帧结构概述
  以太网帧是OSI参考模型数据链路层的封装,网络层的数据包被加上帧头和帧尾,构成可由数据链路层识别的数据帧。虽然帧头和帧尾所用的字节数是固定不变的,但根据被封装数据包大小的不同,以太网帧的长度也随之变化,变化的范围是64-1518字节(不包括8字节的前导字)。

 、典型帧结构:Ethernet_II (最常用,所以只介绍这个)
  Ethernet_II中所包含的字段:
  目的地址:接收端的MAC地址,6字节长;
  源地址:发送端的MAC地址,6字节长;
  类型:数据包的类型(即上层协议的类型),2字节长;
  数据:被封装的数据包,46-1500字节长;
  Ethernet_II的主要特点是通过类型域标识了封装在帧里的数据包所采用的协议,类型域是一个有效的指针,通过它,数据链路层就可以承载多个上层(网络层)协议。但是,Ethernet_II的缺点是没有标识帧长度的字段。

Ethernet II or DIX

0x0600 XNS (Xerox)
0x0800 IP (the Internet protocol)
0x6003 DECNET

 IP: Internet Protocol

MAC 头

IP 头 数据 :::

IP header:

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
版本 IP头长度 区分服务 IP包长度
标示符 标志 Fragment offset
TTL生存周期 协议
Header checksum
Source IP address 源IP地址
Destination IP address 目标IP地址
Options and padding :::

 Protocol.8 bits.
This field specifies the next encapsulated protocol.

Value Protocol References
0 HOPOPT, IPv6 Hop-by-Hop Option. RFC 1883
1 ICMP, Internet Control Message Protocol. RFC 792
2 IGAP, IGMP for user Authentication Protocol.
IGMP, Internet Group Management Protocol.
RGMP, Router-port Group Management Protocol.
RFC1112
3 GGP, Gateway to Gateway Protocol. RFC 823
4 IP in IP encapsulation. RFC 2003
5 ST, Internet Stream Protocol. RFC 1190, RFC 1819
6 TCP, Transmission Control Protocol. RFC 793
7 UCL,CBT.  
8 EGP, Exterior Gateway Protocol. RFC 888
9 IGRP, Interior Gateway Routing Protocol.  
10 BBN RCC Monitoring.  
11 NVP, Network Voice Protocol. RFC 741
12 PUP.  
13 ARGUS.  
14 EMCON, Emission Control Protocol.  
15 XNET, Cross Net Debugger. IEN 158
16 Chaos.  
17 UDP, User Datagram Protocol. RFC 768
18 TMux, Transport Multiplexing Protocol. IEN 90
19 DCN Measurement Subsystems.  
20 HMP, Host Monitoring Protocol. RFC 869
21 Packet Radio Measurement.  
22 XEROX NS IDP.  
23 Trunk-1.  
24 Trunk-2.  
25 Leaf-1.  
26 Leaf-2.  
27 RDP, Reliable Data Protocol. RFC 908
28 IRTP, Internet Reliable Transaction Protocol. RFC 938
29 ISO Transport Protocol Class 4. RFC 905
30 NETBLT, Network Block Transfer.  
31 MFE Network Services Protocol.  
32 MERIT Internodal Protocol.  
33 DCCP, Datagram Congestion Control Protocol.  
34 Third Party Connect Protocol.  
35 IDPR, Inter-Domain Policy Routing Protocol.  
36 XTP, Xpress Transfer Protocol.  
37 Datagram Delivery Protocol.  
38 IDPR, Control Message Transport Protocol.  
39 TP++ Transport Protocol.  
40 IL Transport Protocol.  
41 IPv6 over IPv4. RFC 2473
42 SDRP, Source Demand Routing Protocol.  
43 IPv6 Routing header.  
44 IPv6 Fragment header.  
45 IDRP, Inter-Domain Routing Protocol.  
46 RSVP, Reservation Protocol.  
47 GRE, General Routing Encapsulation.  
48 DSR, Dynamic Source Routing Protocol.  
49 BNA.  
50 ESP, Encapsulating Security Payload.  
51 AH, Authentication Header.  
52 I-NLSP, Integrated Net Layer Security TUBA.  
53 SWIPE, IP with Encryption.  
54 NARP, NBMA Address Resolution Protocol.  
55 Minimal Encapsulation Protocol.  
56 TLSP, Transport Layer Security Protocol using Kryptonet key management.  
57 SKIP.  
58 ICMPv6, Internet Control Message Protocol for IPv6.
MLD, Multicast Listener Discovery.
 
59 IPv6 No Next Header.  
60 IPv6 Destination Options.  
61 Any host internal protocol.  
62 CFTP.  
63 Any local network.  
64 SATNET and Backroom EXPAK.  
65 Kryptolan.  
66 MIT Remote Virtual Disk Protocol.  
67 Internet Pluribus Packet Core.  
68 Any distributed file system.  
69 SATNET Monitoring.  
70 VISA Protocol.  
71 Internet Packet Core Utility.  
72 Computer Protocol Network Executive.  
73 Computer Protocol Heart Beat.  
74 Wang Span Network.  
75 Packet Video Protocol.  
76 Backroom SATNET Monitoring.  
77 SUN ND PROTOCOL-Temporary.  
78 WIDEBAND Monitoring.  
79 WIDEBAND EXPAK.  
80 ISO-IP.  
81 VMTP, Versatile Message Transaction Protocol.  
82 SECURE-VMTP  
83 VINES.  
84 TTP.  
85 NSFNET-IGP.  
86 Dissimilar Gateway Protocol.  
87 TCF.  
88 EIGRP.  
89 OSPF, Open Shortest Path First Routing Protocol.
MOSPF, Multicast Open Shortest Path First.
 
90 Sprite RPC Protocol.  
91 Locus Address Resolution Protocol.  
92 MTP, Multicast Transport Protocol.  
93 AX.25.  
94 IP-within-IP Encapsulation Protocol.  
95 Mobile Internetworking Control Protocol.  
96 Semaphore Communications Sec. Pro.  
97 EtherIP.  
98 Encapsulation Header.  
99 Any private encryption scheme.  
100 GMTP.  
101 IFMP, Ipsilon Flow Management Protocol.  
102 PNNI over IP.  
103 PIM, Protocol Independent Multicast.  
104 ARIS.  
105 SCPS.  
106 QNX.  
107 Active Networks.  
108 IPPCP, IP Payload Compression Protocol. RFC 2393
109 SNP, Sitara Networks Protocol.  
110 Compaq Peer Protocol.  
111 IPX in IP.  
112 VRRP, Virtual Router Redundancy Protocol. RFC 3768, RFC 5798
113 PGM, Pragmatic General Multicast.  
114 any 0-hop protocol.  
115 L2TP, Level 2 Tunneling Protocol.  
116 DDX, D-II Data Exchange.  
117 IATP, Interactive Agent Transfer Protocol.  
118 ST, Schedule Transfer.  
119 SRP, SpectraLink Radio Protocol.  
120 UTI.  
121 SMP, Simple Message Protocol.  
122 SM.  
123 PTP, Performance Transparency Protocol.  
124 ISIS over IPv4.  
125 FIRE.  
126 CRTP, Combat Radio Transport Protocol.  
127 CRUDP, Combat Radio User Datagram.  
128 SSCOPMCE.  
129 IPLT.  
130 SPS, Secure Packet Shield.  
131 PIPE, Private IP Encapsulation within IP.  
132 SCTP, Stream Control Transmission Protocol.  
133 Fibre Channel.  
134 RSVP-E2E-IGNORE. RFC 3175
135 Mobility Header. RFC 3775
136 UDP-Lite, Lightweight User Datagram Protocol. RFC 3828
137 MPLS in IP. RFC 4023
138 MANET protocols. RFC 5498
139 HIP, Host Identity Protocol. RFC 5201
140 Shim6, Level 3 Multihoming Shim Protocol for IPv6. RFC 5533
141 WESP, Wrapped Encapsulating Security Payload. RFC 5840
142 ROHC, Robust Header Compression. RFC 5858
143
-
252
   
253
254
Experimentation and testing.  
255 reserved.

 

TCP: Transmission Control Protocol.

MAC 头 IP 头 TCP 头 Data :::

TCP header:

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Source Port 源端口 Destination Port 目标端口
Sequence Number 序列号
Acknowledgment Number 要接受的序列号
Data Offset reserved ECN Control Bits Window
Checksum Urgent Pointer
Options and padding :::
Data :::

 

目的端口所用的服务可以查看该网站

http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

 

UDP, User Datagram Protocol

MAC 头 IP 头 UDP 头 Data :::

UDP header:

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Source Port 源端口 Destination Port目标端口
Length Checksum
Data :::

 


RIP, Routing Information Protocol 

MAC header IP header UDP header RIP header Data :::

RIPv1 header:

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Command Version 0
RIPv1 entry table :::

RIPv2 header:

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Command Version 0
RIPv2 entry table :::

 


HTTP, HyperText Transfer Protocol

MAC 头 IP 头 TCP 头 HTTP 消息 :::

HTTP message:


Methods:

Method References
DELETE RFC 1945
GET RFC 1945
HEAD RFC 1945
LINK RFC 1945
OPTIONS RFC 2068
PATCH RFC 2068
POST RFC 1945
PUT RFC 1945
TRACE RFC 2068
UNLINK RFC 1945

 

 列举了最常见的几个协议,下面来分析一下一段帧来练习巩固一下。

在这之前先给大家介绍一款国外很优秀的抓取数据包的软件 Wireshark,是想从事网络协议工作人士必不可少的好软件;存在着各种版(不过基于linux的版本安装起来比较麻烦,因为事先得自己手动安装很多个依赖的包)

官方下载地址:

http://www.wireshark.org/download.html 

先给个截图

以下是完整的一个数据包

 序列

0000  | 00 1d 92 32 d0 90 00 1f  3a 3a f6 72 08 00 45 00                
0010  | 00 40 f6 67 40 00 80 06  80 ea c0 a8 01 0b c0 a8              
0020  | 01 0a 12 08 00 8b e0 b1  20 42 00 00 00 00 b0 02  
0030  | 20 00 7f 14 00 00 02 04  05 b4 01 03 03 00 01 01   
0040  | 08 0a 00 00 00 00 00 00  00 00 01 01 04 02

所有都是以16进制表示一个byte字节(8个bits),也就是每个数字都代表4个bits。

首先我们知道最低层肯定是Ehernet协议,所以这段数据的最开头就是该协议的头,可以参考上面资料

第一行 


00 1d 92 32 d0 90 | 00 1f  3a 3a f6 72 |                      08 00                               | 45
00 

   目的MAC地址              源MAC地址              接下来数据协议类型(0x0800是IP)


所以从第一行最后2个字节开始就是IP包的头了:

                                                                                  4                             5                                           00

                                                                               版本是4         ip包头长度是5(也32bits为单位)        区别服务(不太重要)

                                                                                                          ,也就是20个字节


        00 
40                          |   f6 67      |                40 00                                           |                              80                                                    

ip包的总共长度,                         序列号        转化成bit是 010 0 0000 0000 0000                TTL = 128 (windows默认128,linux默认 64)

4*16 = 64 个字节                                       更加flag的定义是:不要分段,且是最后一段         该数据包生存周期(即最多能通过路由的个数,等于0时抛弃)      

  |             06                    |     80 ea    |  c0 a8 01 0b |c0 a8

    在IP协议之上的协议          checksum       源机器IP        目标

     06是TCP协议                                  192.168.1.11


01 0a    
           |     
  12 08       |                        00 8b                                   
|   e0 b1  20 42    |

ip地址                      源机器端口                        目标机器端口                                    该tcp的序列号                  

 192.168.1.10            4616                 139(NETBIOS Session Service.)

          00 00 00 00                     |        b                        |     0      02                                               

    接下来想接受到的tcp包序列号           TCP包头长度                 bits:0000 0000 0010

                                                     11*4 = 44字节                 零都代表默认,唯一的一个1代表SYN


20 00               |    7f 14       |   00 00     |  02 04   05 b4      01 03 03 00     01 01 |     08  0a          00  00  | 

00  00    00  00 |  00  00     01  01 |  04  02      

窗口大小8192       checksum      紧急标志           Options (不太重要)

           

表示Ethernet头,共14字节

                  

IP头, 共 20 字节 

           

TCP头,共44字节

跟内容完全符合

抱歉!评论已关闭.