From 07bc658c4a8f5e3dcf7f4e0cc776f7e91222d480 Mon Sep 17 00:00:00 2001 From: dmiller Date: Wed, 29 Jun 2022 20:40:30 +0000 Subject: [PATCH] Hide error traceback for nmap.new_try() handled exceptions. Fixes #2463 --- CHANGELOG | 5 +++++ nse_main.lua | 10 +++++++++- nse_nmaplib.cc | 9 ++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 7961d4331..dbee50269 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,10 @@ #Nmap Changelog ($Id$); -*-text-*- +o [NSE][GH#2463] NSE "exception handling" with nmap.new_try() will no longer + result in a stack traceback in debug output nor a "ERROR: script execution + failed" message in script output, since the intended behavior has always been + to end the script immediately without output. [Daniel Miller] + o Upgrade libssh2 to 1.10.0. o [NSE][GH#2496] Fix newtargets support: since Nmap 7.92, scripts could not add diff --git a/nse_main.lua b/nse_main.lua index 188862e3e..42b8d5f88 100644 --- a/nse_main.lua +++ b/nse_main.lua @@ -523,7 +523,15 @@ do self.action_started = true return self:resume(timeouts); elseif not ok then - if debugging() > 0 then + -- Extend this to create new types of errors with custom handling. + -- nmap.new_try does equivalent of: error({errtype="nmap.new_try", message="TIMEOUT"}) + if type(r1) == "table" and r1.errtype == "nmap.new_try" then + -- nmap.new_try "exception" is closing the script + if debugging() > 0 then + self:d("Finished %THREAD_AGAINST. Reason: %s\n", r1.message); + end + r1 = r1.message + elseif debugging() > 0 then self:d("%THREAD_AGAINST threw an error!\n%s\n", traceback(self.co, tostring(r1))); else self:set_output("ERROR: Script execution failed (use -d to debug)"); diff --git a/nse_nmaplib.cc b/nse_nmaplib.cc index 6b7c0e1b6..1a6b30ffa 100644 --- a/nse_nmaplib.cc +++ b/nse_nmaplib.cc @@ -624,7 +624,14 @@ static int l_log_write (lua_State *L) static int finalize_cleanup (lua_State *L, int status, lua_KContext ctx) { - lua_settop(L, 2); + lua_settop(L, 2); // top of stack: error message + lua_createtable(L, 0, 2); // error object table + lua_pushliteral(L, "errtype"); + lua_pushliteral(L, "nmap.new_try"); + lua_rawset(L, -3); + lua_pushliteral(L, "message"); // stack: err(string), err(table), "message" + lua_rotate(L, -3, -1); // stack: err(table), "message", err(string) + lua_rawset(L, -3); return lua_error(L); }