summaryrefslogtreecommitdiffstats
path: root/scripts/smtp-open-relay.nse
blob: 040d232de334aeefecb6b07ad527ea4d66c4d6aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
local nmap = require "nmap"
local shortport = require "shortport"
local smtp = require "smtp"
local stdnse = require "stdnse"
local string = require "string"
local table = require "table"

description = [[
Attempts to relay mail by issuing a predefined combination of SMTP commands. The goal
of this script is to tell if a SMTP server is vulnerable to mail relaying.

An SMTP server that works as an open relay, is a email server that does not verify if the
user is authorised to send email from the specified email address. Therefore, users would
be able to send email originating from any third-party email address that they want.

The checks are done based in combinations of MAIL FROM and RCPT TO commands. The list is
hardcoded in the source file. The script will output all the working combinations that the
server allows if nmap is in verbose mode otherwise the script will print the number of
successful tests. The script will not output if the server requires authentication.

If debug is enabled and an error occurs while testing the target host, the error will be
printed with the list of any combinations that were found prior to the error.
]]

---
-- @usage
-- nmap --script smtp-open-relay.nse [--script-args smtp-open-relay.domain=<domain>,smtp-open-relay.ip=<address>,...] -p 25,465,587 <host>
--
-- @output
-- Host script results:
-- | smtp-open-relay: Server is an open relay (1/16 tests)
-- |_MAIL FROM:<antispam@insecure.org> -> RCPT TO:<relaytest@insecure.org>
--
-- @args smtp.domain or smtp-open-relay.domain Define the domain to be used in the anti-spam tests and EHLO command (default
-- is nmap.scanme.org)
-- @args smtp-open-relay.ip Use this to change the IP address to be used (default is the target IP address)
-- @args smtp-open-relay.from Define the source email address to be used (without the domain, default is
-- antispam)
-- @args smtp-open-relay.to Define the destination email address to be used (without the domain, default is
-- relaytest)

-- changelog
-- 2007-05-16 Arturo 'Buanzo' Busleiman <buanzo@buanzo.com.ar>
--   + Added some strings to return in different places
--   * Changed "HELO www.[ourdomain]" to "EHLO [ourdomain]"
--   * Fixed some API differences
--   * The "ourdomain" variable's contents are used instead of hardcoded "insecure.org". Settable by the user.
--   * Fixed tags -> categories (reported by Jasey DePriest to nmap-dev)
-- 2009-09-20 Duarte Silva <duarte.silva@serializing.me>
--   * Rewrote the script
--   + Added documentation and some more comments
--   + Parameter to define the domain to be used instead of "ourdomain" variable
--   + Parameter to define the IP address to be used instead of the target IP address
--   * Script now detects servers that enforce authentication
--   * Changed script categories from demo to discovery and intrusive
--   * Renamed "spamtest" strings to "antispam"
-- 2010-02-20 Duarte Silva <duarte.silva@serializing.me>
--   * Renamed script parameters to follow the new naming convention
--   * Fixed problem with broken connections
--   * Changed script output to show all the successful tests
--   * Changed from string concatenation to string formatting
--   + External category
--   + Now the script will issue the QUIT message as specified in the SMTP RFC
-- 2010-02-27 Duarte Silva <duarte.silva@serializing.me>
--   + More information in the script description
--   + Script will output the reason for failed commands (at the connection level)
--   * If some combinations were already found before an error, the script will report them
-- 2010-03-07 Duarte Silva <duarte.silva@serializing.me>
--   * Fixed socket left open when receive_lines function call fails
--   * Minor comments changes
-- 2010-03-14 Duarte Silva <duarte.silva@serializing.me>
--   * Made the script a little more verbose
-- 2011-06-03
--   * Rewrite the script to use the smtp.lua library.

author = "Arturo 'Buanzo' Busleiman"
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"
categories = {"discovery","intrusive","external"}


portrule = shortport.port_or_service({ 25, 465, 587 },
  { "smtp", "smtps", "submission" })

---Gets the user specified parameters to be used in the tests.
--
--@param host Target host (used for the ip parameter default value)
--@return Domain, from, to and ip to be used in the tests
function get_parameters(host)
  -- call smtp.get_domain() without the host table to use the
  -- 'nmap.scanme.org' host name, we are scanning for open relays.
  local domain = stdnse.get_script_args('smtp-open-relay.domain') or
  smtp.get_domain()

  local from = stdnse.get_script_args('smtp-open-relay.from') or "antispam"

  local to = stdnse.get_script_args('smtp-open-relay.to') or "relaytest"

  local ip = stdnse.get_script_args('smtp-open-relay.ip') or host.ip

  return domain, from, to, ip
end

function go(host, port)
  local options = {
    timeout = 10000,
    recv_before = true,
    ssl = true,
  }

  local result, status, index = {}

  local domain, from, to, ip = get_parameters(host)

  local socket, response = smtp.connect(host, port, options)
  if not socket then
    return false, string.format("Couldn't establish connection on port %i",
      port.number)
  end

  local srvname = string.match(response, "%d+%s([%w]+[%w%.-]*)")

  local status, response = smtp.ehlo(socket, domain)
  if not status then
    return status, response
  end

  if not srvname then
    srvname = string.match(response, "%d+%-([%w]+[%w%.-]*)")
  end

  -- Antispam tests.
  local tests = {
    {
      from = "",
      to = string.format("%s@%s", to, domain)
    },
    {
      from = string.format("%s@%s", from, domain),
      to = string.format("%s@%s", to, domain)
    },
    {
      from = string.format("%s@%s", from, srvname),
      to = string.format("%s@%s", to, domain)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("%s@%s", to, domain)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("%s%%%s@[%s]", to, domain, ip)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("%s%%%s@%s", to, domain, srvname)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("\"%s@%s\"", to, domain)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("\"%s%%%s\"", to, domain)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("%s@%s@[%s]", to, domain, ip)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("\"%s@%s\"@[%s]", to, domain, ip)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("%s@%s@%s", to, domain, srvname)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("@[%s]:%s@%s", ip, to, domain)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("@%s:%s@%s", srvname, to, domain)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("%s!%s", domain, to)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("%s!%s@[%s]", domain, to, ip)
    },
    {
      from = string.format("%s@[%s]", from, ip),
      to = string.format("%s!%s@%s", domain, to, srvname)
    },
  }

  -- This function is used when something goes wrong with the connection.
  -- It makes sure that if it found working combinations before the error
  -- occurred, they will be returned. If the debug flag is enabled the
  -- error message will be appended to the combinations list.
  local failure = function(message)
    if #result > 0 then
      table.insert(result, message)
      return true, result
    else
      return false, message
    end
  end

  for index = 1, #tests do
    status, response = smtp.reset(socket)
    if not status then
      if string.match(response, "530") then
        return false, "Server isn't an open relay, authentication needed"
      end
      return failure(response)
    end

    status, response = smtp.query(socket, "MAIL",
      string.format("FROM:<%s>",
      tests[index]["from"]))
    -- If this command fails to be sent, then something went
    -- wrong with the connection.
    if not status then
      return failure(string.format("Failed to issue %s command (%s)",
        tests[index]["from"], response))
    end

    if string.match(response, "530") then
      smtp.quit(socket)
      return false, "Server isn't an open relay, authentication needed"
    elseif smtp.check_reply("MAIL", response) then
      -- Lets try to actually relay.
      status, response = smtp.query(socket, "RCPT",
        string.format("TO:<%s>",
        tests[index]["to"]))
      if not status then
        return failure(string.format("Failed to issue %s command (%s)",
          tests[index]["to"], response))
      end

      if string.match(response, "530") then
        smtp.quit(socket)
        return false, "Server isn't an open relay, authentication needed"
      elseif smtp.check_reply("RCPT", response) then
        -- Save the working from and to combination.
        table.insert(result,
          string.format("MAIL FROM:<%s> -> RCPT TO:<%s>",
          tests[index]["from"], tests[index]["to"]))
      end
    end
  end

  smtp.quit(socket)
  return true, result
end

action = function(host, port)
  local status, result = go(host, port)

  -- The go function returned false, this means that the result is
  -- a simple error message.
  if not status then
    return result
  else
    -- Combinations were found. If verbosity is active, the script
    -- will print all the successful tests. Otherwise it will only
    -- print the conclusion.
    if #result > 0 then
      local final = {}
      table.insert(final,
        string.format("Server is an open relay (%i/16 tests)",
        (#result)))

      if nmap.verbosity() > 1 then
        for index, test in ipairs(result) do
          table.insert(final, test)
        end
      end

      return table.concat(final, "\n ")
    end

    return "Server doesn't seem to be an open relay, all tests failed"
  end
end