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.
The only thing that I cannot figure out is how the seq / ack numbers are determined.
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.
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
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
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.
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.
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.
RFC 793, the original TCP protocol specification, can be of great help.
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 = x
to the sender. The sequence will then be x and the sender will send the data. Similarly the receiver will count the lengthx + length = y
and send the ACK asy
and so on... Its how the the seq/ack is generated...If you want to show it practically try to sniff a packet in Wireshark and follow the TCP stream and see the scenario...
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
Numbers are randomly generated from both sides, then increased by number of octets (bytes) send.