diff --git a/scripts/smtp-command-timing.nse b/scripts/smtp-command-timing.nse new file mode 100644 index 000000000..92e8d8442 --- /dev/null +++ b/scripts/smtp-command-timing.nse @@ -0,0 +1,67 @@ +local shortport = require "shortport" +local smtp = require "smtp" +local nmap = require "nmap" + +description = [[ +Measures response times for selected SMTP commands +to characterize server response behavior. +]] + +author = "Aravind M" +license = "Same as Nmap--See https://nmap.org/book/man-legal.html" +categories = {"discovery", "safe"} + +portrule = shortport.port_or_service( + {25, 587}, + {"smtp", "submission"} +) + +action = function(host, port) + local options = { + timeout = 10000, + recv_before = true, + ssl = false + } + + local socket, err = smtp.connect(host, port, options) + + if not socket then + return "Connection failed: " .. tostring(err) + end + + local status, response = smtp.ehlo( + socket, + smtp.get_domain(host) + ) + + if not status then + return "EHLO failed: " .. tostring(response) + end + + local results = {} + + for _, command in ipairs({"NOOP", "RSET"}) do + local start_time = nmap.clock_ms() + local ok, reply = smtp.query(socket, command) + local elapsed = nmap.clock_ms() - start_time + + if ok then + table.insert(results, string.format( + "%s: %d ms; response: %s", + command, + elapsed, + (reply or ""):gsub("[\r\n]+", " ") + )) + else + table.insert(results, string.format( + "%s failed: %s", + command, + tostring(reply) + )) + end + end + + smtp.quit(socket) + + return table.concat(results, "\n") +end diff --git a/scripts/smtp-response-scanner.nse b/scripts/smtp-response-scanner.nse new file mode 100644 index 000000000..4c6884832 --- /dev/null +++ b/scripts/smtp-response-scanner.nse @@ -0,0 +1,62 @@ +local nmap = require "nmap" +local shortport = require "shortport" + +description = [[ +Scans an SMTP service and records the response codes returned +for selected standard SMTP commands. +]] + +author = "Aravind M" +license = "Same as Nmap" +categories = {"discovery", "safe"} + +portrule = shortport.port_or_service({25, 587}, "smtp") + +action = function(host, port) + + local socket = nmap.new_socket() + socket:set_timeout(5000) + + local status = socket:connect(host.ip, port.number) + + if not status then + return "Could not connect to SMTP service" + end + + local output = {} + + -- Read SMTP banner + local _, banner = socket:receive_lines(1) + if banner then + table.insert(output, "Banner: " .. banner:gsub("\r?\n", "")) + end + + -- Commands to scan + local commands = { + "EHLO localhost", + "NOOP", + "HELP", + "VRFY test" + } + + for _, command in ipairs(commands) do + + socket:send(command .. "\r\n") + + local _, response = socket:receive_lines(1) + + if response then + local code = response:match("^(%d%d%d)") + + table.insert(output, + command .. ": " .. (code or "Response received")) + else + table.insert(output, command .. ": No response") + end + end + + socket:send("QUIT\r\n") + socket:close() + + return table.concat(output, "\n") +end