Nmap Development mailing list archives

Re: [NSE][PATCH] make --script-updatedb skip scripts it cannot load instead of failing


From: Sven Klemm <sven () c3d2 de>
Date: Tue, 23 Sep 2008 14:48:42 +0200

Hi Patrick,

currently nmap --script-updatedb fails if it cannot load all scripts. With
the attached patch it skips scripts it cannot load.

Example output:

./nmap --script-updatedb

Starting Nmap 4.76 ( http://nmap.org ) at 2008-09-23 11:07 CEST
SCRIPT ENGINE: Skipping script './scripts//ssltest.nse' because it produced
errors while loading.
SCRIPT ENGINE: Skipping script './scripts//SSH-hostkey.nse' because it
produced errors while loading.
NSE script database updated successfully.
Nmap done: 0 IP addresses (0 hosts up) scanned in 0.13 seconds

This looks ok except pcall's fourth argument is an int (a stack
position, usually 0 indicating no error function), not a pointer.

I've fixed the lua_pcall() call and I now use lua_settop() to rewind the stack. I think rewinding to an absolute position is better because I don't know what happens to the stack in the loading script. I prefered lua_settop() over lua_pop() because I thought using a negative index with lua_pop() might be confusing.

Cheers,
Sven

--
Sven Klemm
http://cthulhu.c3d2.de/~sven/

Index: nse_init.cc
===================================================================
--- nse_init.cc (revision 10330)
+++ nse_init.cc (working copy)
@@ -408,7 +408,16 @@
         luaL_error(L, "file '%s' could not be loaded", file);
       lua_pushvalue(L, -2); // push environment
       lua_setfenv(L, -2); // set it
-      lua_call(L, 0, 0);
+      if ( lua_pcall(L, 0, 0, 0) != 0 ) {
+        // skip scripts that produce errors
+        log_write(LOG_STDOUT, "%s: Skipping script '%s' because it produced errors while loading.\n", 
+          SCRIPT_ENGINE, file );
+        SCRIPT_ENGINE_VERBOSE(
+          error("%s", lua_tostring(L, -1));
+        )
+        lua_settop(L, 2); // rewind stack
+        continue;
+      }
 
       lua_getfield(L, -1, "categories");
       if (lua_isnil(L, -1))

_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://SecLists.Org

Current thread: