From a0b05c0f4fca3f8ed295a2790fff6d202e0a3e7b Mon Sep 17 00:00:00 2001 From: dmiller Date: Fri, 28 Aug 2020 15:06:10 +0000 Subject: [PATCH] Require trailing '/' to match a directory name with --script. See #2051 --- docs/refguide.xml | 4 ++-- docs/scripting.xml | 4 ++-- nse_main.cc | 10 ++++++++-- nse_main.lua | 2 ++ 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/refguide.xml b/docs/refguide.xml index 54b0b70ad..83351639d 100644 --- a/docs/refguide.xml +++ b/docs/refguide.xml @@ -2285,7 +2285,7 @@ way. - + @@ -2329,7 +2329,7 @@ executable, followed by ../share/nmap (not searched on Wind -When a directory name is given, Nmap loads every file in the directory +When a directory name ending in / is given, Nmap loads every file in the directory whose name ends with .nse. All other files are ignored and directories are not searched recursively. When a filename is given, it does not have to have the .nse extension; diff --git a/docs/scripting.xml b/docs/scripting.xml index 549bcd4f9..1027549ca 100644 --- a/docs/scripting.xml +++ b/docs/scripting.xml @@ -627,7 +627,7 @@ Black Hat Briefings in 2010. - + @@ -676,7 +676,7 @@ executable, followed by ../share/nmap (not searched on Wind -When a directory name is given, Nmap loads every file in the directory +When a directory name ending in / is given, Nmap loads every file in the directory whose name ends with .nse. All other files are ignored and directories are not searched recursively. When a filename is given, it does not have to have the .nse extension; diff --git a/nse_main.cc b/nse_main.cc index 9d84d4d20..7b028d420 100644 --- a/nse_main.cc +++ b/nse_main.cc @@ -271,7 +271,8 @@ static int l_protect_xml(lua_State *L) static int nse_fetch (lua_State *L, int (*fetch)(char *, size_t, const char *)) { char path[MAXPATHLEN]; - switch (fetch(path, sizeof(path), luaL_checkstring(L, 1))) + const char *input = luaL_checkstring(L, 1); + switch (fetch(path, sizeof(path), input)) { case 0: // no such path lua_pushnil(L); @@ -282,7 +283,12 @@ static int nse_fetch (lua_State *L, int (*fetch)(char *, size_t, const char *)) lua_pushstring(L, path); break; case 2: // directory returned - lua_pushliteral(L, "directory"); + if (input[strlen(input) - 1] == '/') { + lua_pushliteral(L, "directory"); + } + else { + lua_pushliteral(L, "bare_directory"); + } lua_pushstring(L, path); break; default: diff --git a/nse_main.lua b/nse_main.lua index 1afa26f23..cdf691959 100644 --- a/nse_main.lua +++ b/nse_main.lua @@ -817,6 +817,8 @@ local function get_chosen_scripts (rules) if not (cnse.scriptversion and rule == "version") then error("'"..rule.."' did not match a category, filename, or directory"); end + elseif t == "bare_directory" then + error("directory '"..path.."' found, but will not match without '/'") elseif t == "file" and not files_loaded[path] then script_params.selection = "file path"; script_params.verbosity = true;