[crypto][ipsec] 简述ESP协议的sequence number机制
预备
首先提及一个概念叫重放攻击,对应的机制叫做:anti-replay
https://en.wikipedia.org/wiki/Anti-replay
IPsec协议的anti-replay特性就是用来应对重放攻击的一种机制,方法是:
增加两个机制:序列号(seqence number)和收包窗口(sliding window)
发包方从0开始计数,每发一个包就把序号加1。 收包方拥有一个长度为N的滑动窗口,序号在窗口外的包都认为是无效包。
序号在窗口内的重复包,也被认为是无效包。窗口下边界处的包会导致窗口向前滑动。
一
以下内容都在ESP内讨论。
基于前文,我们已经了解到了ESP内的两个概念seq num,reply window,和一个属性anti-replay
seq num
是在报文内的,由发包者决定,并加到报文上。见图:

reply window
replay window是收包方本地的,自维护不协商。
anti-replay
anti-replay可以理解为是一个特性。
RFC中规定任何IPsec实现中都必须实现,且默认开启,不可以协商。 但是接收端可以关掉这个特效。(这好像矛盾了??--!)
另外,ipsec还支持多播和单播,这个时候以上讨论的内容都没有区别。
但是同时ipsec还支持多个sender共用一个SA。在这种情况下,anti-replay就是失效的,自然seq num和reply window也就没有用了。
当anti-replay生效的时候,seq number满了之后就只能重协商chlidsa。
当anti-replay不生效的是,sender不再关心seq number,就一直加加加,然后溢出就变成0.
二
于是,这里有了一个新的问题,
seq number满了就要重协商。看包我们发现这个字段是uint32的,所以,在高速网络中,每2^32个包就要重协商一次。
为了解决这个问题,现在引入一个新的概念,叫做:Extended Sequence Number(ESN)
见包结构:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Security Parameters Index (SPI) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+---
| IV (optional) | ^ p
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | a
| Rest of Payload Data (variable) | | y
~ ~ | l
| | | o
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | a
| | TFC Padding * (optional, variable) | v d
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+---
| | Padding (- bytes) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | Pad Length | Next Header |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Integrity Check Value-ICV (variable) |
~ ~
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ What What What
# of Requ'd Encrypt Integ is
bytes [] Covers Covers Xmtd
------ ------ ------ ------ ------
SPI M Y plain
Seq# (low-order bits) M Y plain p
------ a
IV variable O Y plain | y
IP datagram [] variable M or D Y Y cipher[] |-l
TFC padding [] variable O Y Y cipher[] | o
------ a
Padding - M Y Y cipher[] d
Pad Length M Y Y cipher[]
Next Header M Y Y cipher[]
Seq# (high-order bits) if ESN [] Y not xmtd
ICV Padding variable if need Y not xmtd
ICV variable M [] plain
这样的话,seq number便扩展到了64个bit。
ESN塞在了Next Header的后面。包头里面的seq number和原来一样是明文传输的,用来存这个数的低32位。 ESN是密文传输的(或者不传输),用来存储这个数的高32位。
需要注意的是,这个地方还有点复杂我还没有深入分析。这个ESN在不同的场景下的作用好像还不一样。还会被用于做完整性验证信息?
详见:
https://tools.ietf.org/html/rfc4303#section-2.2
https://tools.ietf.org/html/rfc4303#section-3.3.2.2
https://tools.ietf.org/html/rfc4303#section-3.4.3
二点一
ESN number的高32bit会被双方计数维护,不会被放在报文的payload里参与传输。但是在计算消息认证码的时候,会把它放进去一起参与计算。
https://tools.ietf.org/html/rfc4303#section-3.3.3
If ESN (see Appendix) is selected, only the low-order bits of the
sequence number are transmitted in the Sequence Number field,
although both sender and receiver maintain full -bit ESN counters.
The high order bits are included in the integrity check in an
algorithm/mode-specific fashion, e.g., the high-order bits may be
appended after the Next Header field when a separate integrity
algorithm is employed.
见下图,这是一张启用了ESN的esp包结构,与没有ESN的包,并没有什么区别。

