From 0bdd1740dc93bf28bc2d5ed715ee4f440cbd07bf Mon Sep 17 00:00:00 2001 From: Yashwini Krishna Murthy Date: Thu, 15 Oct 2026 02:52:49 -0400 Subject: [PATCH] Add configurable SSDP search target to UPnP discovery --- nselib/upnp.lua | 22 +++++++++++++++++----- scripts/broadcast-upnp-info.nse | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/nselib/upnp.lua b/nselib/upnp.lua index f24bcb1f1..848c40412 100644 --- a/nselib/upnp.lua +++ b/nselib/upnp.lua @@ -111,11 +111,12 @@ Comm = { sendRequest = function( self ) -- for details about the UPnP message format, see http://upnp.org/resources/documents.asp - local payload = 'M-SEARCH * HTTP/1.1\r\n\z - Host:239.255.255.250:1900\r\n\z - ST:upnp:rootdevice\r\n\z - Man:"ssdp:discover"\r\n\z - MX:3\r\n\r\n' + local search_target = self.search_target or "upnp:rootdevice" + local payload = 'M-SEARCH * HTTP/1.1\r\n' .. + 'Host:239.255.255.250:1900\r\n' .. + 'ST:' .. search_target .. '\r\n' .. + 'Man:"ssdp:discover"\r\n' .. + 'MX:3\r\n\r\n' local status, err @@ -364,6 +365,17 @@ Helper = { -- @param mcast boolean true if multicast is to be used, false otherwise setMulticast = function( self, mcast ) self.comm:setMulticast(mcast) end, + --- Sets the SSDP search target for discovery requests. + -- + -- @param search_target string containing the SSDP search target + setSearchTarget = function( self, search_target ) + assert(type(search_target) == "string" and + #search_target > 0 and + not search_target:find("%c"), + "search_target must be a non-empty string without control characters") + self.comm.search_target = search_target + end, + --- Enables or disables whether the script will override the IP address in the Location URL -- -- @param override boolean true if override is to be enabled, false otherwise diff --git a/scripts/broadcast-upnp-info.nse b/scripts/broadcast-upnp-info.nse index 8acac81bf..1ff7af9b3 100644 --- a/scripts/broadcast-upnp-info.nse +++ b/scripts/broadcast-upnp-info.nse @@ -6,6 +6,13 @@ Attempts to extract system information from the UPnP service by sending a multic ]] --- +-- @usage +-- nmap --script broadcast-upnp-info +-- nmap --script broadcast-upnp-info --script-args 'broadcast-upnp-info.st=ssdp:all' +-- +-- @args broadcast-upnp-info.st SSDP search target used in M-SEARCH requests. +-- Defaults to upnp:rootdevice. +-- -- @output -- | broadcast-upnp-info: -- | 1.2.3.50 @@ -42,6 +49,15 @@ prerule = function() return true end action = function() local helper = upnp.Helper:new() helper:setMulticast(true) + + local search_target = stdnse.get_script_args(SCRIPT_NAME .. ".st") + if search_target ~= nil then + if type(search_target) ~= "string" or + search_target == "" or search_target:find("%c") then + return "Invalid SSDP search target" + end + helper:setSearchTarget(search_target) + end local status, result = helper:queryServices() if ( status ) then