From ab67d380d6e0e15526dd9609c7bcb1f51a4a335e Mon Sep 17 00:00:00 2001 From: dmiller Date: Tue, 11 Sep 2018 15:27:44 +0000 Subject: [PATCH] Remove bin.lua from a couple libs --- nselib/stun.lua | 22 +++++++++------------ nselib/versant.lua | 49 +++++++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/nselib/stun.lua b/nselib/stun.lua index 0def7e7f2..f7e52ff4f 100644 --- a/nselib/stun.lua +++ b/nselib/stun.lua @@ -9,7 +9,6 @@ -- @author Patrik Karlsson -- -local bin = require "bin" local ipOps = require "ipOps" local match = require "match" local math = require "math" @@ -50,15 +49,14 @@ Header = { -- @name Header.parse parse = function(data) local header = Header:new() - local pos - pos, header.type, header.length, header.trans_id = bin.unpack(">SSA16", data) + header.type, header.length, header.trans_id = string.unpack(">I2I2 c16", data) return header end, -- converts the header to an opaque string -- @return string containing the header instance __tostring = function(self) - return bin.pack(">SSA", self.type, self.length, self.trans_id) + return string.pack(">I2I2", self.type, self.length) .. self.trans_id end, } @@ -135,14 +133,12 @@ Attribute = { local attr = Attribute:new() local pos = 1 - pos, attr.type, attr.length = bin.unpack(">SS", data, pos) + attr.type, attr.length, pos = string.unpack(">I2I2", data, pos) local function parseAddress(data, pos) - local _, addr = nil, {} - pos, _, addr.family, addr.port, addr.ip = bin.unpack(">CCSI", data, pos) - if ( addr.ip ) then - addr.ip = ipOps.fromdword(addr.ip) - end + local addr = {} + addr.family, addr.port, addr.ip, pos = string.unpack(">xBI2c4", data, pos) + addr.ip = ipOps.str_to_ip(addr.ip) return addr end @@ -155,7 +151,7 @@ Attribute = { end attr.addr = parseAddress(data, pos) elseif( attr.type == Attribute.SERVER ) then - pos, attr.server = bin.unpack("A" .. attr.length-1, data, pos) + attr.server = data:sub(pos, pos + attr.length - 1) end return attr @@ -164,7 +160,7 @@ Attribute = { -- converts an attribute to string -- @return string containing the serialized attribute __tostring = function(self) - return bin.pack(">SSA", self.type, self.length, self.data or "") + return string.pack(">I2I2", self.type, self.length) .. (self.data or "") end, } @@ -350,7 +346,7 @@ Helper = { if ( self.mode == "classic" ) then trans_id = Util.randomString(16) else - trans_id = bin.pack("HA","2112A442", Util.randomString(12)) + trans_id = "\x21\x12\xA4\x42" .. Util.randomString(12) end local req = Request.Bind:new(trans_id) diff --git a/nselib/versant.lua b/nselib/versant.lua index 78f30525d..27252a50b 100644 --- a/nselib/versant.lua +++ b/nselib/versant.lua @@ -9,7 +9,6 @@ -- local stdnse = require "stdnse" -local bin = require "bin" local match = require "match" local nmap = require "nmap" local string = require "string" @@ -60,8 +59,8 @@ Versant = { ver = ver or Versant.VERSION arg = arg or "" - local data = bin.pack("Hzzz", - "000100000000000000020002000000010000000000000000000000000000000000010000", + local data = stdnse.fromhex("000100000000000000020002000000010000000000000000000000000000000000010000") + .. string.pack("zzz", cmd, user, ver @@ -69,8 +68,8 @@ Versant = { -- align to even 4 bytes data = data .. string.rep("\0", 4 - ((#data % 4) or 0)) - data = data .. bin.pack("Hzxxxxxxxxxxz", - "0000000b000001000000000000000000", + data = data .. stdnse.fromhex("0000000b000001000000000000000000") + .. string.pack("zxxxxxxxxxxz", ("%s:%d"):format(self.host.ip, self.port.number), arg ) @@ -109,7 +108,7 @@ Versant = { return false, "Failed to read response from server" end - local _, db_count = bin.unpack(">I", data) + local db_count = string.unpack(">I4", data) if ( db_count == 0 ) then return false, "Database count was zero" end @@ -119,17 +118,17 @@ Versant = { return false, "Failed to read response from server" end - local _, buf_size = bin.unpack(">I", data) + local buf_size = string.unpack(">I4", data) local dbs = {} for i=1, db_count do status, data = self.socket:receive_buf(match.numbytes(buf_size), true) - local _, db = nil, {} + local db = {} - _, db.name = bin.unpack("z", data, 23) - _, db.owner = bin.unpack("z", data, 599) - _, db.created= bin.unpack("z", data, 631) - _, db.version= bin.unpack("z", data, 663) + db.name = string.unpack("z", data, 23) + db.owner = string.unpack("z", data, 599) + db.created= string.unpack("z", data, 631) + db.version= string.unpack("z", data, 663) -- remove trailing line-feed db.created = db.created:match("^(.-)\n*$") @@ -156,13 +155,13 @@ Versant = { return false, "Failed to read response from server" end - local pos, success = bin.unpack(">I", data) + local success, pos = string.unpack(">I4", data) if ( success ~= 0 ) then return false, "Response contained invalid data" end local port = { protocol = "tcp" } - pos, port.number = bin.unpack(">S", data, pos) + port.number, pos = string.unpack(">I2", data, pos) return true, port end, @@ -183,7 +182,7 @@ Versant = { return false, "Failed to read response from server" end - local _, len = bin.unpack(">I", data) + local len = string.unpack(">I4", data) if ( len == 0 ) then return false, "Failed to retrieve license file" end @@ -210,8 +209,8 @@ Versant = { return false, "Failed to connect to database" end - local _, port = nil, { protocol = "tcp" } - _, port.number = bin.unpack(">I", data, 27) + local port = { protocol = "tcp" } + port.number = string.unpack(">I4", data, 27) if ( port == 0 ) then return false, "Failed to determine database port" end @@ -254,10 +253,9 @@ Versant.OBE = { -- lib_path - the library directory -- hostname - the database host name getVODInfo = function(self) - local data = bin.pack("Hz", - "1002005d00000000000100000000000d000000000000000000000000", --28 - "-noprint -i " --12 + 1 (for null) - ) .. string.rep("\0", 215) -- 256 - (28 + 12 + 1) + local data = stdnse.fromhex("1002005d00000000000100000000000d000000000000000000000000") --28 + .. "-noprint -i " --12 + .. string.rep("\0", 216) -- 256 - (28 + 12) self.socket:send(data) local status, data = self.socket:receive_buf(match.numbytes(256), true) @@ -265,17 +263,18 @@ Versant.OBE = { return false, "Failed to read response from server" end - local pos, len = bin.unpack(">I", data, 13) + local len = string.unpack(">I4", data, 13) status, data = self.socket:receive_buf(match.numbytes(len), true) if ( not(status) ) then return false, "Failed to read response from server" end - local result, pos, offset = {}, 1, 13 - pos, result.version = bin.unpack("z", data) + local result = {} + local offset = 13 + result.version = string.unpack("z", data) for _, item in ipairs({"root_path", "db_path", "lib_path", "hostname"}) do - pos, result[item] = bin.unpack("z", data, offset) + result[item] = string.unpack("z", data, offset) offset = offset + 256 end return true, result