From 00ec940ead186b1f177a12faa7973bf8520c36c6 Mon Sep 17 00:00:00 2001 From: dmiller Date: Wed, 19 Sep 2018 21:43:44 +0000 Subject: [PATCH] Replace bin.lua packing in msrpc libraries --- nselib/msrpc.lua | 152 ++++++++++++------------- nselib/msrpctypes.lua | 254 ++++++++++++++++++++++-------------------- 2 files changed, 210 insertions(+), 196 deletions(-) diff --git a/nselib/msrpc.lua b/nselib/msrpc.lua index 2a15416f1..bc25c2414 100644 --- a/nselib/msrpc.lua +++ b/nselib/msrpc.lua @@ -50,7 +50,6 @@ --@copyright Same as Nmap--See https://nmap.org/book/man-legal.html ----------------------------------------------------------------------- -local bin = require "bin" local datetime = require "datetime" local ipOps = require "ipOps" local math = require "math" @@ -245,7 +244,7 @@ function bind(smbstate, interface_uuid, interface_version, transfer_syntax) transfer_syntax = TRANSFER_SYNTAX end - data = bin.pack("II4III4I 0 and detail_level < 2, "detail_level must be either 0 or 1") local datadesc = ( detail_level == 0 and "B16" or "B16BBDz") - local data = bin.pack(" 0 ) then local comment_offset, _ server.version = {} - pos, server.version.major, server.version.minor, - server.type, comment_offset, _ = bin.unpack("c2c6", uuid) + return ("%08x-%04x-%04x-%s-%s"):format(A, B, C, stdnse.tohex(D), stdnse.tohex(E)) end --- Helper function that maps known UUIDs to corresponding exe/services. @@ -1234,7 +1233,7 @@ function epmapper_lookup(smbstate,handle) -- stdnse.debug1("new_handle: %s", stdnse.tohex(new_handle)) local num_entries - pos, num_entries = bin.unpack(" 1 then lookup_response.annotation = string.sub(data,pos,pos+annotation_length-2) end @@ -1254,11 +1253,11 @@ function epmapper_lookup(smbstate,handle) --skip lengths pos = pos + 8 local num_floors,floor_len,uuid, address_type,address_len,tcp_port,udp_port,ip_addr,saved_pos,ncalrpc,ncacn_np,netbios,ncacn_http - pos, num_floors = bin.unpack("S",data,pos) + lookup_response.tcp_port, pos = string.unpack(">I2", data, pos) elseif address_type == 0x08 then - pos,lookup_response.udp_port = bin.unpack(">S",data,pos) + lookup_response.udp_port, pos = string.unpack(">I2", data, pos) elseif address_type == 0x09 then local ip ip, pos = string.unpack("c4", data, pos) @@ -1285,7 +1284,7 @@ function epmapper_lookup(smbstate,handle) lookup_response.netbios = string.sub(data,pos,pos+address_len-2) floor_len = floor_len + address_len - 2 elseif address_type == 0x1f then - pos, lookup_response.ncacn_http = bin.unpack(">S",data,pos) + lookup_response.ncacn_http, pos = string.unpack(">I2", data, pos) else stdnse.debug1("unknown address type %x",address_type) end @@ -2844,7 +2843,7 @@ function winreg_queryvalue(smbstate, handle, value) if(result['data'] ~= nil) then local _ if(result['type'] == "REG_DWORD") then - _, result['value'] = bin.unpack("bin.unpack would, optionally eat the null terminator, +--- Read a unicode string from a buffer, optionally eat the null terminator, -- and optionally align it to 4-byte boundaries. -- --@param buffer The buffer to read from, typically the full 'arguments' value for MSRPC ---@param pos The position in the buffer to start (just like bin.unpack) +--@param pos The position in the buffer to start --@param length The number of ascii characters that will be read (including the null, if do_null is set). --@param do_null [optional] Remove a null terminator from the string as the last character. Default false. ---@return (pos, string) The new position and the string read, again imitating bin.unpack. If there was an +--@return pos The new position +--@return string The string read. If there was an -- attempt to read off the end of the string, then 'nil' is returned for both parameters. function unicode_to_string(buffer, pos, length, do_null) stdnse.debug4("MSRPC: Entering unicode_to_string(pos = %d, length = %d)", pos, length) @@ -250,9 +250,9 @@ local function marshall_ptr(location, func, args, value) -- If we're marshalling the HEAD section, add a REFERENT_ID. if(location == HEAD or location == ALL) then if(func == nil or args == nil or value == nil) then - result = result .. bin.pack("data, and a table representing the datatype. local function unmarshall_guid(data, pos) - local guid = {} + local fmt = "SI", data, pos) - if(sid['authority_low'] == nil) then + if #data - pos + 1 < 6 then stdnse.debug1("MSRPC: ERROR: Ran off the end of a packet in unmarshall_dom_sid2(). Please report!") + return pos, nil end - sid['authority'] = (sid['authority_high'] << 32) | sid['authority_low'] + sid.authority, pos = string.unpack(">I6", data, pos) sid['sub_auths'] = {} for i = 1, sid['num_auths'], 1 do @@ -1609,12 +1618,11 @@ function marshall_dom_sid2(sid) local pos = 3 pos_next = string.find(sid, "-", pos) - sid_array['sid_rev_num'] = string.sub(sid, pos, pos_next - 1) + sid_array.sid_rev_num = tonumber(string.sub(sid, pos, pos_next - 1)) pos = pos_next + 1 pos_next = string.find(sid, "-", pos) - sid_array['authority_high'] = string.sub(sid, pos, pos_next - 1) >> 32 - sid_array['authority_low'] = string.sub(sid, pos, pos_next - 1) & 0xFFFFFFFF + sid_array.authority_high = tonumber(string.sub(sid, pos, pos_next - 1)) sid_array['sub_auths'] = {} i = 1 @@ -1622,22 +1630,25 @@ function marshall_dom_sid2(sid) pos = pos_next + 1 pos_next = string.find(sid, "-", pos) if(pos_next == nil) then - sid_array['sub_auths'][i] = string.sub(sid, pos) + sid_array['sub_auths'][i] = tonumber(string.sub(sid, pos)) else - sid_array['sub_auths'][i] = string.sub(sid, pos, pos_next - 1) + sid_array['sub_auths'][i] = tonumber(string.sub(sid, pos, pos_next - 1)) end i = i + 1 until pos_next == nil sid_array['num_auths'] = i - 1 - result = bin.pack("SI", sid_array['sid_rev_num'], sid_array['num_auths'], sid_array['authority_high'], sid_array['authority_low']) + result = { + -- TODO: Is the first 32-bit integer here supposed to be num_auths, or some + -- other count value? + string.pack("I6", sid_array.num_auths, sid_array.sid_rev_num, sid_array.num_auths, sid_array.authority), + } for i = 1, sid_array['num_auths'], 1 do - result = result .. bin.pack("