1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-29 02:49:01 +00:00

Replace the mix of spaces and tabs in the main NSE files with uniform two-space

indents, which seems to be the dominant style.
This commit is contained in:
david
2008-11-18 21:11:27 +00:00
parent 85deff9ede
commit 20cf487b33
7 changed files with 939 additions and 952 deletions

View File

@@ -2,63 +2,63 @@
#include "output.h"
void l_dumpStack(lua_State *L) {
int stack_height = lua_gettop(L);
int i;
log_write(LOG_PLAIN, "-== Stack Dump Begin ==-\n");
for(i = -1; i >= 0 - stack_height; i--) {
log_write(LOG_PLAIN, "%d: ", i);
l_dumpValue(L, i);
}
int stack_height = lua_gettop(L);
int i;
log_write(LOG_PLAIN, "-== Stack Dump End ==-\n");
log_write(LOG_PLAIN, "-== Stack Dump Begin ==-\n");
for(i = -1; i >= 0 - stack_height; i--) {
log_write(LOG_PLAIN, "%d: ", i);
l_dumpValue(L, i);
}
log_write(LOG_PLAIN, "-== Stack Dump End ==-\n");
}
void l_dumpValue(lua_State *L, int i) {
switch (lua_type(L, i))
{
case LUA_TTABLE:
l_dumpTable(L, i);
break;
case LUA_TFUNCTION:
l_dumpFunction(L, i);
break;
case LUA_TSTRING:
log_write(LOG_PLAIN, "string '%s'\n", lua_tostring(L, i));
break;
case LUA_TBOOLEAN:
log_write(LOG_PLAIN, "boolean: %s\n",
lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER:
log_write(LOG_PLAIN, "number: %g\n", lua_tonumber(L, i));
break;
default:
log_write(LOG_PLAIN, "%s\n", lua_typename(L, lua_type(L, i)));
}
switch (lua_type(L, i))
{
case LUA_TTABLE:
l_dumpTable(L, i);
break;
case LUA_TFUNCTION:
l_dumpFunction(L, i);
break;
case LUA_TSTRING:
log_write(LOG_PLAIN, "string '%s'\n", lua_tostring(L, i));
break;
case LUA_TBOOLEAN:
log_write(LOG_PLAIN, "boolean: %s\n",
lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER:
log_write(LOG_PLAIN, "number: %g\n", lua_tonumber(L, i));
break;
default:
log_write(LOG_PLAIN, "%s\n", lua_typename(L, lua_type(L, i)));
}
}
void l_dumpTable(lua_State *L, int index) {
log_write(LOG_PLAIN, "table\n");
lua_pushnil(L);
log_write(LOG_PLAIN, "table\n");
lua_pushnil(L);
if (index<0) --index;
while(lua_next(L, index) != 0)
{
l_dumpValue(L, -2);
l_dumpValue(L, -1);
lua_pop(L, 1);
}
if (index<0) --index;
while(lua_next(L, index) != 0)
{
l_dumpValue(L, -2);
l_dumpValue(L, -1);
lua_pop(L, 1);
}
}
void l_dumpFunction(lua_State *L, int index) {
// lua_Debug ar;
// lua_Debug ar;
log_write(LOG_PLAIN, "function\n");
log_write(LOG_PLAIN, "function\n");
// lua_pushvalue(L, index);
// lua_getinfo(L, ">n", &ar);
//
// log_write(LOG_PLAIN, "\tname: %s %s\n", ar.namewhat, ar.name);
fflush(stdout);
// lua_pushvalue(L, index);
// lua_getinfo(L, ">n", &ar);
//
// log_write(LOG_PLAIN, "\tname: %s %s\n", ar.namewhat, ar.name);
fflush(stdout);
}

233
nse_fs.cc
View File

@@ -1,5 +1,5 @@
#ifndef WIN32
#include "dirent.h"
#include "dirent.h"
#endif
#include "errno.h"
@@ -12,13 +12,13 @@
extern NmapOps o;
static bool filename_is_absolute(const char *file) {
if (file[0] == '/')
return true;
if (file[0] == '/')
return true;
#ifdef WIN32
if ((file[0] != '\0' && file[1] == ':') || file[0] == '\\')
return true;
if ((file[0] != '\0' && file[1] == ':') || file[0] == '\\')
return true;
#endif
return false;
return false;
}
/* This is simply the most portable way to check
@@ -37,154 +37,149 @@ int nse_check_extension (const char* ext, const char* path)
}
int nse_fetchfile(char *path, size_t path_len, const char *file) {
int type = nmap_fetchfile(path, path_len, file);
int type = nmap_fetchfile(path, path_len, file);
// lets look in <nmap>/scripts too
if(type == 0) {
std::string alt_path = std::string(SCRIPT_ENGINE_LUA_DIR) + std::string(file);
type = nmap_fetchfile(path, path_len, alt_path.c_str());
}
// lets look in <nmap>/scripts too
if(type == 0) {
std::string alt_path = std::string(SCRIPT_ENGINE_LUA_DIR) + std::string(file);
type = nmap_fetchfile(path, path_len, alt_path.c_str());
}
return type;
return type;
}
/* This is a modification of nse_fetchfile that first looks for an
* absolute file name.
*/
int nse_fetchfile_absolute(char *path, size_t path_len, const char *file) {
if (filename_is_absolute(file)) {
if (o.debugging > 1)
log_write(LOG_STDOUT, "%s: Trying absolute path %s\n", SCRIPT_ENGINE, file);
Strncpy(path, file, path_len);
return nmap_fileexistsandisreadable(file);
}
if (filename_is_absolute(file)) {
if (o.debugging > 1)
log_write(LOG_STDOUT, "%s: Trying absolute path %s\n", SCRIPT_ENGINE, file);
Strncpy(path, file, path_len);
return nmap_fileexistsandisreadable(file);
}
return nse_fetchfile(path, path_len, file);
return nse_fetchfile(path, path_len, file);
}
#ifdef WIN32
int nse_scandir (lua_State *L) {
HANDLE dir;
WIN32_FIND_DATA entry;
std::string path;
BOOL morefiles = FALSE;
const char *dirname = luaL_checkstring(L, 1);
int files_or_dirs = luaL_checkint(L, 2);
HANDLE dir;
WIN32_FIND_DATA entry;
std::string path;
BOOL morefiles = FALSE;
const char *dirname = luaL_checkstring(L, 1);
int files_or_dirs = luaL_checkint(L, 2);
lua_createtable(L, 100, 0); // 100 files average
lua_createtable(L, 100, 0); // 100 files average
dir = FindFirstFile((std::string(dirname) + "\\*").c_str(), &entry);
dir = FindFirstFile((std::string(dirname) + "\\*").c_str(), &entry);
if (dir == INVALID_HANDLE_VALUE)
{
error("%s: No files in '%s\\*'", SCRIPT_ENGINE, dirname);
return SCRIPT_ENGINE_ERROR;
}
if (dir == INVALID_HANDLE_VALUE)
{
error("%s: No files in '%s\\*'", SCRIPT_ENGINE, dirname);
return SCRIPT_ENGINE_ERROR;
}
while(!(morefiles == FALSE && GetLastError() == ERROR_NO_MORE_FILES)) {
// if we are looking for files and this file doesn't end with .nse or
// is a directory, then we don't look further at it
if(files_or_dirs == FILES) {
if(!(
(nse_check_extension(SCRIPT_ENGINE_EXTENSION, entry.cFileName))
&& !(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
)) {
morefiles = FindNextFile(dir, &entry);
continue;
}
while(!(morefiles == FALSE && GetLastError() == ERROR_NO_MORE_FILES)) {
// if we are looking for files and this file doesn't end with .nse or
// is a directory, then we don't look further at it
if(files_or_dirs == FILES) {
if(!((nse_check_extension(SCRIPT_ENGINE_EXTENSION, entry.cFileName))
&& !(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
)) {
morefiles = FindNextFile(dir, &entry);
continue;
}
// if we are looking for dirs and this dir
// isn't a directory, then we don't look further at it
} else if(files_or_dirs == DIRS) {
if(!(
(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
)) {
morefiles = FindNextFile(dir, &entry);
continue;
}
// they have passed an invalid value for files_or_dirs
} else {
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
}
// if we are looking for dirs and this dir
// isn't a directory, then we don't look further at it
} else if(files_or_dirs == DIRS) {
if(!(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
morefiles = FindNextFile(dir, &entry);
continue;
}
// otherwise we add it to the results
// we assume that dirname ends with a directory separator of some kind
path = std::string(dirname) + "\\" + std::string(entry.cFileName);
lua_pushstring(L, path.c_str());
lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
morefiles = FindNextFile(dir, &entry);
}
// they have passed an invalid value for files_or_dirs
} else {
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
}
// otherwise we add it to the results
// we assume that dirname ends with a directory separator of some kind
path = std::string(dirname) + "\\" + std::string(entry.cFileName);
lua_pushstring(L, path.c_str());
lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
morefiles = FindNextFile(dir, &entry);
}
return 1;
return 1;
}
#else
int nse_scandir (lua_State *L) {
DIR* dir;
struct dirent* entry;
struct stat stat_entry;
const char *dirname = luaL_checkstring(L, 1);
int files_or_dirs = luaL_checkint(L, 2);
DIR* dir;
struct dirent* entry;
struct stat stat_entry;
const char *dirname = luaL_checkstring(L, 1);
int files_or_dirs = luaL_checkint(L, 2);
lua_createtable(L, 100, 0); // 100 files average
dir = opendir(dirname);
if(dir == NULL) {
error("%s: Could not open directory '%s'.", SCRIPT_ENGINE, dirname);
return SCRIPT_ENGINE_ERROR;
}
// note that if there is a symlink in the dir, we have to rely on
// the .nse extension
// if they provide a symlink to a dir which ends with .nse, things
// break :/
while((entry = readdir(dir)) != NULL) {
std::string path = std::string(dirname) + "/" + std::string(entry->d_name);
if(stat(path.c_str(), &stat_entry) != 0)
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
dir = opendir(dirname);
if(dir == NULL) {
error("%s: Could not open directory '%s'.", SCRIPT_ENGINE, dirname);
return SCRIPT_ENGINE_ERROR;
}
// if we are looking for files and this file doesn't end with .nse and
// isn't a file or a link, then we don't look further at it
if(files_or_dirs == FILES) {
if(!(
(nse_check_extension(SCRIPT_ENGINE_EXTENSION, entry->d_name))
&& (S_ISREG(stat_entry.st_mode)
|| S_ISLNK(stat_entry.st_mode))
)) {
continue;
}
// note that if there is a symlink in the dir, we have to rely on
// the .nse extension
// if they provide a symlink to a dir which ends with .nse, things
// break :/
while((entry = readdir(dir)) != NULL) {
std::string path = std::string(dirname) + "/" + std::string(entry->d_name);
// if we are looking for dirs and this dir
// isn't a dir or a link, then we don't look further at it
} else if(files_or_dirs == DIRS) {
if(!(
(S_ISDIR(stat_entry.st_mode)
|| S_ISLNK(stat_entry.st_mode))
)) {
continue;
}
if(stat(path.c_str(), &stat_entry) != 0)
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
// they have passed an invalid value for files_or_dirs
} else {
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
}
// if we are looking for files and this file doesn't end with .nse and
// isn't a file or a link, then we don't look further at it
if(files_or_dirs == FILES) {
if(!(nse_check_extension(SCRIPT_ENGINE_EXTENSION, entry->d_name)
&& (S_ISREG(stat_entry.st_mode)
|| S_ISLNK(stat_entry.st_mode))
)) {
continue;
}
// otherwise we add it to the results
lua_pushstring(L, path.c_str());
lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
}
closedir(dir);
// if we are looking for dirs and this dir
// isn't a dir or a link, then we don't look further at it
} else if(files_or_dirs == DIRS) {
if(!(S_ISDIR(stat_entry.st_mode)
|| S_ISLNK(stat_entry.st_mode)
)) {
continue;
}
return 1;
// they have passed an invalid value for files_or_dirs
} else {
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
}
// otherwise we add it to the results
lua_pushstring(L, path.c_str());
lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
}
closedir(dir);
return 1;
}
#endif

View File

@@ -85,7 +85,7 @@ static int loadfile (lua_State *L)
lua_pop(L, 2);
lua_createtable(L, 0, 11); // Environment for script (index 2)
lua_pushvalue(L, 1); // tell the script about its filename
lua_setfield(L, -2, FILENAME);
@@ -93,7 +93,7 @@ static int loadfile (lua_State *L)
lua_setfield(L, -2, RUNLEVEL);
lua_createtable(L, 0, 1); // script gets access to global env
lua_pushvalue(L, LUA_GLOBALSINDEX); // We may want to use G(L)->mainthread
lua_pushvalue(L, LUA_GLOBALSINDEX); // We may want to use G(L)->mainthread
// later if this function becomes
// exposed. See lstate.h
lua_setfield(L, -2, "__index");
@@ -193,7 +193,7 @@ static int loaddir (lua_State *L)
{
int i;
luaL_checkstring(L, 1); // directory to load
lua_pushcclosure(L, nse_scandir, 0);
lua_pushvalue(L, 1);
lua_pushinteger(L, FILES);
@@ -217,7 +217,7 @@ static int loaddir (lua_State *L)
static int init_setpath (lua_State *L)
{
char path[MAX_FILENAME_LEN];
/* set the path lua searches for modules*/
if (nmap_fetchfile(path, MAX_FILENAME_LEN, SCRIPT_ENGINE_LIB_DIR) != 2)
luaL_error(L, "'%s' not a directory", SCRIPT_ENGINE_LIB_DIR);
@@ -285,7 +285,7 @@ int init_lua (lua_State *L)
lua_pushcclosure(L, init_setpath, 0);
lua_call(L, 0, 0);
lua_newtable(L);
current_hosts = luaL_ref(L, LUA_REGISTRYINDEX);
@@ -398,7 +398,7 @@ int init_updatedb (lua_State *L)
luaL_error(L, "Could not open file '%s' for writing.", path);
SCRIPT_ENGINE_DEBUGGING(
log_write(LOG_STDOUT, "%s: Trying to add %u scripts to the database.\n",
log_write(LOG_STDOUT, "%s: Trying to add %u scripts to the database.\n",
SCRIPT_ENGINE, lua_objlen(L, 1));
)
@@ -425,7 +425,7 @@ int init_updatedb (lua_State *L)
lua_setfenv(L, -2); // set it
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",
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));
@@ -619,7 +619,7 @@ static int loadcategories (lua_State *L)
{
int i, top = lua_gettop(L);
char c_dbpath[MAX_FILENAME_LEN];
static const char *dbpath = SCRIPT_ENGINE_LUA_DIR SCRIPT_ENGINE_DATABASE;
static const char *dbpath = SCRIPT_ENGINE_LUA_DIR SCRIPT_ENGINE_DATABASE;
/* Build the script database if it doesn't exist. */
if (nmap_fetchfile(c_dbpath, sizeof(c_dbpath), dbpath) == 0)
@@ -744,7 +744,7 @@ int init_rules (lua_State *L)
lua_replace(L, -2); // remove value
type = nse_fetchfile_absolute(path, sizeof(path), lua_tostring(L, -1));
}
switch (type)
{
case 0: // no such path
@@ -766,13 +766,13 @@ int init_rules (lua_State *L)
lua_call(L, 1, 0);
break;
default:
fatal("%s: In: %s:%i This should never happen.",
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
}
lua_pop(L, 1);
}
// Compute some stats
// Compute some stats
SCRIPT_ENGINE_DEBUGGING(
size_t rules_count;
lua_getfield(L, LUA_REGISTRYINDEX, HOSTTESTS);

File diff suppressed because it is too large Load Diff

View File

@@ -2,9 +2,9 @@
#define NMAP_LUA_H
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include <vector>

View File

@@ -17,17 +17,17 @@
#define SCRIPT_ENGINE_GETSTRING(name) \
char* name; \
lua_getfield(L, -1, #name); \
if(lua_isnil(L, -1)) \
name = NULL; \
else \
name = strdup(lua_tostring(L, -1)); \
lua_pop(L, 1); \
char* name; \
lua_getfield(L, -1, #name); \
if(lua_isnil(L, -1)) \
name = NULL; \
else \
name = strdup(lua_tostring(L, -1)); \
lua_pop(L, 1); \
#define SCRIPT_ENGINE_PUSHSTRING_NOTNULL(c_str, str) if(c_str != NULL) {\
lua_pushstring(L, c_str); \
lua_setfield(L, -2, str); \
lua_pushstring(L, c_str); \
lua_setfield(L, -2, str); \
}
extern NmapOps o;
@@ -137,7 +137,7 @@ int luaopen_nmap (lua_State *L)
{"timing_level", l_get_timing_level},
{"mutex", l_mutex},
{"get_dns_servers", l_get_dns_servers},
{NULL, NULL}
{NULL, NULL}
};
lua_settop(L, 0); // clear stack
@@ -167,100 +167,100 @@ int luaopen_nmap (lua_State *L)
* table which is currently on the stack
* */
void set_portinfo(lua_State *L, Port* port) {
struct serviceDeductions sd;
struct serviceDeductions sd;
port->getServiceDeductions(&sd);
port->getServiceDeductions(&sd);
lua_pushnumber(L, (double) port->portno);
lua_setfield(L, -2, "number");
lua_pushnumber(L, (double) port->portno);
lua_setfield(L, -2, "number");
lua_pushstring(L, sd.name);
lua_setfield(L, -2, "service");
lua_pushstring(L, sd.name);
lua_setfield(L, -2, "service");
lua_pushstring(L, (port->proto == IPPROTO_TCP)? "tcp": "udp");
lua_setfield(L, -2, "protocol");
lua_pushstring(L, (port->proto == IPPROTO_TCP)? "tcp": "udp");
lua_setfield(L, -2, "protocol");
lua_newtable(L);
set_version(L, sd);
lua_setfield(L, -2, "version");
lua_newtable(L);
set_version(L, sd);
lua_setfield(L, -2, "version");
lua_pushstring(L, statenum2str(port->state));
lua_setfield(L, -2, "state");
lua_pushstring(L, statenum2str(port->state));
lua_setfield(L, -2, "state");
lua_pushstring(L, reason_str(port->reason.reason_id, 1));
lua_setfield(L, -2, "reason");
lua_pushstring(L, reason_str(port->reason.reason_id, 1));
lua_setfield(L, -2, "reason");
}
void set_version(lua_State *L, struct serviceDeductions sd) {
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.name, "name");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.name, "name");
lua_pushnumber(L, sd.name_confidence);
lua_setfield(L, -2, "name_confidence");
lua_pushnumber(L, sd.name_confidence);
lua_setfield(L, -2, "name_confidence");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.product, "product");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.version, "version");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.extrainfo, "extrainfo");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.hostname, "hostname");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.ostype, "ostype");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.devicetype, "devicetype");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.product, "product");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.version, "version");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.extrainfo, "extrainfo");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.hostname, "hostname");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.ostype, "ostype");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.devicetype, "devicetype");
switch(sd.service_tunnel) {
case(SERVICE_TUNNEL_NONE):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("none", "service_tunnel");
break;
case(SERVICE_TUNNEL_SSL):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("ssl", "service_tunnel");
break;
default:
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
break;
}
switch(sd.service_tunnel) {
case(SERVICE_TUNNEL_NONE):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("none", "service_tunnel");
break;
case(SERVICE_TUNNEL_SSL):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("ssl", "service_tunnel");
break;
default:
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
break;
}
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.service_fp, "service_fp");
SCRIPT_ENGINE_PUSHSTRING_NOTNULL(sd.service_fp, "service_fp");
switch(sd.dtype) {
case(SERVICE_DETECTION_TABLE):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("table", "service_fp");
break;
case(SERVICE_DETECTION_PROBED):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("probed", "service_fp");
break;
default:
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
break;
}
switch(sd.dtype) {
case(SERVICE_DETECTION_TABLE):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("table", "service_fp");
break;
case(SERVICE_DETECTION_PROBED):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("probed", "service_fp");
break;
default:
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
break;
}
switch(sd.rpc_status) {
case(RPC_STATUS_UNTESTED):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("untested", "rpc_status");
break;
case(RPC_STATUS_UNKNOWN):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("unknown", "rpc_status");
break;
case(RPC_STATUS_GOOD_PROG):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("good_prog", "rpc_status");
break;
case(RPC_STATUS_NOT_RPC):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("not_rpc", "rpc_status");
break;
default:
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
break;
}
switch(sd.rpc_status) {
case(RPC_STATUS_UNTESTED):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("untested", "rpc_status");
break;
case(RPC_STATUS_UNKNOWN):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("unknown", "rpc_status");
break;
case(RPC_STATUS_GOOD_PROG):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("good_prog", "rpc_status");
break;
case(RPC_STATUS_NOT_RPC):
SCRIPT_ENGINE_PUSHSTRING_NOTNULL("not_rpc", "rpc_status");
break;
default:
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
break;
}
if(sd.rpc_status == RPC_STATUS_GOOD_PROG) {
lua_pushnumber(L, sd.rpc_program);
lua_setfield(L, -2, "rpc_program");
if(sd.rpc_status == RPC_STATUS_GOOD_PROG) {
lua_pushnumber(L, sd.rpc_program);
lua_setfield(L, -2, "rpc_program");
lua_pushnumber(L, sd.rpc_lowver);
lua_setfield(L, -2, "rpc_lowver");
lua_pushnumber(L, sd.rpc_lowver);
lua_setfield(L, -2, "rpc_lowver");
lua_pushnumber(L, sd.rpc_highver);
lua_setfield(L, -2, "rpc_highver");
}
lua_pushnumber(L, sd.rpc_highver);
lua_setfield(L, -2, "rpc_highver");
}
}
/* set host ip, host name and target name onto the
@@ -273,149 +273,149 @@ void set_version(lua_State *L, struct serviceDeductions sd) {
* points to nil!
* */
void set_hostinfo(lua_State *L, Target *currenths) {
unsigned int i;
char hostname[1024];
unsigned int i;
char hostname[1024];
lua_pushstring(L, strncpy(hostname, currenths->targetipstr(), 1024));
lua_setfield(L, -2, "ip");
lua_pushstring(L, strncpy(hostname, currenths->targetipstr(), 1024));
lua_setfield(L, -2, "ip");
lua_pushstring(L, strncpy(hostname, currenths->HostName(), 1024));
lua_setfield(L, -2, "name");
lua_pushstring(L, strncpy(hostname, currenths->HostName(), 1024));
lua_setfield(L, -2, "name");
if ( currenths->TargetName() ) { // else nil
lua_pushstring(L, strncpy(hostname, currenths->TargetName(), 1024));
lua_setfield(L, -2, "targetname");
}
if ( currenths->TargetName() ) { // else nil
lua_pushstring(L, strncpy(hostname, currenths->TargetName(), 1024));
lua_setfield(L, -2, "targetname");
}
if(currenths->directlyConnectedOrUnset() != -1){
lua_pushboolean(L, currenths->directlyConnected());
lua_setfield(L, -2, "directly_connected");
}
if(currenths->directlyConnectedOrUnset() != -1){
lua_pushboolean(L, currenths->directlyConnected());
lua_setfield(L, -2, "directly_connected");
}
if(currenths->MACAddress()){ // else nil
lua_pushlstring (L, (const char*)currenths->MACAddress() , 6);
lua_setfield(L, -2, "mac_addr");
}
if(currenths->SrcMACAddress()){ // else nil
lua_pushlstring(L, (const char*)currenths->SrcMACAddress(), 6);
lua_setfield(L, -2, "mac_addr_src");
}
if(currenths->deviceName()){
lua_pushstring(L, strncpy(hostname, currenths->deviceName(), 1024));
lua_setfield(L, -2, "interface");
}
if( (u32)(currenths->v4host().s_addr) ){
struct in_addr adr = currenths->v4host();
lua_pushlstring(L, (char*)&adr, 4);
lua_setfield(L, -2, "bin_ip");
}
if( (u32)(currenths->v4source().s_addr) ){
struct in_addr adr = currenths->v4source();
lua_pushlstring(L, (char*)&adr, 4);
lua_setfield(L, -2, "bin_ip_src");
}
FingerPrintResults *FPR = NULL;
if(currenths->MACAddress()){ // else nil
lua_pushlstring (L, (const char*)currenths->MACAddress() , 6);
lua_setfield(L, -2, "mac_addr");
}
if(currenths->SrcMACAddress()){ // else nil
lua_pushlstring(L, (const char*)currenths->SrcMACAddress(), 6);
lua_setfield(L, -2, "mac_addr_src");
}
if(currenths->deviceName()){
lua_pushstring(L, strncpy(hostname, currenths->deviceName(), 1024));
lua_setfield(L, -2, "interface");
}
if( (u32)(currenths->v4host().s_addr) ){
struct in_addr adr = currenths->v4host();
lua_pushlstring(L, (char*)&adr, 4);
lua_setfield(L, -2, "bin_ip");
}
if( (u32)(currenths->v4source().s_addr) ){
struct in_addr adr = currenths->v4source();
lua_pushlstring(L, (char*)&adr, 4);
lua_setfield(L, -2, "bin_ip_src");
}
FPR = currenths->FPR;
FingerPrintResults *FPR = NULL;
/* if there has been an os scan which returned a pretty certain
* result, we will use it in the scripts
* matches which aren't perfect are not needed in the scripts
*/
if( currenths->osscanPerformed() &&
FPR != NULL &&
FPR->overall_results == OSSCAN_SUCCESS &&
FPR->num_perfect_matches > 0 &&
FPR->num_perfect_matches <= 8 ) {
FPR = currenths->FPR;
lua_newtable(L);
/* if there has been an os scan which returned a pretty certain
* result, we will use it in the scripts
* matches which aren't perfect are not needed in the scripts
*/
if(currenths->osscanPerformed() &&
FPR != NULL &&
FPR->overall_results == OSSCAN_SUCCESS &&
FPR->num_perfect_matches > 0 &&
FPR->num_perfect_matches <= 8 ) {
// this will run at least one time and at most 8 times, see if condition
for(i = 0; FPR->accuracy[i] == 1; i++) {
lua_pushstring(L, FPR->prints[i]->OS_name);
lua_rawseti(L, -2, i);
}
lua_setfield(L, -2, "os");
}
lua_newtable(L);
// this will run at least one time and at most 8 times, see if condition
for(i = 0; FPR->accuracy[i] == 1; i++) {
lua_pushstring(L, FPR->prints[i]->OS_name);
lua_rawseti(L, -2, i);
}
lua_setfield(L, -2, "os");
}
}
static int l_port_accessor(lua_State *L) {
int retvalues = 0;
int retvalues = 0;
char* function_name;
const char *target_ip;
int portno;
int proto;
char* function_name;
const char *target_ip;
int portno;
int proto;
Target* target;
PortList* plist;
Port* port;
Target* target;
PortList* plist;
Port* port;
lua_Debug ldebug;
lua_getstack(L, 0, &ldebug);
lua_getinfo(L, "n", &ldebug);
function_name = strdup(ldebug.name);
lua_Debug ldebug;
lua_getstack(L, 0, &ldebug);
lua_getinfo(L, "n", &ldebug);
function_name = strdup(ldebug.name);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TTABLE);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TTABLE);
lua_getfield(L, 1, "ip");
luaL_checktype(L, -1, LUA_TSTRING);
target_ip = lua_tostring(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "ip");
luaL_checktype(L, -1, LUA_TSTRING);
target_ip = lua_tostring(L, -1);
lua_pop(L, 1);
lua_getfield(L, 2, "number");
luaL_checktype(L, -1, LUA_TNUMBER);
portno = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_getfield(L, 2, "number");
luaL_checktype(L, -1, LUA_TNUMBER);
portno = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_getfield(L, 2, "protocol");
luaL_checktype(L, -1, LUA_TSTRING);
proto = (strcmp(lua_tostring(L, -1), "tcp") == 0)? IPPROTO_TCP : IPPROTO_UDP;
lua_pop(L, 1);
lua_getfield(L, 2, "protocol");
luaL_checktype(L, -1, LUA_TSTRING);
proto = (strcmp(lua_tostring(L, -1), "tcp") == 0)? IPPROTO_TCP : IPPROTO_UDP;
lua_pop(L, 1);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_hosts);
lua_pushstring(L, target_ip);
lua_gettable(L, -2);
if (lua_isnil(L, -1))
return luaL_argerror(L, 1, "Host isn't being processed right now.");
else
{
target = (Target *) lua_touserdata(L, -1);
lua_pop(L, 2);
}
lua_rawgeti(L, LUA_REGISTRYINDEX, current_hosts);
lua_pushstring(L, target_ip);
lua_gettable(L, -2);
if (lua_isnil(L, -1))
return luaL_argerror(L, 1, "Host isn't being processed right now.");
else
{
target = (Target *) lua_touserdata(L, -1);
lua_pop(L, 2);
}
plist = &(target->ports);
port = NULL;
plist = &(target->ports);
port = NULL;
while((port = plist->nextPort(port, proto, PORT_UNKNOWN)) != NULL) {
if(port->portno == portno)
break;
}
while((port = plist->nextPort(port, proto, PORT_UNKNOWN)) != NULL) {
if(port->portno == portno)
break;
}
// if the port wasn't scanned we return nil
if(port == NULL) {
free(function_name);
return 0;
}
// if the port wasn't scanned we return nil
if(port == NULL) {
free(function_name);
return 0;
}
if(strcmp(function_name, "set_port_state") == 0)
retvalues = l_set_port_state(L, target, port);
else if(strcmp(function_name, "set_port_version") == 0)
retvalues = l_set_port_version(L, target, port);
else if(strcmp(function_name, "get_port_state") == 0)
retvalues = l_get_port_state(L, target, port);
if(strcmp(function_name, "set_port_state") == 0)
retvalues = l_set_port_state(L, target, port);
else if(strcmp(function_name, "set_port_version") == 0)
retvalues = l_set_port_version(L, target, port);
else if(strcmp(function_name, "get_port_state") == 0)
retvalues = l_get_port_state(L, target, port);
// remove host and port argument from the stack
lua_remove(L, 2);
lua_remove(L, 1);
free(function_name);
return retvalues;
// remove host and port argument from the stack
lua_remove(L, 2);
lua_remove(L, 1);
free(function_name);
return retvalues;
}
/* this function can be called from lua to obtain the port state
* of a port different from the one the script rule is matched
* of a port different from the one the script rule is matched
* against
* it retrieves the host.ip of the host on which the script is
* currently running, looks up the host in the table of currently
@@ -425,10 +425,10 @@ static int l_port_accessor(lua_State *L) {
* the state of more than one port
* */
static int l_get_port_state(lua_State *L, Target* target, Port* port) {
lua_newtable(L);
set_portinfo(L, port);
lua_newtable(L);
set_portinfo(L, port);
return 1;
return 1;
}
/* unlike set_portinfo() this function sets the port state in nmap.
@@ -436,210 +436,210 @@ static int l_get_port_state(lua_State *L, Target* target, Port* port) {
* filtered, the script is free to say so.
* */
static int l_set_port_state(lua_State *L, Target* target, Port* port) {
char* state;
PortList* plist = &(target->ports);
char* state;
PortList* plist = &(target->ports);
luaL_checktype(L, -1, LUA_TSTRING);
state = strdup(lua_tostring(L, -1));
lua_pop(L, 1);
luaL_checktype(L, -1, LUA_TSTRING);
state = strdup(lua_tostring(L, -1));
lua_pop(L, 1);
switch(state[0]) {
case 'o':
if (strcmp(state, "open"))
luaL_argerror (L, 4, "Invalid port state.");
if (port->state == PORT_OPEN)
goto noset;
plist->addPort(port->portno, port->proto, NULL, PORT_OPEN);
port->state = PORT_OPEN;
break;
case 'c':
if (strcmp(state, "closed"))
luaL_argerror (L, 4, "Invalid port state.");
if (port->state == PORT_CLOSED)
goto noset;
plist->addPort(port->portno, port->proto, NULL, PORT_CLOSED);
port->state = PORT_CLOSED;
break;
default:
luaL_argerror (L, 4, "Invalid port state.");
}
switch(state[0]) {
case 'o':
if (strcmp(state, "open"))
luaL_argerror (L, 4, "Invalid port state.");
if (port->state == PORT_OPEN)
goto noset;
plist->addPort(port->portno, port->proto, NULL, PORT_OPEN);
port->state = PORT_OPEN;
break;
case 'c':
if (strcmp(state, "closed"))
luaL_argerror (L, 4, "Invalid port state.");
if (port->state == PORT_CLOSED)
goto noset;
plist->addPort(port->portno, port->proto, NULL, PORT_CLOSED);
port->state = PORT_CLOSED;
break;
default:
luaL_argerror (L, 4, "Invalid port state.");
}
port->reason.reason_id = ER_SCRIPT;
port->reason.reason_id = ER_SCRIPT;
noset:
free(state);
return 0;
free(state);
return 0;
}
static int l_set_port_version(lua_State *L, Target* target, Port* port) {
luaL_checktype(L, 3, LUA_TSTRING);
char* c_probestate = strdup(lua_tostring(L, -1));
lua_pop(L, 1);
luaL_checktype(L, 3, LUA_TSTRING);
char* c_probestate = strdup(lua_tostring(L, -1));
lua_pop(L, 1);
enum service_tunnel_type tunnel = SERVICE_TUNNEL_NONE;
enum serviceprobestate probestate = PROBESTATE_INITIAL;
enum service_tunnel_type tunnel = SERVICE_TUNNEL_NONE;
enum serviceprobestate probestate = PROBESTATE_INITIAL;
lua_getfield(L, -1, "version");
SCRIPT_ENGINE_GETSTRING(name);
SCRIPT_ENGINE_GETSTRING(product);
SCRIPT_ENGINE_GETSTRING(version);
SCRIPT_ENGINE_GETSTRING(extrainfo);
SCRIPT_ENGINE_GETSTRING(hostname);
SCRIPT_ENGINE_GETSTRING(ostype);
SCRIPT_ENGINE_GETSTRING(devicetype);
// SCRIPT_ENGINE_GETSTRING(fingerprint);
SCRIPT_ENGINE_GETSTRING(service_tunnel);
if(service_tunnel == NULL)
tunnel = SERVICE_TUNNEL_NONE;
else if(strcmp(service_tunnel, "none") == 0)
tunnel = SERVICE_TUNNEL_NONE;
else if(strcmp(service_tunnel, "ssl") == 0)
tunnel = SERVICE_TUNNEL_SSL;
else
luaL_argerror(L, 2, "Invalid value for port.version.service_tunnel");
lua_pop(L, 1);
lua_getfield(L, -1, "version");
SCRIPT_ENGINE_GETSTRING(name);
SCRIPT_ENGINE_GETSTRING(product);
SCRIPT_ENGINE_GETSTRING(version);
SCRIPT_ENGINE_GETSTRING(extrainfo);
SCRIPT_ENGINE_GETSTRING(hostname);
SCRIPT_ENGINE_GETSTRING(ostype);
SCRIPT_ENGINE_GETSTRING(devicetype);
// SCRIPT_ENGINE_GETSTRING(fingerprint);
if(c_probestate == NULL)
probestate = PROBESTATE_INITIAL;
if(strcmp(c_probestate, "hardmatched") == 0)
probestate = PROBESTATE_FINISHED_HARDMATCHED;
else if(strcmp(c_probestate, "softmatched") == 0)
probestate = PROBESTATE_FINISHED_SOFTMATCHED;
else if(strcmp(c_probestate, "nomatch") == 0)
probestate = PROBESTATE_FINISHED_NOMATCH;
else if(strcmp(c_probestate, "tcpwrapped") == 0)
probestate = PROBESTATE_FINISHED_TCPWRAPPED;
else if(strcmp(c_probestate, "incomplete") == 0)
probestate = PROBESTATE_INCOMPLETE;
else
luaL_argerror(L, 3, "Invalid value for probestate.");
SCRIPT_ENGINE_GETSTRING(service_tunnel);
if(service_tunnel == NULL)
tunnel = SERVICE_TUNNEL_NONE;
else if(strcmp(service_tunnel, "none") == 0)
tunnel = SERVICE_TUNNEL_NONE;
else if(strcmp(service_tunnel, "ssl") == 0)
tunnel = SERVICE_TUNNEL_SSL;
else
luaL_argerror(L, 2, "Invalid value for port.version.service_tunnel");
lua_pop(L, 1);
// port->setServiceProbeResults(probestate, name,
// tunnel, product, version,
// extrainfo, hostname, ostype,
// devicetype, fingerprint);
if(c_probestate == NULL)
probestate = PROBESTATE_INITIAL;
if(strcmp(c_probestate, "hardmatched") == 0)
probestate = PROBESTATE_FINISHED_HARDMATCHED;
else if(strcmp(c_probestate, "softmatched") == 0)
probestate = PROBESTATE_FINISHED_SOFTMATCHED;
else if(strcmp(c_probestate, "nomatch") == 0)
probestate = PROBESTATE_FINISHED_NOMATCH;
else if(strcmp(c_probestate, "tcpwrapped") == 0)
probestate = PROBESTATE_FINISHED_TCPWRAPPED;
else if(strcmp(c_probestate, "incomplete") == 0)
probestate = PROBESTATE_INCOMPLETE;
else
luaL_argerror(L, 3, "Invalid value for probestate.");
//should prevent a assertion-failure during output if the OutputTable does
//not contain columns for the fields other than the name
if(o.servicescan){
port->setServiceProbeResults(probestate, name,
tunnel, product, version,
extrainfo, hostname, ostype,
devicetype, NULL);
}else{
port->setServiceProbeResults(probestate, name,
tunnel, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL);
}
// port->setServiceProbeResults(probestate, name,
// tunnel, product, version,
// extrainfo, hostname, ostype,
// devicetype, fingerprint);
//should prevent a assertion-failure during output if the OutputTable does
//not contain columns for the fields other than the name
if(o.servicescan){
port->setServiceProbeResults(probestate, name,
tunnel, product, version,
extrainfo, hostname, ostype,
devicetype, NULL);
}else{
port->setServiceProbeResults(probestate, name,
tunnel, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL);
}
free(service_tunnel);
free(name);
free(product);
free(version);
free(extrainfo);
free(hostname);
free(ostype);
free(devicetype);
// free(fingerprint);
return 0;
free(service_tunnel);
free(name);
free(product);
free(version);
free(extrainfo);
free(hostname);
free(ostype);
free(devicetype);
// free(fingerprint);
return 0;
}
static int l_print_debug_unformatted(lua_State *L) {
int verbosity=1;
const char *out;
if (lua_gettop(L) != 2) return luaL_error(L, "Incorrect number of arguments\n");
verbosity = luaL_checkinteger(L, 1);
if (verbosity > o.verbose) return 0;
out = luaL_checkstring(L, 2);
log_write(LOG_STDOUT, "%s DEBUG: %s\n", SCRIPT_ENGINE, out);
return 0;
}
static int l_exc_finalize(lua_State *L) {
if (!lua_toboolean(L, 1)) {
/* false or nil. */
lua_pushvalue(L, lua_upvalueindex(1));
lua_call(L, 0, 0);
lua_settop(L, 2);
lua_error(L);
return 0;
} else if(lua_isboolean(L, 1) && lua_toboolean(L, 1)) {
/* true. */
lua_remove(L, 1);
return lua_gettop(L);
} else {
fatal("%s: In: %s:%i Trying to finalize a non conforming function. Are you sure you return true on success followed by the remaining return values and nil on failure followed by an error string?",
SCRIPT_ENGINE, __FILE__, __LINE__);
if (!lua_toboolean(L, 1)) {
/* false or nil. */
lua_pushvalue(L, lua_upvalueindex(1));
lua_call(L, 0, 0);
lua_settop(L, 2);
lua_error(L);
return 0;
} else if(lua_isboolean(L, 1) && lua_toboolean(L, 1)) {
/* true. */
lua_remove(L, 1);
return lua_gettop(L);
} else {
fatal("%s: In: %s:%i Trying to finalize a non conforming function. Are you sure you return true on success followed by the remaining return values and nil on failure followed by an error string?",
SCRIPT_ENGINE, __FILE__, __LINE__);
return 0;
}
return 0;
}
}
static int l_exc_do_nothing(lua_State *L) {
(void) L;
return 0;
(void) L;
return 0;
}
static int l_exc_newtry(lua_State *L) {
lua_settop(L, 1);
if (lua_isnil(L, 1))
lua_pushcfunction(L, l_exc_do_nothing);
lua_pushcclosure(L, l_exc_finalize, 1);
return 1;
lua_settop(L, 1);
if (lua_isnil(L, 1))
lua_pushcfunction(L, l_exc_do_nothing);
lua_pushcclosure(L, l_exc_finalize, 1);
return 1;
}
static int l_get_verbosity(lua_State *L)
{
lua_pushnumber(L, o.verbose);
return 1;
lua_pushnumber(L, o.verbose);
return 1;
}
static int l_get_debugging(lua_State *L)
{
lua_pushnumber(L, o.debugging);
return 1;
lua_pushnumber(L, o.debugging);
return 1;
}
static int l_get_have_ssl(lua_State *L) {
#if HAVE_OPENSSL
lua_pushboolean(L, true);
lua_pushboolean(L, true);
#else
lua_pushboolean(L, false);
lua_pushboolean(L, false);
#endif
return 1;
return 1;
}
static int l_fetchfile(lua_State *L)
{
char buf[FILENAME_MAX];
const char *req = lua_tostring(L, -1);
char buf[FILENAME_MAX];
const char *req = lua_tostring(L, -1);
if (!req)
goto err;
if (!req)
goto err;
if (nmap_fetchfile(buf, sizeof buf, (char *) req) != 1)
goto err;
if (nmap_fetchfile(buf, sizeof buf, (char *) req) != 1)
goto err;
lua_pop(L, 1);
lua_pushstring(L, buf);
return 1;
lua_pop(L, 1);
lua_pushstring(L, buf);
return 1;
err:
lua_pop(L, 1);
lua_pushnil(L);
return 0;
lua_pop(L, 1);
lua_pushnil(L);
return 0;
}
static int l_get_timing_level(lua_State *L)
{
lua_pushnumber(L, o.timing_level);
return 1;
lua_pushnumber(L, o.timing_level);
return 1;
}

View File

@@ -2,9 +2,9 @@
#define NSE_NMAPLIB
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
class Target;