三
丢包与乱序。
https://tools.ietf.org/html/rfc4303#page-38
在这个机制里,有几个值W(anti window的大小),T(合法包的seq的上限),B(合法包的seq的下限)
当累计的连续的不合法包的数量(T-B)大于2^32个的时候,将触发ESP的重认证(re-synchronization)机制。收到了一个合法包之后,计数将被重置为0
为什么是,2^32这么大的一个数呢?
因为ESP认为,上层应该会更早的感知链路出来问题。如TCP会自行发现。有交互的UDP,应用层也会发现。
这里的re-synchronization机制只针对一种情况,就是udp单侧发包,对方无回应的应用场景。
写到这里。。突然发现后边的内容理解的不是很好。。。就先这样了。。。。
到底re-synchronization机制是什么??
TO BE continue
A3.. Triggering Re-synchronization For each SA, the receiver records the number of consecutive packets
that fail authentication. This count is used to trigger the re-
synchronization process, which should be performed in the background
or using a separate processor. Receipt of a valid packet on the SA
resets the counter to zero. The value used to trigger the re-
synchronization process is a local parameter. There is no
requirement to support distinct trigger values for different SAs,
although an implementer may choose to do so. A3.. Re-synchronization Process When the above trigger point is reached, a "bad" packet is selected
for which authentication is retried using successively larger values
for the upper half of the sequence number (Seqh). These values are
generated by incrementing by one for each retry. The number of
retries should be limited, in case this is a packet from the "past"
or a bogus packet. The limit value is a local parameter. (Because
the Seqh value is implicitly placed after the ESP (or AH) payload, it
may be possible to optimize this procedure by executing the integrity
algorithm over the packet up to the endpoint of the payload, then
compute different candidate ICVs by varying the value of Seqh.)
Successful authentication of a packet via this procedure resets the
consecutive failure count and sets the value of T to that of the
received packet. This solution requires support only on the part of the receiver,
thereby allowing for backward compatibility. Also, because re-
synchronization efforts would either occur in the background or
utilize an additional processor, this solution does not impact
traffic processing and a denial of service attack cannot divert
resources away from traffic processing.
[crypto][ipsec] 简述ESP协议的sequence number机制的更多相关文章
- 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)
原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ from:ht ...
- [转] 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)
点击阅读原译文 原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ ...
- mysql oom之后的page 447 log sequence number 292344272 is in the future
mysql oom之后,重启时发生130517 16:00:10 InnoDB: Error: page 447 log sequence number 292344272InnoDB: is in ...
- Sequence Number
1570: Sequence Number 时间限制: 1 Sec 内存限制: 1280 MB 题目描述 In Linear algebra, we have learned the definit ...
- InnoDB: The log sequence number in ibdata files does not match
InnoDB: The log sequence number in ibdata files does not matchInnoDB的:在ibdata文件的日志序列号不匹配 可能ibdata文件损 ...
- hzau 1205 Sequence Number(二分)
G. Sequence Number In Linear algebra, we have learned the definition of inversion number: Assuming A ...
- Thread <number> cannot allocate new log, sequence <number>浅析
有时候,你会在ORACLE数据库的告警日志中发现"Thread <number> cannot allocate new log, sequence <number> ...
- ORA-02287: sequence number not allowed here问题的解决
当插入值需要从另外一张表中检索得到的时候,如下语法的sql语句已经不能完成该功能:insert into my_table(id, name) values ((select seq_my_table ...
- Python之路迭代器协议、for循环机制、三元运算、列表解析式、生成器
Python之路迭代器协议.for循环机制.三元运算.列表解析式.生成器 一.迭代器协议 a迭代的含义 迭代器即迭代的工具,那什么是迭代呢? #迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的 ...
随机推荐
- 一张图让你懂Python安装第三方库
- 斜率优化dp 的简单入门
不想写什么详细的讲解了...而且也觉得自己很难写过某大佬(大米饼),于是建议把他的 blog 先看一遍,然后自己加了几道题目以及解析...顺便建议看看算法竞赛(蓝皮书)的 0x5A 斜率优化(P294 ...
- C++设计模式——备忘录模式
备忘录模式 在GOF的<设计模式:可复用面向对象软件的基础>一书中对备忘录模式是这样说的:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢 ...
- 007_wireshark分析TCP的三次握手和四次断开
要想进行抓包分析,必须先了解TCP的原理.这里介绍了TCP的建立连接的三次握手和断开连接的四次握手. 一.前言:介绍三次握手之前,先介绍TCP层的几个FLAGS字段,这个字段有如下的几种标示 SYN表 ...
- python学习第20天
python中的继承 单继承 多继承
- iOS -- Effective Objective-C 阅读笔记 (6)
1: 在 既有类中使用 关联对象存放自定义数据 有时候需要在对象中存放相关信息, 这是我们经常会从对象所属的类中继承一个子类, 然后改用这个子类对象, 然而并非所有的情况下都能这么做, 有时候类的实 ...
- pl/sql学习(4): 包package
本文简单介绍包, 目前来看我用的不多, 除了之前 为了实现 一个procedure 的输出参数是结果集的时候用到过 package. 概念: 包是一组相关过程.函数.变量.常量和游标等PL/SQL程序 ...
- cf1154G 埃氏筛应用
直接用埃氏筛也可以做,但是这题写起来有点恶臭.. 更加简单的写法是直接枚举gcd=k,然后里面再枚举一次i*k,即找到k两个最小的倍数,看起来复杂度很高,但其实也是埃氏筛的复杂度 因为每次枚举gcd, ...
- 微信支付的安全漏洞之XXE
1.场景:国外安全社区公布微信支付官方SDK存在严重漏洞,可导致商家服务器被入侵(绕过支付的效果).目前,漏洞详细信息以及攻击方式已被公开,影响范围巨大(已确认陌陌.vivo因使用该SDK而存在该漏洞 ...
- Integer Replacement
https://leetcode.com/problems/integer-replacement/#/solutions 这题是一道典型的搜索问题,我采用广度搜索,可以直接输出最短路径.这题的tes ...