diff --git a/nselib/iscsi.lua b/nselib/iscsi.lua index 299001099..6b7747438 100644 --- a/nselib/iscsi.lua +++ b/nselib/iscsi.lua @@ -34,7 +34,6 @@ -- with multiple addresses -local bin = require "bin" local ipOps = require "ipOps" local match = require "match" local nmap = require "nmap" @@ -133,12 +132,12 @@ Packet = { local opcode = self.opcode + ((self.immediate or 0) << 6) - local data = bin.pack(">CCCCICSCSSISSIILLAA", opcode, + local data = string.pack(">BBBB I4 BI2B I2I2 I4 I2I2 I4 I4 I8I8", opcode, flags, self.ver_max, self.ver_min, len, ( self.isid.t << 6 ) + ( self.isid.a & 0x3f), self.isid.b, self.isid.c, self.isid.d, self.tsih, self.initiator_task_tag, self.cid, reserved, self.cmdsn, - self.expstatsn, reserved, reserved, kvps, string.rep('\0', pad) ) + self.expstatsn, reserved, reserved) .. kvps .. string.rep('\0', pad) return data end @@ -205,11 +204,11 @@ Packet = { end local resp = Packet.LoginResponse:new() - local pos, len = bin.unpack(">I", header, 5) + local len, pos = string.unpack(">I4", header, 5) resp.total_ahs_len = len >> 24 resp.data_seg_len = len & 0x00ffffff - pos, resp.status_code = bin.unpack(">S", header, 37) + resp.status_code, pos = string.unpack(">I2", header, 37) local pad = ( 4 - ( resp.data_seg_len % 4 ) ) pad = ( pad == 4 ) and 0 or pad @@ -273,9 +272,9 @@ Packet = { local len = ( self.total_ahs_len << 24 ) + self.data_seg_len local reserved = 0 - local data = bin.pack(">CCSILIIIILLA", self.opcode, flags, reserved, + local data = string.pack(">BBI2 I4 I8 I4 I4 I4 I4 I8I8", self.opcode, flags, reserved, len, self.lun, self.initiator_task_tag, self.target_trans_tag, - self.cmdsn, self.expstatsn, reserved, reserved, kvps) + self.cmdsn, self.expstatsn, reserved, reserved) .. kvps return data end, @@ -306,7 +305,7 @@ Packet = { repeat local status, header = s:receive_buf(match.numbytes(48), true) if not status then return status, header end - local pos, _, flags, _, _, len = bin.unpack(">CCCCI", header) + local flags, len, pos = string.unpack(">xBxxI4", header) local cont = ( (flags & 0x40) == 0x40 ) resp.total_ahs_len = len >> 24 @@ -383,7 +382,8 @@ Packet = { local opcode = self.opcode + ((self.immediate or 0) << 6) local reserved = 0 local len = ( self.total_ahs_len << 24 ) + self.data_seg_len - local data = bin.pack(">CCSILISSIILL", opcode, (0x80 + self.reasoncode), + local data = string.pack(">BBI2 I4 I8 I4 I2I2 I4 I4 I8I8", + opcode, (0x80 + self.reasoncode), reserved, len, reserved,self.initiator_task_tag, self.cid, reserved, self.cmdsn, self.expstatsn, reserved, reserved ) @@ -688,14 +688,14 @@ Helper = { local chall = resp.kvp:get("CHAP_C")[1] if ( not(chall) ) then return false, "Failed to decode challenge" end - chall = bin.pack("H", chall:sub(3)) + chall = stdnse.fromhex(chall:sub(3)) local ident = resp.kvp:get("CHAP_I")[1] if (not(ident)) then return false, "Failed to decoded identifier" end ident = string.char(tonumber(ident)) local resp = CHAP.calcResponse( ident, chall, password ) - resp = "0x" .. select(2, bin.unpack("H16", resp)) + resp = "0x" .. stdnse.tohex(resp) p = Packet.LoginRequest:new() p:setImmediate(true) diff --git a/nselib/pgsql.lua b/nselib/pgsql.lua index 32922d203..57866b025 100644 --- a/nselib/pgsql.lua +++ b/nselib/pgsql.lua @@ -12,7 +12,6 @@ -- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html -- @author Patrik Karlsson -local bin = require "bin" local nmap = require "nmap" local stdnse = require "stdnse" local openssl = stdnse.silent_require "openssl" @@ -69,13 +68,13 @@ v2 = [MessageType.AuthRequest] = function( data, len, pos ) local _, authtype local response = {} - pos, authtype = bin.unpack(">I", data, pos) + authtype, pos = string.unpack(">I4", data, pos) if ( authtype == AuthenticationType.MD5 ) then if ( len - pos + 1 ) < 3 then return -1, "ERROR: Malformed AuthRequest received" end - pos, response.salt = bin.unpack("A4", data, pos) + response.salt, pos = string.unpack("c4", data, pos) elseif ( authtype == AuthenticationType.Plain ) then --do nothing elseif ( authtype == 0 ) then @@ -105,7 +104,7 @@ v2 = local pos_end = pos + len response.error = {} - pos, response.error.message = bin.unpack("z", data, pos) + response.error.message, pos = string.unpack("z", data, pos) return pos, response end, @@ -119,7 +118,7 @@ v2 = local ptype, len, status, response local pos = pos or 1 - pos, ptype = bin.unpack("C", data, pos) + ptype, pos = string.unpack("B", data, pos) len = data:len() - 1 if v2.messageDecoder[ptype] then @@ -176,7 +175,12 @@ v2 = proto_ver = 0x0020000 user = v2.zeroPad(user, 32) database = v2.zeroPad(database, 64) - data = bin.pack(">I>IAAAAA", 296, proto_ver, database, user, v2.zeroPad(args, 64), v2.zeroPad(unused, 64), v2.zeroPad(tty,64) ) + data = string.pack(">I4I4", 296, proto_ver) + .. database + .. user + .. v2.zeroPad(args, 64) + .. v2.zeroPad(unused, 64) + .. v2.zeroPad(tty,64) socket:send( data ) @@ -217,11 +221,11 @@ v2 = if ( params.authtype == AuthenticationType.MD5 ) then local hash = createMD5LoginHash(username,password,salt) - data = bin.pack( ">Iz", 40, hash) + data = string.pack( ">I4z", 40, hash) try( socket:send( data ) ) elseif ( params.authtype == AuthenticationType.Plain ) then local data - data = bin.pack(">Iz", password:len() + 4, password) + data = string.pack(">I4z", password:len() + 4, password) try( socket:send( data ) ) elseif ( params.authtype == AuthenticationType.Success ) then return true, nil @@ -263,13 +267,13 @@ v3 = local _, authtype local response = {} - pos, authtype = bin.unpack(">I", data, pos) + authtype, pos = string.unpack(">I4", data, pos) if ( authtype == AuthenticationType.MD5 ) then if ( len - pos + 1 ) < 3 then return -1, "ERROR: Malformed AuthRequest received" end - pos, response.salt = bin.unpack("A4", data, pos) + response.salt, pos = string.unpack("c4", data, pos) elseif ( authtype == AuthenticationType.Plain ) then --do nothing elseif ( authtype == 0 ) then @@ -290,11 +294,10 @@ v3 = -- @return pos number containing the offset after decoding -- @return response table containing zero or more of the following key and value [MessageType.ParameterStatus] = function( data, len, pos ) - local tmp, _ local response = {} - tmp = data:sub(pos, pos + len - 4) - _, response.key, response.value = bin.unpack("zz", tmp) + local tmp = data:sub(pos, pos + len - 4) + response.key, response.value = string.unpack("zz", tmp) return pos + len - 4, response end, @@ -316,7 +319,7 @@ v3 = response.error = {} while ( pos < pos_end - 5 ) do - pos, prefix, value = bin.unpack("Az", data, pos) + prefix, value, pos = string.unpack("c1z", data, pos) if prefix == 'S' then response.error.severity = value @@ -350,7 +353,7 @@ v3 = return -1, "ERROR: Invalid BackendKeyData packet" end - pos, response.pid, response.key = bin.unpack(">I>I", data, pos) + response.pid, response.key, pos = string.unpack(">I4I4", data, pos) return pos, response end, @@ -369,7 +372,7 @@ v3 = return -1, "ERROR: Invalid ReadyForQuery packet" end - pos, response.status = bin.unpack("C", data, pos ) + response.status, pos = string.unpack("B", data, pos ) return pos, response end, }, @@ -421,7 +424,7 @@ v3 = decodeHeader = function(data, pos) local ptype, len - pos, ptype, len = bin.unpack("C>I", data, pos) + ptype, len, pos = string.unpack(">BI4", data, pos) return pos, { ['type'] = ptype, ['len'] = len } end, @@ -474,11 +477,11 @@ v3 = if ( params.authtype == AuthenticationType.MD5 ) then local hash = createMD5LoginHash(username, password, salt) - data = bin.pack( "C>Iz", MessageType.PasswordMessage, 40, hash ) + data = string.pack( ">BI4z", MessageType.PasswordMessage, 40, hash ) try( socket:send( data ) ) elseif ( params.authtype == AuthenticationType.Plain ) then local data - data = bin.pack("C>Iz", MessageType.PasswordMessage, password:len() + 4, password) + data = string.pack(">BI4z", MessageType.PasswordMessage, password:len() + 4, password) try( socket:send( data ) ) elseif ( params.authtype == AuthenticationType.Success ) then return true, nil @@ -522,8 +525,8 @@ v3 = local proto_ver, ptype, _, tmp proto_ver = 0x0030000 - data = bin.pack(">IzzzzH", proto_ver, "user", user, "database", database, 0) - data = bin.pack(">I", data:len() + 4) .. data + data = string.pack(">I4zzzzB", proto_ver, "user", user, "database", database, 0) + data = string.pack(">I4", data:len() + 4) .. data socket:send( data ) @@ -557,7 +560,7 @@ v3 = function requestSSL(socket) -- SSLRequest local ssl_req_code = 80877103 - local data = bin.pack( ">I>I", 8, ssl_req_code) + local data = string.pack( ">I4I4", 8, ssl_req_code) local status, response socket:send(data) @@ -581,8 +584,8 @@ end -- @param salt salt -- @return string suitable for login request function createMD5LoginHash(username, password, salt) - local md5_1 = select( 2, bin.unpack( "H16", openssl.md5(password..username) ) ):lower() - return "md5" .. select( 2, bin.unpack("H16", openssl.md5( md5_1 .. salt ) ) ):lower() + local md5_1 = stdnse.tohex(openssl.md5(password..username)) + return "md5" .. stdnse.tohex(openssl.md5(md5_1 .. salt)) end --- Prints the contents of the error table returned from the Error message decoder