tryhackme: Packets & Frames: TCP/IP

Transfer Control Protocol (TCP) is a connection-oriented protocol requiring a TCP three-way-handshake to establish a connection. TCP provides reliable data transfer, flow control and congestion control. Higher-level protocols such as HTTP, POP3, IMAP and SMTP use TCP

----------------------------------------------------------------------------------------------------------------------------

The TCP/IP protocol consists of four layers and is arguably just a summarized version of the OSI model. These layers are:


- Application

- Transport

- Internet

- Network Interface


Information is added to each layer of the TCP model as the piece of data  traverses it (encapsulation / decapsulation)


TCP must establish a connection between both a client and a device acting as a server before data is sent.


This process is named the Three-way handshake.

----------------------------------------------------------------------------------------------------------------------------


TCP packets contain various sections of information known as headers that are added from encapsulation.


some of the crucial headers:


1. Source Port.


This value is the port opened by the sender to send the TCP packet from. 


This value is chosen randomly (ports: 0-65535 that are not already in use at the time)


2. Destination Port. 


This value is the port number that an application or service is running on the remote host ( aka the one that is receiving the data )


for example: a webserver running on port 80. Unlike the source port, this value is not chosen randomly.


3. Source IP.


This is the IP address of the device that is sending the packet. 


4. Destination IP.


This is the IP address of the device that the packet is destined for. 


5. Sequence Number. 


When a connection occurs, the first piece of data transmitted is given a random number.


6. Acknowledgement Number. 


After a piece of data has been given a sequence number, the number for the next piece of data will have the sequence number +1.


7. Checksum.


This value is what gives TCP integrity. A mathematical calculation is made where the output is remembered. When the receiving device performs the mathematical calculation, the data must be corrupt if the output is different from what was sent.


8. Data. 


This header is where the data at. 

i.e. bytes of a file that is being transmitted, is stored.


9. Flag.


This header determines how the packet should be handled by either device during the handshake process.


Flags will determine specific behavior's.


----------------------------------------------------------------------------------------------------------------------------

Three-way handshake:


This process used to establish a connection between two devices. The three-way handshake communicates using a few special messages: 


1. SYN.


A SYN message is the initial packet sent by a client during the handshake. 


This packet used to initiate a connection and synchronize the two devices together. 


2. SYN/ACK.


This packet is sent by the receiving device ( could be a server ) to acknowledge the synchronization attempt from the client.


3. ACK.


The acknowledgement packet can be used by either the client or server to acknowledge that a series of messages/packets have been successfully received. 


4. DATA.


Once a connection has been established, data ( such as bytes of a file ) is sent via the "DATA" message.


5. FIN


This packet is used to cleanly (Properly ) close the connection after it has been complete.


6.  RST


This packet abruptly ends all communication.


This is the last resort and indicates there was some problem during the process. 


For example: if the service or application is not working correctly, or the system has faults such as low resources.

----------------------------------------------------------------------------------------------------------------------------

ISN: Initial Number Sequence

Any sent data is given a random number sequence and is reconstructed using this number sequence incremented by 1.

----------------------------------------------------------------------------------------------------------------------------

- Three-way handshake connection:


device 1 SYN ================>      device 2 

device 1 <============= SYN/ACK     device 2

device 1 ACK =================>     device 2


- TCP closing a connection:


* TCP will close a connection once a device has determined that the other device has successfully received all of the data. *


* Since TCP reserves system resources on a device, it is best practice to close TCP connections as soon as possible. *


* To initiate the closure of a TCP connection the device will send a "FIN" packet to the other device. The other device will have to acknowledge the packet. 

device 1 FIN =================>     device 2 

device 1 <================= ACK     device 2

device 1 <================= FIN     device 2

device 1 ACK =================>     device 2


Thanks for reading! 👨‍💻

Roger

Comments

Popular posts from this blog

Common Network Commands: Ping

Common Network Commands: Route

John The Ripper