diff --git a/nse_main.lua b/nse_main.lua index ce83c56ff..eba37c862 100644 --- a/nse_main.lua +++ b/nse_main.lua @@ -1114,13 +1114,15 @@ if script_database_update then sort(scripts); for i, script in ipairs(scripts) do script = Script.new(script); - sort(script.categories); - db:write('Entry { filename = "', script.basename, '", '); - db:write('categories = {'); - for j, category in ipairs(script.categories) do - db:write(' "', lower(category), '",'); + if ( script ) then + sort(script.categories); + db:write('Entry { filename = "', script.basename, '", '); + db:write('categories = {'); + for j, category in ipairs(script.categories) do + db:write(' "', lower(category), '",'); + end + db:write(' } }\n'); end - db:write(' } }\n'); end db:close(); log_write("stdout", "Script Database updated successfully."); diff --git a/nselib/afp.lua b/nselib/afp.lua index 75cf0f722..040116b9e 100644 --- a/nselib/afp.lua +++ b/nselib/afp.lua @@ -114,18 +114,13 @@ local bin = require "bin" local bit = require "bit" local nmap = require "nmap" -local openssl = require "openssl" local os = require "os" local stdnse = require "stdnse" local string = require "string" local table = require "table" _ENV = stdnse.module("afp", stdnse.seeall); -local HAVE_SSL = false - -if pcall(require,'openssl') then - HAVE_SSL = true -end +local HAVE_SSL, openssl = pcall(require,'openssl') -- Table of valid REQUESTs local REQUEST = { diff --git a/nselib/bitcoin.lua b/nselib/bitcoin.lua index 11084abfc..034ffd8a1 100644 --- a/nselib/bitcoin.lua +++ b/nselib/bitcoin.lua @@ -35,11 +35,10 @@ local bin = require "bin" local ipOps = require "ipOps" local nmap = require "nmap" -local openssl = require "openssl" local os = require "os" local stdnse = require "stdnse" local table = require "table" -stdnse.silent_require('openssl') +local openssl = stdnse.silent_require('openssl') _ENV = stdnse.module("bitcoin", stdnse.seeall) -- A class that supports the BitCoin network address structure diff --git a/nselib/http.lua b/nselib/http.lua index e5380dff4..8fc25951e 100644 --- a/nselib/http.lua +++ b/nselib/http.lua @@ -105,7 +105,6 @@ local base64 = require "base64" local comm = require "comm" local coroutine = require "coroutine" local nmap = require "nmap" -local openssl = require "openssl" local os = require "os" local stdnse = require "stdnse" local string = require "string" @@ -114,7 +113,7 @@ local url = require "url" _ENV = stdnse.module("http", stdnse.seeall) ---Use ssl if we have it -local have_ssl = (nmap.have_ssl() and pcall(require, "openssl")) +local have_ssl, openssl = pcall(require,'openssl') local USER_AGENT = stdnse.get_script_args('http.useragent') or "Mozilla/5.0 (compatible; Nmap Scripting Engine; http://nmap.org/book/nse.html)" local MAX_REDIRECT_COUNT = 5 diff --git a/nselib/iax2.lua b/nselib/iax2.lua index 57a82c990..4d23b8372 100644 --- a/nselib/iax2.lua +++ b/nselib/iax2.lua @@ -9,9 +9,9 @@ local bin = require "bin" local bit = require "bit" local math = require "math" local nmap = require "nmap" -local openssl = require "openssl" local os = require "os" local stdnse = require "stdnse" +local openssl = stdnse.silent_require "openssl" local table = require "table" _ENV = stdnse.module("iax2", stdnse.seeall) diff --git a/nselib/iscsi.lua b/nselib/iscsi.lua index cf6b7f9b9..f7a479ac7 100644 --- a/nselib/iscsi.lua +++ b/nselib/iscsi.lua @@ -40,8 +40,8 @@ local bin = require "bin" local bit = require "bit" local ipOps = require "ipOps" local nmap = require "nmap" -local openssl = require "openssl" local stdnse = require "stdnse" +local openssl = stdnse.silent_require "openssl" local string = require "string" local table = require "table" _ENV = stdnse.module("iscsi", stdnse.seeall) diff --git a/nselib/mssql.lua b/nselib/mssql.lua index 4b432e335..c24fbe3ea 100644 --- a/nselib/mssql.lua +++ b/nselib/mssql.lua @@ -107,7 +107,6 @@ local bit = require "bit" local math = require "math" local match = require "match" local nmap = require "nmap" -local openssl = require "openssl" local os = require "os" local shortport = require "shortport" local smb = require "smb" @@ -132,7 +131,7 @@ _ENV = stdnse.module("mssql", stdnse.seeall) -- -- (Patrik Karlsson, Chris Woodbury) -HAVE_SSL = (nmap.have_ssl() and pcall(require, "openssl")) +local openssl, HAVE_SSL = pcall(require, "openssl") do namedpipes = smb.namedpipes diff --git a/nselib/mysql.lua b/nselib/mysql.lua index ce6052192..1aa73bcfc 100644 --- a/nselib/mysql.lua +++ b/nselib/mysql.lua @@ -10,7 +10,6 @@ local bin = require "bin" local bit = require "bit" local nmap = require "nmap" -local openssl = require "openssl" local stdnse = require "stdnse" local string = require "string" local table = require "table" @@ -26,11 +25,7 @@ _ENV = stdnse.module("mysql", stdnse.seeall) local tab = require('tab') -local HAVE_SSL = false - -if pcall(require,'openssl') then - HAVE_SSL = true -end +local HAVE_SSL, openssl = pcall(require,'openssl') Capabilities = { diff --git a/nselib/pgsql.lua b/nselib/pgsql.lua index 65ed904aa..2873f731e 100644 --- a/nselib/pgsql.lua +++ b/nselib/pgsql.lua @@ -14,8 +14,8 @@ local bin = require "bin" local nmap = require "nmap" -local openssl = require "openssl" local stdnse = require "stdnse" +local openssl = stdnse.silent_require "openssl" local string = require "string" local table = require "table" _ENV = stdnse.module("pgsql", stdnse.seeall) diff --git a/nselib/pop3.lua b/nselib/pop3.lua index ded922644..1f45fa3af 100644 --- a/nselib/pop3.lua +++ b/nselib/pop3.lua @@ -6,18 +6,12 @@ local base64 = require "base64" local comm = require "comm" local nmap = require "nmap" -local openssl = require "openssl" local stdnse = require "stdnse" local string = require "string" local table = require "table" _ENV = stdnse.module("pop3", stdnse.seeall) -local HAVE_SSL = false - -if pcall(require,'openssl') then - HAVE_SSL = true -end - +local HAVE_SSL, openssl = pcall(require,'openssl') err = { diff --git a/nselib/sasl.lua b/nselib/sasl.lua index 10984c758..c542b583f 100644 --- a/nselib/sasl.lua +++ b/nselib/sasl.lua @@ -43,206 +43,201 @@ local bin = require "bin" local bit = require "bit" -local openssl = require "openssl" local smbauth = require "smbauth" local stdnse = require "stdnse" local string = require "string" _ENV = stdnse.module("sasl", stdnse.seeall) -local HAVE_SSL = false - +local HAVE_SSL, openssl = pcall(require, 'openssl') +if ( not(HAVE_SSL) ) then + stdnse.print_debug(1, + "sasl.lua: OpenSSL not present, SASL support limited.") +end local MECHANISMS = { } --- Calculates a DIGEST MD5 response -DigestMD5 = { - - --- Instantiates DigestMD5 - -- - -- @param chall string containing the base64 decoded challenge - -- @return a new instance of DigestMD5 - new = function(self, chall, username, password, method, uri, service, realm) - local o = { nc = 0, - chall = chall, - challnvs = {}, - username = username, - password = password, - method = method, - uri = uri, - service = service, - realm = realm } - setmetatable(o, self) - self.__index = self - o:parseChallenge() - return o - end, - - -- parses a challenge received from the server - -- takes care of both quoted and unqoted identifiers - -- regardless of what RFC says - parseChallenge = function(self) - local results = {} - local start, stop = 0,0 - while(true) do - local name, value - start, stop, name = self.chall:find("([^=]*)=%s*", stop + 1) - if ( not(start) ) then break end - if ( self.chall:sub(stop + 1, stop + 1) == "\"" ) then - start, stop, value = self.chall:find("(.-)\"", stop + 2) - else - start, stop, value = self.chall:find("([^,]*)", stop + 1) - end - self.challnvs[name:lower()] = value - start, stop = self.chall:find("%s*,%s*", stop + 1) - if ( not(start) ) then break end - end - end, - - --- Calculates the digest - calcDigest = function( self ) - local uri = self.uri or ("%s/%s"):format(self.service, "localhost") - local realm = self.realm or self.challnvs.realm or "" - local cnonce = stdnse.tohex(openssl.rand_bytes( 8 )) - local qop = "auth" - self.nc = self.nc + 1 - local A1_part1 = openssl.md5(self.username .. ":" .. (self.challnvs.realm or "") .. ":" .. self.password) - local A1 = stdnse.tohex(openssl.md5(A1_part1 .. ":" .. self.challnvs.nonce .. ':' .. cnonce)) - local A2 = stdnse.tohex(openssl.md5(("%s:%s"):format(self.method, uri))) - local digest = stdnse.tohex(openssl.md5(A1 .. ":" .. self.challnvs.nonce .. ":" .. - ("%08d"):format(self.nc) .. ":" .. cnonce .. ":" .. - qop .. ":" .. A2)) - - local response = "username=\"" .. self.username .. "\"" - response = response .. (",%s=\"%s\""):format("realm", realm) - response = response .. (",%s=\"%s\""):format("nonce", self.challnvs.nonce) - response = response .. (",%s=\"%s\""):format("cnonce", cnonce) - response = response .. (",%s=%08d"):format("nc", self.nc) - response = response .. (",%s=%s"):format("qop", "auth") - response = response .. (",%s=\"%s\""):format("digest-uri", uri) - response = response .. (",%s=%s"):format("response", digest) - response = response .. (",%s=%s"):format("charset", "utf-8") - - return response - end, - - -} - --- The NTLM class handling NTLM challenge response authentication -NTLM = { - - --- Creates a new instance of the NTLM class - -- - -- @param chall string containing the challenge received from the server - -- @param username string containing the username - -- @param password string containing the password - -- @return new instance of NTML - new = function(self, chall, username, password) - local o = { nc = 0, - chall = chall, - username = username, - password = password} - setmetatable(o, self) - self.__index = self - o:parseChallenge() - return o - end, - - --- Converst str to "unicode" (adds null bytes for every other byte) - -- @param str containing string to convert - -- @return unicode string containing the unicoded str - to_unicode = function(str) - local unicode = "" - for i = 1, #str, 1 do - unicode = unicode .. bin.pack("CRAM-MD5 mechanism. -- -- @param username string. diff --git a/nselib/sip.lua b/nselib/sip.lua index c971fb959..3c9857e16 100644 --- a/nselib/sip.lua +++ b/nselib/sip.lua @@ -44,9 +44,9 @@ local bin = require "bin" local math = require "math" local nmap = require "nmap" -local openssl = require "openssl" local os = require "os" local stdnse = require "stdnse" +local openssl = stdnse.silent_require "openssl" local string = require "string" local table = require "table" _ENV = stdnse.module("sip", stdnse.seeall) diff --git a/nselib/smbauth.lua b/nselib/smbauth.lua index c40ec5006..eef5031c3 100644 --- a/nselib/smbauth.lua +++ b/nselib/smbauth.lua @@ -82,13 +82,12 @@ local bin = require "bin" local nmap = require "nmap" -local openssl = require "openssl" local stdnse = require "stdnse" local string = require "string" local table = require "table" _ENV = stdnse.module("smbauth", stdnse.seeall) -have_ssl = (nmap.have_ssl() and pcall(require, "openssl")) +local openssl, have_ssl = pcall(require, "openssl") -- Constants local NTLMSSP_NEGOTIATE = 0x00000001 diff --git a/nselib/ssh1.lua b/nselib/ssh1.lua index 1fee0a530..3741aa3c3 100644 --- a/nselib/ssh1.lua +++ b/nselib/ssh1.lua @@ -10,8 +10,8 @@ local bin = require "bin" local bit = require "bit" local math = require "math" local nmap = require "nmap" -local openssl = require "openssl" local stdnse = require "stdnse" +local openssl = stdnse.silent_require "openssl" _ENV = stdnse.module("ssh1", stdnse.seeall) --- Retrieve the size of the packet that is being received diff --git a/nselib/vnc.lua b/nselib/vnc.lua index 4f8aedcd9..420424ea5 100644 --- a/nselib/vnc.lua +++ b/nselib/vnc.lua @@ -31,17 +31,12 @@ local bin = require "bin" local nmap = require "nmap" -local openssl = require "openssl" local stdnse = require "stdnse" local string = require "string" local table = require "table" _ENV = stdnse.module("vnc", stdnse.seeall) -local HAVE_SSL = false - -if pcall(require,'openssl') then - HAVE_SSL = true -end +local HAVE_SSL, openssl = pcall(require,'openssl') VNC = { diff --git a/nselib/wsdd.lua b/nselib/wsdd.lua index e269387fc..172328607 100644 --- a/nselib/wsdd.lua +++ b/nselib/wsdd.lua @@ -34,17 +34,12 @@ local bin = require "bin" local nmap = require "nmap" -local openssl = require "openssl" local stdnse = require "stdnse" local table = require "table" local target = require "target" _ENV = stdnse.module("wsdd", stdnse.seeall) -local HAVE_SSL = false - -if pcall(require,'openssl') then - HAVE_SSL = true -end +local HAVE_SSL, openssl = pcall(require,'openssl') -- The different probes local probes = { diff --git a/scripts/dns-nsec3-enum.nse b/scripts/dns-nsec3-enum.nse index d70b9c2be..dc6bb8970 100644 --- a/scripts/dns-nsec3-enum.nse +++ b/scripts/dns-nsec3-enum.nse @@ -2,7 +2,6 @@ local stdnse = require "stdnse" local shortport = require "shortport" local dns = require "dns" local base32 = require "base32" -local openssl = require "openssl" local msrpc = require "msrpc" -- just for random string generation local math = require "math" local bin = require "bin" @@ -10,6 +9,8 @@ local nmap = require "nmap" local string = require "string" local table = require "table" +local openssl = stdnse.silent_require "openssl" + description = [[ Tries to enumerate domain names from the DNS server that supports DNSSEC NSEC3 records. diff --git a/scripts/http-userdir-enum.nse b/scripts/http-userdir-enum.nse index dbd624e53..af7c6f710 100644 --- a/scripts/http-userdir-enum.nse +++ b/scripts/http-userdir-enum.nse @@ -3,7 +3,6 @@ local bin = require "bin" local datafiles = require "datafiles" local http = require "http" local nmap = require "nmap" -local openssl = require "openssl" local os = require "os" local shortport = require "shortport" local stdnse = require "stdnse" @@ -158,7 +157,8 @@ end function randomstring() local rnd, s, l, _ - if pcall(require, "openssl") then + local status, openssl = pcall(require, "openssl") + if status then rnd = openssl.rand_pseudo_bytes end s = rnd and rnd(8) or tostring( os.time() ) diff --git a/scripts/http-virustotal.nse b/scripts/http-virustotal.nse index 8fd7a4835..fdc041070 100644 --- a/scripts/http-virustotal.nse +++ b/scripts/http-virustotal.nse @@ -1,8 +1,8 @@ local http = require "http" local io = require "io" local json = require "json" -local openssl = require "openssl" local stdnse = require "stdnse" +local openssl = stdnse.silent_require "openssl" local tab = require "tab" local table = require "table" diff --git a/scripts/smb-ls.nse b/scripts/smb-ls.nse index b4b155ecb..4a7d90e39 100644 --- a/scripts/smb-ls.nse +++ b/scripts/smb-ls.nse @@ -3,7 +3,7 @@ local smb = require 'smb' local stdnse = require 'stdnse' local tab = require 'tab' local table = require "table" -local openssl= require 'openssl' +local openssl= stdnse.silent_require 'openssl' description = [[ Attempts to retrieve useful information about files shared on SMB volumes. diff --git a/scripts/svn-brute.nse b/scripts/svn-brute.nse index 25aecc712..0dec6524f 100644 --- a/scripts/svn-brute.nse +++ b/scripts/svn-brute.nse @@ -1,9 +1,9 @@ local brute = require "brute" local creds = require "creds" local nmap = require "nmap" -local openssl = require "openssl" local shortport = require "shortport" local stdnse = require "stdnse" +local openssl = stdnse.silent_require "openssl" description = [[ Performs brute force password auditing against Subversion source code control servers.