diff --git a/CHANGELOG b/CHANGELOG
index 1b60cd7ed..cd3add2a4 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,12 @@
# Nmap Changelog ($Id$); -*-text-*-
+o [NSE] Scripts that are listed by name with the --script option now
+ have their verbosity level automatically increased by one. Many
+ will print negative results ("no infection found") at a higher
+ verbosity level. The idea is that if you ask for a script
+ specifically, you are more interested in such results.
+ [David, Patrick]
+
o [Ncat]Ncat proxy now hides the proxy's response
("HTTP/1.0 200 OK" or whatever it may be). Before, if you
retrieved a file through a proxy, it would have the "HTTP/1.0 200 OK"
diff --git a/nse_main.lua b/nse_main.lua
index d0270d24f..e534c4d5a 100644
--- a/nse_main.lua
+++ b/nse_main.lua
@@ -37,6 +37,7 @@ local YIELD = "NSE_YIELD";
local BASE = "NSE_BASE";
local WAITING_TO_RUNNING = "NSE_WAITING_TO_RUNNING";
local DESTRUCTOR = "NSE_DESTRUCTOR";
+local SELECTED_BY_NAME = "NSE_SELECTED_BY_NAME";
local _R = debug.getregistry(); -- The registry
local _G = _G;
@@ -299,6 +300,7 @@ do
license = rawget(env, "license"),
runlevel = tonumber(rawget(env, "runlevel")) or 1,
threads = {},
+ selected_by_name = false,
}, {__index = Script, __metatable = Script});
end
end
@@ -384,6 +386,11 @@ local function get_chosen_scripts (rules)
assert(type(category) == "string", "bad entry in script database");
r_categories[lower(category)] = true; -- Lowercase the entry
end
+
+ -- Was this entry selected by name with the --script option? We record
+ -- whether it was so that scripts so selected can get a verbosity boost.
+ -- See nmap.verbosity.
+ local selected_by_name = false;
-- A matching function for each script rule.
-- If the pattern directly matches a category (e.g. "all"), then
-- we return true. Otherwise we test if it is a filename or if
@@ -396,17 +403,21 @@ local function get_chosen_scripts (rules)
pattern = gsub(pattern, "[%^%$%(%)%%%.%[%]%+%-%?]", "%%%1"); -- esc magic
pattern = gsub(pattern, "%*", ".*"); -- change to Lua wildcard
pattern = "^"..pattern.."$"; -- anchor to beginning and end
- return not not find(escaped_basename, pattern);
+ local found = not not find(escaped_basename, pattern);
+ selected_by_name = selected_by_name or found;
+ return found;
end
local env = {m = m};
+ local script;
for globalized_rule, rule_table in pairs(entry_rules) do
if setfenv(rule_table.compiled_rule, env)() then -- run the compiled rule
used_rules[rule_table.original_rule] = true;
local t, path = cnse.fetchfile_absolute(filename);
if t == "file" then
if not files_loaded[path] then
- chosen_scripts[#chosen_scripts+1] = Script.new(path);
+ script = Script.new(path);
+ chosen_scripts[#chosen_scripts+1] = script;
files_loaded[path] = true;
-- do not break so other rules can be marked as used
end
@@ -416,6 +427,12 @@ local function get_chosen_scripts (rules)
end
end
end
+ if script then
+ script.selected_by_name = selected_by_name;
+ if script.selected_by_name then
+ print_debug(2, "Script %s was selected by name.", script.basename);
+ end
+ end
end
setfenv(db_closure, {Entry = entry});
@@ -431,7 +448,10 @@ local function get_chosen_scripts (rules)
if t == nil then
error("'"..rule.."' did not match a category, filename, or directory");
elseif t == "file" and not files_loaded[path] then
- chosen_scripts[#chosen_scripts+1] = Script.new(path);
+ local script = Script.new(path);
+ script.selected_by_name = true;
+ chosen_scripts[#chosen_scripts+1] = script;
+ print_debug(2, "Script %s was selected by name.", script.filename);
files_loaded[path] = true;
elseif t == "directory" then
for i, file in ipairs(cnse.dump_dir(path)) do
@@ -508,6 +528,9 @@ local function run (threads)
end
end
end
+ _R[SELECTED_BY_NAME] = function()
+ return current and current.selected_by_name;
+ end
-- Loop while any thread is running or waiting.
while next(running) or next(waiting) do
diff --git a/nse_nmaplib.cc b/nse_nmaplib.cc
index 9207a7ca0..a277ab0b1 100644
--- a/nse_nmaplib.cc
+++ b/nse_nmaplib.cc
@@ -21,6 +21,9 @@ extern "C" {
#include "nse_nmaplib.h"
#include "nse_nsock.h"
+/* This is used to index the registry in nse_main.lua. */
+#define NSE_SELECTED_BY_NAME "NSE_SELECTED_BY_NAME"
+
#define SCRIPT_ENGINE_PUSHSTRING_NOTNULL(c_str, str) if(c_str != NULL) {\
lua_pushstring(L, c_str); \
lua_setfield(L, -2, str); \
@@ -515,7 +518,21 @@ static int l_new_try (lua_State *L)
static int l_get_verbosity (lua_State *L)
{
- lua_pushnumber(L, o.verbose);
+ int verbosity;
+
+ verbosity = o.verbose;
+ /* Call the SELECTED_BY_NAME function in nse_main.lua. When a script is
+ selected by name, we lie to it and say the verbosity is one higher than it
+ really is. */
+ lua_getfield(L, LUA_REGISTRYINDEX, NSE_SELECTED_BY_NAME);
+ if (!lua_isnil(L, -1)) {
+ lua_call(L, 0, 1);
+ if (lua_toboolean(L, -1))
+ verbosity += 1;
+ }
+ lua_pop(L, 1);
+
+ lua_pushnumber(L, verbosity);
return 1;
}
diff --git a/nselib/nmap.luadoc b/nselib/nmap.luadoc
index 687691be1..1f2fef220 100644
--- a/nselib/nmap.luadoc
+++ b/nselib/nmap.luadoc
@@ -23,7 +23,10 @@ function have_ssl()
--- Returns the verbosity level as a non-negative integer.
--
--- The verbosity level can be set with the -v option.
+-- The verbosity level can be set with the -v option. When
+-- a script is given by name with the --script option, as
+-- opposed to being selected by default or by category, its verbosity
+-- level is automatically increased by one.
-- @return The verbosity level.
-- @usage if nmap.verbosity() > 0 then ... end
function verbosity()