From a919fe604c447be4078315d2947bfb1275b3bb5b Mon Sep 17 00:00:00 2001 From: Utkarsh Singh Date: Wed, 14 Oct 2026 19:36:32 +0000 Subject: [PATCH] smb-os-discovery: add pure-SMB2 fallback, fix crash on custom ports Windows 10 (1607+) blanks out the SMB1 NativeOS strings, and many modern hosts disable SMB1 entirely, leaving smb-os-discovery empty or failing (issue #901). Add two primitives to nselib/smb2.lua: - smb2.start(): opens a TCP connection and performs an SMB2 NEGOTIATE with no SMB1 or NetBIOS dependency (honours the smbport script arg) - smb2.session_setup_ntlmssp(): sends a null-session NTLMSSP NEGOTIATE (type 1) wrapped in SPNEGO and returns the server's NTLMSSP CHALLENGE (type 2) security blob without completing authentication smb-os-discovery.nse now falls back to that path when SMB1 fails, and augments SMB1 results with the build number and DNS names that modern hosts only expose through the NTLMSSP challenge. The build number is mapped to a marketing name (e.g. 22631 -> Windows 11 23H2) and a CPE. Also fixes a crash in the version-augmentation block: any SMB port other than 445/139 was assumed to be UDP, so get_port_state() returned nil and port.version was dereferenced unconditionally. SMB session setup only runs over TCP; the port lookup now always uses TCP and is nil-guarded. --- nselib/smb2.lua | 183 ++++++++++++++++++++++++ scripts/smb-os-discovery.nse | 262 +++++++++++++++++++++++++++++------ 2 files changed, 404 insertions(+), 41 deletions(-) diff --git a/nselib/smb2.lua b/nselib/smb2.lua index a709caaae..f11ff68f6 100644 --- a/nselib/smb2.lua +++ b/nselib/smb2.lua @@ -390,4 +390,187 @@ function negotiate_v2(smb, overrides) end end +--- +-- Starts a pure-SMB2 session: opens a TCP connection and performs an SMB2 +-- NEGOTIATE without any SMB1 or NetBIOS dependency. This allows scripts to +-- talk to hosts where SMB1 has been disabled entirely (modern Windows, +-- hardened Samba builds). +-- +-- The port is chosen from the smbport script argument when set, +-- otherwise tcp/445 is used. +-- +-- @param host The host table. +-- @param overrides [optional] Table of overrides passed to negotiate_v2. +-- @return (status, smb) If status is true, smb is a state table suitable for +-- the other smb2 functions (with a negotiated dialect). +-- If status is false, smb contains an error message. +function start(host, overrides) + local nmap = require "nmap" + overrides = overrides or {} + + local port_number = tonumber(nmap.registry.args and nmap.registry.args.smbport) or 445 + local port = nmap.get_port_state(host, {number=port_number, protocol="tcp"}) + if port == nil or port.state ~= "open" then + if port_number ~= 445 then + port = nmap.get_port_state(host, {number=445, protocol="tcp"}) + end + if port == nil or port.state ~= "open" then + return false, "SMB2: Couldn't find an open SMB port to check" + end + end + + local socket = nmap.new_socket() + socket:set_timeout(TIMEOUT) + local status, err = socket:connect(host.ip, port.number, "tcp") + if not status then + socket:close() + return false, "SMB2: Failed to connect to port " .. port.number .. ": " .. tostring(err) + end + + local smb = { + ['socket'] = socket, + ['host'] = host, + ['ip'] = host.ip, + ['port'] = port.number, + ['MessageId'] = 0, + ['SessionId'] = 0, + ['TreeId'] = 0, + } + + status, err = negotiate_v2(smb, overrides) + if not status then + socket:close() + return false, err + end + + return true, smb +end + +--- +-- Encodes a DER (DER/BER) length octet string for the SPNEGO wrapper. +local function der_length(len) + if len < 0x80 then + return string.pack("B", len) + elseif len < 0x100 then + return string.pack("BB", 0x81, len) + end + return string.pack(">BI2", 0x82, len) +end + +--- +-- Sends an SMB2 SESSION_SETUP carrying a null-session NTLMSSP NEGOTIATE +-- (type 1) message wrapped in SPNEGO, and parses the server's NTLMSSP +-- CHALLENGE (type 2) from the response. Authentication is deliberately not +-- completed: the challenge blob is returned so callers can extract host +-- information (NetBIOS/DNS names, OS version/build, timestamp) from it. +-- +-- @param smb The associated SMB connection object from start. +-- @return (status, blob) If status is true, blob is the raw NTLMSSP +-- CHALLENGE security blob (leading SPNEGO unwrapped when present). +-- If status is false, blob is an error message. +function session_setup_ntlmssp(smb) + local unicode = require "unicode" + + -- NTLMSSP NEGOTIATE (type 1), with Version field requested so that + -- servers include the OS major/minor/build in the CHALLENGE. + local flags = 0x00000001 -- NEGOTIATE_UNICODE + | 0x00000004 -- REQUEST_TARGET + | 0x00000200 -- NEGOTIATE_NTLM + | 0x00080000 -- NEGOTIATE_EXTENDED_SESSIONSECURITY + | 0x00200000 -- NEGOTIATE_TARGET_INFO + | 0x02000000 -- NEGOTIATE_VERSION + | 0x20000000 -- NEGOTIATE_128 + local type1 = string.pack(" #header + #data then + return false, "SMB2: SESSION_SETUP response has no security blob" + end + + local full = header .. data + local blob = full:sub(sec_off + 1, sec_off + sec_len) + -- Servers normally answer SMB2 with a raw NTLMSSP message; unwrap SPNEGO + -- just in case (some implementations wrap the CHALLENGE in NegTokenResp). + if blob:sub(1, 8) ~= "NTLMSSP\0" then + local start = blob:find("NTLMSSP\0", 1, true) + if start then + blob = blob:sub(start) + end + end + + return true, blob +end + +--- +-- Politely ends an SMB2 session: best-effort LOGOFF, then close the socket. +-- +-- @param smb The associated SMB connection object. +function stop(smb) + pcall(function() + local header = smb2_encode_header_sync(smb, command_codes['SMB2_COM_LOGOFF'], { + ['CreditCharge'] = 1, + ['CreditR'] = 1, + ['Signature'] = 0, + }) + local data = string.pack("Windows Server (R) 2008 Standard 6001 Service Pack 1 -- cpe:/o:microsoft:windows_2008::sp1 @@ -99,38 +116,55 @@ function make_cpe(result) local os = result.os local parts = {} - if string.match(os, "^Windows 5%.0") then - parts = {"o", "microsoft", "windows_2000"} - elseif string.match(os, "^Windows 5%.1") then - parts = {"o", "microsoft", "windows_xp"} - elseif string.match(os, "^Windows Server.*2003") then - parts = {"o", "microsoft", "windows_server_2003"} - elseif string.match(os, "^Windows Vista") then - parts = {"o", "microsoft", "windows_vista"} - elseif string.match(os, "^Windows Server.*2008") then - parts = {"o", "microsoft", "windows_server_2008"} - elseif string.match(os, "^Windows 7") then - parts = {"o", "microsoft", "windows_7"} - elseif string.match(os, "^Windows 8%f[^%d.]") then - parts = {"o", "microsoft", "windows_8"} - elseif string.match(os, "^Windows 8.1") then - parts = {"o", "microsoft", "windows_8.1"} - elseif string.match(os, "^Windows 10%f[^%d.]") then - parts = {"o", "microsoft", "windows_10"} - elseif string.match(os, "^Windows Server.*2012") then - parts = {"o", "microsoft", "windows_server_2012"} + if os and #os > 0 then + if string.match(os, "^Windows 5%.0") then + parts = {"o", "microsoft", "windows_2000"} + elseif string.match(os, "^Windows 5%.1") then + parts = {"o", "microsoft", "windows_xp"} + elseif string.match(os, "^Windows Server.*2003") then + parts = {"o", "microsoft", "windows_server_2003"} + elseif string.match(os, "^Windows Vista") then + parts = {"o", "microsoft", "windows_vista"} + elseif string.match(os, "^Windows Server.*2008") then + parts = {"o", "microsoft", "windows_server_2008"} + elseif string.match(os, "^Windows 7") then + parts = {"o", "microsoft", "windows_7"} + elseif string.match(os, "^Windows 8%f[^%d.]") then + parts = {"o", "microsoft", "windows_8"} + elseif string.match(os, "^Windows 8.1") then + parts = {"o", "microsoft", "windows_8.1"} + elseif string.match(os, "^Windows 10%f[^%d.]") then + parts = {"o", "microsoft", "windows_10"} + elseif string.match(os, "^Windows Server.*2012") then + parts = {"o", "microsoft", "windows_server_2012"} + end + end + if #parts == 0 and result.os_major_version == 10 and result.os_minor_version == 0 + and result.os_build and result.os_build > 0 then + -- No usable OS string (modern hosts blank it out, or pure SMB2), so fall + -- back to the build number reported by the NTLMSSP Version field. Note + -- that some builds are shared between client and server SKUs; where that + -- is the case we assume the client variant. + local build = result.os_build + if build == 20348 or build == 25398 then + parts = {"o", "microsoft", "windows_server_2022"} + elseif build >= 22000 then + parts = {"o", "microsoft", "windows_11"} + else + parts = {"o", "microsoft", "windows_10"} + end end if parts[1] == "o" and parts[2] == "microsoft" and string.match(parts[3], "^windows") then parts[4] = "" - local sp = string.match(os, "Service Pack (%d+)") + local sp = os and string.match(os, "Service Pack (%d+)") if sp then parts[5] = "sp" .. tostring(sp) else parts[5] = "-" end - if string.match(os, "Professional") then + if os and string.match(os, "Professional") then parts[6] = "professional" end end @@ -146,13 +180,141 @@ function add_to_output(output_table, label, value) end end +-- Map NTLMSSP Version build numbers to marketing names. Windows 10 and +-- Server share build numbers in some cases; the server variants are listed +-- where they are unambiguous. Unknown builds fall back to a family name. +local build_names = { + [10240] = "Windows 10 1507", + [14393] = "Windows 10 1607 / Server 2016", + [15063] = "Windows 10 1703", + [16299] = "Windows 10 1709", + [17134] = "Windows 10 1803", + [17763] = "Windows 10 1809 / Server 2019", + [18362] = "Windows 10 1903", + [18363] = "Windows 10 1909", + [19041] = "Windows 10 2004", + [19042] = "Windows 10 20H2", + [19043] = "Windows 10 21H1", + [19044] = "Windows 10 21H2", + [19045] = "Windows 10 22H2", + [20348] = "Windows Server 2022", + [25398] = "Windows Server 2022 23H2", + [22000] = "Windows 11 21H2", + [22621] = "Windows 11 22H2", + [22631] = "Windows 11 23H2", + [26100] = "Windows 11 24H2 / Server 2025", +} + +function friendly_windows_name(major, minor, build) + if major == nil or minor == nil then return nil end + if major == 10 and minor == 0 then + if build then + if build_names[build] then + return build_names[build] + elseif build >= 22000 and build < 26100 then + return "Windows 11" + elseif build >= 26100 then + return "Windows 11 / Server 2025" + elseif build >= 10240 then + return "Windows 10 / Server 2016+" + end + end + return "Windows 10 or Server 2016+" + elseif major == 6 and minor == 3 then + return "Windows 8.1 / Server 2012 R2" + elseif major == 6 and minor == 2 then + return "Windows 8 / Server 2012" + elseif major == 6 and minor == 1 then + return "Windows 7 / Server 2008 R2" + elseif major == 6 and minor == 0 then + return "Windows Vista / Server 2008" + elseif major == 5 and minor == 2 then + return "Windows XP / Server 2003" + elseif major == 5 and minor == 1 then + return "Windows XP" + elseif major == 5 and minor == 0 then + return "Windows 2000" + end + return nil +end + +--- Collect OS information over a pure SMB2 session (no SMB1, no NetBIOS). +--- Works on hosts where SMB1 is disabled entirely. Reads the OS version and +--- build from the NTLMSSP CHALLENGE Version field and the computer/domain +--- names from the challenge TargetInfo AV pairs. +local function get_os_over_smb2(host) + local status, smbstate = smb2.start(host) + if not status then + stdnse.debug1("SMB2 connect/negotiate failed: %s", tostring(smbstate)) + return nil + end + local dialect_name = smb2.dialect_name(smbstate['dialect']) + + local status2, blob = smb2.session_setup_ntlmssp(smbstate) + if not status2 then + stdnse.debug1("SMB2 session setup failed: %s", tostring(blob)) + smb2.stop(smbstate) + return nil + end + + local info = smbauth.get_host_info_from_security_blob(blob) + local port = smbstate['port'] + smb2.stop(smbstate) + + if type(info) ~= "table" then + stdnse.debug1("SMB2: could not parse NTLMSSP challenge") + return nil + end + + info.dialect = dialect_name + info.port = port + if info.timestamp and info.timestamp > 0 then + info.time = (info.timestamp // 10000000) - 11644473600 + end + return info +end + action = function(host) local response = stdnse.output_table() local request_time = os.time() local status, result = smb.get_os(host) + local smb2_info if not status then - return stdnse.format_output(false, result) + -- SMB1 session setup failed; the host may have SMB1 disabled entirely + -- (modern Windows, hardened Samba). Fall back to a pure SMB2 session. + smb2_info = get_os_over_smb2(host) + if not smb2_info then + return stdnse.format_output(false, result) + end + result = { + port = smb2_info.port, + server = smb2_info.netbios_computer_name, + domain = smb2_info.netbios_domain_name, + time = smb2_info.time, + os_major_version = smb2_info.os_major_version, + os_minor_version = smb2_info.os_minor_version, + os_build = smb2_info.os_build, + fqdn = smb2_info.fqdn, + domain_dns = smb2_info.dns_domain_name, + forest_dns = smb2_info.dns_forest_name, + smb_dialect = smb2_info.dialect, + } + else + -- SMB1 worked. Modern Windows (10 1607+) blanks out the OS strings and + -- never reports the build, so ask over SMB2 for the missing details. + if not result.os_build or not result.fqdn then + smb2_info = get_os_over_smb2(host) + if smb2_info then + result.os_major_version = result.os_major_version or smb2_info.os_major_version + result.os_minor_version = result.os_minor_version or smb2_info.os_minor_version + result.os_build = result.os_build or smb2_info.os_build + result.fqdn = result.fqdn or smb2_info.fqdn + result.domain_dns = result.domain_dns or smb2_info.dns_domain_name + result.forest_dns = result.forest_dns or smb2_info.dns_forest_name + result.smb_dialect = smb2_info.dialect + end + end end -- Collect results. @@ -163,21 +325,35 @@ action = function(host) if result.time and result.timezone then response.date = datetime.format_timestamp(result.time, result.timezone * 60 * 60) datetime.record_skew(host, result.time - result.timezone * 60 * 60, request_time) + elseif result.time then + -- SMB2 TargetInfo timestamps are UTC; the server timezone is unknown. + response.date = datetime.format_timestamp(result.time) end response.fqdn = result.fqdn response.domain_dns = result.domain_dns response.forest_dns = result.forest_dns response.workgroup = result.workgroup + response.smb_dialect = result.smb_dialect + if result.os_build and result.os_build > 0 then + response.os_version = string.format("%d.%d.%d", + result.os_major_version or 0, result.os_minor_version or 0, result.os_build) + end response.cpe = make_cpe(result) -- Build normal output. local output_lines = {} + local friendly = friendly_windows_name(result.os_major_version, result.os_minor_version, result.os_build) if response.os and response.lanmanager then add_to_output(output_lines, "OS", string.format("%s (%s)", smb.get_windows_version(response.os), response.lanmanager)) + elseif friendly and result.os_build then + add_to_output(output_lines, "OS", string.format("%s (build %d)", friendly, result.os_build)) else add_to_output(output_lines, "OS", "Unknown") end add_to_output(output_lines, "OS CPE", response.cpe) + if response.smb_dialect then + add_to_output(output_lines, "SMB dialect", "SMB " .. response.smb_dialect) + end if response.fqdn then -- Pull the first part of the FQDN as the computer name. add_to_output(output_lines, "Computer name", string.match(response.fqdn, "^([^.]+)%.?")) @@ -195,24 +371,28 @@ action = function(host) add_to_output(output_lines, "System time", response.date or "Unknown") -- Augment service version detection - if result.port and response.lanmanager then - local proto - if result.port == 445 or result.port == 139 then - proto = 'tcp' - else - proto = 'udp' - end - - local port = nmap.get_port_state(host,{number=result.port,protocol=proto}) - - local version, product - if string.match(response.lanmanager,"^Samba ") then - port.version.product = 'Samba smbd' - port.version.version = string.match(response.lanmanager,"^Samba (.*)") - nmap.set_port_version(host,port) - elseif smb.get_windows_version(response.os) then - port.version.product = string.format("%s %s",smb.get_windows_version(response.os), port.version.name) - nmap.set_port_version(host,port) + if result.port and (response.lanmanager or friendly) then + -- SMB session setup only ever runs over TCP; don't guess UDP for + -- non-standard ports (this used to crash the script, since + -- get_port_state() then returned nil and port.version was dereferenced + -- unconditionally). + local port = nmap.get_port_state(host, {number=result.port, protocol="tcp"}) + if port and port.version then + if response.lanmanager and string.match(response.lanmanager, "^Samba ") then + port.version.product = 'Samba smbd' + port.version.version = string.match(response.lanmanager, "^Samba (.*)") + nmap.set_port_version(host, port) + elseif smb.get_windows_version(response.os) then + port.version.product = string.format("%s %s", smb.get_windows_version(response.os), port.version.name) + nmap.set_port_version(host, port) + elseif friendly then + port.version.product = string.format("%s %s", friendly, port.version.name) + if result.os_build and result.os_build > 0 then + port.version.version = string.format("%d.%d.%d", + result.os_major_version or 0, result.os_minor_version or 0, result.os_build) + end + nmap.set_port_version(host, port) + end end end