diff --git a/nselib/data/psexec/backdoor.lua b/nselib/data/psexec/backdoor.lua index 72885a77b..062977946 100644 --- a/nselib/data/psexec/backdoor.lua +++ b/nselib/data/psexec/backdoor.lua @@ -1,4 +1,3 @@ -module(... or "backdoor", package.seeall) ---This config file is designed for adding a backdoor to the system. It has a few -- options by default, only one enabled by default. I suggest -- diff --git a/nselib/data/psexec/default.lua b/nselib/data/psexec/default.lua index f7e6ed69f..3cd3e17ef 100644 --- a/nselib/data/psexec/default.lua +++ b/nselib/data/psexec/default.lua @@ -1,4 +1,3 @@ -module(... or "network", package.seeall) ---This is the default configuration file. It simply runs some built-in Window -- programs to gather information about the remote system. It's intended to be -- simple, demonstrate some of the concepts, and not break/alte anything. diff --git a/nselib/data/psexec/drives.lua b/nselib/data/psexec/drives.lua index b749f679d..ddd10350c 100644 --- a/nselib/data/psexec/drives.lua +++ b/nselib/data/psexec/drives.lua @@ -1,4 +1,3 @@ -module(... or "drive", package.seeall) ---This configuration file pulls info about a given harddrive -- Any variable in the 'config' table in smb-psexec.nse can be overriden in the diff --git a/nselib/data/psexec/examples.lua b/nselib/data/psexec/examples.lua index 9b23dbc94..7c034f7a2 100644 --- a/nselib/data/psexec/examples.lua +++ b/nselib/data/psexec/examples.lua @@ -1,4 +1,3 @@ -module(... or "default", package.seeall) ---This configuration file contains the examples given in smb-psexec.nse. -- Any variable in the 'config' table in smb-psexec.nse can be overriden in the diff --git a/nselib/data/psexec/experimental.lua b/nselib/data/psexec/experimental.lua index 005a22b4c..e4264067e 100644 --- a/nselib/data/psexec/experimental.lua +++ b/nselib/data/psexec/experimental.lua @@ -1,4 +1,3 @@ -module(... or "experimental", package.seeall) ---This is the configuration file for modules that aren't quite ready for prime -- time yet. diff --git a/nselib/data/psexec/network.lua b/nselib/data/psexec/network.lua index 0affb8890..b134dd941 100644 --- a/nselib/data/psexec/network.lua +++ b/nselib/data/psexec/network.lua @@ -1,4 +1,3 @@ -module(... or "default", package.seeall) ---More verbose network scripts -- Any variable in the 'config' table in smb-psexec.nse can be overriden in the diff --git a/nselib/data/psexec/pwdump.lua b/nselib/data/psexec/pwdump.lua index 23f0c040d..3ee1594be 100644 --- a/nselib/data/psexec/pwdump.lua +++ b/nselib/data/psexec/pwdump.lua @@ -1,4 +1,3 @@ -module(... or "pwdump", package.seeall) ---This config file is designed for running password-dumping scripts. So far, -- it supports pwdump6 2.0.0 and fgdump. -- diff --git a/nselib/smb.lua b/nselib/smb.lua index 419da0b5d..2ffc7e3bd 100644 --- a/nselib/smb.lua +++ b/nselib/smb.lua @@ -2209,9 +2209,19 @@ function file_upload(host, localfile, share, remotefile, overrides, encoded) local status, err, smbstate local chunk = 1024 - local filename = nmap.fetchfile(localfile) - if(filename == nil) then - return false, "Couldn't find the file" + -- Attempt to open a handle to the file without adding a path to it + local handle = io.open(localfile, "r") + + -- If the open failed, try to search for the file + if(not(handle)) then + stdnse.print_debug(1, "Couldn't open %s directly, searching Nmap's paths...", localfile) + local filename = nmap.fetchfile(localfile) + + -- Check if it was found + if(filename == nil) then + return false, string.format("Couldn't find the file to upload (%s)", localfile) + end + handle = io.open(filename, "r") end -- Create the SMB session @@ -2220,10 +2230,9 @@ function file_upload(host, localfile, share, remotefile, overrides, encoded) return false, smbstate end - local handle = io.open(filename, "r") - local data = handle:read(chunk) local i = 0 + local data = handle:read(chunk) while(data ~= nil and #data > 0) do if(encoded) then diff --git a/scripts/smb-psexec.nse b/scripts/smb-psexec.nse index 141d974f5..ed326228d 100644 --- a/scripts/smb-psexec.nse +++ b/scripts/smb-psexec.nse @@ -661,10 +661,10 @@ end --@param config A table to fill with configuration values. --@return status true or false --@return config The configuration table or an error message. +--require 'nsedebug' local function get_config(host, config) local status local filename = nmap.registry.args.config - local settings_file config.enabled_modules = {} config.disabled_modules = {} @@ -676,11 +676,17 @@ local function get_config(host, config) -- Load the config file stdnse.print_debug(1, "smb-psexec: Attempting to load config file: %s", filename) - settings_file = require(string.sub(filename, 1, #filename - 4)) - if(not(settings_file)) then - return false, "Couldn't load the configuration file" + local file = loadfile(filename) + if(not(file)) then + return false, "Couldn't load module file:\n" .. filename end + -- Run the config file + setfenv(file, setmetatable({modules = {}; overrides = {}; module = function() stdnse.print_debug(1, "WARNING: Selected config file contains an unnecessary call to module()") end}, {__index = _G})) + file() + local modules = getfenv(file)["modules"] + local overrides = getfenv(file)["overrides"] + -- Generate a cipher key if(nmap.registry.args.nocipher == "1" or nmap.registry.args.nocipher == "true") then config.key = "" @@ -717,14 +723,21 @@ local function get_config(host, config) return false, service_name end + -- Make sure the modules loaded properly + -- NOTE: If you're here because of an error that 'modules' is undefined, it's likely because your configuration file doesn't have a + -- proper modules table, or your configuration file has a module() declaration at the top. + if(not(modules) or #modules == 0) then + return false, string.format("Configuration file (%s) doesn't have a proper 'modules' table.", filename) + end + -- Make sure we got a proper modules array - if(type(settings_file.modules) ~= "table") then + if(type(modules) ~= "table") then return false, string.format("The chosen configuration file, %s.lua, doesn't have a proper 'modules' table. If possible, it should be modified to have a public array called 'modules' that contains a list of all modules that will be run.", filename) end -- Loop through the modules for some pre-processing stdnse.print_debug(1, "smb-psexec: Verifying uploadable executables exist") - for i, mod in ipairs(settings_file.modules) do + for i, mod in ipairs(modules) do local enabled = true -- Do some sanity checking if(mod.program == nil) then @@ -877,8 +890,8 @@ local function get_config(host, config) stdnse.print_debug(1, "smb-psexec: Timeout waiting for a response is %d seconds", config.timeout) -- Do config overrides - if(settings_file.overrides) then - config = do_overrides(config, settings_file.overrides) + if(overrides) then + config = do_overrides(config, overrides) end -- Replace variable values in the configuration (this has to go last)