From 90a712ae2b2471fe5b0802609ffb712a3ae38a31 Mon Sep 17 00:00:00 2001 From: batrick Date: Tue, 7 Jul 2009 00:20:52 +0000 Subject: [PATCH] Patch to libraries that were inappropriately using globals. Often two (or more) scripts using the same library would overwrite the globals each was using. This would result in (at best) an error or (at worst) a deadlock. The patch changes the global accesses to local. --- nselib/datafiles.lua | 1 + nselib/dns.lua | 4 +-- nselib/imap.lua | 2 +- nselib/ipOps.lua | 9 +++++-- nselib/match.lua | 2 +- nselib/msrpc.lua | 28 +++++++++----------- nselib/msrpcperformance.lua | 3 +-- nselib/msrpctypes.lua | 13 +++++----- nselib/netbios.lua | 4 +-- nselib/nsedebug.lua | 10 +++---- nselib/packet.lua | 4 +-- nselib/pop3.lua | 22 ++++++++-------- nselib/smb.lua | 52 +++++++++++++++++++++++-------------- nselib/smbauth.lua | 13 +++++----- nselib/snmp.lua | 2 ++ nselib/ssh1.lua | 8 +++--- nselib/ssh2.lua | 14 +++++----- nselib/tab.lua | 2 +- nselib/url.lua | 3 ++- 19 files changed, 105 insertions(+), 91 deletions(-) diff --git a/nselib/datafiles.lua b/nselib/datafiles.lua index 42baccf60..4748a7cd2 100644 --- a/nselib/datafiles.lua +++ b/nselib/datafiles.lua @@ -183,6 +183,7 @@ function parse_lines(lines, data_struct) elseif type(value) == "string" or type(value) == "function" then ret = get_array( lines, value ) elseif type(value) == "table" then + local _ _, ret[index] = parse_lines( lines, value ) else -- TEMP diff --git a/nselib/dns.lua b/nselib/dns.lua index ae12bcf13..a7de67e82 100644 --- a/nselib/dns.lua +++ b/nselib/dns.lua @@ -717,8 +717,8 @@ end -- @param flgStr Flags as a binary digit string. -- @return Table representing flags. local function decodeFlags(flgStr) - flags = {} - flgTbl = str2tbl(flgStr) + local flags = {} + local flgTbl = str2tbl(flgStr) if flgTbl[1] == '1' then flags.QR = true end if flgTbl[2] == '1' then flags.OC1 = true end if flgTbl[3] == '1' then flags.OC2 = true end diff --git a/nselib/imap.lua b/nselib/imap.lua index e280cba07..038fede5c 100644 --- a/nselib/imap.lua +++ b/nselib/imap.lua @@ -21,7 +21,7 @@ function capabilities(host, port) local proto = (port.version and port.version.service_tunnel == "ssl" and "ssl") or "tcp" if not socket:connect(host.ip, port.number, proto) then return nil, "Could Not Connect" end - status, line = socket:receive_lines(1) + local status, line = socket:receive_lines(1) if not string.match(line, "^[%*] OK") then return nil, "No Response" end socket:send("a001 CAPABILITY\r\n") diff --git a/nselib/ipOps.lua b/nselib/ipOps.lua index 3c7f5c0de..e0468be7e 100644 --- a/nselib/ipOps.lua +++ b/nselib/ipOps.lua @@ -31,6 +31,7 @@ module ( "ipOps" ) -- @return True or false (or nil in case of an error). -- @return String error message in case of an error. isPrivate = function( ip ) + local err ip, err = expand_ip( ip ) if err then return nil, err end @@ -77,7 +78,7 @@ todword = function( ip ) return nil, "Error in ipOps.todword: Expected IPv4 address." end - local n, ret = {} + local n, ret, err = {} n, err = get_parts_as_number( ip ) if err then return nil, err end @@ -104,6 +105,7 @@ end -- nil in case of an error). -- @return String error message in case of an error. get_parts_as_number = function( ip ) + local err ip, err = expand_ip( ip ) if err then return nil, err end @@ -250,6 +252,7 @@ end -- nil in case of an error). -- @return String error message in case of an error. expand_ip = function( ip ) + local err if type( ip ) ~= "string" or ip == "" then return nil, "Error in ipOps.expand_ip: Expected IP address as a string." @@ -427,6 +430,7 @@ end -- digits (or nil in case of an error). -- @return String error message in case of an error. ip_to_bin = function( ip ) + local err ip, err = expand_ip( ip ) if err then return nil, err end @@ -473,6 +477,7 @@ bin_to_ip = function( binstring ) return nil, "Error in ipOps.bin_to_ip: Expected string of binary digits." end + local af if string.len( binstring ) == 32 then af = 4 elseif string.len( binstring ) == 128 then @@ -481,7 +486,7 @@ bin_to_ip = function( binstring ) return nil, "Error in ipOps.bin_to_ip: Expected exactly 32 or 128 binary digits." end - t = {} + local t = {} if af == 6 then local pattern = string.rep( "[01]", 16 ) for chunk in string.gmatch( binstring, pattern ) do diff --git a/nselib/match.lua b/nselib/match.lua index 315983cae..f28cfafee 100644 --- a/nselib/match.lua +++ b/nselib/match.lua @@ -29,7 +29,7 @@ regex = function(pattern) local r = pcre.new(pattern, 0,"C") return function(buf) - s,e = r:exec(buf, 0,0); + local s,e = r:exec(buf, 0,0); return s,e end end diff --git a/nselib/msrpc.lua b/nselib/msrpc.lua index 90e4b80d7..6810d681a 100644 --- a/nselib/msrpc.lua +++ b/nselib/msrpc.lua @@ -2122,6 +2122,7 @@ function winreg_queryvalue(smbstate, handle, value) -- Format the type properly and put it in "value" if(result['data'] ~= nil) then + local _ if(result['type'] == "REG_DWORD") then _, result['value'] = bin.unpack(" 0x7f then ch = string.byte(".", 1) end @@ -84,18 +84,18 @@ function print_hex(str) end -- Prints out the final, partial line - line = math.floor((string.len(str)/16)) + 1 + local line = math.floor((string.len(str)/16)) + 1 io.write(string.format("%08x ", (line - 1) * 16)) for char=1, string.len(str) % 16, 1 do - ch = string.byte(str, ((line - 1) * 16) + char) + 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(" ") for char=1, string.len(str) % 16, 1 do - ch = string.byte(str, ((line - 1) * 16) + char) + local ch = string.byte(str, ((line - 1) * 16) + char) if ch < 0x20 or ch > 0x7f then ch = string.byte(".", 1) end diff --git a/nselib/packet.lua b/nselib/packet.lua index 70928760e..f40fd0b0f 100644 --- a/nselib/packet.lua +++ b/nselib/packet.lua @@ -285,14 +285,14 @@ end --- Set the source IP address. -- @param binip The source IP address as a byte string. function Packet:ip_set_bin_src(binip) - nrip = u32(binip, 0) + local nrip = u32(binip, 0) self:set_u32(self.ip_offset + 12, nrip) self.ip_bin_src = self:raw(self.ip_offset + 12,4) -- raw 4-bytes string end --- Set the destination IP address. -- @param binip The destination IP address as a byte string. function Packet:ip_set_bin_dst(binip) - nrip = u32(binip, 0) + local nrip = u32(binip, 0) self:set_u32(self.ip_offset + 16, nrip) self.ip_bin_dst = self:raw(self.ip_offset + 16,4) end diff --git a/nselib/pop3.lua b/nselib/pop3.lua index c182b12e5..566504c66 100644 --- a/nselib/pop3.lua +++ b/nselib/pop3.lua @@ -43,7 +43,7 @@ end -- @return Error code if status is false. function login_user(socket, user, pw) socket:send("USER " .. user .. "\r\n") - status, line = socket:receive_lines(1) + local status, line = socket:receive_lines(1) if not stat(line) then return false, err.user_error end socket:send("PASS " .. pw .. "\r\n") @@ -67,7 +67,7 @@ function login_sasl_plain(socket, user, pw) local auth64 = base64.enc(user .. "\0" .. user .. "\0" .. pw) socket:send("AUTH PLAIN " .. auth64 .. "\r\n") - status, line = socket:receive_lines(1) + local status, line = socket:receive_lines(1) if stat(line) then return true, err.none @@ -91,14 +91,14 @@ function login_sasl_login(socket, user, pw) socket:send("AUTH LOGIN\r\n") - status, line = socket:receive_lines(1) + local status, line = socket:receive_lines(1) if not base64.dec(string.sub(line, 3)) == "User Name:" then return false, err.userError end socket:send(user64) - status, line = socket:receive_lines(1) + local status, line = socket:receive_lines(1) if not base64.dec(string.sub(line, 3)) == "Password:" then return false, err.userError @@ -106,7 +106,7 @@ function login_sasl_login(socket, user, pw) socket:send(pw64) - status, line = socket:receive_lines(1) + local status, line = socket:receive_lines(1) if stat(line) then return true, err.none @@ -129,7 +129,7 @@ function login_apop(socket, user, pw, challenge) local apStr = stdnse.tohex(openssl.md5(challenge .. pw)) socket:send(("APOP %s %s\r\n"):format(user, apStr)) - status, line = socket:receive_lines(1) + local status, line = socket:receive_lines(1) if (stat(line)) then return true, err.none @@ -152,14 +152,14 @@ function capabilities(host, port) local opts = {timeout=10000, recv_before=true} local i = 1 - socket, line, bopt, first_line = comm.tryssl(host, port, "CAPA\r\n" , opts) + local socket, line, bopt, first_line = comm.tryssl(host, port, "CAPA\r\n" , opts) if not socket then return nil, "Could Not Connect" end if not stat(first_line) then return nil, "No Response" end if string.find(first_line, "<[%p%w]+>") then capas.APOP = true end - lines = stdnse.strsplit("\r\n",line) - line = lines[1] + local lines = stdnse.strsplit("\r\n",line) + local line = lines[1] if not stat(line) then capas.capa = false @@ -199,7 +199,7 @@ function login_sasl_crammd5(socket, user, pw) socket:send("AUTH CRAM-MD5\r\n") - status, line = socket:receive_lines(1) + local status, line = socket:receive_lines(1) local challenge = base64.dec(string.sub(line, 3)) @@ -207,7 +207,7 @@ function login_sasl_crammd5(socket, user, pw) local authStr = base64.enc(user .. " " .. digest) socket:send(authStr .. "\r\n") - status, line = socket:receive_lines(1) + local status, line = socket:receive_lines(1) if stat(line) then return true, err.none diff --git a/nselib/smb.lua b/nselib/smb.lua index 4196db07d..470bb24ec 100644 --- a/nselib/smb.lua +++ b/nselib/smb.lua @@ -309,7 +309,7 @@ function add_account(host, username, password) if(string.lower(username) ~= "guest" and string.lower(username) ~= "") then -- Save the new account if this is our first one, or our other account isn't an admin if(nmap.registry[host.ip]['smbaccount'] == nil or nmap.registry[host.ip]['smbaccount']['is_admin'] == false) then - local result + local result, _ nmap.registry[host.ip]['smbaccount'] = {} nmap.registry[host.ip]['smbaccount']['username'] = username @@ -587,7 +587,7 @@ function start_netbios(host, port, name) -- If all else fails, use each substring of the DNS name (this is a HUGE hack, but is actually -- a recommended way of doing this!) if(host.name ~= nil and host.name ~= "") then - new_names = get_subnames(host.name) + local new_names = get_subnames(host.name) for i = 1, #new_names, 1 do names[#names + 1] = new_names[i] end @@ -604,7 +604,7 @@ function start_netbios(host, port, name) -- Some debug information stdnse.print_debug(1, "SMB: Trying to start NetBIOS session with name = '%s'", name) -- Request a NetBIOS session - session_request = bin.pack(">CCSzz", + local session_request = bin.pack(">CCSzz", 0x81, -- session request 0x00, -- flags 0x44, -- length @@ -999,7 +999,7 @@ function negotiate_protocol(smb) -- Send the negotiate request stdnse.print_debug(2, "SMB: Sending SMB_COM_NEGOTIATE") - result, err = smb_send(smb, header, parameters, data) + local result, err = smb_send(smb, header, parameters, data) if(status == false) then return false, err end @@ -1011,6 +1011,7 @@ function negotiate_protocol(smb) end -- Parse out the header + local uid, tid, header4 pos, header1, header2, header3, header4, command, status, flags, flags2, pid_high, signature, unused, tid, pid, uid, mid = bin.unpack("I", buffer ) - padding = 8 - payload_length % 8 + local padding = 8 - payload_length % 8 assert(payload_length) packet_length = buffer:len() if payload_length + 4 + padding > packet_length then return nil end @@ -42,7 +42,7 @@ end -- @return status True or false -- @return packet The packet received receive_ssh_packet = function( socket ) - status, packet = socket:receive_buf(check_packet_length) + local status, packet = socket:receive_buf(check_packet_length) return status, packet end @@ -55,7 +55,7 @@ end -- fingerprint. fetch_host_key = function(host, port) local socket = nmap.new_socket() - local status + local status, _ status = socket:connect(host.ip, port.number) if not status then return end @@ -169,7 +169,7 @@ fingerprint_visual = function( fingerprint, algorithm, bits ) end -- we start in the center and mark it - x, y = math.ceil(fieldsize_x/2), math.ceil(fieldsize_y/2) + local x, y = math.ceil(fieldsize_x/2), math.ceil(fieldsize_y/2) field[x][y] = #characters - 1; -- iterate over fingerprint diff --git a/nselib/ssh2.lua b/nselib/ssh2.lua index 3dc6b138f..d8dc1aa55 100644 --- a/nselib/ssh2.lua +++ b/nselib/ssh2.lua @@ -43,7 +43,7 @@ end -- @return status True or false -- @return packet The packet received transport.receive_packet = function( socket ) - status, packet = socket:receive_buf(check_packet_length) + local status, packet = socket:receive_buf(check_packet_length) return status, packet end @@ -95,12 +95,12 @@ end --- Build a kex_init packet. transport.kex_init = function( cookie, options ) options = options or {} - kex_algorithms = "diffie-hellman-group1-sha1" - host_key_algorithms = options['host_key_algorithms'] or "ssh-dss,ssh-rsa" - encryption_algorithms = "aes128-cbc,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr" - mac_algorithms = "hmac-md5,hmac-sha1,hmac-ripemd160" - compression_algorithms = "none" - languages = "" + local kex_algorithms = "diffie-hellman-group1-sha1" + local host_key_algorithms = options['host_key_algorithms'] or "ssh-dss,ssh-rsa" + local encryption_algorithms = "aes128-cbc,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr" + local mac_algorithms = "hmac-md5,hmac-sha1,hmac-ripemd160" + local compression_algorithms = "none" + local languages = "" local payload = bin.pack( ">cAaa", SSH2.SSH_MSG_KEXINIT, cookie, kex_algorithms, host_key_algorithms ) payload = payload .. bin.pack( ">aa", encryption_algorithms, encryption_algorithms ) diff --git a/nselib/tab.lua b/nselib/tab.lua index d65f826c1..d095dcbbd 100644 --- a/nselib/tab.lua +++ b/nselib/tab.lua @@ -113,7 +113,7 @@ function dump(t) for i=1,t['rows'] do for x=1, t['cols'] do if t[i][x] ~= nil then - length = string.len(t[i][x]) + local length = string.len(t[i][x]) table = table .. t[i][x] table = table .. string.rep(' ', col_len[x]-length) end diff --git a/nselib/url.lua b/nselib/url.lua index 4ac060c81..2dba368b3 100644 --- a/nselib/url.lua +++ b/nselib/url.lua @@ -213,6 +213,7 @@ end -- @return The corresponding absolute URL. ----------------------------------------------------------------------------- function absolute(base_url, relative_url) + local base_parsed; if type(base_url) == "table" then base_parsed = base_url base_url = build(base_parsed) @@ -313,7 +314,7 @@ function parse_query(query) query = string.gsub(query, "<", "<") query = string.gsub(query, ">", ">") - function ginsert(qstr) + local function ginsert(qstr) local first, last = string.find(qstr, "=") if first then parsed[string.sub(qstr, 0, first-1)] = string.sub(qstr, first+1)