mirror of
https://github.com/nmap/nmap.git
synced 2025-12-06 04:31:29 +00:00
Removed nse_auxiliar. Updated Script Argument parsing. Fixed typos in
documentation. Improved MySQLinfo.nse. Nsock/dnet metatabels are now protected.
This commit is contained in:
@@ -60,9 +60,9 @@ INSTALLZENMAP=@INSTALLZENMAP@
|
||||
UNINSTALLZENMAP=@UNINSTALLZENMAP@
|
||||
|
||||
ifneq (@LIBLUA_LIBS@,)
|
||||
NSE_SRC=nse_main.cc nse_auxiliar.cc nse_nsock.cc nse_init.cc nse_nmaplib.cc nse_debug.cc nse_pcrelib.cc nse_string.cc
|
||||
NSE_HDRS=nse_main.h nse_auxiliar.h nse_nsock.h nse_init.h nse_nmaplib.h nse_debug.h nse_macros.h nse_pcrelib.h nse_string.h
|
||||
NSE_OBJS=nse_main.o nse_auxiliar.o nse_nsock.o nse_init.o nse_nmaplib.o nse_debug.o nse_pcrelib.o nse_string.o
|
||||
NSE_SRC=nse_main.cc nse_nsock.cc nse_init.cc nse_nmaplib.cc nse_debug.cc nse_pcrelib.cc nse_string.cc
|
||||
NSE_HDRS=nse_main.h nse_nsock.h nse_init.h nse_nmaplib.h nse_debug.h nse_macros.h nse_pcrelib.h nse_string.h
|
||||
NSE_OBJS=nse_main.o nse_nsock.o nse_init.o nse_nmaplib.o nse_debug.o nse_pcrelib.o nse_string.o
|
||||
NSESTDLIB=nsestdlib
|
||||
endif
|
||||
|
||||
|
||||
@@ -304,7 +304,7 @@ $ nmap -sC --script-args user=foo,pass=bar,anonFTP={pass=ftp@foobar.com}
|
||||
which would result in the Lua table:
|
||||
</para>
|
||||
<programlisting>
|
||||
{user="foo",pass="bar",anonFTP={pass=nobody@foobar.com}}
|
||||
{user="foo",pass="bar",anonFTP={pass="nobody@foobar.com"}}
|
||||
</programlisting>
|
||||
|
||||
<para>You could therefore access the username (<literal>"foo"</literal>)
|
||||
|
||||
@@ -251,10 +251,6 @@
|
||||
RelativePath="..\NmapOutputTable.cc"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\nse_auxiliar.cc"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\nse_debug.cc"
|
||||
>
|
||||
|
||||
132
nse_auxiliar.cc
132
nse_auxiliar.cc
@@ -1,132 +0,0 @@
|
||||
/*=========================================================================*\
|
||||
* Auxiliar routines for class hierarchy manipulation
|
||||
* LuaSocket toolkit
|
||||
* tailored for use with NSE
|
||||
*
|
||||
* RCS ID: $Id: auxiliar.c,v 1.14 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
#include "nmap.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "nse_auxiliar.h"
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Creates a new class with given methods
|
||||
* Methods whose names start with __ are passed directly to the metatable.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void auxiliar_newclass(lua_State *L, const char *classname, luaL_reg *func) {
|
||||
luaL_newmetatable(L, classname); /* mt */
|
||||
/* create __index table to place methods */
|
||||
lua_pushstring(L, "__index"); /* mt,"__index" */
|
||||
lua_newtable(L); /* mt,"__index",it */
|
||||
/* put class name into class metatable */
|
||||
lua_pushstring(L, "class"); /* mt,"__index",it,"class" */
|
||||
lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */
|
||||
lua_rawset(L, -3); /* mt,"__index",it */
|
||||
/* pass all methods that start with _ to the metatable, and all others
|
||||
* to the index table */
|
||||
for (; func->name; func++) { /* mt,"__index",it */
|
||||
lua_pushstring(L, func->name);
|
||||
lua_pushcfunction(L, func->func);
|
||||
lua_rawset(L, func->name[0] == '_' ? -5: -3);
|
||||
}
|
||||
lua_rawset(L, -3); /* mt */
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Prints the value of a class in a nice way
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int auxiliar_tostring(lua_State *L) {
|
||||
char buf[32];
|
||||
if (!lua_getmetatable(L, 1)) goto error;
|
||||
lua_pushstring(L, "__index");
|
||||
lua_gettable(L, -2);
|
||||
if (!lua_istable(L, -1)) goto error;
|
||||
lua_pushstring(L, "class");
|
||||
lua_gettable(L, -2);
|
||||
if (!lua_isstring(L, -1)) goto error;
|
||||
Snprintf(buf, 31, "%p", lua_touserdata(L, 1));
|
||||
lua_pushfstring(L, "%s: %s", lua_tostring(L, -1), buf);
|
||||
return 1;
|
||||
error:
|
||||
lua_pushstring(L, "invalid object passed to 'auxiliar.c:__tostring'");
|
||||
lua_error(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Insert class into group
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) {
|
||||
luaL_getmetatable(L, classname);
|
||||
lua_pushstring(L, groupname);
|
||||
lua_pushboolean(L, 1);
|
||||
lua_rawset(L, -3);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Return userdata pointer if object belongs to a given class, abort with
|
||||
* error otherwise
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) {
|
||||
void *data = auxiliar_getclassudata(L, classname, objidx);
|
||||
if (!data) {
|
||||
char msg[45];
|
||||
Snprintf(msg, 44, "%.35s expected", classname);
|
||||
luaL_argerror(L, objidx, msg);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Return userdata pointer if object belongs to a given group, abort with
|
||||
* error otherwise
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) {
|
||||
void *data = auxiliar_getgroupudata(L, groupname, objidx);
|
||||
if (!data) {
|
||||
char msg[45];
|
||||
Snprintf(msg, 44, "%.35s expected", groupname);
|
||||
luaL_argerror(L, objidx, msg);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Set object class
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void auxiliar_setclass(lua_State *L, const char *classname, int objidx) {
|
||||
luaL_getmetatable(L, classname);
|
||||
if (objidx < 0) objidx--;
|
||||
lua_setmetatable(L, objidx);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Get a userdata pointer if object belongs to a given group. Return NULL
|
||||
* otherwise
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) {
|
||||
if (!lua_getmetatable(L, objidx))
|
||||
return NULL;
|
||||
lua_pushstring(L, groupname);
|
||||
lua_rawget(L, -2);
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 2);
|
||||
return NULL;
|
||||
} else {
|
||||
lua_pop(L, 2);
|
||||
return lua_touserdata(L, objidx);
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Get a userdata pointer if object belongs to a given class. Return NULL
|
||||
* otherwise
|
||||
\*-------------------------------------------------------------------------*/
|
||||
void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) {
|
||||
return luaL_checkudata(L, objidx, classname);
|
||||
}
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
#ifndef NMAP_LUA_AUXILIAR_H
|
||||
#define NMAP_LUA_AUXILIAR_H
|
||||
// code stolen from luasocket and tailored to nmap
|
||||
/*=========================================================================*\
|
||||
* Auxiliar routines for class hierarchy manipulation
|
||||
* LuaSocket toolkit (but completely independent of other LuaSocket modules)
|
||||
*
|
||||
* A LuaSocket class is a name associated with Lua metatables. A LuaSocket
|
||||
* group is a name associated with a class. A class can belong to any number
|
||||
* of groups. This module provides the functionality to:
|
||||
*
|
||||
* - create new classes
|
||||
* - add classes to groups
|
||||
* - set the class of objects
|
||||
* - check if an object belongs to a given class or group
|
||||
* - get the userdata associated to objects
|
||||
* - print objects in a pretty way
|
||||
*
|
||||
* LuaSocket class names follow the convention <module>{<class>}. Modules
|
||||
* can define any number of classes and groups. The module tcp.c, for
|
||||
* example, defines the classes tcp{master}, tcp{client} and tcp{server} and
|
||||
* the groups tcp{client,server} and tcp{any}. Module functions can then
|
||||
* perform type-checking on their arguments by either class or group.
|
||||
*
|
||||
* LuaSocket metatables define the __index metamethod as being a table. This
|
||||
* table has one field for each method supported by the class, and a field
|
||||
* "class" with the class name.
|
||||
*
|
||||
* The mapping from class name to the corresponding metatable and the
|
||||
* reverse mapping are done using lauxlib.
|
||||
*
|
||||
* RCS ID: $Id: auxiliar.h,v 1.9 2005/10/07 04:40:59 diego Exp $
|
||||
\*=========================================================================*/
|
||||
|
||||
#include "nbase.h"
|
||||
|
||||
extern "C" {
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
#include "lauxlib.h"
|
||||
}
|
||||
|
||||
void auxiliar_newclass(lua_State *L, const char *classname, luaL_reg *func);
|
||||
void auxiliar_add2group(lua_State *L, const char *classname, const char *group);
|
||||
void auxiliar_setclass(lua_State *L, const char *classname, int objidx);
|
||||
void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx);
|
||||
void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx);
|
||||
void *auxiliar_getclassudata(lua_State *L, const char *groupname, int objidx);
|
||||
void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx);
|
||||
int auxiliar_tostring(lua_State *L);
|
||||
|
||||
#endif /* NMAP_LUA_AUXILIAR_H */
|
||||
|
||||
29
nse_debug.cc
29
nse_debug.cc
@@ -15,21 +15,27 @@ void l_dumpStack(lua_State* l) {
|
||||
}
|
||||
|
||||
void l_dumpValue(lua_State* l, int i) {
|
||||
if(lua_istable(l, i))
|
||||
switch (lua_type(l, i))
|
||||
{
|
||||
case LUA_TTABLE:
|
||||
l_dumpTable(l, i);
|
||||
else if(lua_isfunction(l, i))
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
l_dumpFunction(l, i);
|
||||
else if(lua_isstring(l, i)) {
|
||||
lua_pushvalue(l, i);
|
||||
log_write(LOG_PLAIN, "string '%s'\n", lua_tostring(l, -1));
|
||||
lua_pop(l, 1);
|
||||
}
|
||||
else if(lua_isboolean(l, i))
|
||||
log_write(LOG_PLAIN, "boolean: %s", lua_toboolean(l, i) ? "true\n" : "false\n");
|
||||
else if(lua_isnumber(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));
|
||||
else
|
||||
break;
|
||||
default:
|
||||
log_write(LOG_PLAIN, "%s\n", lua_typename(l, lua_type(l, i)));
|
||||
}
|
||||
}
|
||||
|
||||
void l_dumpTable(lua_State *l, int index) {
|
||||
@@ -56,4 +62,3 @@ void l_dumpFunction(lua_State* l, int index) {
|
||||
// log_write(LOG_PLAIN, "\tname: %s %s\n", ar.namewhat, ar.name);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
|
||||
116
nse_init.cc
116
nse_init.cc
@@ -36,9 +36,8 @@ int check_extension(const char* ext, const char* path);
|
||||
extern NmapOps o;
|
||||
|
||||
/* open the standard libs */
|
||||
int init_lua(lua_State* l) {
|
||||
|
||||
const luaL_Reg lualibs[] = {
|
||||
int init_lua(lua_State* l) { // FIXME: Use cpcall, let Lua error normally.
|
||||
static const luaL_Reg lualibs[] = {
|
||||
{"", luaopen_base},
|
||||
{LUA_LOADLIBNAME, luaopen_package},
|
||||
{LUA_TABLIBNAME, luaopen_table},
|
||||
@@ -126,6 +125,7 @@ int init_setlualibpath(lua_State* l){
|
||||
lua_pop(l,3);
|
||||
return SCRIPT_ENGINE_SUCCESS;
|
||||
}
|
||||
|
||||
/* parses the argument provided to --script-args and leaves the processed
|
||||
* string on the stack, after this it only has to be prepended with
|
||||
* "<tablename>={" and appended by "}", before it can be called by
|
||||
@@ -134,41 +134,18 @@ int init_setlualibpath(lua_State* l){
|
||||
int init_parseargs(lua_State* l){
|
||||
//FIXME - free o.script-args after we're finished!!!
|
||||
|
||||
if(o.scriptargs==NULL){ //if no arguments are provided we're done
|
||||
return SCRIPT_ENGINE_SUCCESS;
|
||||
}
|
||||
//prepare passed string for loading
|
||||
lua_getglobal(l,"string");
|
||||
lua_getfield(l,-1,"gsub");
|
||||
lua_pushvalue(l,-1);
|
||||
lua_pushstring(l,o.scriptargs);
|
||||
lua_pushstring(l,"=([^{},$]+)");
|
||||
lua_pushstring(l,"=\"%1\"");
|
||||
SCRIPT_ENGINE_TRY(lua_pcall(l,3,1,0));
|
||||
|
||||
/* copy the result on the bottom of the stack, since this is the part
|
||||
* we want to return
|
||||
*/
|
||||
lua_pushvalue(l,-1);
|
||||
lua_insert(l,1);
|
||||
lua_pushstring(l,"%b{}");
|
||||
lua_pushstring(l,"");
|
||||
SCRIPT_ENGINE_TRY(lua_pcall(l,3,1,0));
|
||||
lua_getfield(l,-2,"find");
|
||||
lua_pushvalue(l,-2);
|
||||
lua_pushstring(l,"[{}]");
|
||||
SCRIPT_ENGINE_TRY(lua_pcall(l,2,1,0));
|
||||
if(!lua_isnil(l,-1)){
|
||||
error("unbalanced brackets inside script-options!!");
|
||||
return SCRIPT_ENGINE_ERROR;
|
||||
}
|
||||
lua_settop(l,1); //clear stack
|
||||
if (o.scriptargs==NULL)
|
||||
return SCRIPT_ENGINE_SUCCESS; //if no arguments are provided we're done
|
||||
|
||||
//luaL_loadbuffer(l,tmp,strlen(tmp),"Script-Arguments");
|
||||
//if(lua_pcall(l,0,0,0)!=0){
|
||||
// error("error loading --script-args: %s",lua_tostring(l,-1));
|
||||
// return SCRIPT_ENGINE_ERROR;
|
||||
// }
|
||||
lua_pushstring(l, o.scriptargs);
|
||||
luaL_getmetafield(l, -1, "__index");
|
||||
lua_getfield(l, -1, "gsub");
|
||||
lua_pushvalue(l, -3);
|
||||
lua_pushliteral(l, "=([^{},]+)");
|
||||
lua_pushliteral(l, "=\"%1\"");
|
||||
SCRIPT_ENGINE_TRY(lua_pcall(l,3,1,0));
|
||||
lua_replace(l, 1);
|
||||
lua_settop(l,1); //clear stack
|
||||
|
||||
return SCRIPT_ENGINE_SUCCESS;
|
||||
}
|
||||
@@ -184,12 +161,13 @@ int init_setargs(lua_State *l){
|
||||
* processed using lua's functionality
|
||||
*/
|
||||
SCRIPT_ENGINE_TRY(init_parseargs(l));
|
||||
lua_pushstring(l,"nmap.registry.args={");
|
||||
lua_pushliteral(l,"nmap.registry.args={");
|
||||
lua_insert(l,-2);
|
||||
lua_pushstring(l,"}");
|
||||
lua_pushliteral(l,"}");
|
||||
lua_concat(l,3);
|
||||
argbuf=lua_tolstring(l,-1,&argbuflen);
|
||||
luaL_loadbuffer(l,argbuf,argbuflen,"Script-Arguments-prerun");
|
||||
luaL_loadbuffer(l,argbuf,argbuflen, "Script-Arguments");
|
||||
lua_replace(l, -2); // remove argbuf string
|
||||
if(lua_pcall(l,0,0,0)!=0){
|
||||
error("error loading --script-args: %s",lua_tostring(l,-1));
|
||||
return SCRIPT_ENGINE_ERROR;
|
||||
@@ -357,7 +335,7 @@ int init_updatedb(lua_State* l) {
|
||||
lua_newtable(l);
|
||||
/*give the script global namespace access*/
|
||||
lua_newtable(l);
|
||||
lua_getglobal(l, "_G");
|
||||
lua_pushvalue(l, LUA_GLOBALSINDEX);
|
||||
lua_setfield(l, -2, "__index");
|
||||
lua_setmetatable(l, -2);
|
||||
|
||||
@@ -702,28 +680,26 @@ int init_scandir(char* dirname, std::vector<std::string>& result, int files_or_d
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// Takes a string and converts \, ', and " characters so that
|
||||
// the string is suitable for embedding in a Lua ' or " string.
|
||||
// Remember to free() when finished
|
||||
|
||||
char *make_lua_escaped_string(char *str) {
|
||||
char *tp, *out;
|
||||
out = tp = (char *) safe_malloc((strlen(str)*2) + 1); // assume every character needs escaping
|
||||
|
||||
while(*str) {
|
||||
if (*str == '\\' || *str == '\'' || *str == '"') *tp++ = '\\';
|
||||
*tp++ = *str++;
|
||||
/* Error function if a user script attempts to create a new global */
|
||||
/* TODO: Why wasn't _changing_ globals handled? */
|
||||
static int global_error(lua_State *L)
|
||||
{
|
||||
lua_pushvalue(L, lua_upvalueindex(1));
|
||||
lua_pushvalue(L, 2);
|
||||
if (!lua_tostring(L, -1))
|
||||
{
|
||||
lua_pushliteral(L, "? (of type ");
|
||||
lua_pushstring(L, lua_typename(L, lua_type(L, -2)));
|
||||
lua_pushliteral(L, ")");
|
||||
lua_concat(L, 3);
|
||||
lua_replace(L, -2);
|
||||
}
|
||||
|
||||
*tp = '\0';
|
||||
|
||||
return out;
|
||||
lua_pushvalue(L, lua_upvalueindex(2));
|
||||
lua_concat(L, 3);
|
||||
fprintf(stderr, "%s\n", lua_tostring(L, -1));
|
||||
return lua_error(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* load an nmap-lua script
|
||||
* create a new closure to store the script
|
||||
* tell the closure where to find the standard
|
||||
@@ -735,7 +711,6 @@ char *make_lua_escaped_string(char *str) {
|
||||
* */
|
||||
int init_loadfile(lua_State* l, char* filename) {
|
||||
int rule_count;
|
||||
char *escaped_filename;
|
||||
|
||||
/* create a closure for encapsuled execution
|
||||
* give the closure access to the global enviroment
|
||||
@@ -749,7 +724,7 @@ int init_loadfile(lua_State* l, char* filename) {
|
||||
/* we give the script access to the global name space
|
||||
* */
|
||||
lua_newtable(l);
|
||||
lua_getglobal(l, "_G");
|
||||
lua_pushvalue(l, LUA_GLOBALSINDEX);
|
||||
lua_setfield(l, -2, "__index");
|
||||
lua_setmetatable(l, -2);
|
||||
|
||||
@@ -774,18 +749,15 @@ int init_loadfile(lua_State* l, char* filename) {
|
||||
* */
|
||||
lua_getmetatable(l, -1);
|
||||
|
||||
escaped_filename = make_lua_escaped_string(filename);
|
||||
|
||||
std::string buf =
|
||||
(std::string("err = \"Attempted to change the global '\" .. select(2, ...) .. \"' in ")
|
||||
+ std::string(escaped_filename)
|
||||
+ std::string(" - use nmap.registry if you really want to share data between scripts.\"")
|
||||
+ std::string("error(err)"));
|
||||
SCRIPT_ENGINE_LUA_TRY(luaL_loadbuffer(l, buf.c_str(), buf.length(), "Global Access"));
|
||||
lua_pushliteral(l, "Attempted to change the global '");
|
||||
lua_pushliteral(l, "' in ");
|
||||
lua_pushstring(l, filename);
|
||||
lua_pushliteral(l, " - use nmap.registry if you really want to share "
|
||||
"data between scripts.");
|
||||
lua_concat(l, 3);
|
||||
lua_pushcclosure(l, global_error, 2);
|
||||
lua_setfield(l, -2, "__newindex");
|
||||
|
||||
free(escaped_filename);
|
||||
|
||||
lua_setmetatable(l, -2);
|
||||
|
||||
/* store the initialized test in either
|
||||
|
||||
10
nse_main.cc
10
nse_main.cc
@@ -78,9 +78,9 @@ int script_updatedb() {
|
||||
SCRIPT_ENGINE);
|
||||
)
|
||||
|
||||
l = lua_open();
|
||||
l = luaL_newstate();
|
||||
if(l == NULL) {
|
||||
error("%s: Failed lua_open()", SCRIPT_ENGINE);
|
||||
error("%s: Failed luaL_newstate()", SCRIPT_ENGINE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ int script_check_args(){
|
||||
const char *argbuf;
|
||||
size_t argbuflen;
|
||||
|
||||
l= lua_open();
|
||||
l= luaL_newstate();
|
||||
if(l==NULL){
|
||||
fatal("Error opening lua, for checking arguments\n");
|
||||
}
|
||||
@@ -166,9 +166,9 @@ int script_scan(std::vector<Target*> &targets) {
|
||||
SCRIPT_ENGINE, (*targets.begin())->NameIP(targetstr, sizeof(targetstr)));
|
||||
)
|
||||
|
||||
l = lua_open();
|
||||
l = luaL_newstate();
|
||||
if(l == NULL) {
|
||||
error("%s: Failed lua_open()", SCRIPT_ENGINE);
|
||||
error("%s: Failed luaL_newstate()", SCRIPT_ENGINE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
66
nse_nsock.cc
66
nse_nsock.cc
@@ -1,5 +1,4 @@
|
||||
#include "nse_nsock.h"
|
||||
#include "nse_auxiliar.h"
|
||||
#include "nse_macros.h"
|
||||
#include "nse_string.h"
|
||||
|
||||
@@ -81,7 +80,6 @@ static luaL_reg l_nsock [] = {
|
||||
{"get_info", l_nsock_get_info},
|
||||
{"close", l_nsock_close},
|
||||
{"set_timeout", l_nsock_set_timeout},
|
||||
{"__gc",l_nsock_gc},
|
||||
{"pcap_open", l_nsock_ncap_open},
|
||||
{"pcap_close", l_nsock_ncap_close},
|
||||
{"pcap_register", l_nsock_ncap_register},
|
||||
@@ -156,7 +154,15 @@ struct l_nsock_udata {
|
||||
void l_nsock_clear_buf(lua_State* l, l_nsock_udata* udata);
|
||||
|
||||
int l_nsock_open(lua_State* l) {
|
||||
auxiliar_newclass(l, "nsock", l_nsock);
|
||||
luaL_newmetatable(l, "nsock");
|
||||
lua_createtable(l, 20, 0);
|
||||
luaL_register(l, NULL, l_nsock);
|
||||
lua_setfield(l, -2, "__index");
|
||||
lua_pushcclosure(l, l_nsock_gc, 0);
|
||||
lua_setfield(l, -2, "__gc");
|
||||
lua_pushliteral(l, "");
|
||||
lua_setfield(l, -2, "__metatable"); // protect metatable
|
||||
lua_pop(l, 1);
|
||||
|
||||
nsp = nsp_new(NULL);
|
||||
//nsp_settrace(nsp, o.debugging, o.getStartTime());
|
||||
@@ -169,7 +175,8 @@ int l_nsock_open(lua_State* l) {
|
||||
int l_nsock_new(lua_State* l) {
|
||||
struct l_nsock_udata* udata;
|
||||
udata = (struct l_nsock_udata*) lua_newuserdata(l, sizeof(struct l_nsock_udata));
|
||||
auxiliar_setclass(l, "nsock", -1);
|
||||
luaL_getmetatable(l, "nsock");
|
||||
lua_setmetatable(l, -2);
|
||||
udata->nsiod = NULL;
|
||||
udata->ssl_session = NULL;
|
||||
udata->timeout = DEFAULT_TIMEOUT;
|
||||
@@ -223,7 +230,7 @@ static int l_nsock_connect_queued(lua_State* l) {
|
||||
const int max_descriptors_allowed = MAX(o.max_parallelism, 10);
|
||||
|
||||
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
|
||||
if(udata->nsiod!=NULL){
|
||||
/*should a script try to connect a socket, which is already connected
|
||||
@@ -276,7 +283,7 @@ void l_nsock_connect_queued_handler(nsock_pool nsp, nsock_event nse, void *lua_s
|
||||
|
||||
|
||||
static int l_nsock_connect(lua_State* l) {
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
const char* addr = luaL_checkstring(l, 2);
|
||||
unsigned short port = (unsigned short) luaL_checkint(l, 3);
|
||||
const char *how = luaL_optstring(l, 4, "tcp");
|
||||
@@ -350,7 +357,7 @@ void l_nsock_connect_handler(nsock_pool nsp, nsock_event nse, void *lua_state) {
|
||||
}
|
||||
|
||||
static int l_nsock_send(lua_State* l) {
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
const char* string = luaL_checkstring(l, 2);
|
||||
size_t string_len = lua_objlen (l, 2);
|
||||
char* hexified;
|
||||
@@ -384,7 +391,7 @@ void l_nsock_send_handler(nsock_pool nsp, nsock_event nse, void *lua_state) {
|
||||
}
|
||||
|
||||
static int l_nsock_receive(lua_State* l) {
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
l_nsock_clear_buf(l, udata);
|
||||
|
||||
if(udata->nsiod == NULL) {
|
||||
@@ -399,7 +406,7 @@ static int l_nsock_receive(lua_State* l) {
|
||||
}
|
||||
|
||||
static int l_nsock_receive_lines(lua_State* l) {
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
int nlines = (int) luaL_checknumber(l, 2);
|
||||
|
||||
l_nsock_clear_buf(l, udata);
|
||||
@@ -416,7 +423,7 @@ static int l_nsock_receive_lines(lua_State* l) {
|
||||
}
|
||||
|
||||
static int l_nsock_receive_bytes(lua_State* l) {
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
int nbytes = (int) luaL_checknumber(l, 2);
|
||||
|
||||
l_nsock_clear_buf(l, udata);
|
||||
@@ -526,7 +533,7 @@ unsigned short inet_port_both(int af, const void* v_addr) {
|
||||
}
|
||||
|
||||
static int l_nsock_get_info(lua_State* l) {
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
int status;
|
||||
|
||||
int protocol; // tcp or udp
|
||||
@@ -558,7 +565,7 @@ static int l_nsock_get_info(lua_State* l) {
|
||||
return 5;
|
||||
}
|
||||
static int l_nsock_gc(lua_State* l){
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
if(udata->nsiod == NULL) { //socket obviously got closed already - so no finalization needed
|
||||
return 0;
|
||||
}else{
|
||||
@@ -569,7 +576,7 @@ static int l_nsock_gc(lua_State* l){
|
||||
}
|
||||
|
||||
static int l_nsock_close(lua_State* l) {
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
|
||||
/* Never ever collect nse-pcap connections. */
|
||||
if(udata->ncap_socket){
|
||||
@@ -613,7 +620,7 @@ static int l_nsock_close(lua_State* l) {
|
||||
}
|
||||
|
||||
static int l_nsock_set_timeout(lua_State* l) {
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
int timeout = (unsigned short) luaL_checkint(l, 2);
|
||||
|
||||
udata->timeout = timeout;
|
||||
@@ -623,7 +630,7 @@ static int l_nsock_set_timeout(lua_State* l) {
|
||||
|
||||
/* buffered I/O */
|
||||
static int l_nsock_receive_buf(lua_State* l) {
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
if(lua_gettop(l)==2){
|
||||
/*we were called with 2 arguments only - push the default third one*/
|
||||
lua_pushboolean(l,true);
|
||||
@@ -663,7 +670,7 @@ void l_nsock_receive_buf_handler(nsock_pool nsp, nsock_event nse, void *lua_stat
|
||||
int rcvd_len = 0;
|
||||
char* hexified;
|
||||
int tmpidx;
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
if(l_nsock_checkstatus(l, nse) == NSOCK_WRAPPER_SUCCESS) {
|
||||
|
||||
//l_nsock_checkstatus pushes true on the stack in case of success
|
||||
@@ -722,7 +729,7 @@ int l_nsock_check_buf(lua_State* l ){
|
||||
/*should we return the string including the pattern or without it */
|
||||
keeppattern= lua_toboolean(l,-1);
|
||||
lua_pop(l,1);
|
||||
udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
if(lua_isfunction(l,2)){
|
||||
lua_pushvalue(l,2);
|
||||
lua_rawgeti(l, LUA_REGISTRYINDEX, udata->bufidx); /* the buffer is the only argument to the function */
|
||||
@@ -860,7 +867,7 @@ char *dnet_to_pcap_device_name(const char *device){
|
||||
* 5) bpf - berkeley packet filter, see tcpdump(8)
|
||||
* */
|
||||
static int l_nsock_ncap_open(lua_State* l){
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
const char* device = luaL_checkstring(l, 2);
|
||||
int snaplen = luaL_checkint(l, 3);
|
||||
int promisc = luaL_checkint(l, 4);
|
||||
@@ -915,7 +922,7 @@ static int l_nsock_ncap_open(lua_State* l){
|
||||
/* (LUA) Close nsock-pcap socket.
|
||||
* */
|
||||
static int l_nsock_ncap_close(lua_State* l){
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
struct ncap_socket *ns = udata->ncap_socket;
|
||||
|
||||
if(!udata->nsiod || !udata->ncap_socket) {
|
||||
@@ -1004,7 +1011,7 @@ void ncap_request_map_add(char *key, struct ncap_request *nr){
|
||||
* want to receive first packet
|
||||
* */
|
||||
static int l_nsock_ncap_register(lua_State *l){
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
size_t testdatasz;
|
||||
const char* testdata = luaL_checklstring(l, 2, &testdatasz);
|
||||
|
||||
@@ -1046,7 +1053,7 @@ static int l_nsock_ncap_register(lua_State *l){
|
||||
* return values: status(true/false), capture_len/error_msg, layer2data, layer3data
|
||||
* */
|
||||
int l_nsock_pcap_receive(lua_State *l){
|
||||
l_nsock_udata* udata = (l_nsock_udata*) auxiliar_checkclass(l, "nsock", 1);
|
||||
l_nsock_udata* udata = (l_nsock_udata*) luaL_checkudata(l, 1, "nsock");
|
||||
if(!udata->nsiod || !udata->ncap_socket) {
|
||||
luaL_argerror(l, 1, "You can't receive to nsock-pcap if it wasn't opened.");
|
||||
return 0;
|
||||
@@ -1259,7 +1266,13 @@ static luaL_reg l_dnet [] = {
|
||||
};
|
||||
|
||||
int l_dnet_open(lua_State* l) {
|
||||
auxiliar_newclass(l, "dnet", l_dnet);
|
||||
luaL_newmetatable(l, "dnet");
|
||||
lua_createtable(l, 5, 0);
|
||||
luaL_register(l, NULL, l_dnet);
|
||||
lua_setfield(l, -2, "__index");
|
||||
lua_pushliteral(l, "");
|
||||
lua_setfield(l, -2, "__metatable"); // protect metatable
|
||||
lua_pop(l, 1);
|
||||
return NSOCK_WRAPPER_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1271,7 +1284,8 @@ struct l_dnet_udata {
|
||||
int l_dnet_new(lua_State* l) {
|
||||
struct l_dnet_udata* udata;
|
||||
udata = (struct l_dnet_udata*) lua_newuserdata(l, sizeof(struct l_dnet_udata));
|
||||
auxiliar_setclass(l, "dnet", -1);
|
||||
luaL_getmetatable(l, "dnet");
|
||||
lua_setmetatable(l, -2);
|
||||
udata->interface= NULL;
|
||||
udata->eth = NULL;
|
||||
|
||||
@@ -1352,7 +1366,7 @@ void ldnet_eth_close_cached(const char *device) {
|
||||
}
|
||||
|
||||
static int l_dnet_open_ethernet(lua_State* l){
|
||||
l_dnet_udata* udata = (l_dnet_udata*) auxiliar_checkclass(l, "dnet", 1);
|
||||
l_dnet_udata* udata = (l_dnet_udata*) luaL_checkudata(l, 1, "dnet");
|
||||
const char* interface_name = luaL_checkstring(l, 2);
|
||||
|
||||
struct interface_info *ii = getInterfaceByName((char*)interface_name);
|
||||
@@ -1367,7 +1381,7 @@ static int l_dnet_open_ethernet(lua_State* l){
|
||||
}
|
||||
|
||||
static int l_dnet_close_ethernet(lua_State* l){
|
||||
l_dnet_udata* udata = (l_dnet_udata*) auxiliar_checkclass(l, "dnet", 1);
|
||||
l_dnet_udata* udata = (l_dnet_udata*) luaL_checkudata(l, 1, "dnet");
|
||||
if(!udata->interface || !udata->eth){
|
||||
luaL_argerror(l, 1, "dnet is not valid opened ethernet interface");
|
||||
return 0;
|
||||
@@ -1381,7 +1395,7 @@ static int l_dnet_close_ethernet(lua_State* l){
|
||||
}
|
||||
|
||||
static int l_dnet_send_ethernet(lua_State* l){
|
||||
l_dnet_udata* udata = (l_dnet_udata*) auxiliar_checkclass(l, "dnet", 1);
|
||||
l_dnet_udata* udata = (l_dnet_udata*) luaL_checkudata(l, 1, "dnet");
|
||||
size_t packetsz = 0;
|
||||
const char* packet = luaL_checklstring(l, 2, &packetsz);
|
||||
|
||||
|
||||
@@ -25,14 +25,6 @@ extern "C" {
|
||||
|
||||
#include "nse_pcrelib.h"
|
||||
|
||||
static void L_lua_error(lua_State *L, const char *message)
|
||||
{
|
||||
int status;
|
||||
|
||||
lua_pushstring(L, message);
|
||||
status = lua_error(L);
|
||||
}
|
||||
|
||||
static int get_startoffset(lua_State *L, int stackpos, size_t len)
|
||||
{
|
||||
int startoffset = luaL_optint(L, stackpos, 1);
|
||||
@@ -105,12 +97,12 @@ static const unsigned char *Lpcre_maketables(lua_State *L, int stackpos)
|
||||
char *locale = strdup(luaL_checkstring(L, stackpos));
|
||||
|
||||
if(locale == NULL)
|
||||
L_lua_error(L, "cannot set locale");
|
||||
luaL_error(L, "cannot set locale");
|
||||
|
||||
strncpy(old_locale, setlocale(LC_CTYPE, NULL), 255); /* store the locale */
|
||||
|
||||
if(setlocale(LC_CTYPE, locale) == NULL) /* set new locale */
|
||||
L_lua_error(L, "cannot set locale");
|
||||
luaL_error(L, "cannot set locale");
|
||||
|
||||
tables = pcre_maketables(); /* make tables with new locale */
|
||||
(void)setlocale(LC_CTYPE, old_locale); /* restore the old locale */
|
||||
@@ -132,7 +124,7 @@ static int Lpcre_comp(lua_State *L)
|
||||
if(lua_gettop(L) > 2 && !lua_isnil(L, 3))
|
||||
tables = Lpcre_maketables(L, 3);
|
||||
if(tables == NULL)
|
||||
L_lua_error(L, "PCRE compilation failed");
|
||||
luaL_error(L, "PCRE compilation failed");
|
||||
|
||||
ud = (pcre2*)lua_newuserdata(L, sizeof(pcre2));
|
||||
luaL_getmetatable(L, pcre_handle);
|
||||
@@ -145,11 +137,11 @@ static int Lpcre_comp(lua_State *L)
|
||||
if(!ud->pr) {
|
||||
(void)Snprintf(buf, 255, "%s (pattern offset: %d)", error, erroffset+1);
|
||||
/* show offset 1-based as it's common in Lua */
|
||||
L_lua_error(L, buf);
|
||||
luaL_error(L, buf);
|
||||
}
|
||||
|
||||
ud->extra = pcre_study(ud->pr, 0, &error);
|
||||
if(error) L_lua_error(L, error);
|
||||
if(error) luaL_error(L, error);
|
||||
|
||||
pcre_fullinfo(ud->pr, ud->extra, PCRE_INFO_CAPTURECOUNT, &ud->ncapt);
|
||||
/* need (2 ints per capture, plus one for substring match) * 3/2 */
|
||||
@@ -408,10 +400,13 @@ static const luaL_reg pcrelib[] = {
|
||||
|
||||
LUALIB_API int luaopen_pcrelib(lua_State *L)
|
||||
{
|
||||
createmeta(L, pcre_handle);
|
||||
luaL_openlib(L, NULL, pcremeta, 0);
|
||||
luaL_newmetatable(L, pcre_handle);
|
||||
lua_pushliteral(L, "__index");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_rawset(L, -3);
|
||||
luaL_register(L, NULL, pcremeta);
|
||||
lua_pop(L, 1);
|
||||
luaL_openlib(L, NSE_PCRELIBNAME, pcrelib, 0);
|
||||
luaL_register(L, NSE_PCRELIBNAME, pcrelib);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
-- See nmaps COPYING for licence
|
||||
module(... or "stdnse", package.seeall)
|
||||
|
||||
module(..., package.seeall)
|
||||
|
||||
print_debug = function(...)
|
||||
local verbosity = 1;
|
||||
@@ -28,7 +29,7 @@ function strsplit(delimiter, text)
|
||||
error("delimiter matches empty string!")
|
||||
end
|
||||
|
||||
while 1 do
|
||||
while true do
|
||||
local first, last = string.find(text, delimiter, pos)
|
||||
if first then -- found?
|
||||
table.insert(list, string.sub(text, pos, first-1))
|
||||
|
||||
@@ -21,16 +21,7 @@ require 'bit'
|
||||
|
||||
-- Grabs NUL-terminated string
|
||||
local getstring = function(orig)
|
||||
local str = ""
|
||||
local index = 1
|
||||
|
||||
while orig:byte(index) ~= 0 do
|
||||
str = str .. string.char(orig:byte(index))
|
||||
|
||||
index = index + 1
|
||||
end
|
||||
|
||||
return str
|
||||
return orig:match("^([^%z]*)");
|
||||
end
|
||||
|
||||
-- Convert two bytes into a number
|
||||
|
||||
Reference in New Issue
Block a user