1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-24 00:19:01 +00:00

[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.
This commit is contained in:
david
2009-07-12 19:35:56 +00:00
parent 6a9f1caf3f
commit 276a2f195c
4 changed files with 55 additions and 5 deletions

View File

@@ -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"

View File

@@ -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

View File

@@ -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;
}

View File

@@ -23,7 +23,10 @@ function have_ssl()
--- Returns the verbosity level as a non-negative integer.
--
-- The verbosity level can be set with the <code>-v</code> option.
-- The verbosity level can be set with the <code>-v</code> option. When
-- a script is given by name with the <code>--script</code> 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()