networking, updated

IMAP and POP3

IMAP and POP3 let mail clients fetch messages from a mailbox. IMAP keeps mail on the server and syncs folders; POP3 downloads and usually deletes. Both are text protocols with passwords in the clear unless TLS is used.

OSI layer
7 Application
Carried in
TCP
Ports
143/tcp (IMAP), 993/tcp (IMAPS), 110/tcp (POP3), 995/tcp (POP3S)
Specs
RFC 9051, RFC 3501, RFC 1939, RFC 8314
Wireshark filter
imap || pop
Encrypted
no

Packet structure

Both are line-based text protocols over TCP. IMAP prefixes each command with a client-chosen tag; the server answers with untagged lines (*) and finally a tagged OK, NO or BAD:

S: * OK [CAPABILITY IMAP4rev1 STARTTLS LOGINDISABLED] mail.example.com ready
C: a1 STARTTLS
S: a1 OK Begin TLS negotiation now
   ... TLS handshake, then capabilities again ...
C: a2 LOGIN alice "s3cret"
S: a2 OK [CAPABILITY IMAP4rev1 IDLE MOVE ...] Logged in
C: a3 SELECT INBOX
S: * 172 EXISTS
S: * OK [UIDVALIDITY 1698412345] UIDs valid
S: a3 OK [READ-WRITE] Select completed
C: a4 FETCH 172 (FLAGS BODY.PEEK[HEADER.FIELDS (FROM SUBJECT)])
S: * 172 FETCH (FLAGS (\Seen) BODY[...] {64}
S: From: Bob <bob@example.org>
S: Subject: Invoice
S: )
S: a4 OK Fetch completed
C: a5 LOGOUT

POP3 has no tags: one command, one +OK or -ERR reply.

S: +OK POP3 server ready
C: USER alice
S: +OK
C: PASS s3cret
S: +OK Logged in, 3 messages
C: LIST
S: +OK 3 messages
S: 1 2048
S: 2 15360
S: 3 912
S: .
C: RETR 1
S: +OK 2048 octets
S: ... message ...
S: .
C: DELE 1
C: QUIT
IMAP commandPurposePOP3 commandPurpose
LOGIN, AUTHENTICATELog in (AUTHENTICATE PLAIN / XOAUTH2 is Base64)USER + PASS, APOP, AUTHLog in
LIST, SELECT, EXAMINEFolders, open one (EXAMINE read-only)STAT, LIST, UIDLMailbox size, message list
SEARCH, FETCHFind and read messagesRETR, TOPRead a message (or its first lines)
STORE, COPY, MOVE, EXPUNGEFlags, copy, move, deleteDELEMark for deletion
APPENDUpload a message (sent items, drafts)
IDLEPush: wait for new mail

How it works

Mail arrives at the server by SMTP; the user’s client then fetches it:

Both support implicit TLS (993, 995, recommended by RFC 8314) and STARTTLS on the plain ports.

In Wireshark

FilterShows
imapIMAP on 143 (and 993 once decrypted)
popPOP3 on 110
imap.request.usernameIMAP LOGIN commands (Wireshark extracts user name and password)
pop.request.command == "PASS"POP3 passwords
imap.response.status == "NO"Failed IMAP commands (incl. logins)
pop.response.indicator == "-ERR"POP3 errors
imap.request contains "STARTTLS"Encryption upgrades
tcp.port in {993, 995}IMAPS and POP3S (TLS)

What normal looks like: clients on 993 (or 995) to your mail server, long-lived IMAP connections with IDLE. Clear-text logins on 143/110, many failed logins from one source, or logins from unusual countries to a cloud mailbox are signs of trouble. Port 143 or 110 to the internet from a workstation may be malware checking a mailbox for commands.

Security notes

References

§ home
~/knubertus/home/
1,1 Top