/*************************************************************************** * ncat_lua_listen.c -- Ncat Lua filters listen-mode 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" #include "ncat_listen.h" static void replace_root_functions(int fd) { /* If we reached lua_fdinfo_recv while we're in connect mode, it's most likely because we connected to the proxy. Replace the recv and send functions for this FD so they won't try to use the connect-mode functions. */ lua_fetch_registry("connection_roots"); lua_pushinteger(filters_L, fd); lua_gettable(filters_L, -2); ncat_assert(lua_istable(filters_L, -1)); lua_pushstring(filters_L, "recv"); lua_pushcfunction(filters_L, lua_fdinfo_recv_raw); lua_settable(filters_L, -3); lua_pushstring(filters_L, "send"); lua_pushcfunction(filters_L, lua_fdinfo_send_raw); lua_settable(filters_L, -3); lua_pop(filters_L, 2); } int lua_fdinfo_recv_raw(lua_State *L) { char buf[DEFAULT_TCP_BUF_LEN]; int ret; struct ncat_lua_state *nls = lua_fetch_userdata(filters_L, 1, &ret); if (ret >= 0) return ret; int bytes_read = fdinfo_recv_raw(&nls->fdn, buf, sizeof(buf), &nls->pending); if (bytes_read > 0) { lua_pushlstring(filters_L, buf, bytes_read); return 1; } else { nls->fdinfo_retval = bytes_read; lua_pushnil(filters_L); lua_pushstring(filters_L, socket_strerror(socket_errno())); return 2; } } int lua_fdinfo_recv(struct fdinfo *fdn, char *buf, size_t size, int *pending, int *error) { int created, old_top, num_returned; size_t oldsize = size; struct ncat_lua_state *nl = get_connection(fdn, &created); if (created && !o.listen) replace_root_functions(fdn->fd); *pending = nl->pending = 0; if (nl->recv_buf != NULL) { int chunk_size = nl->recv_buf_size-nl->recv_buf_pos; if (chunk_size > size) chunk_size = size; if (o.debug) logdebug("lua_ncat_recv: sending the next chunk of data.\n"); memcpy(buf, nl->recv_buf+nl->recv_buf_pos, chunk_size); nl->recv_buf_pos += chunk_size; if (nl->recv_buf_pos >= nl->recv_buf_size) { if (o.debug) logdebug("lua_ncat_recv: sent the last chunk.\n"); *pending = 0; nl->recv_buf = NULL; } return chunk_size; } /* Save the number of elements on the stack before the function call. */ old_top = lua_gettop(filters_L); lua_getfield(filters_L, -1, "recv"); /* Move the socket to the top, so we can call socket.recv(socket) or socket:recv(). */ lua_insert(filters_L, -2); if (lua_pcall(filters_L, 1, LUA_MULTRET, 0) != LUA_OK) { if (o.debug) logdebug("Error calling recv: %s", lua_tostring(filters_L, -1)); *error = 1; size = -1; } num_returned = lua_gettop(filters_L) - old_top + 1; if (num_returned > 2) { if (o.debug) logdebug("recv() routine returned more than two values.\n"); lua_pop(filters_L, num_returned); *error = 1; size = -1; } if (num_returned == 2 && lua_isnil(filters_L, -2)) { size = nl->fdinfo_retval; lua_pop(filters_L, 2); /* the nil and the number */ *error = 1; } else { if (num_returned > 1) { if (o.debug) logdebug("recv() routine returned more than one non-nil value.\n"); size = -1; *error = 1; } else if (!lua_isstring(filters_L, -1)) { if (o.debug) logdebug("recv() routine did not return a string.\n"); size = -1; *error = 1; } else { char* ret = (char *) lua_tolstring(filters_L, -1, &size); ncat_assert(size >= 0); if (size > oldsize) { if (o.debug) logdebug("lua_ncat_recv: size=%zu > oldsize, chunking the output.\n", size); nl->recv_buf = ret; nl->recv_buf_size = size; nl->recv_buf_pos = oldsize; *pending = 1; size = oldsize; } memcpy(buf, ret, size); lua_pop(filters_L, 1); /* the string. */ } } *pending = nl->pending; return size; } int lua_fdinfo_send_raw(lua_State *L) { size_t size; int ret; char *buf; struct ncat_lua_state *nls = lua_fetch_userdata(L, 1, &ret); if (ret >= 0) return ret; buf = (char *) lua_tolstring(L, 2, &size); if (fdinfo_send_raw(&nls->fdn, buf, size) < 0) { lua_pushnil(L); lua_pushstring(L, socket_strerror(socket_errno())); return 2; } lua_pushboolean(L, 1); return 1; } int lua_fdinfo_send(struct fdinfo *fdn, const char *buf, size_t size) { int created, old_top, num_returned; get_connection(fdn, &created); if (created && !o.listen) replace_root_functions(fdn->fd); /* Save the number of elements on the stack before the function call. */ old_top = lua_gettop(filters_L); lua_getfield(filters_L, -1, "send"); lua_insert(filters_L, -2); lua_pushlstring(filters_L, buf, size); if (lua_pcall(filters_L, 2, LUA_MULTRET, error_handler_idx) != LUA_OK) { lua_report(filters_L, "send() error", 0); return -1; } num_returned = lua_gettop(filters_L) - old_top + 1; if (num_returned > 2 || num_returned < 1) { if (o.debug) logdebug("lua_fdinfo_send: got wrong number of return values (%d)\n", num_returned); return -1; } if (lua_isnil(filters_L, -1)) { if (o.debug) logdebug("lua_fdinfo_send: got nil, making send() fail.\n"); return -1; } return size; } int lua_fdinfo_close(lua_State *L) { int ret; struct ncat_lua_state *nls = lua_fetch_userdata(L, 1, &ret); if (ret >= 0) return ret; fdinfo_close(&nls->fdn); unregister_fd(nls->fdn.fd); lua_unregister_fd(&nls->fdn); return 0; } int lua_run_connected(struct fdinfo *fdn, const char *ip, int port) { get_connection(fdn, NULL); lua_getfield(filters_L, -1, "connect"); lua_insert(filters_L, -2); lua_pushstring(filters_L, ip); if (port != -1) lua_pushinteger(filters_L, port); else lua_pushnil(filters_L); if (lua_pcall(filters_L, 3, 0, 0) != LUA_OK) { lua_report(filters_L, "connect() error", 0); return -1; } return 0; }