Library smtp

Simple Mail Transfer Protocol (SMTP) operations.

Copyright © Same as Nmap--See https://nmap.org/book/man-legal.html

Source: https://svn.nmap.org/nmap/nselib/smtp.lua

Script Arguments

smtp.domain

The domain to be returned by get_domain, overriding the target's own domain name.

Functions

check_reply (cmd, reply)

Checks the SMTP server reply to see if it supports the previously sent SMTP command.

connect (host, port, opts)

Connects to the SMTP server based on the provided options.

datasend (socket, data)

Sends data to the SMTP server.

ehlo (socket, domain)

Sends the EHLO command to the SMTP server.

get_auth_mech (response)

Gets the authentication mechanisms that are listed in the response of the client's EHLO command.

get_domain (host)

Returns a domain to be used in the SMTP commands that need it.

help (socket)

Sends the HELP command to the SMTP server.

login (socket, username, password, mech)

Attempts to authenticate with the SMTP server. The supported authentication mechanisms are: LOGIN, PLAIN, CRAM-MD5, DIGEST-MD5 and NTLM.

mail (socket, address, esmtp_opts)

Sends the MAIL command to the SMTP server.

query (socket, cmd, data, lines)

Queries the SMTP server for a specific service.

quit (socket)

Sends the QUIT command to the SMTP server, and closes the socket.

recipient (socket, address)

Sends the RCPT command to the SMTP server.

reset (socket)

Sends the RSET command to the SMTP server.

starttls (socket)

Switches the plain text connection to be protected by the TLS protocol by using the SMTP STARTTLS command.

verify (socket, mailbox)

Sends the VRFY command to verify the validity of a mailbox.

Functions

check_reply (cmd, reply)

Checks the SMTP server reply to see if it supports the previously sent SMTP command.

Parameters

cmd
The SMTP command that was sent to the server
reply
The SMTP server reply

Return values:

  1. true if the reply indicates that the SMTP command was processed by the server correctly, or false on failures.
  2. message The reply returned by the server on success, or an error message on failures.
connect (host, port, opts)

Connects to the SMTP server based on the provided options.

Parameters

host
The host table
port
The port table
opts
The connection option table, possible options: ssl: try to connect using TLS timeout: generic timeout value recv_before: receive data before returning lines: a minimum number of lines to receive

Return values:

  1. socket The socket descriptor, or nil on errors
  2. response The response received on success and when the recv_before is set, or the error message on failures.
datasend (socket, data)

Sends data to the SMTP server.

This function will automatically adds <CRLF>.<CRLF> at the end. On network errors or if the SMTP command fails, the connection will be closed and the socket cleared.

Parameters

socket
connected to server.
data
to be sent.

Return values:

  1. true on success, or false on failures.
  2. response returned by the SMTP server on success, or an error message on failures.
ehlo (socket, domain)

Sends the EHLO command to the SMTP server.

On network errors or if the SMTP command fails, the connection will be closed and the socket cleared.

Parameters

socket
connected to server
domain
to use in the EHLO command.

Return values:

  1. true on success, or false on failures.
  2. response returned by the SMTP server on success, or an error message on failures.
get_auth_mech (response)

Gets the authentication mechanisms that are listed in the response of the client's EHLO command.

Parameters

response
The response of the client's EHLO command.

Return value:

An array of authentication mechanisms on success, or nil when it can't find authentication.
get_domain (host)

Returns a domain to be used in the SMTP commands that need it.

If the user specified one through the script argument smtp.domain this function will return it. Otherwise it will try to find the domain from the typed hostname and from the rDNS name. If it still can't find one it will return the nmap.scanme.org by default.

Parameters

host
The host table

Return value:

The hostname to be used by the different SMTP commands.
help (socket)

Sends the HELP command to the SMTP server.

On network errors or if the SMTP command fails, the connection will be closed and the socket cleared.

Parameters

socket
connected to server

Return values:

  1. true on success, or false on failures.
  2. response returned by the SMTP server on success, or an error message on failures.
login (socket, username, password, mech)

Attempts to authenticate with the SMTP server. The supported authentication mechanisms are: LOGIN, PLAIN, CRAM-MD5, DIGEST-MD5 and NTLM.

Parameters

socket
connected to server.
username
SMTP username.
password
SMTP password.
mech
Authentication mechanism.

Return values:

  1. true on success, or false on failures.
  2. response returned by the SMTP server on success, or an error message on failures.
mail (socket, address, esmtp_opts)

Sends the MAIL command to the SMTP server.

On network errors or if the SMTP command fails, the connection will be closed and the socket cleared.

Parameters

socket
connected to server.
address
of the sender.
esmtp_opts
The additional ESMTP options table, possible values: size: a decimal value to represent the message size in octets. ret: include the message in the DSN, should be 'FULL' or 'HDRS'. envid: envelope identifier, printable characters that would be transmitted along with the message and included in the failed DSN. transid: a globally unique case-sensitive value that identifies this particular transaction.

Return values:

  1. true on success, or false on failures.
  2. response returned by the SMTP server on success, or an error message on failures.
query (socket, cmd, data, lines)

Queries the SMTP server for a specific service.

This is a low level function that can be used to have more control over the data exchanged. On network errors the socket will be closed. This function automatically adds <code>CRLF<code> at the end.

Parameters

socket
connected to the server
cmd
The SMTP cmd to send to the server
data
The data to send to the server
lines
The minimum number of lines to receive, default value: 1.

Return values:

  1. true on success, or nil on failures.
  2. response The returned response from the server on success, or an error message on failures.
quit (socket)

Sends the QUIT command to the SMTP server, and closes the socket.

Parameters

socket
connected to server.
recipient (socket, address)

Sends the RCPT command to the SMTP server.

On network errors or if the SMTP command fails, the connection will be closed and the socket cleared.

Parameters

socket
connected to server.
address
of the recipient.

Return values:

  1. true on success, or false on failures.
  2. response returned by the SMTP server on success, or an error message on failures.
reset (socket)

Sends the RSET command to the SMTP server.

On network errors or if the SMTP command fails, the connection will be closed and the socket cleared.

Parameters

socket
connected to server.

Return values:

  1. true on success, or false on failures.
  2. response returned by the SMTP server on success, or an error message on failures.
starttls (socket)

Switches the plain text connection to be protected by the TLS protocol by using the SMTP STARTTLS command.

The socket will be reconnected by using SSL. On network errors or if the SMTP command fails, the connection will be closed and the socket cleared.

Parameters

socket
connected to server.

Return values:

  1. true on success, or nil on failures.
  2. message On success this will contain the SMTP server response to the client's STARTTLS command, or an error message on failures.
verify (socket, mailbox)

Sends the VRFY command to verify the validity of a mailbox.

On network errors or if the SMTP command fails, the connection will be closed and the socket cleared.

Parameters

socket
connected to server.
mailbox
to verify.

Return values:

  1. true on success, or false on failures.
  2. response returned by the SMTP server on success, or an error message on failures.