HackTricks
Search…
Pentesting
Powered By GitBook
143,993 - Pentesting IMAP

Internet Message Access Protocol

As its name implies, IMAP allows you to access your email messages wherever you are; much of the time, it is accessed via the Internet. Basically, email messages are stored on servers. Whenever you check your inbox, your email client contacts the server to connect you with your messages. When you read an email message using IMAP, you aren't actually downloading or storing it on your computer; instead, you are reading it off of the server. As a result, it's possible to check your email from several different devices without missing a thing.
By default, the IMAP protocol works on two ports:
    Port 143 - this is the default IMAP non-encrypted port
    Port 993 - this is the port you need to use if you want to connect using IMAP securely
1
PORT STATE SERVICE REASON
2
143/tcp open imap syn-ack
Copied!

Banner grabbing

1
nc -nv <IP> 143
2
openssl s_client -connect <IP>:993 -quiet
Copied!

NTLM Auth - Information disclosure

If the server supports NTLM auth (Windows) you can obtain sensitive info (versions):
1
[email protected]: telnet example.com 143
2
* OK The Microsoft Exchange IMAP4 service is ready.
3
>> a1 AUTHENTICATE NTLM
4
+
5
>> TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=
6
+ TlRMTVNTUAACAAAACgAKADgAAAAFgooCBqqVKFrKPCMAAAAAAAAAAEgASABCAAAABgOAJQAAAA9JAEkAUwAwADEAAgAKAEkASQBTADAAMQABAAoASQBJAFMAMAAxAAQACgBJAEkAUwAwADEAAwAKAEkASQBTADAAMQAHAAgAHwMI0VPy1QEAAAAA
Copied!
Or automate this with nmap plugin imap-ntlm-info.nse

Syntax

1
Login
2
A1 LOGIN username password
3
Values can be quoted to enclose spaces and special characters. A " must then be escape with a \
4
A1 LOGIN "username" "password"
5
6
List Folders/Mailboxes
7
A1 LIST "" *
8
A1 LIST INBOX *
9
A1 LIST "Archive" *
10
11
Create new Folder/Mailbox
12
A1 CREATE INBOX.Archive.2012
13
A1 CREATE "To Read"
14
15
Delete Folder/Mailbox
16
A1 DELETE INBOX.Archive.2012
17
A1 DELETE "To Read"
18
19
Rename Folder/Mailbox
20
A1 RENAME "INBOX.One" "INBOX.Two"
21
22
List Subscribed Mailboxes
23
A1 LSUB "" *
24
25
Status of Mailbox (There are more flags than the ones listed)
26
A1 STATUS INBOX (MESSAGES UNSEEN RECENT)
27
28
Select a mailbox
29
A1 SELECT INBOX
30
31
List messages
32
A1 FETCH 1:* (FLAGS)
33
A1 UID FETCH 1:* (FLAGS)
34
35
Retrieve Message Content
36
A1 FETCH 2 body[text]
37
A1 FETCH 2 all
38
A1 UID FETCH 102 (UID RFC822.SIZE BODY.PEEK[])
39
40
Close Mailbox
41
A1 CLOSE
42
43
Logout
44
A1 LOGOUT
Copied!
From here

Evolution

1
apt install evolution
Copied!

CURL

Basic navigation is possible with CURL, but the documentation is light on details so checking the source is recommended for precise details.
    1.
    Listing mailboxes (imap command LIST "" "*")
    1
    $ curl -k 'imaps://1.2.3.4/' --user user:pass
    Copied!
    2.
    Listing messages in a mailbox (imap command SELECT INBOX and then SEARCH ALL)
    1
    $ curl -k 'imaps://1.2.3.4/INBOX?ALL' --user user:pass
    Copied!
    The result of this search is a list of message indicies.
    Its also possible to provide more complex search terms. e.g. searching for drafts with password in mail body:
    1
    $ curl -k 'imaps://1.2.3.4/Drafts?TEXT password' --user user:pass
    Copied!
    A nice overview of the search terms possible is located here.
    3.
    Downloading a message (imap command SELECT Drafts and then FETCH 1 BODY[])
    1
    $ curl -k 'imaps://1.2.3.4/Drafts;MAILINDEX=1' --user user:pass
    Copied!
    The mail index will be the same index returned from the search operation.
It is also possible to use UID (unique id) to access messages, however it is less conveniant as the search command needs to be manually formatted. E.g.
1
$ curl -k 'imaps://1.2.3.4/INBOX' -X 'UID SEARCH ALL' --user user:pass
2
$ curl -k 'imaps://1.2.3.4/INBOX;UID=1' --user user:pass
Copied!
Also, possible to download just parts of a message, e.g. subject and sender of first 5 messages (the -v is required to see the subject and sender):
1
$ curl -k 'imaps://1.2.3.4/INBOX' -X 'FETCH 1:5 BODY[HEADER.FIELDS (SUBJECT FROM)]' --user user:pass -v 2>&1 | grep '^<'
Copied!
Although, its probably cleaner to just write a little for loop:
1
for m in {1..5}; do
2
echo $m
3
curl "imap://1.2.3.4/INBOX;MAILINDEX=$m;SECTION=HEADER.FIELDS%20(SUBJECT%20FROM)" --user user:pass
4
done
Copied!

Shodan

    port:143 CAPABILITY
    port:993 CAPABILITY

HackTricks Automatic Commands

1
Protocol_Name: IMAP #Protocol Abbreviation if there is one.
2
Port_Number: 143,993 #Comma separated if there is more than one.
3
Protocol_Description: Internet Message Access Protocol #Protocol Abbreviation Spelled out
4
5
Entry_1:
6
Name: Notes
7
Description: Notes for WHOIS
8
Note: |
9
As its name implies, IMAP allows you to access your email messages wherever you are; much of the time, it is accessed via the Internet. Basically, email messages are stored on servers. Whenever you check your inbox, your email client contacts the server to connect you with your messages. When you read an email message using IMAP, you aren't actually downloading or storing it on your computer; instead, you are reading it off of the server. As a result, it's possible to check your email from several different devices without missing a thing.
10
11
https://book.hacktricks.xyz/pentesting/pentesting-imap
12
13
Entry_2:
14
Name: Banner Grab
15
Description: Banner Grab 143
16
Command: nc -nv {IP} 143
17
18
Entry_3:
19
Name: Secure Banner Grab
20
Description: Banner Grab 993
21
Command: openssl s_client -connect {IP}:993 -quiet
Copied!
Last modified 2mo ago