mirror of
https://github.com/nmap/nmap.git
synced 2026-11-28 10:30:14 +00:00
Session:authenticate() interpolated the username and password directly into the <authenticate> XML request. Credentials containing &, <, >, " or ' produced malformed XML and authentication failed. Escape those five characters before building the request and add unit tests covering each of them plus the request body. Fixes #3311
223 lines
6.8 KiB
Lua
223 lines
6.8 KiB
Lua
---
|
|
-- This library was written to ease interaction with OpenVAS Manager servers
|
|
-- using OMP (OpenVAS Management Protocol) version 2.
|
|
--
|
|
-- A very small subset of the protocol is implemented.
|
|
-- * Connection/authentication
|
|
-- * Targets enumeration
|
|
--
|
|
-- The library can also store accounts in the registry to share them between
|
|
-- scripts.
|
|
--
|
|
-- The complete protocol documentation is no longer available on the official
|
|
-- OpenVAS website. Here is the archived copy:
|
|
-- https://web.archive.org/web/20210304194034/http://www.openvas.org/omp-2-0.html
|
|
--
|
|
-- Sample use:
|
|
-- <code>
|
|
-- local session = omp2.Session:new()
|
|
-- local status, err = session:connect(host, port)
|
|
-- local status, err = session:authenticate(username, password)
|
|
-- ...
|
|
-- session:close()
|
|
-- </code>
|
|
--
|
|
-- @author Henri Doreau
|
|
-- @copyright Same as Nmap -- See https://nmap.org/book/man-legal.html
|
|
--
|
|
-- @args omp2.username The username to use for authentication.
|
|
-- @args omp2.password The password to use for authentication.
|
|
--
|
|
|
|
local nmap = require "nmap"
|
|
local stdnse = require "stdnse"
|
|
local table = require "table"
|
|
local unittest = require "unittest"
|
|
_ENV = stdnse.module("omp2", stdnse.seeall)
|
|
|
|
--- Escape the five XML special characters in text used as element content.
|
|
-- OMP credentials are interpolated into an XML request, so unescaped
|
|
-- characters can produce malformed XML or alter the meaning of the request.
|
|
-- @param s String to escape.
|
|
-- @return The escaped string.
|
|
local function xml_escape(s)
|
|
local escaped = s:gsub("&", "&")
|
|
escaped = escaped:gsub("<", "<")
|
|
escaped = escaped:gsub(">", ">")
|
|
escaped = escaped:gsub('"', """)
|
|
escaped = escaped:gsub("'", "'")
|
|
return escaped
|
|
end
|
|
|
|
local HAVE_SSL = false
|
|
|
|
if pcall(require,'openssl') then
|
|
HAVE_SSL = true
|
|
end
|
|
|
|
--- A Session class holds connection and interaction with the server
|
|
Session = {
|
|
|
|
--- Creates a new session object
|
|
new = function(self, socket)
|
|
|
|
local o = {}
|
|
setmetatable(o, self)
|
|
self.__index = self
|
|
|
|
o.username = nmap.registry.args["omp2.username"]
|
|
o.password = nmap.registry.args["omp2.password"]
|
|
o.socket = socket or nmap.new_socket()
|
|
|
|
return o
|
|
end,
|
|
|
|
--- Establishes the (SSL) connection to the remote server
|
|
connect = function(self, host, port)
|
|
if not HAVE_SSL then
|
|
return false, "The OMP2 module requires OpenSSL support"
|
|
end
|
|
|
|
return self.socket:connect(host, port, "ssl")
|
|
end,
|
|
|
|
--- Closes connection
|
|
close = function(self)
|
|
return self.socket:close()
|
|
end,
|
|
|
|
--- Attempts to authenticate on the current connection
|
|
authenticate = function(self, username, password)
|
|
local status, err, xmldata
|
|
|
|
status, err = self.socket:send("<authenticate><credentials>"
|
|
.. "<username>" .. xml_escape(username) .. "</username>"
|
|
.. "<password>" .. xml_escape(password) .. "</password>"
|
|
.. "</credentials></authenticate>")
|
|
|
|
if not status then
|
|
stdnse.debug1("ERROR: %s", err)
|
|
return false, err
|
|
end
|
|
|
|
status, xmldata = self.socket:receive()
|
|
if not status then
|
|
stdnse.debug1("ERROR: %s", xmldata)
|
|
return false, xmldata
|
|
end
|
|
|
|
return xmldata:match('status="200"')
|
|
end,
|
|
|
|
--- Lists targets defined on the remote server
|
|
ls_targets = function(self)
|
|
local status, err, xmldata
|
|
local res, target_names, target_hosts = {}, {}, {}
|
|
|
|
status, err = self.socket:send("<get_targets/>")
|
|
|
|
if not status then
|
|
stdnse.debug1("ERROR: %s", err)
|
|
return false, err
|
|
end
|
|
|
|
status, xmldata = self.socket:receive()
|
|
if not status then
|
|
stdnse.debug1("ERROR: %s", xmldata)
|
|
return false, xmldata
|
|
end
|
|
|
|
-- As NSE has no XML parser yet, we use regexp to extract the data from the
|
|
-- XML output. Targets are defined as a name and the corresponding host(s).
|
|
-- Thus we gather both and return an associative array, using names as keys
|
|
-- and hosts as values.
|
|
|
|
local i = 0
|
|
for name in xmldata:gmatch("<name>(.-)</name>") do
|
|
-- XXX this is hackish: skip the second and third "<name>" tags, as they
|
|
-- describe other components than the targets.
|
|
-- see: https://web.archive.org/web/20210304194034/http://www.openvas.org/omp-2-0.html#command_get_targets
|
|
if i % 3 == 0 then
|
|
table.insert(target_names, name)
|
|
end
|
|
i = i + 1
|
|
end
|
|
|
|
for hosts in xmldata:gmatch("<hosts>(.-)</hosts>") do
|
|
table.insert(target_hosts, hosts)
|
|
end
|
|
|
|
for i, name in ipairs(target_names) do
|
|
res[name] = target_hosts[i]
|
|
end
|
|
|
|
return res
|
|
end,
|
|
}
|
|
|
|
--- Registers OMP2 credentials for a given host
|
|
function add_account(host, username, password)
|
|
local hostreg = nmap.registry[host.ip]
|
|
if not hostreg then
|
|
hostreg = {}
|
|
nmap.registry[host.ip] = hostreg
|
|
end
|
|
|
|
hostreg.omp2accounts = hostreg.omp2accounts or {}
|
|
table.insert(hostreg.omp2accounts, {username = username, password = password})
|
|
end
|
|
|
|
--- Retrieves the list of accounts for a given host
|
|
function get_accounts(host)
|
|
local accounts = {}
|
|
local username = nmap.registry.args["omp2.username"]
|
|
local password = nmap.registry.args["omp2.password"]
|
|
|
|
if username and password then
|
|
table.insert(accounts, {username = username, password = password})
|
|
end
|
|
|
|
local hostreg = nmap.registry[host.ip]
|
|
for _, account in pairs(hostreg and hostreg.omp2accounts or {}) do
|
|
table.insert(accounts, account)
|
|
end
|
|
|
|
if #accounts > 0 then
|
|
return accounts
|
|
end
|
|
return nil
|
|
end
|
|
|
|
if not unittest.testing() then
|
|
return _ENV
|
|
end
|
|
|
|
test_suite = unittest.TestSuite:new()
|
|
local equal = unittest.equal
|
|
|
|
test_suite:add_test(equal(xml_escape("plain"), "plain"), "plain text unchanged")
|
|
test_suite:add_test(equal(xml_escape("a&b"), "a&b"), "ampersand escaped")
|
|
test_suite:add_test(equal(xml_escape("a<b"), "a<b"), "less-than escaped")
|
|
test_suite:add_test(equal(xml_escape("a>b"), "a>b"), "greater-than escaped")
|
|
test_suite:add_test(equal(xml_escape('a"b'), "a"b"), "double quote escaped")
|
|
test_suite:add_test(equal(xml_escape("a'b"), "a'b"), "apostrophe escaped")
|
|
test_suite:add_test(equal(xml_escape("&<>'\""), "&<>'""),
|
|
"all special characters escaped")
|
|
test_suite:add_test(equal(xml_escape("&"), "&amp;"),
|
|
"pre-escaped entity is escaped again")
|
|
|
|
-- Verify the actual request built by authenticate() uses the escaped values.
|
|
local sent
|
|
local mock_socket = {
|
|
send = function(self, data) sent = data; return true end,
|
|
receive = function(self) return true, '<authenticate status="200"/>' end,
|
|
}
|
|
local session = setmetatable({socket = mock_socket}, Session)
|
|
test_suite:add_test(equal(session:authenticate("user&x", "p<ass>") and true, true),
|
|
"authenticate succeeds with special characters")
|
|
test_suite:add_test(equal(sent,
|
|
"<authenticate><credentials><username>user&x</username>"
|
|
.. "<password>p<ass></password></credentials></authenticate>"),
|
|
"authenticate escapes credentials in the request")
|
|
|
|
return _ENV;
|