/*************************************************************************** * ncat_main.c -- main function: option parsing and checking, dispatching * * to mode-specific functions. * ***********************IMPORTANT NMAP LICENSE TERMS************************ * * * The Nmap Security Scanner is (C) 1996-2015 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. * * * * 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 "nsock.h" #include "ncat.h" #include "util.h" #include "sys_wrap.h" #include #ifndef WIN32 #include #endif #include #include #include #include #ifndef WIN32 #include #endif #include #ifdef HAVE_OPENSSL #include #include #endif #ifdef HAVE_LUA #include "ncat_lua.h" #endif static int ncat_connect_mode(void); static int ncat_listen_mode(void); /* Determines if it's parsing HTTP or SOCKS by looking at defport */ static size_t parseproxy(char *str, struct sockaddr_storage *ss, size_t *sslen, unsigned short *portno) { char *c = strrchr(str, ':'), *ptr; int rc; ptr = str; if (c) *c = 0; if (c && strlen((c + 1))) *portno = (unsigned short) atoi(c + 1); rc = resolve(ptr, *portno, ss, sslen, o.af); if (rc != 0) { loguser("Could not resolve proxy \"%s\": %s.\n", ptr, gai_strerror(rc)); if (o.af == AF_INET6 && *portno) loguser("Did you specify the port number? It's required for IPv6.\n"); exit(EXIT_FAILURE); } return *sslen; } /* These functions implement a simple linked list to hold allow/deny specifications until the end of option parsing. */ struct host_list_node { /* If false, then spec is the name of a file containing host patterns. */ int is_filename; char *spec; struct host_list_node *next; }; static void host_list_add_spec(struct host_list_node **list, char *spec) { struct host_list_node *node = (struct host_list_node *) safe_malloc(sizeof(*node)); node->is_filename = 0; node->spec = spec; node->next = *list; *list = node; } static void host_list_add_filename(struct host_list_node **list, char *filename) { struct host_list_node *node = (struct host_list_node *) safe_malloc(sizeof(*node)); node->is_filename = 1; node->spec = filename; node->next = *list; *list = node; } static void host_list_free(struct host_list_node *list) { struct host_list_node *next; for ( ; list != NULL; list = next) { next = list->next; free(list); } } static void host_list_to_set(struct addrset *set, struct host_list_node *list) { struct host_list_node *node; for (node = list; node != NULL; node = node->next) { if (node->is_filename) { FILE *fd; fd = fopen(node->spec, "r"); if (fd == NULL) bye("can't open %s: %s.", node->spec, strerror(errno)); if (!addrset_add_file(set, fd, o.af, !o.nodns)) bye("error in hosts file %s.", node->spec); fclose(fd); } else { char *spec, *commalist; commalist = node->spec; while ((spec = strtok(commalist, ",")) != NULL) { commalist = NULL; if (!addrset_add_spec(set, spec, o.af, !o.nodns)) bye("error in host specification \"%s\".", node->spec); } } } } static void print_banner(void) { loguser("Version %s ( %s )\n", NCAT_VERSION, NCAT_URL); } int main(int argc, char *argv[]) { /* We have to buffer the lists of hosts to allow and deny until after option parsing is done. Adding hosts to an addrset can require name resolution, which may differ as a result of options like -n and -6. */ struct host_list_node *allow_host_list = NULL; struct host_list_node *deny_host_list = NULL; unsigned short proxyport = DEFAULT_PROXY_PORT; int srcport = -1; char *source = NULL; struct option long_options[] = { {"4", no_argument, NULL, '4'}, {"6", no_argument, NULL, '6'}, #if HAVE_SYS_UN_H {"unixsock", no_argument, NULL, 'U'}, #endif {"crlf", no_argument, NULL, 'C'}, {"g", required_argument, NULL, 'g'}, {"G", required_argument, NULL, 'G'}, {"exec", required_argument, NULL, 'e'}, {"sh-exec", required_argument, NULL, 'c'}, #ifdef HAVE_LUA {"lua-exec", required_argument, NULL, 0}, {"lua-exec-internal",required_argument, NULL, 0}, #endif {"max-conns", required_argument, NULL, 'm'}, {"help", no_argument, NULL, 'h'}, {"delay", required_argument, NULL, 'd'}, {"listen", no_argument, NULL, 'l'}, {"output", required_argument, NULL, 'o'}, {"hex-dump", required_argument, NULL, 'x'}, {"append-output", no_argument, NULL, 0}, {"idle-timeout", required_argument, NULL, 'i'}, {"keep-open", no_argument, NULL, 'k'}, {"recv-only", no_argument, &o.recvonly, 1}, {"source-port", required_argument, NULL, 'p'}, {"source", required_argument, NULL, 's'}, {"send-only", no_argument, &o.sendonly, 1}, {"broker", no_argument, NULL, 0}, {"chat", no_argument, NULL, 0}, {"talk", no_argument, NULL, 0}, {"deny", required_argument, NULL, 0}, {"denyfile", required_argument, NULL, 0}, {"allow", required_argument, NULL, 0}, {"allowfile", required_argument, NULL, 0}, {"telnet", no_argument, NULL, 't'}, {"udp", no_argument, NULL, 'u'}, {"sctp", no_argument, NULL, 0}, {"version", no_argument, NULL, 0}, {"verbose", no_argument, NULL, 'v'}, {"wait", required_argument, NULL, 'w'}, {"nodns", no_argument, NULL, 'n'}, {"proxy", required_argument, NULL, 0}, {"proxy-type", required_argument, NULL, 0}, {"proxy-auth", required_argument, NULL, 0}, {"nsock-engine", required_argument, NULL, 0}, {"test", no_argument, NULL, 0}, {"ssl", no_argument, &o.ssl, 1}, #ifdef HAVE_OPENSSL {"ssl-cert", required_argument, NULL, 0}, {"ssl-key", required_argument, NULL, 0}, {"ssl-verify", no_argument, NULL, 0}, {"ssl-trustfile", required_argument, NULL, 0}, {"ssl-ciphers", required_argument, NULL, 0}, #else {"ssl-cert", optional_argument, NULL, 0}, {"ssl-key", optional_argument, NULL, 0}, {"ssl-verify", no_argument, NULL, 0}, {"ssl-trustfile", optional_argument, NULL, 0}, {"ssl-ciphers", optional_argument, NULL, 0}, #endif {0, 0, 0, 0} }; gettimeofday(&start_time, NULL); /* Set default options. */ options_init(); #ifdef WIN32 windows_init(); #endif while (1) { /* handle command line arguments */ int option_index; int c = getopt_long(argc, argv, "46UCc:e:g:G:i:km:hp:d:lo:x:ts:uvw:n", long_options, &option_index); /* That's the end of the options. */ if (c == -1) break; switch (c) { case '4': o.af = AF_INET; break; case '6': #ifdef HAVE_IPV6 o.af = AF_INET6; #else bye("-6 chosen when IPv6 wasn't compiled in."); #endif break; #if HAVE_SYS_UN_H case 'U': o.af = AF_UNIX; break; #endif case 'C': o.crlf = 1; break; case 'c': if (o.cmdexec != NULL) bye("Only one of --exec, --sh-exec, and --lua-exec is allowed."); o.cmdexec = optarg; o.execmode = EXEC_SHELL; break; case 'e': if (o.cmdexec != NULL) bye("Only one of --exec, --sh-exec, and --lua-exec is allowed."); o.cmdexec = optarg; o.execmode = EXEC_PLAIN; break; case 'g': { char *a = strtok(optarg, ","); do { union sockaddr_u addr; size_t sslen; int rc; rc = resolve(a, 0, &addr.storage, &sslen, AF_INET); if (rc != 0) { bye("Sorry, could not resolve source route hop \"%s\": %s.", a, gai_strerror(rc)); } o.srcrtes[o.numsrcrtes] = addr.in.sin_addr; } while (++o.numsrcrtes < 8 && (a = strtok(NULL, ","))); if (strtok(NULL, ",")) bye("Sorry, you gave too many source route hops."); break; } case 'G': o.srcrteptr = atoi(optarg); if (o.srcrteptr < 4 || (o.srcrteptr % 4) || o.srcrteptr > 28) bye("Invalid source-route hop pointer %d.", o.srcrteptr); break; case 'k': o.keepopen = 1; break; case 'm': o.conn_limit = atoi(optarg); break; case 'd': o.linedelay = tval2msecs(optarg); if (o.linedelay <= 0) bye("Invalid -d delay \"%s\" (must be greater than 0).", optarg); if (o.linedelay >= 100 * 1000 && tval_unit(optarg) == NULL) bye("Since April 2010, the default unit for -d is seconds, so your time of \"%s\" is %.1f minutes. Use \"%sms\" for %g milliseconds.", optarg, o.linedelay / 1000.0 / 60, optarg, o.linedelay / 1000.0); break; case 'o': o.normlog = optarg; break; case 'x': o.hexlog = optarg; break; case 'p': srcport = atoi(optarg); if (srcport < 0 || srcport > 0xffff) bye("Invalid source port %d.", srcport); break; case 'i': o.idletimeout = tval2msecs(optarg); if (o.idletimeout <= 0) bye("Invalid -i timeout (must be greater than 0)."); if (o.idletimeout >= 100 * 1000 && tval_unit(optarg) == NULL) bye("Since April 2010, the default unit for -i is seconds, so your time of \"%s\" is %.1f minutes. Use \"%sms\" for %g milliseconds.", optarg, o.idletimeout / 1000.0 / 60, optarg, o.idletimeout / 1000.0); break; case 's': source = optarg; break; case 'l': o.listen = 1; break; case 'u': o.proto = IPPROTO_UDP; break; case 'v': /* One -v activates verbose, after that it's debugging. */ if (o.verbose == 0) o.verbose++; else o.debug++; break; case 'n': o.nodns = 1; break; case 'w': o.conntimeout = tval2msecs(optarg); if (o.conntimeout <= 0) bye("Invalid -w timeout (must be greater than 0)."); if (o.conntimeout >= 100 * 1000 && tval_unit(optarg) == NULL) bye("Since April 2010, the default unit for -w is seconds, so your time of \"%s\" is %.1f minutes. Use \"%sms\" for %g milliseconds.", optarg, o.conntimeout / 1000.0 / 60, optarg, o.conntimeout / 1000.0); break; case 't': o.telnet = 1; break; case 0: if (strcmp(long_options[option_index].name, "version") == 0) { print_banner(); exit(EXIT_SUCCESS); } else if (strcmp(long_options[option_index].name, "proxy") == 0) { if (o.proxyaddr) bye("You can't specify more than one --proxy."); o.proxyaddr = optarg; } else if (strcmp(long_options[option_index].name, "proxy-type") == 0) { if (o.proxytype) bye("You can't specify more than one --proxy-type."); o.proxytype = optarg; } else if (strcmp(long_options[option_index].name, "proxy-auth") == 0) { if (o.proxy_auth) bye("You can't specify more than one --proxy-auth."); o.proxy_auth = optarg; } else if (strcmp(long_options[option_index].name, "nsock-engine") == 0) { if (nsock_set_default_engine(optarg) < 0) bye("Unknown or non-available engine: %s.", optarg); o.nsock_engine = 1; } else if (strcmp(long_options[option_index].name, "test") == 0) { o.test = 1; } else if (strcmp(long_options[option_index].name, "broker") == 0) { o.broker = 1; /* --broker implies --listen. */ o.listen = 1; } else if (strcmp(long_options[option_index].name, "chat") == 0 || strcmp(long_options[option_index].name, "talk") == 0) { /* --talk is an older name for --chat. */ o.chat = 1; /* --chat implies --broker. */ o.broker = 1; } else if (strcmp(long_options[option_index].name, "allow") == 0) { o.allow = 1; host_list_add_spec(&allow_host_list, optarg); } else if (strcmp(long_options[option_index].name, "allowfile") == 0) { o.allow = 1; host_list_add_filename(&allow_host_list, optarg); } else if (strcmp(long_options[option_index].name, "deny") == 0) { host_list_add_spec(&deny_host_list, optarg); } else if (strcmp(long_options[option_index].name, "denyfile") == 0) { host_list_add_filename(&deny_host_list, optarg); } else if (strcmp(long_options[option_index].name, "append-output") == 0) { o.append = 1; } else if (strcmp(long_options[option_index].name, "sctp") == 0) { o.proto = IPPROTO_SCTP; } #ifdef HAVE_OPENSSL else if (strcmp(long_options[option_index].name, "ssl-cert") == 0) { o.ssl = 1; o.sslcert = Strdup(optarg); } else if (strcmp(long_options[option_index].name, "ssl-key") == 0) { o.ssl = 1; o.sslkey = Strdup(optarg); } else if (strcmp(long_options[option_index].name, "ssl-verify") == 0) { o.sslverify = 1; o.ssl = 1; } else if (strcmp(long_options[option_index].name, "ssl-trustfile") == 0) { o.ssl = 1; if (o.ssltrustfile != NULL) bye("The --ssl-trustfile option may be given only once."); o.ssltrustfile = Strdup(optarg); /* If they list a trustfile assume they want certificate verification. */ o.sslverify = 1; } else if (strcmp(long_options[option_index].name, "ssl-ciphers") == 0) { o.ssl = 1; o.sslciphers = Strdup(optarg); } #else else if (strcmp(long_options[option_index].name, "ssl-cert") == 0) { bye("OpenSSL isn't compiled in. The --ssl-cert option cannot be chosen."); } else if (strcmp(long_options[option_index].name, "ssl-key") == 0) { bye("OpenSSL isn't compiled in. The --ssl-key option cannot be chosen."); } else if (strcmp(long_options[option_index].name, "ssl-verify") == 0) { bye("OpenSSL isn't compiled in. The --ssl-verify option cannot be chosen."); } else if (strcmp(long_options[option_index].name, "ssl-trustfile") == 0) { bye("OpenSSL isn't compiled in. The --ssl-trustfile option cannot be chosen."); } else if (strcmp(long_options[option_index].name, "ssl-ciphers") == 0) { bye("OpenSSL isn't compiled in. The --ssl-ciphers option cannot be chosen."); } #endif #ifdef HAVE_LUA else if (strcmp(long_options[option_index].name, "lua-exec") == 0) { if (o.cmdexec != NULL) bye("Only one of --exec, --sh-exec, and --lua-exec is allowed."); o.cmdexec = optarg; o.execmode = EXEC_LUA; } else if (strcmp(long_options[option_index].name, "lua-exec-internal") == 0) { /* This command-line switch is undocumented on purpose. Do NOT use it explicitly as its behavior might differ between Ncat releases. Its goal is to switch the Ncat process to the Lua interpreter state so that its standard output and input can be redirected to particular connection's streams. Although it is implemented by forking in POSIX builds, Windows does not have the fork() system call and thus requires this workaround. More info here: http://seclists.org/nmap-dev/2013/q2/492 */ #ifdef WIN32 if (o.debug) logdebug("Enabling binary stdout for the Lua output.\n"); int result = _setmode(_fileno(stdout), _O_BINARY); if (result == -1) perror("Cannot set mode"); #endif ncat_assert(argc == 3); o.cmdexec = argv[2]; lua_setup(); lua_run(); } #endif break; case 'h': printf("%s %s ( %s )\n", NCAT_NAME, NCAT_VERSION, NCAT_URL); printf( "Usage: ncat [options] [hostname] [port]\n" "\n" "Options taking a time assume seconds. Append 'ms' for milliseconds,\n" "'s' for seconds, 'm' for minutes, or 'h' for hours (e.g. 500ms).\n" " -4 Use IPv4 only\n" " -6 Use IPv6 only\n" #if HAVE_SYS_UN_H " -U, --unixsock Use Unix domain sockets only\n" #endif " -C, --crlf Use CRLF for EOL sequence\n" " -c, --sh-exec Executes the given command via /bin/sh\n" " -e, --exec Executes the given command\n" #ifdef HAVE_LUA " --lua-exec Executes the given Lua script\n" #endif " -g hop1[,hop2,...] Loose source routing hop points (8 max)\n" " -G Loose source routing hop pointer (4, 8, 12, ...)\n" " -m, --max-conns Maximum simultaneous connections\n" " -h, --help Display this help screen\n" " -d, --delay