Computer Networks / Practical class / Practical class 17
Practical class · Computer Networks

The FTP, TFTP and SFTP protocols

Uploading a lab report to the department server, backing up a router’s configuration file, moving a website to a hosting provider — all of this is the work of file transfer protocols. Today you will meet three “brothers”: classic FTP (two channels, active and passive modes), the very simple TFTP (over UDP, for network devices) and the secure SFTP (inside SSH). You will play through an FTP session in a simulator and analyze protocol commands in Python.

:ik-kitob: Practical class 17 ⏱ 2 academic hours (80 minutes) 🚇 Packet simulators ⚡ A file is a passenger too
17

Lesson goals and plan

By the end of the lesson you will…

  • you can explain FTP’s control channel (21) and data channel (20 or dynamic) and its main commands
  • you can explain the difference between active and passive modes and the problems they have with firewalls/NAT
  • you can calculate the IP and port number from PORT and PASV replies
  • you can read FTP reply codes (1xx–5xx)
  • you can explain TFTP’s UDP 69, 512-byte blocks and its stop-and-wait (lock-step) mechanism
  • you can compare SFTP, FTPS and FTP in terms of security and choose the right one for a task

Plan (according to the syllabus)

  1. The FTP, TFTP and SFTP protocols

Think of FTP as two phone lines: on one you talk about “what shall we do?” (the control channel), on the other the file itself arrives (the data channel). The whole difference between active and passive mode is who makes the second call :ik-quti:

Why do you connect to an FTP server over the university Wi-Fi, yet the folder listing just “hangs”?

The login and password went through, “230 Login successful” appeared — but after the ls command nothing happens. The reason: FTP sends the file, and even the folder listing, over a separate connection. In active mode the server tries to open this connection to your computer — and NAT and the firewall will not let it in. Today you will see this problem in a simulator and understand how passive mode solves it.

Key terms

Theory reminder

Table 17.1. The three protocols at a glance
FeatureFTPTFTPSFTP
Transport / portTCP 21 (+ data channel)UDP 69 (then a dynamic port)TCP 22 (SSH)
AuthenticationLogin and password (in plain text)NoneSSH password or key
EncryptionNone (in FTPS — TLS)NoneYes, everything
Folder listing, delete, renamePresentNo — only read/writePresent
Typical useOld hosting services, public archivesRouter/switch configuration, IP phones and network boot (PXE)Server administration, secure file exchange
Table 17.2. Main FTP commands and reply codes
CommandRoleReply codeMeaning
(connect)Connect to TCP 21220Server is ready (greeting on connect)
USER azizLogin331Login accepted, password needed
PASS ****Password230 / 530Login successful / wrong login or password
PWD, CWD folderCurrent folder, change folder257 / 250Current folder shown / folder changed
PASV / PORT h1,h2,h3,h4,p1,p2Data channel mode227 / 200Switched to passive mode (with an address) / command completed
LISTFolder listing150Data channel is being opened
RETR file / STOR fileDownload / upload226 / 550Transfer finished, data channel closed / file not found or no permission
TYPE I / TYPE ABinary / text mode200Command completed
QUITQuit221Goodbye

The first digit of the reply code is the most important: 1 — the process has started, wait; 2 — completed successfully; 3 — more information is needed; 4 — a temporary error (try again); 5 — a permanent error.

The FTP, TFTP and SFTP protocols

Three sample tasks: reading an FTP session from the command line, comparing active and passive modes in a simulator, and working with TFTP and SFTP.

Sample task 1. Reading an FTP session

We connect to the department’s training server with the built-in Windows ftp client (it supports only active mode) or a graphical client such as FileZilla. In the session below debug mode is on — the commands the client sends are shown with --->.

