diff --git a/scripts/http-trane-info.nse b/scripts/http-trane-info.nse index 911c2ec4f..33f10ba40 100644 --- a/scripts/http-trane-info.nse +++ b/scripts/http-trane-info.nse @@ -1,7 +1,7 @@ -local nmap = require "nmap" local http = require "http" -local stdnse = require "stdnse" +local nmap = require "nmap" local shortport = require "shortport" +local stdnse = require "stdnse" local table = require "table" description = [[ @@ -12,53 +12,51 @@ Attempts to obtain information from Trane Tracer SC devices. Trane Tracer SC The information is obtained from the web server that exposes sensitive content to unauthenticated users. -Tested on Trane Tracer SC version 4.40.1211 and below. - References: * http://websec.mx/publicacion/blog/Scripts-de-Nmap-para-Trane-Tracer-SC-HVAC ]] --- --- @usage nmap -p80 --script trane-info.nse +-- @usage nmap -p80,443 --script http-trane-info -- -- @output --- | http-trane-info: --- | serverName: XXXXX --- | serverTime: 2017-09-24T01:03:08-05:00 --- | serverBootTime: 2017-08-03T02:06:39-05:00 --- | vendorName: Trane --- | productName: Tracer SC --- | productVersion: v4.20.1128 (release) --- | kernelVersion: 2.6.30_HwVer12AB-hydra --- | hardwareType: HwVer12AB --- | hardwareSerialNumber: XXXXX --- | devices: --- | --- | isOffline: false --- | equipmentUri: /equipment/dac/generic/1 --- | displayName: RTU-01 --- | equipmentFamily: AirHandler --- | roleDocument: BCI-I_9a8c9b8116cd392fc0b4a233405f3f5964fa6b885809c810a8d0ed5478XXXXXX__RTU_Ipak_VAV --- | deviceName: RTU-01 +-- | http-trane-info: +-- | serverName: XXXXX +-- | serverTime: 2017-09-24T01:03:08-05:00 +-- | serverBootTime: 2017-08-03T02:06:39-05:00 +-- | vendorName: Trane +-- | productName: Tracer SC +-- | productVersion: v4.20.1128 (release) +-- | kernelVersion: 2.6.30_HwVer12AB-hydra +-- | hardwareType: HwVer12AB +-- | hardwareSerialNumber: XXXXX +-- | devices: +-- | +-- | equipmentUri: /equipment/dac/generic/1 +-- | displayName: RTU-01 +-- | deviceName: RTU-01 +-- | equipmentFamily: AirHandler +-- | roleDocument: BCI-I_9a8c9b8116cd392fc0b4a233405f3f5964fa6b885809c810a8d0ed5478XXXXXX__RTU_Ipak_VAV +-- |_ isOffline: false -- -- @xmloutput --- XXXXX --- 2017-09-24T01:05:28-05:00 --- 2017-08-03T02:06:39-05:00 --- Trane --- Tracer SC --- v4.20.1128 (release) --- 2.6.30_HwVer12AB-hydra --- HwVer12AB --- XXXXX +-- XXXXX +-- 2017-09-24T01:05:28-05:00 +-- 2017-08-03T02:06:39-05:00 +-- Trane +-- Tracer SC +-- v4.20.1128 (release) +-- 2.6.30_HwVer12AB-hydra +-- HwVer12AB +-- XXXXX -- --
--- /equipment/dac/generic/1 --- AirHandler --- RTU-01 --- false --- BCI-I_9a8c9b8116cd392fc0b4a233405f3f5964fa6b885809c810a8d0ed5478XXXXX__RTU_Ipak_VAV --- RTU-01 +-- /equipment/dac/generic/1 +-- RTU-01 +-- RTU-01 +-- AirHandler +-- BCI-I_9a8c9b8116cd392fc0b4a233405f3f5964fa6b885809c810a8d0ed5478XXXXX__RTU_Ipak_VAV +-- false --
--- @@ -67,101 +65,131 @@ license = "Same as Nmap--See https://nmap.org/book/man-legal.html" categories = {"discovery", "version", "safe"} portrule = function(host, port) - return (shortport.http(host,port) and nmap.version_intensity() >= 7) + return (shortport.http(host, port) and nmap.version_intensity() >= 7) end -local function GetInformation(host, port) - local output = stdnse.output_table() - --Get information from /evox/about - local uri = '/evox/about' - local response = http.get(host, port, uri) - if response['status-line'] and response['status-line']:match("200") then - --Verify parsing of XML from /evox/about - local deviceType = response['body']:match('serverName" val=([^<]*)/>') - if not deviceType then - stdnse.debug1("Problem with XML parsing of /evox/about") - return nil,"Problem with XML parsing of /evox/about" - end +local ABOUT_URI = "/evox/about" +local SUMMARY_URI = "/evox/equipment/installedSummary" - --Parse information from /evox/about - local keylist = {"serverName","serverTime","serverBootTime","vendorName","productName","productVersion","kernelVersion","hardwareType","hardwareSerialNumber"} - for _,key in ipairs(keylist) do - stdnse.debug2("Looking for : "..key) - output[key] = response['body']:match(key..'" val=([^<]*)/>') - stdnse.debug2("Found : "..output[key]) - output[key] = output[key]:gsub('"', "") - end +local ABOUT_KEYS = {"serverName", "serverTime", "serverBootTime", "vendorName", + "productName", "productVersion", "kernelVersion", "hardwareType", + "hardwareSerialNumber"} +local DEVICE_KEYS = {"equipmentUri", "displayName", "deviceName", + "equipmentFamily", "roleDocument", "isOffline"} - - - --Get information from /evox/equipment/installedSummary - local uri = '/evox/equipment/installedSummary' - local response = http.get(host, port, uri) - if response['status-line'] and response['status-line']:match("200") then - --Verify parsing of XML from /evox/equipment/installedSummary - local error = response['body']:match('Error code: 00017') - if error then - stdnse.debug1("/evox/equipment/installedSummary is not available") - end - local equipmentUri = response['body']:match('equipmentUri" val=([^<]*)/>') - if not equipmentUri then - stdnse.debug1("Problem with XML parsing") - end - if not error then - --Parse information from /evox/equipment/installedSummary - local keylist = {"equipmentUri","displayName","deviceName","equipmentFamily","roleDocument","isOffline"} - local _,lastequipmentUri = response['body']:find(".*equipmentUri") - stdnse.debug2("lastequipmentUri : "..lastequipmentUri) - local count = 1 - local nextequipmentUri = 1 - local devices = {} - while nextequipmentUri < lastequipmentUri do - local device = {} - for _,key in ipairs(keylist) do - stdnse.debug2("Looking for : "..key) - device[key] = response['body']:match(key..'" val=([^<]*)/>',nextequipmentUri) - if not device[key] then - device[key] = "Not available" - else - device[key] = device[key]:gsub('"', "") - stdnse.debug2("Found : ".. device[key]) - end - end - _,nextequipmentUri = response['body']:find("equipmentUri",nextequipmentUri) - table.insert(devices, device) - count = count + 1 - end - output["devices"] = devices +local function get_val(xml, key) + return xml:match('name="' .. key .. '"%s+val="([^"]*)"') +end + +-- Copy every key in keylist that exists in xml into an ordered table. +local function collect(xml, keylist) + local out = stdnse.output_table() + for _, key in ipairs(keylist) do + local v = get_val(xml, key) + if v then + out[key] = v + else + stdnse.debug2("Key not present: %s", key) end end - stdnse.debug2("status-line: "..response['status-line']) - local error = response['status-line']:match('Error') - if error then - stdnse.debug2("Request returned a network error.") - return nil, "Request returned a network error." + return out +end + +local function parse_devices(xml) + local starts = {} + local init = 1 + while true do + local s = xml:find('name="equipmentUri"', init, true) + if not s then break end + starts[#starts + 1] = s + init = s + 1 end - -- Set the port version + local devices = {} + for i, s in ipairs(starts) do + local e = starts[i + 1] or (#xml + 1) + local device = collect(xml:sub(s, e - 1), DEVICE_KEYS) + table.insert(devices, device) + end + return devices +end + +local function get_page(host, port, uri, result_404, known_404) + -- Never follow redirects: a redirect to a generic landing page would come + -- back as a 200 and be mistaken for the Evox endpoint. + local response = http.get(host, port, uri, {redirect_ok = false}) + if response.status ~= 200 or not response.body then + stdnse.debug1("%s: %s", uri, response["status-line"] or "no response") + return nil + end + -- Compare against the server's "200 for everything" page, if it has one. + if not http.page_exists(response, result_404, known_404, uri, false) then + stdnse.debug1("%s matches the server's generic 200 page", uri) + return nil + end + return response +end + +-- CPE 2.2 URI components: lowercase, spaces to "_", anything else outside +-- [A-Za-z0-9._-] percent-encoded (Nmap writes tracer_sc%2b, never tracer_sc+). +local function cpe_component(s) + s = s:lower():gsub("%s+", "_") + return (s:gsub("[^%w%._%-]", function(c) + return ("%%%02x"):format(c:byte()) + end)) +end + +local function set_version(host, port, info) port.version.name = "http" port.version.name_confidence = 10 - port.version.product = output["productName"] - port.version.version = output["productVersion"] - port.version.devicetype = output["hardwareType"] - table.insert(port.version.cpe, "cpe:/h:".. output["vendorName"] .. ":" .. output["productName"]) - - nmap.set_port_version(host, port, "hardmatched") - return output + if info.vendorName and info.productName then + port.version.product = info.vendorName .. " " .. info.productName + table.insert(port.version.cpe, ("cpe:/h:%s:%s"):format( + cpe_component(info.vendorName), cpe_component(info.productName))) end + if info.productVersion then + port.version.version = info.productVersion + end + port.version.devicetype = "specialized" + nmap.set_port_version(host, port, "hardmatched") end -action = function(host,port) - - -- Identify servers that answer 200 to invalid HTTP requests and exit as these would invalidate the tests - local status_404, result_404, _ = http.identify_404(host,port) - if ( status_404 and result_404 == 200 ) then - stdnse.debug1("Exiting due to ambiguous response from web server on %s:%s. All URIs return status 200.", host.ip, port.number) +action = function(host, port) + -- Learn how this server answers requests for pages that do not exist. + -- identify_404 returns false when it cannot tell (for example 200 with a + -- body that changes on every request); treat that as "cannot trust a 200". + local status_404, result_404, known_404 = http.identify_404(host, port) + if not status_404 then + stdnse.debug1("Cannot characterize 404 behavior on %s:%s (%s); exiting.", + host.ip, port.number, tostring(result_404)) return nil end - return GetInformation(host, port) + local about = get_page(host, port, ABOUT_URI, result_404, known_404) + if not about then + return nil + end + + if not get_val(about.body, "serverName") then + stdnse.debug1("%s is not Evox XML (no serverName element)", ABOUT_URI) + return nil + end + local output = collect(about.body, ABOUT_KEYS) + + local summary = get_page(host, port, SUMMARY_URI, result_404, known_404) + if summary then + if summary.body:find("Error code: 00017", 1, true) then + stdnse.debug1("%s is not available on this device", SUMMARY_URI) + else + local devices = parse_devices(summary.body) + if #devices > 0 then + output.devices = devices + else + stdnse.debug1("%s returned no installed equipment", SUMMARY_URI) + end + end + end + + set_version(host, port, output) + return output end