/*************************************************************************** * ncrack.cc -- ncrack's core engine along with all nsock callback * * handlers reside in here. Simple options' (not host or service-options * * specification handling) parsing also happens in main() here. * * * ***********************IMPORTANT NMAP LICENSE TERMS************************ * * * The Nmap Security Scanner is (C) 1996-2011 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 with the clarifications and exceptions described * * below. 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@insecure.com). Dozens of software vendors already * * license Nmap technology such as host discovery, port scanning, OS * * detection, and version detection. * * * * Note that the GPL places important restrictions on "derived works", yet * * it does not provide a detailed definition of that term. To avoid * * misunderstandings, we consider an application to constitute a * * "derivative work" for the purpose of this license if it does any of the * * following: * * o Integrates source code from Nmap * * o Reads or includes Nmap copyrighted data files, such as * * nmap-os-db or nmap-service-probes. * * o Executes Nmap and parses the results (as opposed to typical shell or * * execution-menu apps, which simply display raw Nmap output and so are * * not derivative works.) * * o Integrates/includes/aggregates Nmap into a proprietary executable * * installer, such as those produced by InstallShield. * * o Links to a library or executes a program that does any of the above * * * * The term "Nmap" should be taken to also include any portions or derived * * works of Nmap. This list is not exclusive, but is meant to clarify our * * interpretation of derived works with some common examples. Our * * interpretation applies only to Nmap--we don't speak for other people's * * GPL works. * * * * If you have any questions about the GPL licensing restrictions on using * * Nmap in non-GPL works, we would be 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 as well as helping to * * fund the continued development of Nmap technology. Please email * * sales@insecure.com for further information. * * * * As a 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. You must obey the GNU GPL in all * * respects for all of the code used other than OpenSSL. If you modify * * this file, you may extend this exception to your version of the file, * * but you are not obligated to do so. * * * * If you received these files with a written license agreement or * * contract stating terms other than the terms above, then that * * alternative license agreement takes precedence over these comments. * * * * 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 nmap-dev@insecure.org for possible incorporation into the main * * distribution. By sending these changes to Fyodor or one of the * * Insecure.Org development mailing lists, it is assumed 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 GNU * * General Public License v2.0 for more details at * * http://www.gnu.org/licenses/gpl-2.0.html , or in the COPYING file * * included with Nmap. * * * ***************************************************************************/ #include "ncrack.h" #include "NcrackOps.h" #include "utils.h" #include "services.h" #include "targets.h" #include "TargetGroup.h" #include "ServiceGroup.h" #include "nsock.h" #include "global_structures.h" #include "NcrackOutputTable.h" #include "modules.h" #include "ncrack_error.h" #include "output.h" #include "ncrack_tty.h" #include "ncrack_input.h" #include "ncrack_resume.h" #include #include #if HAVE_SIGNAL #include #endif #if HAVE_OPENSSL #include #endif #ifdef WIN32 #include "winfix.h" #endif #define DEFAULT_CONNECT_TIMEOUT 5000 /* includes connect() + ssl negotiation */ #define DEFAULT_CONNECT_SSL_TIMEOUT 8000 #define DEFAULT_USERNAME_FILE "default.usr" #define DEFAULT_PASSWORD_FILE "default.pwd" /* (in milliseconds) every such interval we poll for interactive user input */ #define KEYPRESSED_INTERVAL 500 /* (in milliseconds) every such interval check for pending signals */ #define SIGNAL_CHECK_INTERVAL 1000 #define SERVICE_TIMEDOUT "Service timed-out as specified by user option." extern NcrackOps o; using namespace std; /* global lookup table for available services */ vector ServicesTable; /* global login and pass array */ vector UserArray; vector PassArray; /* schedule additional connections */ static void ncrack_probes(nsock_pool nsp, ServiceGroup *SG); /* ncrack initialization */ static int ncrack(ServiceGroup *SG); /* Poll for interactive user input every time this timer is called. */ static void status_timer_handler(nsock_pool nsp, nsock_event nse, void *mydata); static void signal_timer_handler(nsock_pool nsp, nsock_event nse, void *mydata); /* module name demultiplexor */ static void call_module(nsock_pool nsp, Connection* con); static void parse_login_list(char *const arg, int mode); static void load_login_file(const char *filename, int mode); enum mode { USER, PASS }; static void print_usage(void); static void lookup_init(const char *const filename); static int file_readable(const char *pathname); static int ncrack_fetchfile(char *filename_returned, int bufferlen, const char *file, int useroption = 0); static char *grab_next_host_spec(FILE *inputfd, int argc, char **argv); static void startTimeOutClocks(ServiceGroup *SG); static void sigcatch(int signo); static void sigcheck(ServiceGroup *SG); static int ncrack_main(int argc, char **argv); static void print_usage(void) { log_write(LOG_STDOUT, "%s %s ( %s )\n" "Usage: ncrack [Options] {target and service specification}\n" "TARGET SPECIFICATION:\n" " Can pass hostnames, IP addresses, networks, etc.\n" " Ex: scanme.nmap.org, microsoft.com/24, 192.168.0.1; " "10.0.0-255.1-254\n" " -iX : Input from Nmap's -oX XML output format\n" " -iN : Input from Nmap's -oN Normal output format\n" " -iL : Input from list of hosts/networks\n" " --exclude : Exclude hosts/networks\n" " --excludefile : Exclude list from file\n" "SERVICE SPECIFICATION:\n" " Can pass target specific services in ://target (standard) " "notation or\n" " using -p which will be applied to all hosts in non-standard " "notation.\n" " Service arguments can be specified to be host-specific, type of " "service-specific\n" " (-m) or global (-g). Ex: ssh://10.0.0.10,at=10,cl=30 -m ssh:at=50 " "-g cd=3000\n" " Ex2: ncrack -p ssh,ftp:3500,25 10.0.0.10 scanme.nmap.org " "google.com:80,ssl\n" " -p : services will be applied to all non-standard " "notation hosts\n" " -m :: options will be applied to all services " "of this type\n" " -g : options will be applied to every service globally\n" " Misc options:\n" " ssl: enable SSL over this service\n" " path : used in modules like HTTP ('=' needs escaping if " "used)\n" "TIMING AND PERFORMANCE:\n" " Options which take