16 Aralık 2019 Pazartesi

TCP Handshake

Giriş
TCP three-way Handshake şöyledir. TCP'yi kapatırken four-way termination uygulanır.
Client ------SYN-----> Server
Client <---ACK/SYN---- Server
Client ------ACK-----> Server
3 tane paket göndermenin amacı her iki tarafın da birbirlerinin Sequence Number değerlerini bilmeleridir. Bu da 3 tane paket göndermekle olur.
Alice ---> Bob    SYNchronize with my Initial Sequence Number of X
Alice <--- Bob    I received your syn, I ACKnowledge that I am ready for [X+1]
Alice <--- Bob    SYNchronize with my Initial Sequence Number of Y
Alice ---> Bob    I received your syn, I ACKnowledge that I am ready for [Y+1]
Dolayısıyla yukarıdaki 3 paket şöyle de okunabilir.
Alice ---> Bob    SYN
Alice <--- Bob    ACK SYN
Alice ---> Bob        ACK
Neden 2 Paket Yetmiyor ?
Eğer iki paket gönderseydik yani şöyle olsaydı
Client ------SYN-----> Server
Client <-----ACK------ Server
Sadece server client'in Sequence Numarasını bilecekti. Server ACK almadığı için Client'ın kendi Sequence Numarasını bilip bilmediğinden emin olamayacaktı.

SYN Flood Saldırısı
İstemci sadece SYN paketini gönderir. Kaynak adres olarak rastgele bir değer kullanılır. Açıklaması şöyle.
A client opens a TCP connection by sending a SYN packet to a server. The server replies with a single SYN+ACK, and the client responds again with an ACK. Because of natural network latency, the server may wait a short time after sending SYN+ACK to the specified source address for an ACK reply, and this behavior is what a SYN flood exploits. Because the source address was spoofed, the reply will never come. If the server is waiting on enough fake connections that will never be completed, it will become unable to open any new connections, legitimate or not. This condition is called denial of service.

SYN flood attacks do not require the attacker receive a reply from the victim, so there is no need for the attacker to use its real source address. Spoofing the source address both improves anonymity by making it harder to track down the attacker, as well as making it more difficult for the victim to filter traffic based on IP. After all, if each packet used the same source address (whether spoofed or not), any decent firewall would quickly begin blocking all SYN packets from that address and the attack would fail.
Handshake Sonrası İstemcinin Gönderdiği İlk Veri
Şöyle olabilir. İlginç bir şekilde istemci ACK için kullandığı bilgiyi 4. satırdaki veri paketi için de kullanabilir.
#1   SN 0 -----> SERVER:80 -- SYN
#2   <----- SERVER:80 SN 0, AN 1 -- SYN ACK
#3   SN 1 AN 1 -----> SERVER:80 -- ACK
#4   SN 1 AN 1 -----> SERVER:80 -- PSH ACK(HTTP GET)
Kodlama
Biz programlarken bu 3 yönlü el sıkışma işlemini görmüyoruz. Sadece sunucu socket'e accept() çağrısında bulunuyoruz. Bu çağrı el sıkışma işlemini yapıp bize yeni bir socket dönüyor. Yeni socket te aslında sunucu socket ile aynı portu kullanıyor. Sunucu tarafında socketlere bakarsak 80 numaralı porta iki tane istemcinin bağlanmış olduğunu görebiliriz.
10.0.0.1:1234 - 192.168.1.1:80
10.0.0.2:5678 - 192.168.1.1:80
TCP Alanları
Şimdi TCP zarfındaki bu alanlara bakalım.

1. Sequence Number
Sequence Number ve Acknowledgement Number rastgele sayılardan başlarlar. Sequence ve Acknowledgement numaraları duplication ve reordering problemlerini çözmeye yararlar. Aynı sequence sayısına sahip iki paket gelirse, sonradan gelen dikkate alınmaz.

2. RST (Reset) Flag
Karşı tarafa bağlantının kapalı olduğunu belirtmek için gönderilir. Açıklaması şöyle
The RST is in response to receiving something on a connection that no longer exists. The connection was closed by FIN, and it was acknowledged, so the connection no longer exists, and then something was received on the closed connection, so a RST was sent.
3. SYN Flag
Bağlantı kurarken, bağlantıyı başlatan taraf gönderir. Bağlantı kabul edilmezse karşı taraf RST (reset) gönderir.

4. Maximum Segment Size (MSS)
Maximum Segment Size yazısına taşıdım

Yanlış Bayrak Kullanımı
El sıkışma esnasında veri taşınmaz. Amaç sadece session açmaktır.
SYN ve FIN bayrakları aynı anda kaldırılamaz. Hem bağlantıyı açmak hem de kapatmak istiyorum anlamına gelir.

(A to B)SYN

(B to A)ACK+SYN

(A to B) ACK (Ungraceful connection closure)
şeklinde bir şey görüyorsak birisi bağlandıktan sonra bağlantıyı kapatmış anlamına gelir. Portlarımızı tarayan bir program olabilir.

Hiç yorum yok:

Yorum Gönder