cmd — ftp 192.168.10.20
C:\Users\Talaba> ftp 192.168.10.20
Connected to 192.168.10.20.
220 (vsFTPd 3.0.5)
User (192.168.10.20:(none)): aziz
---> USER aziz
331 Please specify the password.
Password:
---> PASS ********
230 Login successful.
ftp> ls
---> PORT 192,168,10,105,200,10
200 PORT command successful. Consider using PASV.
---> NLST
150 Here comes the directory listing.
hisobot_lab5.docx
topologiya.pkt
226 Directory send OK.
ftp> get topologiya.pkt
---> RETR topologiya.pkt
150 Opening BINARY mode data connection for topologiya.pkt (48213 bytes).
226 Transfer complete.
ftp: 48213 bytes received in 0.05Seconds 964.26Kbytes/sec.
ftp> bye
---> QUIT
221 Goodbye.
  1. 220 — greeting

    A TCP connection to port 21 was opened and the server introduced itself.

  2. USER → 331, PASS → 230

    Login and password. Note: they travelled over the network unencrypted — visible in Wireshark with the ftp filter.

  3. PORT 192,168,10,105,200,10

    In active mode the client announced its address: IP = 192.168.10.105, port = 200 × 256 + 10 = 51210. The server connects to this address from its own port 20.

  4. 150 → data → 226

    For every ls or get a new data connection is opened and closed after the transfer.

Sample task 2. Active and passive mode simulator

The student is behind the NAT/firewall of a home router, and the server is at the department. Choose the mode and the firewall state, then send the LIST command. Watch which side initiates the connection (the arrow direction).

:ik-papka: FTP: active and passive mode

Mode:
:ik-noutbuk: Client192.168.1.105 (internal)external: 203.0.113.5
control: client → server:21
NAT / FW
 
:ik-server: FTP server198.51.100.2021 / 20 / 50000–50100
data channel
 
Choose a mode and press “Send LIST”.

Active mode (PORT)

  • The server opens the data connection (from port 20).
  • An incoming connection must be allowed on the client side — it does not work behind NAT/firewall.
  • Only 20 and 21 are needed on the server side.

Passive mode (PASV)

  • The client opens the data connection.
  • Works from home/university networks — the default in today’s clients.
  • A range of passive ports must be opened in the server firewall.

Sample task 3. TFTP and SFTP

TFTP is used mainly with network devices: for example, backing up a Cisco router’s configuration to a TFTP server.

Cisco IOS — copying the configuration to a TFTP serverRouter# copy running-config tftp:
Address or name of remote host []? 192.168.10.5
Destination filename [router-confg]? R1-2026-11-28.cfg
!!
1432 bytes copied in 0.312 secs
  1. RRQ / WRQ (UDP 69)

    The client sends the server’s port 69 a read request (Read Request, opcode 1) or a write request (Write Request, opcode 2): the file name and the mode (octet — binary, netascii — text).

  2. New port

    The server sends its reply from a different, random port — the rest of the exchange takes place between this pair of ports (this is why TFTP also struggles with firewalls and NAT).

  3. DATA (3) and ACK (4)

    The file is sent in numbered 512-byte blocks. After each block an ACK is awaited (lock-step). If no ACK arrives, the block is sent again.

  4. Last block

    A block smaller than 512 bytes signals the end of the file. If the file size is a multiple of 512, an empty 0-byte block is sent at the end. In case of an error — ERROR (opcode 5).

SFTP works inside an SSH connection: a single TCP 22 connection, everything (the password and the files) is encrypted. Windows 10/11 has a built-in sftp client (OpenSSH).

cmd — sftp aziz@192.168.10.20
C:\Users\Talaba> sftp aziz@192.168.10.20
The authenticity of host '192.168.10.20' can't be established.
ED25519 key fingerprint is SHA256:Vq3n0Jb4…
Are you sure you want to continue connecting (yes/no)? yes
aziz@192.168.10.20's password:
Connected to 192.168.10.20.
sftp> ls
hisobot_lab5.docx   topologiya.pkt
sftp> put lab6.pkt
Uploading lab6.pkt to /home/aziz/lab6.pkt
lab6.pkt                         100%   51KB   2.1MB/s   00:00
sftp> bye
Security

The “key fingerprint” question on the first connection is your chance to verify the server’s authenticity. If the warning “REMOTE HOST IDENTIFICATION HAS CHANGED” appears later for the same server, it may be a MITM attack — tell the administrator immediately.

Test runs: first predict, then check

Independent tasks

7 tasks: calculating, building and analyzing protocol messages. Use input() without a prompt.

Common mistakes

