From 5c9959104df4bd4f2e2dd98ba39672a03bad152e Mon Sep 17 00:00:00 2001 From: dmiller Date: Sun, 2 Sep 2018 20:51:04 +0000 Subject: [PATCH] Check available data before unpacking --- nselib/smb.lua | 185 +++++++++++++++++++++++++------------------------ 1 file changed, 94 insertions(+), 91 deletions(-) diff --git a/nselib/smb.lua b/nselib/smb.lua index b6e24cf84..94215c05c 100644 --- a/nselib/smb.lua +++ b/nselib/smb.lua @@ -583,9 +583,6 @@ function start_netbios(host, port, name) return false, "SMB: Failed to close socket: " .. result end result, flags, length, pos = string.unpack(">BBI2", result) - if(result == nil or length == nil) then - return false, "SMB: ERROR: Server returned less data than it was supposed to (one or more fields are missing); aborting [1]" - end -- Check for a positive session response (0x82) if result == 0x82 then @@ -855,9 +852,6 @@ function smb_read(smb, read_data) -- The length of the packet is 4 bytes of big endian (for our purposes). -- The NetBIOS header is 24 bits, big endian netbios_length, pos = string.unpack(">I4", netbios_data) - if(netbios_length == nil) then - return false, "SMB: ERROR: Server returned less data than it was supposed to (one or more fields are missing); aborting [2]" - end -- Make the length 24 bits netbios_length = (netbios_length & 0x00FFFFFF) @@ -883,6 +877,10 @@ function smb_read(smb, read_data) return false, "SMB: ERROR: Server returned invalid signature" end + local header_format = "= 8 then + smb.time, pos = string.unpack("= 2 then + smb.timezone, pos = string.unpack("= 1 then + smb.key_length, pos = string.unpack("B", parameters, pos) + if (#parameters - pos + 1) >= 2 then + smb.byte_count, pos = string.unpack(" 0 ) then @@ -1042,11 +1035,6 @@ function negotiate_v1(smb, overrides) pos = #data + 1 end else - smb['server_challenge'], pos = string.unpack(string.format(" ( #response.data - NE_UP_TO_FNAME_SIZE ) ) do @@ -2804,7 +2800,7 @@ function find_files(smbstate, fname, options) local function next_item() local status, response = send_and_receive_find_request(smbstate, TRANS2_FIND_FIRST2, function_parameters) - if ( not(status) ) then + if not status or #response.parameters < 4 then return end @@ -2818,7 +2814,7 @@ function find_files(smbstate, fname, options) if ( not(first) ) then local function_parameters = string.pack("= string.packsize(fe_format) do + local fe = {} + local last_pos = pos + local ne, f_len, ea_len, sf_len ne, fe.fi, fe.created, fe.accessed, fe.write, fe.change, - fe.eof, fe.alloc_size, fe.attrs, f_len, ea_len, sf_len, _, pos = string.unpack(" response.data:len() ) + if ne == 0 then + break + end + end first = false until(stop_loop) return