mirror of
https://github.com/nmap/nmap.git
synced 2026-11-28 10:30:14 +00:00
Merge b92b31a6f6 into 4568fbc1b8
This commit is contained in:
commit
0f0b36e797
1 changed files with 48 additions and 3 deletions
|
|
@ -32,8 +32,23 @@
|
|||
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
|
||||
|
|
@ -75,10 +90,9 @@ Session = {
|
|||
authenticate = function(self, username, password)
|
||||
local status, err, xmldata
|
||||
|
||||
-- TODO escape credentials
|
||||
status, err = self.socket:send("<authenticate><credentials>"
|
||||
.. "<username>" .. username .. "</username>"
|
||||
.. "<password>" .. password .. "</password>"
|
||||
.. "<username>" .. xml_escape(username) .. "</username>"
|
||||
.. "<password>" .. xml_escape(password) .. "</password>"
|
||||
.. "</credentials></authenticate>")
|
||||
|
||||
if not status then
|
||||
|
|
@ -174,5 +188,36 @@ function get_accounts(host)
|
|||
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue