diff --git a/nselib/netbios.lua b/nselib/netbios.lua index 330e2777e..8c81a79e0 100644 --- a/nselib/netbios.lua +++ b/nselib/netbios.lua @@ -6,7 +6,6 @@ -- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html local bin = require "bin" -local bit = require "bit" local dns = require "dns" local math = require "math" local nmap = require "nmap" @@ -61,8 +60,8 @@ function name_encode(name, scope) local L1_encoded = {} for i=1, #name, 1 do local b = string.byte(name, i) - L1_encoded[i*2-1] = string.char(bit.rshift(bit.band(b, 0xF0), 4) + 0x41) - L1_encoded[i*2] = string.char(bit.rshift(bit.band(b, 0x0F), 0) + 0x41) + L1_encoded[i*2-1] = string.char(((b & 0xF0) >> 4) + 0x41) + L1_encoded[i*2] = string.char((b & 0x0F) + 0x41) end -- Do the L2 encoding @@ -100,9 +99,7 @@ function name_decode(encoded_name) stdnse.debug3("Decoding name '%s'", encoded_name) name = name:gsub("(.)(.)", function (a, b) - local ch = 0 - ch = bit.bor(ch, bit.lshift(string.byte(a) - 0x41, 4)) - ch = bit.bor(ch, bit.lshift(string.byte(b) - 0x41, 0)) + local ch = ((string.byte(a) - 0x41) << 4) | (string.byte(b) - 0x41) return string.char(ch) end) @@ -199,7 +196,7 @@ function get_server_name(host, names) end for i = 1, #names, 1 do - if names[i]['suffix'] == 0x00 && (names[i]['flags'] & 0x8000 == 0) then + if names[i]['suffix'] == 0x00 and (names[i]['flags'] & 0x8000) == 0 then return true, names[i]['name'] end end @@ -366,11 +363,11 @@ function do_nbstat(host) if(ANCOUNT ~= 1) then return false, "Server returned an invalid number of answers" end - if(bit.band(FLAGS, 0x8000) == 0) then + if FLAGS & 0x8000 == 0 then return false, "Server's flags didn't indicate a response" end - if(bit.band(FLAGS, 0x0007) ~= 0) then - return false, string.format("Server returned a NetBIOS error: 0x%02x", bit.band(FLAGS, 0x0007)) + if FLAGS & 0x0007 ~= 0 then + return false, string.format("Server returned a NetBIOS error: 0x%02x", FLAGS & 0x0007) end -- Start parsing the answer field @@ -462,31 +459,31 @@ end --@param flags The 16-bit flags field --@return A string representing the flags function flags_to_string(flags) - local result = "" + local result = {} - if(bit.band(flags, 0x8000) ~= 0) then - result = result .. "" + if flags & 0x8000 ~= 0 then + result[#result+1] = "" else - result = result .. "" + result[#result+1] = "" end - if(bit.band(flags, 0x1000) ~= 0) then - result = result .. "" + if flags & 0x1000 ~= 0 then + result[#result+1] = "" end - if(bit.band(flags, 0x0800) ~= 0) then - result = result .. "" + if flags & 0x0800 ~= 0 then + result[#result+1] = "" end - if(bit.band(flags, 0x0400) ~= 0) then - result = result .. "" + if flags & 0x0400 ~= 0 then + result[#result+1] = "" end - if(bit.band(flags, 0x0200) ~= 0) then - result = result .. "" + if flags & 0x0200 ~= 0 then + result[#result+1] = "" end - return result + return table.concat(result) end diff --git a/scripts/nbstat.nse b/scripts/nbstat.nse index 1a3e7329a..a62712397 100644 --- a/scripts/nbstat.nse +++ b/scripts/nbstat.nse @@ -114,16 +114,8 @@ end action = function(host) - local i - local status - local names, statistics - local server_name, user_name - local mac, prefix, manuf - local response = {} - - -- Get the list of NetBIOS names - status, names, statistics = netbios.do_nbstat(host) + local status, names, statistics = netbios.do_nbstat(host) status, names, statistics = netbios.do_nbstat(host) status, names, statistics = netbios.do_nbstat(host) status, names, statistics = netbios.do_nbstat(host) @@ -132,24 +124,28 @@ action = function(host) end -- Get the server name - status, server_name = netbios.get_server_name(host, names) + local status, server_name = netbios.get_server_name(host, names) if(status == false) then return stdnse.format_output(false, server_name) end -- Get the workstation name - status, workstation_name = netbios.get_workstation_name(host, names) + local status, workstation_name = netbios.get_workstation_name(host, names) if(status == false) then return stdnse.format_output(false, workstation_name) end -- Get the logged in user - status, user_name = netbios.get_user_name(host, names) + local status, user_name = netbios.get_user_name(host, names) if(status == false) then return stdnse.format_output(false, user_name) end -- Format the Mac address in the standard way + local mac = { + address = "", + manuf = "unknown" + } if(#statistics >= 6) then local status, mac_prefixes = datafiles.parse_mac_prefixes() if not status then @@ -158,11 +154,10 @@ action = function(host) end -- MAC prefixes are matched on the first three bytes, all uppercase - prefix = string.upper(string.format("%02x%02x%02x", statistics:byte(1), statistics:byte(2), statistics:byte(3))) - mac = { - address = ("%02x:%02x:%02x:%02x:%02x:%02x"):format( statistics:byte(1), statistics:byte(2), statistics:byte(3), statistics:byte(4), statistics:byte(5), statistics:byte(6) ), - manuf = mac_prefixes[prefix] or "unknown" - } + local prefix = string.upper(string.format("%02x%02x%02x", statistics:byte(1), statistics:byte(2), statistics:byte(3))) + mac.address = ("%02x:%02x:%02x:%02x:%02x:%02x"):format( statistics:byte(1), statistics:byte(2), statistics:byte(3), statistics:byte(4), statistics:byte(5), statistics:byte(6) ) + mac.manuf = mac_prefixes[prefix] or "unknown" + host.registry['nbstat'] = { server_name = server_name, workstation_name = workstation_name, @@ -173,11 +168,6 @@ action = function(host) mac.address = "" mac.manuf = "unknown" end - else - mac = { - address = "", - manuf = "unknown" - } end setmetatable(mac, { -- MAC is formatted as "00:11:22:33:44:55 (Manufacturer)" @@ -189,6 +179,7 @@ action = function(host) user_name = "" end + local response = stdnse.output_table() response["server_name"] = server_name response["workstation_name"] = workstation_name response["user"] = user_name