From c47e3961b64c5b2cf931493dbe561af05f1e2d6d Mon Sep 17 00:00:00 2001 From: dmiller Date: Wed, 5 Sep 2018 20:34:20 +0000 Subject: [PATCH] Remove bin.lua from rpc.lua --- nselib/rpc.lua | 142 +++++++++++++++++++++++-------------------------- 1 file changed, 67 insertions(+), 75 deletions(-) diff --git a/nselib/rpc.lua b/nselib/rpc.lua index f87b76199..32c6c808d 100644 --- a/nselib/rpc.lua +++ b/nselib/rpc.lua @@ -75,7 +75,6 @@ -- @args rpc.protocol table If set overrides the preferred order in which -- protocols are tested. (ie. "tcp", "udp") -local bin = require "bin" local datafiles = require "datafiles" local datetime = require "datetime" local math = require "math" @@ -332,10 +331,10 @@ Comm = { local xid = xid or math.random(1234567890) local procedure = procedure or 0 - packet = bin.pack( ">IIIIII", xid, Portmap.MessageType.CALL, RPC_VERSION, + packet = string.pack( ">I4 I4 I4 I4 I4 I4", xid, Portmap.MessageType.CALL, RPC_VERSION, self.program_id, self.version, procedure ) if auth.type == Portmap.AuthType.NULL then - packet = packet .. bin.pack( "IIII", 0, 0, 0, 0 ) + packet = packet .. string.pack( ">I4 I4 I4 I4", 0, 0, 0, 0 ) elseif auth.type == Portmap.AuthType.UNIX then packet = packet .. Util.marshall_int32(auth.type) local blob = ( @@ -353,7 +352,7 @@ Comm = { blob = blob .. Util.marshall_int32(0) end packet = (packet .. Util.marshall_vopaque(blob) - .. bin.pack( "II", 0, 0 ) --AUTH_NULL verf + .. string.pack( ">I4 I4", 0, 0 ) --AUTH_NULL verf ) else return false, "Comm.CreateHeader: invalid authentication type specified" @@ -376,7 +375,8 @@ Comm = { header.verifier = {} - if ( data:len() - pos < HEADER_LEN ) then + pos = pos or 1 + if ( data:len() - pos + 1 < HEADER_LEN ) then local tmp status, tmp = self:GetAdditionalBytes( data, pos, HEADER_LEN - ( data:len() - pos ) ) if not status then @@ -386,15 +386,15 @@ Comm = { data = data .. tmp end - pos, header.xid, header.type, header.state = bin.unpack(">III", data, pos) + header.xid, header.type, header.state, pos = string.unpack(">I4 I4 I4", data, pos) if ( header.state == Portmap.State.MSG_DENIED ) then - pos, header.denied_state = bin.unpack(">I", data, pos ) + header.denied_state, pos = string.unpack(">I4", data, pos ) return pos, header end - pos, header.verifier.flavor = bin.unpack(">I", data, pos) - pos, header.verifier.length = bin.unpack(">I", data, pos) + header.verifier.flavor, pos = string.unpack(">I4", data, pos) + header.verifier.length, pos = string.unpack(">I4", data, pos) if header.verifier.length - 8 > 0 then status, data = self:GetAdditionalBytes( data, pos, header.verifier.length - 8 ) @@ -402,9 +402,9 @@ Comm = { stdnse.debug4("Comm.DecodeHeader: failed to call GetAdditionalBytes") return -1, nil end - pos, header.verifier.data = bin.unpack("A" .. header.verifier.length - 8, data, pos ) + header.verifier.data, pos = string.unpack("c" .. header.verifier.length - 8, data, pos ) end - pos, header.accept_state = bin.unpack(">I", data, pos ) + header.accept_state, pos = string.unpack(">I4", data, pos ) return pos, header end, @@ -437,7 +437,7 @@ Comm = { return false, "Comm.ReceivePacket: failed to call GetAdditionalBytes" end - pos, tmp = bin.unpack(">i", data, pos ) + tmp, pos = string.unpack(">I4", data, pos ) length = tmp & 0x7FFFFFFF if (tmp & 0x80000000) == 0x80000000 then @@ -504,7 +504,7 @@ Comm = { else -- set the high bit as this is our last fragment len = 0x80000000 + packet:len() - return bin.pack(">I", len) .. packet + return string.pack(">I4", len) .. packet end end, @@ -696,12 +696,12 @@ Portmap = if ( not(status) ) then return false, "Portmap.Dump: Failed to call GetAdditionalBytes" end - pos, vfollows = bin.unpack( ">I", data, pos ) + vfollows, pos = string.unpack(">I4", data, pos) if ( vfollows == 0 ) then break end - pos, program, version, protocol, port = bin.unpack(">IIII", data, pos) + program, version, protocol, port, pos = string.unpack(">I4 I4 I4 I4", data, pos) if ( protocol == Portmap.PROTOCOLS.tcp ) then protocol = "tcp" elseif ( protocol == Portmap.PROTOCOLS.udp ) then @@ -740,7 +740,7 @@ Portmap = return false, ("Portmap.Callit: Unknown program name: %s"):format(program) end - local data = bin.pack(">IIII", Util.ProgNameToNumber(program), version, 0, 0 ) + local data = string.pack(">I4 I4 I4 I4", Util.ProgNameToNumber(program), version, 0, 0 ) local packet = comm:EncodePacket(nil, Portmap.Procedure[comm.version].CALLIT, { type=Portmap.AuthType.NULL }, data ) @@ -788,7 +788,7 @@ Portmap = return false, ("Portmap.GetPort: Unknown program name: %s"):format(program) end - data = bin.pack(">I>I>I>I", Util.ProgNameToNumber(program), version, + data = string.pack(">I4 I4 I4 I4", Util.ProgNameToNumber(program), version, Portmap.PROTOCOLS[protocol], 0 ) packet = comm:EncodePacket(xid, Portmap.Procedure[comm.version].GETPORT, { type=Portmap.AuthType.NULL }, data ) @@ -839,7 +839,7 @@ Portmap = return false, "Portmap.GetPort: Failed to call GetAdditionalBytes" end - return true, select(2, bin.unpack(">I", data, pos ) ) + return true, string.unpack(">I4", data, pos) end, } @@ -1042,10 +1042,9 @@ Mount = { -- @return fhandle string containing the filehandle of the remote export Mount = function(self, comm, path) local packet, mount_status - local _, pos, data, header, fhandle = "", 1, "", "", {} local status, len - data = Util.marshall_vopaque(path) + local data = Util.marshall_vopaque(path) packet = comm:EncodePacket( nil, Mount.Procedure.MOUNT, { type=Portmap.AuthType.UNIX }, data ) if (not(comm:SendPacket(packet))) then @@ -1057,7 +1056,7 @@ Mount = { return false, "Mount: Failed to read data from socket" end - pos, header = comm:DecodeHeader( data, pos ) + local pos, header = comm:DecodeHeader(data) if not header then return false, "Mount: Failed to decode header" end @@ -1100,23 +1099,24 @@ Mount = { end end + local fhandle if ( comm.version == 3 ) then status, data = comm:GetAdditionalBytes( data, pos, 4 ) if (not(status)) then return false, "Mount: Failed to call GetAdditionalBytes" end - _, len = bin.unpack(">I", data, pos ) + len = string.unpack(">I4", data, pos) status, data = comm:GetAdditionalBytes( data, pos, len + 4 ) if (not(status)) then return false, "Mount: Failed to call GetAdditionalBytes" end - pos, fhandle = bin.unpack( "A" .. len + 4, data, pos ) + fhandle, pos = string.unpack( "c" .. len + 4, data, pos ) elseif ( comm.version < 3 ) then status, data = comm:GetAdditionalBytes( data, pos, 32 ) if (not(status)) then return false, "Mount: Failed to call GetAdditionalBytes" end - pos, fhandle = bin.unpack( "A32", data, pos ) + fhandle, pos = string.unpack( "c32", data, pos ) else return false, "Mount failed" end @@ -1484,7 +1484,7 @@ NFS = { stdnse.debug4("NFS.ReadDirDecode: Failed to call GetAdditionalBytes") return -1, nil end - pos, _ = bin.unpack(">L", data, pos) + _, pos = string.unpack(">I8", data, pos) end response.entries = {} @@ -1570,9 +1570,9 @@ NFS = { if ( comm.version == 3 ) then local opaque_data = 0 - data = bin.pack("A>L>L>I", file_handle, cookie, opaque_data, count) + data = file_handle .. string.pack("I8 I8 I4", cookie, opaque_data, count) else - data = bin.pack("A>I>I", file_handle, cookie, count) + data = file_handle .. string.pack(">I4 I4", cookie, count) end packet = comm:EncodePacket( nil, NFS.Procedure[comm.version].READDIR, { type=Portmap.AuthType.UNIX }, data ) @@ -1622,7 +1622,7 @@ NFS = { stdnse.debug4("NFS.LookUpDecode: Failed to call GetAdditionalBytes") return -1, nil end - pos, lookup.fhandle = bin.unpack( "A" .. len + 4, data, pos) + lookup.fhandle, pos = string.unpack( "c" .. len + 4, data, pos) status, data = comm:GetAdditionalBytes( data, pos, 4) if (not(status)) then @@ -1668,7 +1668,7 @@ NFS = { stdnse.debug4("NFS.LookUpDecode: Failed to call GetAdditionalBytes") return -1, nil end - pos, lookup.fhandle = bin.unpack("A32", data, pos) + lookup.fhandle, pos = string.unpack("c32", data, pos) status, data = comm:GetAdditionalBytes( data, pos, 64 ) if (not(status)) then stdnse.debug4("NFS.LookUpDecode: Failed to call GetAdditionalBytes") @@ -1737,7 +1737,7 @@ NFS = { return -1, nil end - pos, value_follows = bin.unpack(">I", data, pos) + value_follows, pos = string.unpack(">I4", data, pos) if value_follows == 0 then stdnse.debug4("NFS.ReadDirPlusDecode: Attributes follow failed") return -1, nil @@ -1757,7 +1757,7 @@ NFS = { stdnse.debug4("NFS.ReadDirPlusDecode: Failed to call GetAdditionalBytes") return -1, nil end - pos, _ = bin.unpack(">L", data, pos) + _, pos = string.unpack(">I8", data, pos) response.entries = {} while true do @@ -1768,7 +1768,7 @@ NFS = { return -1, nil end - pos, value_follows = bin.unpack(">I", data, pos) + value_follows, pos = string.unpack(">I4", data, pos) if (value_follows == 0) then break @@ -1778,7 +1778,7 @@ NFS = { stdnse.debug4("NFS.ReadDirPlusDecode: Failed to call GetAdditionalBytes") return -1, nil end - pos, entry.fileid = bin.unpack(">L", data, pos) + entry.fileid, pos = string.unpack(">I8", data, pos) status, data = comm:GetAdditionalBytes(data, pos, 4) @@ -1787,7 +1787,7 @@ NFS = { return -1, nil end - pos, entry.length = bin.unpack(">I", data, pos) + entry.length, pos = string.unpack(">I4", data, pos) status, data = comm:GetAdditionalBytes( data, pos, entry.length ) if not status then stdnse.debug4("NFS.ReadDirPlusDecode: Failed to call GetAdditionalBytes") @@ -1800,7 +1800,7 @@ NFS = { stdnse.debug4("NFS.ReadDirPlusDecode: Failed to call GetAdditionalBytes") return -1, nil end - pos, entry.cookie = bin.unpack(">L", data, pos) + entry.cookie, pos = string.unpack(">I8", data, pos) status, data = comm:GetAdditionalBytes(data, pos, 4) if not status then stdnse.debug4("NFS.ReadDirPlusDecode: Failed to call GetAdditionalBytes") @@ -1808,7 +1808,7 @@ NFS = { end entry.attributes = {} - pos, value_follows = bin.unpack(">I", data, pos) + value_follows, pos = string.unpack(">I4", data, pos) if (value_follows ~= 0) then status, data = comm:GetAdditionalBytes(data, pos, 84) if not status then @@ -1828,7 +1828,7 @@ NFS = { end entry.fhandle = "" - pos, value_follows = bin.unpack(">I", data, pos) + value_follows, pos = string.unpack(">I4", data, pos) if (value_follows ~= 0) then status, data = comm:GetAdditionalBytes(data, pos, 4) if not status then @@ -1836,13 +1836,13 @@ NFS = { return -1, nil end - _, len = bin.unpack(">I", data, pos) + len = string.unpack(">I4", data, pos) status, data = comm:GetAdditionalBytes(data, pos, len + 4) if not status then stdnse.debug4("NFS.ReadDirPlusDecode: Failed to call GetAdditionalBytes") return -1, nil end - pos, entry.fhandle = bin.unpack( "A" .. len + 4, data, pos ) + entry.fhandle, pos = string.unpack( "c" .. len + 4, data, pos ) else stdnse.debug4("NFS.ReadDirPlusDecode: %s handle follow failed", entry.name) @@ -1868,8 +1868,7 @@ NFS = { return false, "ReadDirPlus: No filehandle received" end - data = bin.pack("A>L>L>I>I", file_handle, cookie, - opaque_data, dircount, maxcount) + data = file_handle .. string.pack(">I8 I8 I4 I4", cookie, opaque_data, dircount, maxcount) packet = comm:EncodePacket(nil, NFS.Procedure[comm.version].READDIRPLUS, {type = Portmap.AuthType.UNIX }, data) @@ -3043,60 +3042,52 @@ Util = Util.TimeToString(attr[time].seconds)) end, - marshall_int32 = function(int32, count) - if count then - return bin.pack(">i" .. count, int32) - end - return bin.pack(">i", int32) + marshall_int32 = function(int32) + return string.pack(">i4", int32) end, unmarshall_int32 = function(data, pos, count) - if count then - return bin.unpack(">i" .. count, data, pos) + local ints = {} + for i=1,(count or 1) do + ints[i], pos = string.unpack(">i4", data, pos) end - return bin.unpack(">i", data, pos) + return pos, table.unpack(ints) end, - marshall_uint32 = function(uint32, count) - if count then - return bin.pack(">I" .. count, uint32) - end - return bin.pack(">I", uint32) + marshall_uint32 = function(uint32) + return string.pack(">I4", uint32) end, unmarshall_uint32 = function(data, pos, count) - if count then - return bin.unpack(">I" .. count, data, pos) + local ints = {} + for i=1,(count or 1) do + ints[i], pos = string.unpack(">I4", data, pos) end - return bin.unpack(">I", data, pos) + return pos, table.unpack(ints) end, - marshall_int64 = function(int64, count) - if count then - return bin.pack(">l" .. count, int64) - end - return bin.pack(">l", int64) + marshall_int64 = function(int64) + return string.pack(">i8", int64) end, unmarshall_int64 = function(data, pos, count) - if count then - return bin.unpack(">l" .. count, data, pos) + local ints = {} + for i=1,(count or 1) do + ints[i], pos = string.unpack(">i8", data, pos) end - return bin.unpack(">l", data, pos) + return pos, table.unpack(ints) end, - marshall_uint64 = function(uint64, count) - if count then - return bin.pack(">L" .. count, uint64) - end - return bin.pack(">L", uint64) + marshall_uint64 = function(uint64) + return string.pack(">I8", uint64) end, unmarshall_uint64 = function(data, pos, count) - if count then - return bin.unpack(">L" .. count, data, pos) + local ints = {} + for i=1,(count or 1) do + ints[i], pos = string.unpack(">I8", data, pos) end - return bin.unpack(">L", data, pos) + return pos, table.unpack(ints) end, marshall_opaque = function(data) @@ -3104,7 +3095,8 @@ Util = end, unmarshall_opaque = function(len, data, pos) - return bin.unpack(">A" .. len, data, pos) + local opaque, pos = string.unpack("c" .. len, data, pos) + return pos, opaque end, marshall_vopaque = function(data) @@ -3118,7 +3110,7 @@ Util = unmarshall_vopaque = function(len, data, pos) local opaque, pad pad = Util.CalcFillBytes(len) - pos, opaque = bin.unpack(">A" .. len, data, pos) + opaque, pos = string.unpack("c" .. len, data, pos) return pos + pad, opaque end,