ErrorConsequenceCorrect answer
Treating SFTP and FTPS as the same thingWrong port and client settingsSFTP is SSH, 22; FTPS is FTP + TLS, 21 (or 990)
Using active mode behind NATLogin works, ls/get hangPassive mode
Transferring an image or archive in TYPE A (ASCII) modeLine-ending characters get replaced and the file is corruptedUse binary (TYPE I) for binary files
Leaving a TFTP server open to the InternetNo login — anyone can read the configurationsOnly on a dedicated management network, with an ACL
Swapping p1 and p2 when calculating the portConnecting to the wrong portport = p1 × 256 + p2 (p1 is the high byte)
Using the university password on plain FTPThe password travels across the network in plain textSFTP/FTPS, a separate password

Boss level

Boss level: mini FTP server

A virtual FTP server. User: aziz, password: adu2026. File system: inside / there is a lab folder and a readme.txt file; inside /lab there are topologiya.pkt and hisobot.docx. Input: the number of commands n and n commands. For each command print one line of reply:

  • At the start of the session (before the first command) — 220 Xush kelibsiz.
  • USER name → 331 Parol kerak. PASS p → if the previous USER was aziz and the password is correct 230 Kirildi, otherwise 530 Login xato.
  • If not logged in, any command other than USER, PASS, QUIT → 530 Avval kiring.
  • PWD → 257 "/current". CWD folder (only lab, .. or /) → 250 OK; if it does not exist 550 Papka yo‘q.
  • LIST → 226 + the names in the current folder in alphabetical order, separated by a comma and a space.
  • RETR file → if the file is in the current folder 226 fayl yuborildi, otherwise 550 Fayl yo‘q.
  • QUIT → 221 Xayr and the remaining commands are not executed. Unknown command → 502 Buyruq qo‘llanmaydi.

+60 XP

Reinforcement

Review questions

Why does FTP use two channels?

The control channel (21) stays open for commands and replies during the whole session; the data channel is opened and closed separately for each file or listing.

Explain the difference between active and passive modes.

In active mode the client announces an address with PORT and the server connects to it from port 20; in passive mode the server announces a port with PASV and the client connects to it.

Why does active mode not work behind NAT?

The client’s private address cannot be reached from outside, and the firewall blocks the incoming connection.

How is the port in PORT/PASV calculated?

From the last two of the six numbers: port = p1 × 256 + p2.

What does the first digit of an FTP reply code mean?

1 — started, 2 — success, 3 — more information needed, 4 — temporary error, 5 — permanent error.

How does TFTP work and where is it used?

An RRQ/WRQ to UDP 69, then 512-byte DATA blocks and ACKs; the last block is smaller than 512. Used for router configurations, IP phones and PXE boot.

What is the difference between SFTP and FTPS?

SFTP is a separate protocol inside SSH, with a single port-22 connection; FTPS is FTP encrypted with TLS, and the two channels are kept.

What is FTP’s main security weakness?

The login, password and files are transmitted unencrypted — they can be intercepted on the network.

Independent work

The independent-study topic in the course syllabus: “Email and file transfer protocols: SMTP, POP3, IMAP, FTP and SFTP.” (6 hours). Complete the practical work for your variant and document every step with a screenshot.

Assessment criteria (individual assignment)
CriterionPoints
The practical work is fully done, results (screenshots or code) are present2
Protocol commands, codes and ports are explained correctly1
The security aspect (encryption, mode choice) is analyzed1
The write-up is orderly, the conclusion is well-founded1

Summary

FTPTCP, two channels: control 21, data — separate; the password in plain text.
Active modePORT — the server connects to the client; a problem behind NAT.
Passive modePASV — the client connects to the server; today’s standard.
Port formulah1,h2,h3,h4,p1,p2 → port = p1·256 + p2.
Reply codes1 wait, 2 done, 3 more needed, 4 temporary, 5 permanent error.
TFTPUDP 69, no login, 512-byte block + ACK; for network devices.
SFTPInside SSH, port 22, everything encrypted; do not confuse it with FTPS.

Your lesson results

Your results are saved in this browser. Read all the sections and complete the exercises and assignments.

My personal notes