1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 12:41:29 +00:00

Add --script-timeout option to limit the script's runtime. Closes #330 and Fixes #234.

This commit is contained in:
abhishek
2016-08-20 18:57:47 +00:00
parent b5656da197
commit cd0b373130
5 changed files with 31 additions and 3 deletions

View File

@@ -377,6 +377,7 @@ void NmapOps::Initialize() {
scripttrace = 0; scripttrace = 0;
scriptupdatedb = 0; scriptupdatedb = 0;
scripthelp = false; scripthelp = false;
scripttimeout = 0;
chosenScripts.clear(); chosenScripts.clear();
#endif #endif
memset(&sourcesock, 0, sizeof(sourcesock)); memset(&sourcesock, 0, sizeof(sourcesock));

View File

@@ -390,6 +390,7 @@ class NmapOps {
int scripttrace; int scripttrace;
int scriptupdatedb; int scriptupdatedb;
bool scripthelp; bool scripthelp;
double scripttimeout;
void chooseScripts(char* argument); void chooseScripts(char* argument);
std::vector<std::string> chosenScripts; std::vector<std::string> chosenScripts;
#endif #endif

View File

@@ -681,6 +681,8 @@ void parse_options(int argc, char **argv) {
{"script_args_file", required_argument, 0, 0}, {"script_args_file", required_argument, 0, 0},
{"script-help", required_argument, 0, 0}, {"script-help", required_argument, 0, 0},
{"script_help", required_argument, 0, 0}, {"script_help", required_argument, 0, 0},
{"script-timeout", required_argument, 0, 0},
{"script_timeout", required_argument, 0, 0},
#endif #endif
{"ip_options", required_argument, 0, 0}, {"ip_options", required_argument, 0, 0},
{"ip-options", required_argument, 0, 0}, {"ip-options", required_argument, 0, 0},
@@ -719,6 +721,11 @@ void parse_options(int argc, char **argv) {
} else if (optcmp(long_options[option_index].name, "script-help") == 0) { } else if (optcmp(long_options[option_index].name, "script-help") == 0) {
o.scripthelp = true; o.scripthelp = true;
o.chooseScripts(optarg); o.chooseScripts(optarg);
} else if (optcmp(long_options[option_index].name, "script-timeout") == 0) {
l = tval2secs(optarg);
if ( l <= 0 )
fatal("Bogus --script-timeout argument specified");
o.scripttimeout = l;
} else } else
#endif #endif
if (optcmp(long_options[option_index].name, "max-os-tries") == 0) { if (optcmp(long_options[option_index].name, "max-os-tries") == 0) {

View File

@@ -384,6 +384,7 @@ static void open_cnse (lua_State *L)
nseU_setsfield(L, -1, "scriptargs", o.scriptargs); nseU_setsfield(L, -1, "scriptargs", o.scriptargs);
nseU_setsfield(L, -1, "scriptargsfile", o.scriptargsfile); nseU_setsfield(L, -1, "scriptargsfile", o.scriptargsfile);
nseU_setsfield(L, -1, "NMAP_URL", NMAP_URL); nseU_setsfield(L, -1, "NMAP_URL", NMAP_URL);
nseU_setnfield(L, -1, "script_timeout", o.scripttimeout);
} }

View File

@@ -385,10 +385,16 @@ do
-- prerule/postrule scripts may be timed out in the future -- prerule/postrule scripts may be timed out in the future
-- based on start time and script lifetime? -- based on start time and script lifetime?
function Thread:timed_out () function Thread:timed_out ()
if self.type == "hostrule" or self.type == "portrule" then local host_timeout, script_timeout = false, false;
return cnse.timedOut(self.host); -- checking whether user gave --script-timeout option or not
if cnse.script_timeout and cnse.script_timeout > 0 then
-- comparing script's timeout with time elapsed
script_timeout = cnse.script_timeout < os.difftime(os.time(), self.start_time)
end end
return nil; if self.type == "hostrule" or self.type == "portrule" then
host_timeout = cnse.timedOut(self.host);
end
return script_timeout or host_timeout;
end end
function Thread:start_time_out_clock () function Thread:start_time_out_clock ()
@@ -409,6 +415,12 @@ do
timeouts[self.host] = timeouts[self.host] or {}; timeouts[self.host] = timeouts[self.host] or {};
timeouts[self.host][self.co] = true; timeouts[self.host][self.co] = true;
end end
-- storing script's start time so as to account for script's timeout later
if self.worker then
self.start_time = self.parent.start_time
else
self.start_time = os.time()
end
end end
-- Remove scripts from the timeouts list and call their -- Remove scripts from the timeouts list and call their
@@ -474,6 +486,7 @@ do
script = self, script = self,
type = script_type, type = script_type,
worker = false, worker = false,
start_time = 0, --for script timeout
}; };
thread.parent = thread; thread.parent = thread;
setmetatable(thread, Thread) setmetatable(thread, Thread)
@@ -490,6 +503,7 @@ do
info = format("%s W:%s", self.id, match(tostring(co), "^thread: 0?[xX]?(.*)")); info = format("%s W:%s", self.id, match(tostring(co), "^thread: 0?[xX]?(.*)"));
parent = self, parent = self,
worker = true, worker = true,
start_time = 0,
}; };
setmetatable(thread, Worker) setmetatable(thread, Worker)
local function info () local function info ()
@@ -1175,6 +1189,10 @@ do
args[#args+1] = cnse.scriptargs; args[#args+1] = cnse.scriptargs;
end end
if cnse.script_timeout and cnse.script_timeout > 0 then
print_debug(1, "Set script-timeout as: %d seconds", cnse.script_timeout);
end
args = concat(args, ","); args = concat(args, ",");
if #args > 0 then if #args > 0 then
print_debug(1, "Arguments parsed: %s", args); print_debug(1, "Arguments parsed: %s", args);