mirror of
https://github.com/nmap/nmap.git
synced 2026-01-04 05:39:01 +00:00
Re-indent some libs and scripts, change 4 to 2-space indent
Mostly found with:
for i in nselib/*.lua scripts/*.nse; do
echo $(perl -lne 'BEGIN{$a=$p=0}next unless $_;/^(\s*)/;' \
-e '$l=length$1;next if$l==$p;$a+=(abs($l-$p)-$a)/$.;' \
-e '$p=$l;END{print$a}' $i) $i
done | sort -nr
And indented with: https://gist.github.com/bonsaiviking/8845871
whois-ip.nse was particularly mangled (probably my fault due to using
vim's built-in indentation script, but it could be structured better)
This commit is contained in:
@@ -54,14 +54,14 @@ categories = {"auth","external","intrusive"}
|
||||
|
||||
|
||||
portrule = shortport.port_or_service({ 25, 465, 587 },
|
||||
{ "smtp", "smtps", "submission" })
|
||||
{ "smtp", "smtps", "submission" })
|
||||
|
||||
STATUS_CODES = {
|
||||
ERROR = 1,
|
||||
NOTPERMITTED = 2,
|
||||
VALID = 3,
|
||||
INVALID = 4,
|
||||
UNKNOWN = 5
|
||||
ERROR = 1,
|
||||
NOTPERMITTED = 2,
|
||||
VALID = 3,
|
||||
INVALID = 4,
|
||||
UNKNOWN = 5
|
||||
}
|
||||
|
||||
---Counts the number of occurrences in a table. Helper function
|
||||
@@ -71,14 +71,14 @@ STATUS_CODES = {
|
||||
-- @param what What element to count
|
||||
-- @return Number of occurrences
|
||||
function table_count(from, what)
|
||||
local result = 0
|
||||
local result = 0
|
||||
|
||||
for index, item in ipairs(from) do
|
||||
if item == what then
|
||||
result = result + 1
|
||||
end
|
||||
for index, item in ipairs(from) do
|
||||
if item == what then
|
||||
result = result + 1
|
||||
end
|
||||
return result
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
---Creates a new table from a source without the duplicates. Helper
|
||||
@@ -87,15 +87,15 @@ end
|
||||
-- @param from Source table
|
||||
-- @return New table without the duplicates
|
||||
function table_unique(from)
|
||||
local result = {}
|
||||
local result = {}
|
||||
|
||||
for index, item in ipairs(from) do
|
||||
if (table_count(result, item) == 0) then
|
||||
result[#result + 1] = item
|
||||
end
|
||||
for index, item in ipairs(from) do
|
||||
if (table_count(result, item) == 0) then
|
||||
result[#result + 1] = item
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
return result
|
||||
end
|
||||
|
||||
---Get the method or methods to be used. If the user didn't specify any
|
||||
@@ -103,32 +103,32 @@ end
|
||||
--
|
||||
-- @return A table containing the methods to try
|
||||
function get_method()
|
||||
local result = {}
|
||||
local result = {}
|
||||
|
||||
local methods = stdnse.get_script_args('smtp-enum-users.methods')
|
||||
if methods and type(methods) == "table" then
|
||||
-- For each method specified.
|
||||
for _, method in ipairs(methods) do
|
||||
-- Are the elements of the argument valid methods.
|
||||
local upper = string.upper(method)
|
||||
local methods = stdnse.get_script_args('smtp-enum-users.methods')
|
||||
if methods and type(methods) == "table" then
|
||||
-- For each method specified.
|
||||
for _, method in ipairs(methods) do
|
||||
-- Are the elements of the argument valid methods.
|
||||
local upper = string.upper(method)
|
||||
|
||||
if (upper == "RCPT") or (upper == "EXPN") or
|
||||
(upper == "VRFY") then
|
||||
table.insert(result, upper)
|
||||
else
|
||||
return false, method
|
||||
end
|
||||
end
|
||||
if (upper == "RCPT") or (upper == "EXPN") or
|
||||
(upper == "VRFY") then
|
||||
table.insert(result, upper)
|
||||
else
|
||||
return false, method
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- The methods weren't specified.
|
||||
if #result == 0 then
|
||||
result = { "RCPT", "VRFY", "EXPN" }
|
||||
else
|
||||
result = table_unique(result)
|
||||
end
|
||||
-- The methods weren't specified.
|
||||
if #result == 0 then
|
||||
result = { "RCPT", "VRFY", "EXPN" }
|
||||
else
|
||||
result = table_unique(result)
|
||||
end
|
||||
|
||||
return true, result
|
||||
return true, result
|
||||
end
|
||||
|
||||
---Generic function to perform user discovery.
|
||||
@@ -139,44 +139,44 @@ end
|
||||
-- @param domain Domain to use in the command
|
||||
-- @return Status and depending on the code, a error message
|
||||
function do_gnrc(socket, command, username, domain)
|
||||
local combinations = {
|
||||
string.format("%s", username),
|
||||
string.format("%s@%s", username, domain)
|
||||
}
|
||||
local combinations = {
|
||||
string.format("%s", username),
|
||||
string.format("%s@%s", username, domain)
|
||||
}
|
||||
|
||||
for index, combination in ipairs(combinations) do
|
||||
-- Lets try to issue the command.
|
||||
local status, response = smtp.query(socket, command, combination)
|
||||
for index, combination in ipairs(combinations) do
|
||||
-- Lets try to issue the command.
|
||||
local status, response = smtp.query(socket, command, combination)
|
||||
|
||||
-- If this command fails to be sent, then something
|
||||
-- went wrong with the connection.
|
||||
if not status then
|
||||
return STATUS_CODES.ERROR,
|
||||
string.format("Failed to issue %s %s command (%s)\n",
|
||||
command, combination, response)
|
||||
end
|
||||
|
||||
if string.match(response, "^530") then
|
||||
-- If the command failed, check if authentication is
|
||||
-- needed because all the other attempts will fail.
|
||||
return STATUS_CODES.AUTHENTICATION
|
||||
elseif string.match(response, "^502") or
|
||||
string.match(response, "^252") or
|
||||
string.match(response, "^550") then
|
||||
-- The server doesn't implement the command or it is disallowed.
|
||||
return STATUS_CODES.NOTPERMITTED
|
||||
elseif smtp.check_reply(command, response) then
|
||||
-- User accepted.
|
||||
if nmap.verbosity() > 1 then
|
||||
return STATUS_CODES.VALID,
|
||||
string.format("%s, %s", command, username)
|
||||
else
|
||||
return STATUS_CODES.VALID, username
|
||||
end
|
||||
end
|
||||
-- If this command fails to be sent, then something
|
||||
-- went wrong with the connection.
|
||||
if not status then
|
||||
return STATUS_CODES.ERROR,
|
||||
string.format("Failed to issue %s %s command (%s)\n",
|
||||
command, combination, response)
|
||||
end
|
||||
|
||||
return STATUS_CODES.INVALID
|
||||
if string.match(response, "^530") then
|
||||
-- If the command failed, check if authentication is
|
||||
-- needed because all the other attempts will fail.
|
||||
return STATUS_CODES.AUTHENTICATION
|
||||
elseif string.match(response, "^502") or
|
||||
string.match(response, "^252") or
|
||||
string.match(response, "^550") then
|
||||
-- The server doesn't implement the command or it is disallowed.
|
||||
return STATUS_CODES.NOTPERMITTED
|
||||
elseif smtp.check_reply(command, response) then
|
||||
-- User accepted.
|
||||
if nmap.verbosity() > 1 then
|
||||
return STATUS_CODES.VALID,
|
||||
string.format("%s, %s", command, username)
|
||||
else
|
||||
return STATUS_CODES.VALID, username
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return STATUS_CODES.INVALID
|
||||
end
|
||||
|
||||
---Verify if a username is valid using the EXPN command (wrapper
|
||||
@@ -187,7 +187,7 @@ end
|
||||
-- @param domain Domain to use in the command
|
||||
-- @return Status and depending on the code, a error message
|
||||
function do_expn(socket, username, domain)
|
||||
return do_gnrc(socket, "EXPN", username, domain)
|
||||
return do_gnrc(socket, "EXPN", username, domain)
|
||||
end
|
||||
|
||||
---Verify if a username is valid using the VRFY command (wrapper
|
||||
@@ -198,7 +198,7 @@ end
|
||||
-- @param domain Domain to use in the command
|
||||
-- @return Status and depending on the code, a error message
|
||||
function do_vrfy(socket, username, domain)
|
||||
return do_gnrc(socket, "VRFY", username, domain)
|
||||
return do_gnrc(socket, "VRFY", username, domain)
|
||||
end
|
||||
|
||||
issued_from = false
|
||||
@@ -214,59 +214,59 @@ issued_from = false
|
||||
-- @param domain Domain to use in the command
|
||||
-- @return Status and depending on the code, a error message
|
||||
function do_rcpt(socket, username, domain)
|
||||
local status, response
|
||||
if not issued_from then
|
||||
local status, response
|
||||
if not issued_from then
|
||||
-- Lets try to issue MAIL FROM command.
|
||||
status, response = smtp.query(socket, "MAIL",
|
||||
string.format("FROM:<usertest@%s>", domain))
|
||||
string.format("FROM:<usertest@%s>", domain))
|
||||
|
||||
if not status then
|
||||
-- If this command fails to be sent, then something went wrong
|
||||
-- with the connection.
|
||||
return STATUS_CODES.ERROR,
|
||||
string.format("Failed to issue MAIL FROM:<usertest@%s> command (%s)",
|
||||
domain, response)
|
||||
elseif string.match(response, "^530") then
|
||||
-- If the command failed, check if authentication is needed
|
||||
-- because all the other attempts will fail.
|
||||
return STATUS_CODES.ERROR,
|
||||
"Couldn't perform user enumeration, authentication needed"
|
||||
elseif not smtp.check_reply("MAIL", response) then
|
||||
-- Only accept 250 code as success.
|
||||
return STATUS_CODES.NOTPERMITTED,
|
||||
"Server did not accept the MAIL FROM command"
|
||||
end
|
||||
end
|
||||
|
||||
status, response = smtp.query(socket, "RCPT",
|
||||
string.format("TO:<%s@%s>", username, domain))
|
||||
|
||||
if not status then
|
||||
return STATUS_CODES.ERROR,
|
||||
string.format("Failed to issue RCPT TO:<%s@%s> command (%s)",
|
||||
username, domain, response)
|
||||
elseif string.match(response, "^550") then
|
||||
-- 550 User Unknown
|
||||
return STATUS_CODES.UNKNOWN
|
||||
elseif string.match(response, "^553") then
|
||||
-- 553 Relaying Denied
|
||||
return STATUS_CODES.NOTPERMITTED
|
||||
-- If this command fails to be sent, then something went wrong
|
||||
-- with the connection.
|
||||
return STATUS_CODES.ERROR,
|
||||
string.format("Failed to issue MAIL FROM:<usertest@%s> command (%s)",
|
||||
domain, response)
|
||||
elseif string.match(response, "^530") then
|
||||
-- If the command failed, check if authentication is needed because
|
||||
-- all the other attempts will fail.
|
||||
return STATUS_CODES.AUTHENTICATION
|
||||
elseif smtp.check_reply("RCPT", response) then
|
||||
issued_from = true
|
||||
-- User is valid.
|
||||
if nmap.verbosity() > 1 then
|
||||
return STATUS_CODES.VALID, string.format("RCPT, %s", username)
|
||||
else
|
||||
return STATUS_CODES.VALID, username
|
||||
end
|
||||
-- If the command failed, check if authentication is needed
|
||||
-- because all the other attempts will fail.
|
||||
return STATUS_CODES.ERROR,
|
||||
"Couldn't perform user enumeration, authentication needed"
|
||||
elseif not smtp.check_reply("MAIL", response) then
|
||||
-- Only accept 250 code as success.
|
||||
return STATUS_CODES.NOTPERMITTED,
|
||||
"Server did not accept the MAIL FROM command"
|
||||
end
|
||||
end
|
||||
|
||||
status, response = smtp.query(socket, "RCPT",
|
||||
string.format("TO:<%s@%s>", username, domain))
|
||||
|
||||
if not status then
|
||||
return STATUS_CODES.ERROR,
|
||||
string.format("Failed to issue RCPT TO:<%s@%s> command (%s)",
|
||||
username, domain, response)
|
||||
elseif string.match(response, "^550") then
|
||||
-- 550 User Unknown
|
||||
return STATUS_CODES.UNKNOWN
|
||||
elseif string.match(response, "^553") then
|
||||
-- 553 Relaying Denied
|
||||
return STATUS_CODES.NOTPERMITTED
|
||||
elseif string.match(response, "^530") then
|
||||
-- If the command failed, check if authentication is needed because
|
||||
-- all the other attempts will fail.
|
||||
return STATUS_CODES.AUTHENTICATION
|
||||
elseif smtp.check_reply("RCPT", response) then
|
||||
issued_from = true
|
||||
return STATUS_CODES.INVALID
|
||||
-- User is valid.
|
||||
if nmap.verbosity() > 1 then
|
||||
return STATUS_CODES.VALID, string.format("RCPT, %s", username)
|
||||
else
|
||||
return STATUS_CODES.VALID, username
|
||||
end
|
||||
end
|
||||
|
||||
issued_from = true
|
||||
return STATUS_CODES.INVALID
|
||||
end
|
||||
|
||||
---Script function that does all the work.
|
||||
@@ -275,108 +275,108 @@ end
|
||||
-- @param port Target port
|
||||
-- @return The user accounts or a error message.
|
||||
function go(host, port)
|
||||
-- Get the current usernames list from the file.
|
||||
local status, nextuser = unpwdb.usernames()
|
||||
-- Get the current usernames list from the file.
|
||||
local status, nextuser = unpwdb.usernames()
|
||||
|
||||
if not status then
|
||||
return false, "Failed to read the user names database"
|
||||
if not status then
|
||||
return false, "Failed to read the user names database"
|
||||
end
|
||||
|
||||
local options = {
|
||||
timeout = 10000,
|
||||
recv_before = true,
|
||||
ssl = true,
|
||||
}
|
||||
local domain = stdnse.get_script_args('smtp-enum-users.domain') or
|
||||
smtp.get_domain(host)
|
||||
|
||||
local methods
|
||||
status, methods = get_method()
|
||||
|
||||
if not status then
|
||||
return false, string.format("Invalid method found, %s", methods)
|
||||
end
|
||||
|
||||
local socket, response = smtp.connect(host, port, options)
|
||||
|
||||
-- Failed connection attempt.
|
||||
if not socket then
|
||||
return false, string.format("Couldn't establish connection on port %i",
|
||||
port.number)
|
||||
end
|
||||
|
||||
status, response = smtp.ehlo(socket, domain)
|
||||
if not status then
|
||||
return status, response
|
||||
end
|
||||
|
||||
local result = {}
|
||||
|
||||
-- This function is used when something goes wrong with
|
||||
-- the connection. It makes sure that if it found users before
|
||||
-- the error occurred, they will be returned.
|
||||
local failure = function(message)
|
||||
if #result > 0 then
|
||||
table.insert(result, message)
|
||||
return true, result
|
||||
else
|
||||
return false, message
|
||||
end
|
||||
end
|
||||
|
||||
-- Get the first user to be tested.
|
||||
local username = nextuser()
|
||||
|
||||
for index, method in ipairs(methods) do
|
||||
while username do
|
||||
if method == "RCPT" then
|
||||
status, response = do_rcpt(socket, username, domain)
|
||||
elseif method == "VRFY" then
|
||||
status, response = do_vrfy(socket, username, domain)
|
||||
elseif method == "EXPN" then
|
||||
status, response = do_expn(socket, username, domain)
|
||||
end
|
||||
|
||||
if status == STATUS_CODES.NOTPERMITTED then
|
||||
-- Invalid method. Don't test anymore users with
|
||||
-- the current method.
|
||||
break
|
||||
elseif status == STATUS_CODES.VALID then
|
||||
-- User found, lets save it.
|
||||
table.insert(result, response)
|
||||
elseif status == STATUS_CODES.ERROR then
|
||||
-- An error occurred with the connection.
|
||||
return failure(response)
|
||||
elseif status == STATUS_CODES.AUTHENTICATION then
|
||||
smtp.quit(socket)
|
||||
return false, "Couldn't perform user enumeration, authentication needed"
|
||||
elseif status == STATUS_CODES.INVALID then
|
||||
table.insert(result,
|
||||
string.format("Method %s returned a unhandled status code.",
|
||||
method))
|
||||
break
|
||||
end
|
||||
username = nextuser()
|
||||
end
|
||||
|
||||
local options = {
|
||||
timeout = 10000,
|
||||
recv_before = true,
|
||||
ssl = true,
|
||||
}
|
||||
local domain = stdnse.get_script_args('smtp-enum-users.domain') or
|
||||
smtp.get_domain(host)
|
||||
|
||||
local methods
|
||||
status, methods = get_method()
|
||||
|
||||
if not status then
|
||||
return false, string.format("Invalid method found, %s", methods)
|
||||
-- No more users to test, don't test with other methods.
|
||||
if username == nil then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
local socket, response = smtp.connect(host, port, options)
|
||||
|
||||
-- Failed connection attempt.
|
||||
if not socket then
|
||||
return false, string.format("Couldn't establish connection on port %i",
|
||||
port.number)
|
||||
end
|
||||
|
||||
status, response = smtp.ehlo(socket, domain)
|
||||
if not status then
|
||||
return status, response
|
||||
end
|
||||
|
||||
local result = {}
|
||||
|
||||
-- This function is used when something goes wrong with
|
||||
-- the connection. It makes sure that if it found users before
|
||||
-- the error occurred, they will be returned.
|
||||
local failure = function(message)
|
||||
if #result > 0 then
|
||||
table.insert(result, message)
|
||||
return true, result
|
||||
else
|
||||
return false, message
|
||||
end
|
||||
end
|
||||
|
||||
-- Get the first user to be tested.
|
||||
local username = nextuser()
|
||||
|
||||
for index, method in ipairs(methods) do
|
||||
while username do
|
||||
if method == "RCPT" then
|
||||
status, response = do_rcpt(socket, username, domain)
|
||||
elseif method == "VRFY" then
|
||||
status, response = do_vrfy(socket, username, domain)
|
||||
elseif method == "EXPN" then
|
||||
status, response = do_expn(socket, username, domain)
|
||||
end
|
||||
|
||||
if status == STATUS_CODES.NOTPERMITTED then
|
||||
-- Invalid method. Don't test anymore users with
|
||||
-- the current method.
|
||||
break
|
||||
elseif status == STATUS_CODES.VALID then
|
||||
-- User found, lets save it.
|
||||
table.insert(result, response)
|
||||
elseif status == STATUS_CODES.ERROR then
|
||||
-- An error occurred with the connection.
|
||||
return failure(response)
|
||||
elseif status == STATUS_CODES.AUTHENTICATION then
|
||||
smtp.quit(socket)
|
||||
return false, "Couldn't perform user enumeration, authentication needed"
|
||||
elseif status == STATUS_CODES.INVALID then
|
||||
table.insert(result,
|
||||
string.format("Method %s returned a unhandled status code.",
|
||||
method))
|
||||
break
|
||||
end
|
||||
username = nextuser()
|
||||
end
|
||||
|
||||
-- No more users to test, don't test with other methods.
|
||||
if username == nil then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
smtp.quit(socket)
|
||||
return true, result
|
||||
smtp.quit(socket)
|
||||
return true, result
|
||||
end
|
||||
|
||||
action = function(host, port)
|
||||
local status, result = go(host, port)
|
||||
local status, result = go(host, port)
|
||||
|
||||
-- The go function returned true, lets check if it
|
||||
-- didn't found any accounts.
|
||||
if status and #result == 0 then
|
||||
return stdnse.format_output(true, "Couldn't find any accounts")
|
||||
end
|
||||
-- The go function returned true, lets check if it
|
||||
-- didn't found any accounts.
|
||||
if status and #result == 0 then
|
||||
return stdnse.format_output(true, "Couldn't find any accounts")
|
||||
end
|
||||
|
||||
return stdnse.format_output(true, result)
|
||||
return stdnse.format_output(true, result)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user