diff --git a/nse_main.cc b/nse_main.cc index d3855b6a2..64a4d50f3 100644 --- a/nse_main.cc +++ b/nse_main.cc @@ -629,10 +629,14 @@ static int run_main (lua_State *L) lua_newtable(L); set_hostinfo(L, target); lua_rawseti(L, targets_table, lua_rawlen(L, targets_table) + 1); - if (TargetName != NULL && strcmp(TargetName, "") != 0) + /* Index this Target in NSE_CURRENT_HOSTS under targetname and IP so we can + * retrieve it later */ + if (TargetName != NULL && strcmp(TargetName, "") != 0) { lua_pushstring(L, TargetName); - else - lua_pushstring(L, targetipstr); + lua_pushlightuserdata(L, target); + lua_rawset(L, current_hosts); /* add to NSE_CURRENT_HOSTS */ + } + lua_pushstring(L, targetipstr); lua_pushlightuserdata(L, target); lua_rawset(L, current_hosts); /* add to NSE_CURRENT_HOSTS */ } diff --git a/nse_utility.cc b/nse_utility.cc index 3c0e88abe..058878fb5 100644 --- a/nse_utility.cc +++ b/nse_utility.cc @@ -184,16 +184,19 @@ Target *nseU_gettarget (lua_State *L, int idx) lua_getfield(L, idx, "ip"); if (!(lua_isstring(L, -2) || lua_isstring(L, -1))) luaL_error(L, "host table does not have a 'ip' or 'targetname' field"); - if (lua_isstring(L, -2)) /* targetname */ + /* IP is preferred to targetname because it is more unique. Really, though, a + * user can scan the same IP or targetname multiple times, and NSE will get + * all mixed up. */ + if (lua_isstring(L, -1)) /* ip */ { - nse_gettarget(L, -2); /* use targetname */ + nse_gettarget(L, -1); /* use ip */ if (lua_islightuserdata(L, -1)) goto done; else lua_pop(L, 1); } - if (lua_isstring(L, -1)) /* ip */ - nse_gettarget(L, -1); /* use ip */ + if (lua_isstring(L, -2)) /* targetname */ + nse_gettarget(L, -2); /* use targetname */ if (!lua_islightuserdata(L, -1)) luaL_argerror(L, 1, "host is not being processed right now"); done: