/*************************************************************************** * ncat_lua_filters.c -- Ncat Lua filters shared code * ***********************IMPORTANT NMAP LICENSE TERMS************************ * * * The Nmap Security Scanner is (C) 1996-2013 Insecure.Com LLC. Nmap is * * also a registered trademark of Insecure.Com LLC. This program is free * * software; you may redistribute and/or modify it under the terms of the * * GNU General Public License as published by the Free Software * * Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE CLARIFICATIONS * * AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your right to use, * * modify, and redistribute this software under certain conditions. If * * you wish to embed Nmap technology into proprietary software, we sell * * alternative licenses (contact sales@nmap.com). Dozens of software * * vendors already license Nmap technology such as host discovery, port * * scanning, OS detection, version detection, and the Nmap Scripting * * Engine. * * * * Note that the GPL places important restrictions on "derivative works", * * yet it does not provide a detailed definition of that term. To avoid * * misunderstandings, we interpret that term as broadly as copyright law * * allows. For example, we consider an application to constitute a * * derivative work for the purpose of this license if it does any of the * * following with any software or content covered by this license * * ("Covered Software"): * * * * o Integrates source code from Covered Software. * * * * o Reads or includes copyrighted data files, such as Nmap's nmap-os-db * * or nmap-service-probes. * * * * o Is designed specifically to execute Covered Software and parse the * * results (as opposed to typical shell or execution-menu apps, which will * * execute anything you tell them to). * * * * o Includes Covered Software in a proprietary executable installer. The * * installers produced by InstallShield are an example of this. Including * * Nmap with other software in compressed or archival form does not * * trigger this provision, provided appropriate open source decompression * * or de-archiving software is widely available for no charge. For the * * purposes of this license, an installer is considered to include Covered * * Software even if it actually retrieves a copy of Covered Software from * * another source during runtime (such as by downloading it from the * * Internet). * * * * o Links (statically or dynamically) to a library which does any of the * * above. * * * * o Executes a helper program, module, or script to do any of the above. * * * * This list is not exclusive, but is meant to clarify our interpretation * * of derived works with some common examples. Other people may interpret * * the plain GPL differently, so we consider this a special exception to * * the GPL that we apply to Covered Software. Works which meet any of * * these conditions must conform to all of the terms of this license, * * particularly including the GPL Section 3 requirements of providing * * source code and allowing free redistribution of the work as a whole. * * * * As another special exception to the GPL terms, Insecure.Com LLC grants * * permission to link the code of this program with any version of the * * OpenSSL library which is distributed under a license identical to that * * listed in the included docs/licenses/OpenSSL.txt file, and distribute * * linked combinations including the two. * * * * Any redistribution of Covered Software, including any derived works, * * must obey and carry forward all of the terms of this license, including * * obeying all GPL rules and restrictions. For example, source code of * * the whole work must be provided and free redistribution must be * * allowed. All GPL references to "this License", are to be treated as * * including the terms and conditions of this license text as well. * * * * Because this license imposes special exceptions to the GPL, Covered * * Work may not be combined (even as part of a larger work) with plain GPL * * software. The terms, conditions, and exceptions of this license must * * be included as well. This license is incompatible with some other open * * source licenses as well. In some cases we can relicense portions of * * Nmap or grant special permissions to use it in other open source * * software. Please contact fyodor@nmap.org with any such requests. * * Similarly, we don't incorporate incompatible open source software into * * Covered Software without special permission from the copyright holders. * * * * If you have any questions about the licensing restrictions on using * * Nmap in other works, are happy to help. As mentioned above, we also * * offer alternative license to integrate Nmap into proprietary * * applications and appliances. These contracts have been sold to dozens * * of software vendors, and generally include a perpetual license as well * * as providing for priority support and updates. They also fund the * * continued development of Nmap. Please email sales@nmap.com for further * * information. * * * * If you have received a written license agreement or contract for * * Covered Software stating terms other than these, you may choose to use * * and redistribute Covered Software under those terms instead of these. * * * * Source is provided to this software because we believe users have a * * right to know exactly what a program is going to do before they run it. * * This also allows you to audit the software for security holes (none * * have been found so far). * * * * Source code also allows you to port Nmap to new platforms, fix bugs, * * and add new features. You are highly encouraged to send your changes * * to the dev@nmap.org mailing list for possible incorporation into the * * main distribution. By sending these changes to Fyodor or one of the * * Insecure.Org development mailing lists, or checking them into the Nmap * * source code repository, it is understood (unless you specify otherwise) * * that you are offering the Nmap Project (Insecure.Com LLC) the * * unlimited, non-exclusive right to reuse, modify, and relicense the * * code. Nmap will always be available Open Source, but this is important * * because the inability to relicense code has caused devastating problems * * for other Free Software projects (such as KDE and NASM). We also * * occasionally relicense the code to third parties as discussed above. * * If you wish to specify special license conditions of your * * contributions, just say so when you send them. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Nmap * * license file for more details (it's in a COPYING file included with * * Nmap, and also available from https://svn.nmap.org/nmap/COPYING * * * ***************************************************************************/ /* $Id$ */ #include "ncat.h" #include "ncat_lua.h" #include "ncat_lua_filters.h" static int make_socket_function_idx; static int lua_do_nothing(lua_State *L) { return 0; } static void lua_create_supersocket(lua_State *L) { lua_newtable(L); lua_pushstring(L, "recv"); if (o.listen) lua_pushcfunction(L, lua_do_nothing); /* TODO */ else lua_pushcfunction(L, ncat_ncat_lua_nsock_read_raw); lua_settable(L, -3); lua_pushstring(L, "send"); if (o.listen) lua_pushcfunction(L, lua_do_nothing); else lua_pushcfunction(L, ncat_ncat_lua_nsock_write_raw); lua_settable(L, -3); lua_setfield(L, LUA_REGISTRYINDEX, "socket"); } lua_State* ncat_lua_filters_setup() { lua_State *L = luaL_newstate(); luaL_openlibs(L); /* Initialize the registry. */ lua_pushlightuserdata(L, L); lua_newtable(L); lua_settable(L, LUA_REGISTRYINDEX); lua_create_supersocket(L); luaL_loadstring(L, "return function(function_self, self, arg2) " "local super = arg2 or self.super " "local ret " "if super then " "ret = {} " "for _, m in pairs({'send','recv'}) do " /* Set the method to one that calls super and passes it all the arguments we got, returning everything we'd get. */ "ret[m] = function(self, ...) " "return table.unpack({self.super[m](self.super, ...)}) " "end " "end " "else " "ret = {} " "end " "for k, v in pairs(self) do " "ret[k] = v " "end " "if super then " "ret.super = function_self(function_self, super) " "end " "return ret " "end "); ncat_lua_call_traceback(L, 0, 1); make_socket_function_idx = lua_gettop(L); return L; } void ncat_ncat_lua_run_filter(lua_State *L, char *filename) { ncat_assert(filename != NULL); if (luaL_loadfile(L, filename) != 0) ncat_lua_report(L, "Error loading the Lua script", 1); ncat_lua_call_traceback(L, 0, 1); if (!lua_istable(L, -1)) bye("%s did not return a table.", filename); /* Overwrite the socket variable with make_socket(socket_from_file, socket). */ lua_pushvalue(L, make_socket_function_idx); lua_insert(L, -2); lua_pushvalue(L, make_socket_function_idx); lua_insert(L, -3); lua_getfield(L, LUA_REGISTRYINDEX, "socket"); if (ncat_lua_call_traceback(L, 3, 1) != LUA_OK) ncat_lua_report(L, filename, 1); lua_setfield(L, LUA_REGISTRYINDEX, "socket"); } struct ncat_lua_state* ncat_lua_create_connection(lua_State *L, struct fdinfo *fdn) { struct ncat_lua_state *ret; /* Basically: connections[fd] = make_socket(socket) */ lua_pushvalue(L, make_socket_function_idx); lua_pushvalue(L, make_socket_function_idx); lua_getfield(L, LUA_REGISTRYINDEX, "socket"); if (ncat_lua_call_traceback(L, 2, 1) != LUA_OK) ncat_lua_report(L, "Error creating the socket", 1); lua_pushvalue(L, -1); /* Make a copy of the connection we created. */ fdn->connection_ref = luaL_ref(L, LUA_REGISTRYINDEX); /* Make another copy of the table - we'll work on the current one looking for the topmost super. */ lua_pushvalue(L, -1); for(;;) { lua_pushvalue(L, -1); /* Copy the current table */ lua_pushstring(L, "super"); lua_gettable(L, -2); lua_insert(L, -2); /* Move the copy to the top, pop it */ lua_pop(L, 1); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* Pop the nil */ break; /* There's no super, we're at the top */ } lua_insert(L, -2); /* Get rid of the old table */ lua_pop(L, 1); } ret = (struct ncat_lua_state *) Calloc(1, sizeof(*ret)); if (fdn != NULL) ret->fdn = fdn; /* Set the "lua_state" to a pointer to ret and save the reference. */ lua_pushlightuserdata(L, ret); lua_setfield(L, -2, "lua_state"); fdn->super_ref = luaL_ref(L, LUA_REGISTRYINDEX); return ret; } /* Find a connection based on connection_ret field in in the given struct fdinfo. The connection has to first be registered using ncat_lua_create_connection(). */ struct ncat_lua_state* ncat_lua_get_connection(lua_State *L, struct fdinfo *fdn) { struct ncat_lua_state *ret; lua_rawgeti(L, LUA_REGISTRYINDEX, fdn->connection_ref); ncat_assert(!lua_isnil(L, -1)); lua_rawgeti(L, LUA_REGISTRYINDEX, fdn->super_ref); lua_getfield(L, -1, "lua_state"); ret = (struct ncat_lua_state *) lua_touserdata(L, -1); lua_pop(L, 2); /* Pop the userdata and the table. */ return ret; } /* Read "lua_state" field from socket table available under stack index given as the second argument. If after the call *ret is set to a non-negative value, it must be returned. */ struct ncat_lua_state* ncat_lua_fetch_userdata(lua_State *L, int idx, int *ret) { struct ncat_lua_state *nls; *ret = -1; lua_getfield(L, idx, "lua_state"); nls = (struct ncat_lua_state *) lua_touserdata(L, -1); if (nls == NULL) { lua_pushnil(L); lua_pushstring(L, "Socket already closed"); *ret = 2; } return nls; }