Skip to content

antony@notes:~/networking$ cat "TCP-IP-通訊協定.md"

TCP/IP 通訊協定

2022-06-04· networking ·網路基礎

TCP/IP 通訊協定

目錄 [TOC]


準備環境

下載WiresharkPortable64_3.6.3.paf.exe

下載連結,請點我

Wire 電線 shark 鯊魚 Wireshark 中文簡稱:線鯊

學習筆記本

TCP/IP學習筆記本

鳥哥基礎網路概念


Ethernet 乙太網路

電腦的網路系統分兩大類

  • 有線網路
    • 硬體規格:Ethernet
      • 組成:乙太網路卡、網路連接設備和網路線
        • 網路卡:重點在速度
        • 網路連接設備:讓很多電腦能夠互通傳遞資訊
        • 網路線:傳遞資訊
    • IP Address
      • 格式:由 Network ID + Host ID 組成
        Network IDHost ID
      • :::spoiler Ethernet 架構圖 :::
      • Network ID 由IP 位址和子網路遮罩做and運算得出
      • :::spoiler 為什麼要分Network ID和Host ID ? 因為Router負責幫我們在網路上傳遞訊息,為了不為難Router,於是我們把IP位址頭一組數字相同的IP放在一起,這樣Router就只要記同一堆電腦的第一組數字位置,而不需要記錄到每一台電腦的IP Address。 :::
    • ==電腦之間要溝通除了軟體層的IP Address還要有硬體層的Mac Address==
    • Mac Address
      • 定義:實體網路卡的出廠代號(16進位)
      • :::spoiler 檢查指令(windows):ipconfig /all
        C:\Users\ybean>ipconfig /all
        乙太網路卡 乙太網路:
        ...
          實體位址 . . . . . . . . . . . . .: C8-D9-D2-04-57-3D	
        ...
        乙太網路卡 VMware Network Adapter VMnet1:
        ...
          實體位址 . . . . . . . . . . . . .: 00-50-56-C0-00-01
        ...

        VMware的Mac Address開頭一定是00-50-56 :::

      • 電腦跟電腦之間溝通要透過Mac address,但是很難記怎麼辦? 答:靠ARP 通訊協定
      • 功能:根據IP Address,幫我們把資料傳給對方
    • ARP 通訊協定(Address Resolution Protocal)
      • 他負責把IP位址轉換成Mac Address
  • 無線網路
    • 辦公室、學校和手機會使用

一個網段要怎麼配置IP位址

口訣:去頭去尾中間切一半,前十給IP Server,後面5個給網路設備(Switch不會有),剩下的前面手動設定IP位址,後面的給DHCP Server配發

以下用J409教室的網段當範例:

120.96.143.128  Network ID
120.96.143.129 ~ 120.96.143.138 IP Server
120.96.143.139 ~ 120.96.143.190 (手動設定IP)
120.96.143.191 ~ 120.96.143.249 (DHCP Server)
120.96.143.250 ~ 120.96.143.254 Network Device (Router)
120.96.143.255  Broadcast

設定固定 IP 位址(Alpine linux)

$ sudo nano /etc/network/interfaces
auto lo
iface lo inet loopback

#auto eth0
#iface eth0 inet dhcp

auto eth0  (auto 的意思是作業系統一起來,就啟動這張網卡)
iface eth0 inet static       (iface 全名就是interface ,static 代表固定IP)
        address 120.96.143.180/25
        gateway 120.96.143.254  (Router的IP位址就是120.96.143.254)

重啟網路服務

$ sudo /etc/init.d/networking restart

在區域網路下,電腦的IP位址在相同的Network ID,就可以透過網路設備Switch互相溝通,Network ID的算法為IP位址跟遮罩作AND運算 TCP/IP屬於軟體層的標準,真正硬體層(Ethernet)在溝通靠的是Mac address :::spoiler 示意圖 :::

當我們電腦要上網到不同的Network id,電腦就要透過Router的IP位址,才能出去上網,而Router的IP位址就是Deafault Gateway,Router出去以後會透過海底電纜,到其他台Router傳送封包,再到另一個Router,到達我們的8.8.8.8這台主機,

