diff --git a/nselib/asn1.lua b/nselib/asn1.lua index 8fa33416f..45780c53e 100644 --- a/nselib/asn1.lua +++ b/nselib/asn1.lua @@ -272,7 +272,7 @@ ASN1Encoder = { --- -- Encodes an ASN1 sequence encodeSeq = function(self, seqData) - return bin.pack('HAA' , '30', self.encodeLength(string.len(seqData)), seqData) + return bin.pack('HAA' , '30', self.encodeLength(#seqData), seqData) end, --- @@ -318,13 +318,13 @@ ASN1Encoder = { -- Integer encoder self.encoder['number'] = function( self, val ) local ival = self.encodeInt(val) - local len = self.encodeLength(string.len(ival)) + local len = self.encodeLength(#ival) return bin.pack('HAA', '02', len, ival) end -- Octet String encoder self.encoder['string'] = function( self, val ) - local len = self.encodeLength(string.len(val)) + local len = self.encodeLength(#val) return bin.pack('HAA', '04', len, val) end diff --git a/nselib/base64.lua b/nselib/base64.lua index 065773ffb..bef7eceb9 100644 --- a/nselib/base64.lua +++ b/nselib/base64.lua @@ -163,7 +163,7 @@ function dec(b64data) local pos = 1 local byte local nbyte = '' - for pos = 1, #b64data do -- while pos <= string.len(b64data) do + for pos = 1, #b64data do -- while pos <= #b64data do byte = b64dec6bit(substr(b64data, pos, pos)) if not byte then return end nbyte = nbyte .. byte diff --git a/nselib/dns.lua b/nselib/dns.lua index 8df8180b9..7dc1b1a90 100644 --- a/nselib/dns.lua +++ b/nselib/dns.lua @@ -336,7 +336,7 @@ function reverse(ip) -- padding local mask = "0000" for i, part in ipairs(ipParts) do - ipParts[i] = mask:sub(1, string.len(mask) - string.len(part)) .. part + ipParts[i] = mask:sub(1, #mask - #part) .. part end -- 32 parts from 8 local temp = {} diff --git a/nselib/http.lua b/nselib/http.lua index df89c0e9a..32038eacd 100644 --- a/nselib/http.lua +++ b/nselib/http.lua @@ -1461,12 +1461,12 @@ local function read_quoted_string(s, pos) end pos = pos + 1 pos = skip_space(s, pos) - while pos <= string.len(s) and string.sub(s, pos, pos) ~= "\"" do + while pos <= #s and string.sub(s, pos, pos) ~= "\"" do local c c = string.sub(s, pos, pos) if c == "\\" then - if pos < string.len(s) then + if pos < #s then pos = pos + 1 c = string.sub(s, pos, pos) else @@ -1477,7 +1477,7 @@ local function read_quoted_string(s, pos) chars[#chars + 1] = c pos = pos + 1 end - if pos > string.len(s) or string.sub(s, pos, pos) ~= "\"" then + if pos > #s or string.sub(s, pos, pos) ~= "\"" then return nil end @@ -1561,7 +1561,7 @@ local function read_auth_challenge(s, pos) params = {} pos = skip_space(s, pos) - while pos < string.len(s) do + while pos < #s do local name, val local tmp_pos @@ -1595,7 +1595,7 @@ local function read_auth_challenge(s, pos) pos = skip_space(s, pos) if string.sub(s, pos, pos) == "," then pos = skip_space(s, pos + 1) - if pos > string.len(s) then + if pos > #s then return nil end end @@ -1615,7 +1615,7 @@ function parse_www_authenticate(s) local pos pos = 1 - while pos <= string.len(s) do + while pos <= #s do local challenge pos, challenge = read_auth_challenge(s, pos) @@ -1696,7 +1696,7 @@ function can_use_head(host, port, result_404, path) if data.status and data.status == 200 and data.header then -- check that a body wasn't returned - if string.len(data.body) > 0 then + if #data.body > 0 then stdnse.print_debug(1, "HTTP: Warning: Host returned data when performing HEAD.") return false end @@ -1892,7 +1892,7 @@ function page_exists(data, result_404, known_404, page, displayall) if(data.status == 200) then if(result_404 == 200) then -- If the 404 response is also "200", deal with it (check if the body matches) - if(string.len(data.body) == 0) then + if(#data.body == 0) then -- I observed one server that returned a blank string instead of an error, on some occasions stdnse.print_debug(1, "HTTP: Page returned a totally empty body; page likely doesn't exist") return false diff --git a/nselib/ipOps.lua b/nselib/ipOps.lua index 3859d9b63..31befc361 100644 --- a/nselib/ipOps.lua +++ b/nselib/ipOps.lua @@ -248,7 +248,7 @@ compare_ip = function( left, op, right ) return nil, table.concat( err, " " ) end - if string.len( left ) ~= string.len( right ) then + if # left ~= # right then -- shouldn't happen... return nil, "Error in ipOps.compare_ip: Binary IP addresses were of different lengths." end @@ -262,7 +262,7 @@ compare_ip = function( left, op, right ) -- starting from the leftmost bit, subtract the bit in right from the bit in left local compare - for i = 1, string.len( left ), 1 do + for i = 1, # left , 1 do compare = tonumber( string.sub( left, i, i ) ) - tonumber( string.sub( right, i, i ) ) if compare == 1 then return true @@ -485,7 +485,7 @@ get_last_ip = function( ip, prefix ) if err then return nil, err end prefix = tonumber( prefix ) - if not prefix or ( prefix < 0 ) or ( prefix > string.len( first ) ) then + if not prefix or ( prefix < 0 ) or ( prefix > # first ) then return nil, "Error in ipOps.get_last_ip: Invalid prefix length." end @@ -534,7 +534,7 @@ ip_to_bin = function( ip ) -- padding for i, v in ipairs( t ) do - t[i] = mask:sub( 1, string.len( mask ) - string.len( v ) ) .. v + t[i] = mask:sub( 1, # mask - # v ) .. v end return hex_to_bin( table.concat( t ) ) @@ -559,9 +559,9 @@ bin_to_ip = function( binstring ) end local af - if string.len( binstring ) == 32 then + if # binstring == 32 then af = 4 - elseif string.len( binstring ) == 128 then + elseif # binstring == 128 then af = 6 else return nil, "Error in ipOps.bin_to_ip: Expected exactly 32 or 128 binary digits." @@ -609,7 +609,7 @@ hex_to_bin = function( hex ) local t, mask, binchar = {}, "0000" for hexchar in string.gmatch( hex, "%x" ) do binchar = stdnse.tobinary( tonumber( hexchar, 16 ) ) - t[#t+1] = mask:sub( 1, string.len( mask ) - string.len( binchar ) ) .. binchar + t[#t+1] = mask:sub( 1, # mask - # binchar ) .. binchar end return table.concat( t ) diff --git a/nselib/ldap.lua b/nselib/ldap.lua index 055fc915f..99a937c48 100644 --- a/nselib/ldap.lua +++ b/nselib/ldap.lua @@ -76,15 +76,15 @@ local tagEncoder = {} tagEncoder['table'] = function(self, val) if (val._ldap == '0A') then local ival = self.encodeInt(val[1]) - local len = self.encodeLength(string.len(ival)) + local len = self.encodeLength(#ival) return bin.pack('HAA', '0A', len, ival) end if (val._ldaptype) then local len - if val[1] == nil or string.len(val[1]) == 0 then + if val[1] == nil or #val[1] == 0 then return bin.pack('HC', val._ldaptype, 0) else - len = self.encodeLength(string.len(val[1])) + len = self.encodeLength(#val[1]) return bin.pack('HAA', val._ldaptype, len, val[1]) end end @@ -97,7 +97,7 @@ tagEncoder['table'] = function(self, val) if (val["_snmp"]) then tableType = bin.pack("H", val["_snmp"]) end - return bin.pack('AAA', tableType, self.encodeLength(string.len(encVal)), encVal) + return bin.pack('AAA', tableType, self.encodeLength(#encVal), encVal) end diff --git a/nselib/match.lua b/nselib/match.lua index 39d90b1d7..0224724cd 100644 --- a/nselib/match.lua +++ b/nselib/match.lua @@ -47,7 +47,7 @@ end numbytes = function(num) local n = num return function(buf) - if(string.len(buf) >=n) then + if(#buf >=n) then return n, n end return nil diff --git a/nselib/mongodb.lua b/nselib/mongodb.lua index 1a0c15a75..b867847b1 100644 --- a/nselib/mongodb.lua +++ b/nselib/mongodb.lua @@ -113,7 +113,7 @@ function toBson(dict) end end -- Get length - local length = string.len(elements) + 5 + local length = #elements + 5 if length > 4 * 1024 * 1024 then return false, "document too large - BSON documents are limited to 4 MB" @@ -563,7 +563,7 @@ function query(socket, data) end local bsonData = responseHeader["bson"] - if string.len(bsonData) == 0 then + if #bsonData == 0 then dbg("No BSon data returned ") return false, "No Bson data returned" end @@ -626,7 +626,7 @@ function test() local res res = versionQuery() print(type(res),res:len(),res) - local out= bin.unpack('C'..string.len(res),res) + local out= bin.unpack('C'..#res,res) printBuffer(res) end --test() diff --git a/nselib/msrpc.lua b/nselib/msrpc.lua index a51477739..a0b40981d 100644 --- a/nselib/msrpc.lua +++ b/nselib/msrpc.lua @@ -309,7 +309,7 @@ function call_function(smbstate, opnum, arguments) 0x00, -- Packet type (0x00 = request) 0x03, -- Packet flags (0x03 = first frag + last frag) 0x10000000, -- Data representation (big endian) - 0x18 + string.len(arguments), -- Frag length (0x18 = the size of this data) + 0x18 + #arguments, -- Frag length (0x18 = the size of this data) 0x0000, -- Auth length 0x41414141, -- Call ID (I use 'AAAA' because it's easy to recognize) 0x00000038, -- Alloc hint @@ -318,7 +318,7 @@ function call_function(smbstate, opnum, arguments) arguments ) - stdnse.print_debug(3, "MSRPC: Calling function 0x%02x with %d bytes of arguments", string.len(arguments), opnum) + stdnse.print_debug(3, "MSRPC: Calling function 0x%02x with %d bytes of arguments", #arguments, opnum) -- Pass the information up to the smb layer status, result = smb.write_file(smbstate, data, 0) @@ -391,7 +391,7 @@ function call_function(smbstate, opnum, arguments) result['arguments'] = arguments - stdnse.print_debug(3, "MSRPC: Function call successful, %d bytes of returned argumenst", string.len(result['arguments'])) + stdnse.print_debug(3, "MSRPC: Function call successful, %d bytes of returned argumenst", #result['arguments']) return true, result end @@ -4462,7 +4462,7 @@ RRAS_Opnums["RasRpcGetVersion"] = 15 function RRAS_SubmitRequest(smbstate, pReqBuffer, dwcbBufSize) --sanity check if(dwcbBufSize == nil) then - dwcbBufSize = string.len(pReqBuffer) + dwcbBufSize = #pReqBuffer end --pack the request local req_blob @@ -4581,9 +4581,9 @@ function DNSSERVER_Query(smbstate, server_name, zone, operation) srv_name_utf16 = msrpctypes.string_to_unicode(server_name, true) req_blob = bin.pack("= 16) then + if(#name >= 16) then name = string.sub(name, 1, 16) else local padding = " " @@ -41,7 +41,7 @@ function name_encode(name, scope) repeat name = name .. padding - until string.len(name) == 16 + until #name == 16 end -- Convert to uppercase @@ -49,7 +49,7 @@ function name_encode(name, scope) -- Do the L1 encoding local L1_encoded = "" - for i=1, string.len(name), 1 do + for i=1, #name, 1 do local b = string.byte(name, i) L1_encoded = L1_encoded .. string.char(bit.rshift(bit.band(b, 0xF0), 4) + 0x41) L1_encoded = L1_encoded .. string.char(bit.rshift(bit.band(b, 0x0F), 0) + 0x41) @@ -62,7 +62,7 @@ function name_encode(name, scope) -- Split the scope at its periods local piece for piece in string.gmatch(scope, "[^.]+") do - L2_encoded = L2_encoded .. string.char(string.len(piece)) .. piece + L2_encoded = L2_encoded .. string.char(#piece) .. piece end end @@ -97,15 +97,15 @@ function name_decode(encoded_name) -- Decode the scope local pos = 34 - while string.len(encoded_name) > pos do + while #encoded_name > pos do local len = string.byte(encoded_name, pos) scope = scope .. string.sub(encoded_name, pos + 1, pos + len) .. "." pos = pos + 1 + len end -- If there was a scope, remove the trailing period - if(string.len(scope) > 0) then - scope = string.sub(scope, 1, string.len(scope) - 1) + if(#scope > 0) then + scope = string.sub(scope, 1, #scope - 1) end stdnse.print_debug(3, "=> '%s'", name) diff --git a/nselib/nsedebug.lua b/nselib/nsedebug.lua index 9f1f08db7..37f9edf29 100644 --- a/nselib/nsedebug.lua +++ b/nselib/nsedebug.lua @@ -62,7 +62,7 @@ end function print_hex(str) -- Prints out the full lines - for line=1, string.len(str)/16, 1 do + for line=1, #str/16, 1 do io.write(string.format("%08x ", (line - 1) * 16)) -- Loop through the string, printing the hex @@ -86,17 +86,17 @@ function print_hex(str) end -- Prints out the final, partial line - local line = math.floor((string.len(str)/16)) + 1 + local line = math.floor((#str/16)) + 1 io.write(string.format("%08x ", (line - 1) * 16)) - for char=1, string.len(str) % 16, 1 do + for char=1, #str % 16, 1 do local ch = string.byte(str, ((line - 1) * 16) + char) io.write(string.format("%02x ", ch)) end - io.write(string.rep(" ", 16 - (string.len(str) % 16))); + io.write(string.rep(" ", 16 - (#str % 16))); io.write(" ") - for char=1, string.len(str) % 16, 1 do + for char=1, #str % 16, 1 do local ch = string.byte(str, ((line - 1) * 16) + char) if ch < 0x20 or ch > 0x7f then ch = string.byte(".", 1) @@ -105,7 +105,7 @@ function print_hex(str) end -- Print out the length - io.write(string.format("\n Length: %d [0x%x]\n", string.len(str), string.len(str))) + io.write(string.format("\n Length: %d [0x%x]\n", #str, #str)) end diff --git a/nselib/packet.lua b/nselib/packet.lua index 9e2025366..a27bbc7a3 100644 --- a/nselib/packet.lua +++ b/nselib/packet.lua @@ -128,7 +128,7 @@ Packet = {} --- Create a new Packet object. -- @param packet Binary string with packet data. -- @param packet_len Packet length. It could be more than --- string.len(packet). +-- #packet. -- @param force_continue whether an error in parsing headers should be fatal or -- not. This is especially useful when parsing ICMP packets, where a small ICMP -- payload could be a TCP header. The problem is that parsing this payload @@ -236,7 +236,7 @@ end -- @return Whether the parsing succeeded. function Packet:ip_parse(force_continue) self.ip_offset = 0 - if string.len(self.buf) < 20 then -- too short + if #self.buf < 20 then -- too short return false end self.ip_v = bit.rshift(bit.band(self:u8(self.ip_offset + 0), 0xF0), 4) @@ -391,7 +391,7 @@ end -- @return Whether the parsing succeeded. function Packet:icmp_parse(force_continue) self.icmp_offset = self.ip_data_offset - if string.len(self.buf) < self.icmp_offset + 8 then -- let's say 8 bytes minimum + if #self.buf < self.icmp_offset + 8 then -- let's say 8 bytes minimum return false end self.icmp = true @@ -403,7 +403,7 @@ function Packet:icmp_parse(force_continue) self.icmp_payload = true self.icmp_r0 = self:u32(self.icmp_offset + 4) self.icmp_payload_offset = self.icmp_offset + 8 - if string.len(self.buf) < self.icmp_payload_offset + 24 then + if #self.buf < self.icmp_payload_offset + 24 then return false end self.icmp_payload = Packet:new(self.buf:sub(self.icmp_payload_offset+1), self.packet_len - self.icmp_payload_offset, true) @@ -423,12 +423,12 @@ end function Packet:tcp_parse(force_continue) self.tcp = true self.tcp_offset = self.ip_data_offset - if string.len(self.buf) < self.tcp_offset + 4 then + if #self.buf < self.tcp_offset + 4 then return false end self.tcp_sport = self:u16(self.tcp_offset + 0) self.tcp_dport = self:u16(self.tcp_offset + 2) - if string.len(self.buf) < self.tcp_offset + 20 then + if #self.buf < self.tcp_offset + 20 then if force_continue then return true else @@ -609,12 +609,12 @@ end function Packet:udp_parse(force_continue) self.udp = true self.udp_offset = self.ip_data_offset - if string.len(self.buf) < self.udp_offset + 4 then + if #self.buf < self.udp_offset + 4 then return false end self.udp_sport = self:u16(self.udp_offset + 0) self.udp_dport = self:u16(self.udp_offset + 2) - if string.len(self.buf) < self.udp_offset + 8 then + if #self.buf < self.udp_offset + 8 then if force_continue then return true else diff --git a/nselib/smb.lua b/nselib/smb.lua index 4c74ed20e..38e71e013 100644 --- a/nselib/smb.lua +++ b/nselib/smb.lua @@ -670,7 +670,7 @@ local function smb_encode_parameters(parameters, overrides) -- Make sure we have an overrides array overrides = overrides or {} - return bin.pack("II column_width[x] then column_width[x] = elem_width end diff --git a/nselib/url.lua b/nselib/url.lua index 1c3dfa025..96a39f39d 100644 --- a/nselib/url.lua +++ b/nselib/url.lua @@ -350,5 +350,5 @@ function build_query(query) for i,v in pairs(query) do qstr = qstr .. i .. '=' .. v .. '&' end - return string.sub(qstr, 0, string.len(qstr)-1) + return string.sub(qstr, 0, #qstr-1) end