C++ TCP:seq/ack 号是如何产生的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/692880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:47:43  来源:igfitidea点击:

TCP: How are the seq / ack numbers generated?

c++networkingtcpwinpcap

提问by xian

I am currently working on a program which sniffs TCP packets being sent and received to and from a particular address. What I am trying to accomplish is replying with custom tailored packets to certain received packets. I've already got the parsing done. I can already generated valid Ethernet, IP, and--for the most part--TCP packets.

我目前正在开发一个程序,该程序可以嗅探从特定地址发送和接收的 TCP 数据包。我想要完成的是用定制的数据包回复某些收到的数据包。我已经完成了解析。我已经可以生成有效的以太网、IP 和——大多数情况下——TCP 数据包。

The only thing that I cannot figure out is how the seq / ack numbers are determined.

我唯一无法弄清楚的是 seq / ack 编号是如何确定的。

While this may be irrelevant to the problem, the program is written in C++ using WinPCap. I am asking for any tips, articles, or other resources that may help me.

虽然这可能与问题无关,但该程序是使用 WinPCap 用 C++ 编写的。我正在寻求任何可能对我有帮助的提示、文章或其他资源。

回答by DGentry

When a TCP connection is established, each side generates a random number as its initial sequence number. It is a strongly random number: there are security problems if anybody on the internet can guess the sequence number, as they can easily forge packets to inject into the TCP stream.

当 TCP 连接建立时,每一方都会生成一个随机数作为其初始序列号。它是一个强随机数:如果互联网上的任何人都能猜出序列号,就会存在安全问题,因为他们可以轻松伪造数据包注入 TCP 流。

Thereafter, for every byte transmitted the sequence number will increment by 1. The ACK field is the sequence number from the other side, sent back to acknowledge reception.

此后,对于传输的每个字节,序列号将增加 1。ACK 字段是来自另一端的序列号,发送回确认接收。

RFC 793, the original TCP protocol specification, can be of great help.

RFC 793是最初的 TCP 协议规范,可以提供很大帮助。

回答by zainee khan

I have the same job to do. Firstly the initial seq# will be generated randomly(0-4294967297). Then the receiver will count the length of the data it received and send the ACK of seq# + length = xto the sender. The sequence will then be x and the sender will send the data. Similarly the receiver will count the length x + length = yand send the ACK as yand so on... Its how the the seq/ack is generated...

我有同样的工作要做。首先初始 seq# 将随机生成(0-4294967297)。然后接收方会计算它接收到的数据的长度并向发送seq# + length = x方发送ACK 。序列将是 x 并且发送方将发送数据。类似地,接收器将计算长度x + length = y并发送 ACKy等等......它是如何生成 seq/ack 的......

If you want to show it practically try to sniff a packet in Wireshark and follow the TCP stream and see the scenario...

如果您想实际展示它,请尝试在 Wireshark 中嗅探数据包并跟踪 TCP 流并查看场景...

回答by Mark Brackett

If I understand you correctly - you're trying to mount a TCP SEQ prediction attack. If that's the case, you'll want to study the specifics of your target OS's Initial Sequence Numbergenerator.

如果我理解正确的话-您正在尝试发起 TCP SEQ 预测攻击。如果是这种情况,您将需要研究目标操作系统的初始序列号生成器的细节。

There were widely publicized vulnerabilties in pretty much all the major OS'swrt their ISN generators being predictable. I haven't followed the fallout closely, but my understanding is that most vendors released patches to randomize their ISN increments.

几乎所有主要操作系统都存在广泛宣传的漏洞,因为它们的 ISN 生成器是可预测的。我没有密切关注后果,但我的理解是大多数供应商发布了补丁来随机化他们的 ISN 增量

回答by Milan

Seems that the rest of the answers explained pretty much all about where to find detailed and official information about ACK's, namely TCP RFC

似乎其余的答案几乎解释了在哪里可以找到有关 ACK 的详细和官方信息,即TCP RFC

Here's a more practical and "easy understood" page that I found when I was doing similar implementations that may also help TCP Analysis - Section 2: Sequence & Acknowledgement Numbers

这是我在做类似实现时发现的一个更实用且“易于理解”的页面,这也可能有助于TCP 分析 - 第 2 节:序列和确认号

回答by John Ellinwood

RFC 793section 3.3 covers sequence numbers. Last time I wrote code at that level, I think we just kept a one-up counter for sequence numbers that persisted.

RFC 793第 3.3 节涵盖序列号。上次我在那个级别编写代码时,我认为我们只是为持续存在的序列号保留了一个递增计数器。

回答by tvanfosson

These values reference the expected offsets of the start of the payload for the packet relative to the initial sequence number for the connection.

这些值引用了数据包的有效负载开始相对于连接的初始序列号的预期偏移量。

Reference

参考

Sequence number (32 bits) – has a dual role If the SYN flag is set, then this is the initial sequence number. The sequence number of the actual first data byte will then be this sequence number plus 1. If the SYN flag is not set, then this is the sequence number of the first data byte

Acknowledgement number (32 bits) – if the ACK flag is set then the value of this field is the next expected byte that the receiver is expecting.

序列号(32 位)——具有双重作用 如果设置了 SYN 标志,则这是初始序列号。实际第一个数据字节的序列号将是这个序列号加 1。如果没有设置 SYN 标志,那么这是第一个数据字节的序列号

确认编号(32 位)——如果设置了 ACK 标志,则该字段的值是接收器期望的下一个期望字节。

回答by Kazimieras Aliulis

Numbers are randomly generated from both sides, then increased by number of octets (bytes) send.

数字是从双方随机生成的,然后增加发送的八位字节(字节)的数量。

回答by sipwiz

The sequence numbers increment after a connection is established. The initial sequence number on a new connection is ideally chosen at random but a lot of OS's have some semi-random algorithm. The RFC's are the best place to find out more TCP RFC.

建立连接后,序列号会增加。理想情况下,新连接上的初始序列号是随机选择的,但许多操作系统都有一些半随机算法。RFC 是了解更多TCP RFC的最佳场所。