Router(路由器)是走海底電纜傳輸至國外,會有多種路徑規劃選擇 而經過周轉的Router越少傳送速度越快,最少的就是最佳路徑

:::spoiler 示意圖 :::


網路等級

公有 IP

IP等級最低位址~最高位址
A0.0.0.0~127.255.255.255
B128.0.0.0~191.255.255.255
C192.0.0.0~223.255.255.2555

私有 IP


Address Resolution Protocol (ARP 通訊協定)

:::spoiler ARP 運作架構圖 :::

:::spoiler 第一步 線鯊圖 :::

當Host A要傳給Host B資料時, Host A主機會先檢查自己的ARP cache(存放在記憶體裡)有沒有Host B的MAC位址,如果沒有紀錄,

:::spoiler 第二部 線鯊圖 ::: 它就會進到第二部,在Switch上面廣播"ARP requset"的封包

  • ARP requset 內容:
    • Host A自己的IP address 和 MAC address
    • Host B的IP Address和MAC Address,但因為這時候不知道Mac Address,所以顯示都是00:00:00:00:00:00( 00:00:00:00:00:00 )

廣播的意思:連接在Switch上面的所有電腦都會收到 (同一個Network ID 上面)

第三部 Host B主機收到ARP request後,會在自己這邊的ARP cache紀錄Host A主機的IP Address 和MAC Address

:::spoiler 第四部 線鯊圖 ::: Host B主機會將自己的IP位址和MAC address位址傳給Host A

第五部 Host A主機把收到的Host B 主機的IP位址和MAC address紀錄起來

ARP實戰

登入m1虛擬機

C:\Users\ybean>ssh bigred@192.168.61.4

arp 命令在任何linux作業系統都是內建的

bigred@m1:~$ arp --help
BusyBox v1.34.1 (2022-04-04 10:19:27 UTC) multi-call binary.

