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.
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)
- 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:
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
| Feature | FTP | TFTP | SFTP |
|---|---|---|---|
| Transport / port | TCP 21 (+ data channel) | UDP 69 (then a dynamic port) | TCP 22 (SSH) |
| Authentication | Login and password (in plain text) | None | SSH password or key |
| Encryption | None (in FTPS — TLS) | None | Yes, everything |
| Folder listing, delete, rename | Present | No — only read/write | Present |
| Typical use | Old hosting services, public archives | Router/switch configuration, IP phones and network boot (PXE) | Server administration, secure file exchange |
| Command | Role | Reply code | Meaning |
|---|---|---|---|
| (connect) | Connect to TCP 21 | 220 | Server is ready (greeting on connect) |
USER aziz | Login | 331 | Login accepted, password needed |
PASS **** | Password | 230 / 530 | Login successful / wrong login or password |
PWD, CWD folder | Current folder, change folder | 257 / 250 | Current folder shown / folder changed |
PASV / PORT h1,h2,h3,h4,p1,p2 | Data channel mode | 227 / 200 | Switched to passive mode (with an address) / command completed |
LIST | Folder listing | 150 | Data channel is being opened |
RETR file / STOR file | Download / upload | 226 / 550 | Transfer finished, data channel closed / file not found or no permission |
TYPE I / TYPE A | Binary / text mode | 200 | Command completed |
QUIT | Quit | 221 | Goodbye |
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 --->.
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.
- 220 — greeting
A TCP connection to port 21 was opened and the server introduced itself.
- USER → 331, PASS → 230
Login and password. Note: they travelled over the network unencrypted — visible in Wireshark with the
ftpfilter. - 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.
- 150 → data → 226
For every
lsorgeta 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
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
- 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). - 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).
- 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.
- 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).
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
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
| Error | Consequence | Correct answer |
|---|---|---|
| Treating SFTP and FTPS as the same thing | Wrong port and client settings | SFTP is SSH, 22; FTPS is FTP + TLS, 21 (or 990) |
| Using active mode behind NAT | Login works, ls/get hang | Passive mode |
Transferring an image or archive in TYPE A (ASCII) mode | Line-ending characters get replaced and the file is corrupted | Use binary (TYPE I) for binary files |
| Leaving a TFTP server open to the Internet | No login — anyone can read the configurations | Only on a dedicated management network, with an ACL |
| Swapping p1 and p2 when calculating the port | Connecting to the wrong port | port = p1 × 256 + p2 (p1 is the high byte) |
| Using the university password on plain FTP | The password travels across the network in plain text | SFTP/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 correct230 Kirildi, otherwise530 Login xato.- If not logged in, any command other than USER, PASS, QUIT →
530 Avval kiring. PWD→257 "/current".CWD folder(onlylab,..or/) →250 OK; if it does not exist550 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 folder226 fayl yuborildi, otherwise550 Fayl yo‘q.QUIT→221 Xayrand 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.
| Criterion | Points |
|---|---|
| The practical work is fully done, results (screenshots or code) are present | 2 |
| Protocol commands, codes and ports are explained correctly | 1 |
| The security aspect (encryption, mode choice) is analyzed | 1 |
| The write-up is orderly, the conclusion is well-founded | 1 |
Summary
Your lesson results
Your results are saved in this browser. Read all the sections and complete the exercises and assignments.