Usage: arp
[-vn]   [-H HWTYPE] [-i IF] -a [HOSTNAME]
[-v]                [-i IF] -d HOSTNAME [pub]
[-v]    [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [temp]
[-v]    [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [netmask MASK] pub
[-v]    [-H HWTYPE] [-i IF] -Ds HOSTNAME IFACE [netmask MASK] pub

Manipulate ARP cache

        -a              Display (all) hosts
        -d              Delete ARP entry
        -s              Set new entry
        -v              Verbose
        -n              Don't resolve names
        -i IF           Network interface
        -D              Read HWADDR from IFACE
        -A,-p AF        Protocol family
        -H HWTYPE       Hardware address type

先檢查本機的ARP Cache紀錄

bigred@m1:~$ arp -a
x2 (192.168.61.1) at 00:50:56:c0:00:08 [ether]  on eth0
x3 (192.168.61.2) at 00:50:56:e0:f3:4a [ether]  on eth0

ping 另一台主機

bigred@m1:~$ ping 192.168.61.6
PING 192.168.61.6 (192.168.61.6) 56(84) bytes of data.
64 bytes from 192.168.61.6: icmp_seq=1 ttl=64 time=0.479 ms
64 bytes from 192.168.61.6: icmp_seq=2 ttl=64 time=0.382 ms
64 bytes from 192.168.61.6: icmp_seq=3 ttl=64 time=0.438 ms
64 bytes from 192.168.61.6: icmp_seq=4 ttl=64 time=0.833 ms

--- 192.168.61.6 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3108ms
rtt min/avg/max/mdev = 0.382/0.533/0.833/0.176 ms

再檢查本機的ARP紀錄

bigred@m1:~$ arp -a
x2 (192.168.61.1) at 00:50:56:c0:00:08 [ether]  on eth0
w1 (192.168.61.6) at 00:50:56:ab:00:06 [ether]  on eth0
x3 (192.168.61.2) at 00:50:56:e0:f3:4a [ether]  on eth0

我們再ping 另一台沒記錄過Mac Address的主機

bigred@m1:~$ ping 192.168.61.7
PING 192.168.61.7 (192.168.61.7) 56(84) bytes of data.
64 bytes from 192.168.61.7: icmp_seq=1 ttl=64 time=0.562 ms
64 bytes from 192.168.61.7: icmp_seq=2 ttl=64 time=0.786 ms
64 bytes from 192.168.61.7: icmp_seq=3 ttl=64 time=0.465 ms
64 bytes from 192.168.61.7: icmp_seq=4 ttl=64 time=0.949 ms

--- 192.168.61.7 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3092ms
rtt min/avg/max/mdev = 0.465/0.690/0.949/0.189 ms

快點兩下,啟動WiresharkPortable64_3.6.3.paf.exe,找到VMnet8那張網卡,因為他負責連接我們的那3台電腦

可以看第二步的廣播封包

硬體層: broadcast 它的Mac Address都是f

軟體層:

第四部的reply

ARP 攻擊

ARP Cache 的儲存時間(10年前的規格) Dynamic ARP cache entries update persist for 2-20 minutes depending on the system.
Windows 2003 Server = 10 minutes
Windows 2000 professional/Server = 10 minutes
Windows XP = 2 minutes
Solaris = 5 minutes

提問:ARP Cache的時間要設長還設短 答:在內網的話,要設長,原因是因為在內網不可能一直換網路卡,這樣可以讓ARP的封包降到最低,甚至不會產生。

bigred@m1:~$ arp -a
w2 (192.168.61.7) at 00:50:56:ab:00:07 [ether]  on eth0
x2 (192.168.61.1) at 00:50:56:c0:00:08 [ether]  on eth0
w1 (192.168.61.6) at 00:50:56:ab:00:06 [ether]  on eth0
x3 (192.168.61.2) at 00:50:56:e0:f3:4a [ether]  on eth0
bigred@m1:~$ sudo arp -d w2
bigred@m1:~$ sudo arp -d w1
bigred@m1:~$ sudo arp -d x2
bigred@m1:~$ sudo arp -d x3
bigred@m1:~$ arp -a
x2 (192.168.61.1) at 00:50:56:c0:00:08 [ether]  on eth0  ## 不理他

Add static entry to ARP table

添加靜態的Mac address,不會因為時間到就消失。

arp -s <ip address> <mac address>

範例: 將192.168.61.6對應的Mac Address設成00:22:33:44:55:66

bigred@m1:~$ sudo arp -s 192.168.61.6 00:22:33:44:55:66
bigred@m1:~$ arp -a
x2 (192.168.61.1) at 00:50:56:c0:00:08 [ether]  on eth0
w1 (192.168.61.6) at 00:22:33:44:55:66 [ether] PERM on eth0
x3 (192.168.61.2) at 00:50:56:e0:f3:4a [ether]  on eth0

發現ping 不到192.168.61.6這台電腦

bigred@m1:~$ ping 192.168.61.6
PING 192.168.61.6 (192.168.61.6) 56(84) bytes of data.
^C
--- 192.168.61.6 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3161ms

ping 外網

ping 8.8.8.8

bigred@m1:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=3.13 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=4.01 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=2.99 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=128 time=3.05 ms

--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 2.985/3.293/4.009/0.416 ms

發現ping 不通啦!!!

bigred@m1:~$ ping 192.168.61.6
PING 192.168.61.6 (192.168.61.6) 56(84) bytes of data.
^C
--- 192.168.61.6 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3161m

提問:為何ping 8.8.8.8,我的主機裡面沒有它的Mac Address?

bigred@m1:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=3.13 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=4.01 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=2.99 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=128 time=3.05 ms

--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 2.985/3.293/4.009/0.416 ms

檢查真的沒有8.8.8.8的Mac Address,為什麼!?

bigred@m1:~$ arp -a
x2 (192.168.61.1) at 00:50:56:c0:00:08 [ether]  on eth0
w1 (192.168.61.6) at 00:22:33:44:55:66 [ether] PERM on eth0
x3 (192.168.61.2) at 00:50:56:e0:f3:4a [ether]  on eth0

答:因為8.8.8.8的Network ID和我們的主機不同,所以不會觸發ARP通訊協定,但是IP要出去外網之前,一定會經過Default Gateway這個大門,所以我們這台主機的ARP Cache裡面會紀錄Default Gateway的Mac Address

bigred@m1:~$ arp -a
x2 (192.168.61.1) at 00:50:56:c0:00:08 [ether]  on eth0
w1 (192.168.61.6) at 00:22:33:44:55:66 [ether] PERM on eth0
## 下面這行就是Default Gateway的IP位址和它的Mac Address
x3 (192.168.61.2) at 00:50:56:e0:f3:4a [ether]  on eth0     

:::danger 以後設定機器時,在開機程序裡面埋一個bash script,設定靜態的Mac address,不但可以降低ARP 封包的產生,還可以避免被攻擊。 :::


IP Header 重要欄位

  • Source IP address

  • Destination IP address

  • Time-to-Live

    • Time to live
    • IP 的封包,只要經過一個Router,他的數字(TTL)就會減一 ,數字為0時,封包就不會再傳送

提問:ping 這個命令有沒有設定TTL 答:

bigred@m1:~$ ping --help
...
 -t <ttl>           define time to live
...

自行設定TTL = 3,代表只能跳躍3個Router

bigred@m1:~$ ping -t 3 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
From 120.96.98.121 icmp_seq=1 Time to live exceeded
From 120.96.98.121 icmp_seq=2 Time to live exceeded
From 120.96.98.121 icmp_seq=3 Time to live exceeded
From 120.96.98.121 icmp_seq=4 Time to live exceeded

--- 8.8.8.8 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3074ms

由以下可知,到8.8.8.8這台電腦,要經過12台Router

bigred@m1:~$ ping -t 12 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=3.16 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=4.07 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=4.14 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=128 time=3.76 ms

--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 3.160/3.783/4.142/0.387 ms

ICMP

ICMP這個封包,所有Router都會送,但通常這個功能會關閉,因為會造成網路癱瘓,但是企業內網的ICMP回應的功能可能還有打開。

UDP

提問:TCP與UDP的差別? 答:UDP 通訊協定的特性,丟了就跑,代表不會跟對方建立connection,也就是說對方如果沒有回應他就會再丟一次封包,Domain NameServer在使用,也就是名稱在查詢時,透過UDP,把名稱在查詢的封包丟給DNS Server,如果DNS Server沒有回應,我們的機器就會再送一次封包出去,總共會丟3次,如果都失敗,就判定連不到對方的DNS Server。

RIP 通訊協定和 SNMP 網路設備在監控用的通訊協定 都是通過UDP通訊協定,送出去有回應就繼續作業,如果沒有回應就繼續送,累積3次沒有回應就爆錯。

TCP通訊協定有一個必要條件一定要發生:叫做Handshake握手,意思就是說我要跟對方做連接一定會跟對方握手,也就代表等等資料再傳送時,一定保證可以送到對方手上。

SMTP 電子郵件的通訊協定一定會透過TCP/IP再傳 TCP/IP 在傳遞時一定很耗資源,因為他要handshake(握手)後,才會傳資料。 UDP就是我丟給你就等你,有回應我就做,沒有回應就丟3次。

SSH通訊協定一定要走TCP RDP(Remote Desktop Protocol)通訊協定一定要走TCP VNC也是走TCP

NTP時間同步的通訊協定走UDP


Socket

TCP上面有6萬多個插座,以SMTP這個網路服務,他會把他的整個資料溝通會插在第25port,也就是第25個插座,FTP插在21 port,

定義: 一個應用的通訊協定,一定會找TCP或UDP上面的插座,插進去,代表服務會透過TCP/IP做操作,講TCP一定會經過IP這層,有IP這層一定會透過實體層,以上就叫做Socket。 Socket要讓他通一定要提供IP Adress和port Number

分類:

  • TCP Socket,只要服務插在上面,代表資料一定會送達
  • UDP Socket,DNS 插在第53個插孔

以數學公式表示: TCP/UDP Socket = Port + IP

Unix domain socket

Unix domain Socket

  • IPC Socket
    • 運作方式一定在同一台機器

tags: 網路基礎