nmap.cc
来自「Ubuntu packages of security software。 相」· CC 代码 · 共 1,556 行 · 第 1/5 页
CC
1,556 行
* --None have been specified AND * --We are root and doing tcp ping OR * --We are doing a raw sock scan and NOT pinging anyone */ if (o.af() == AF_INET && o.v4sourceip() && !*o.device) { if (ipaddr2devname(o.device, o.v4sourceip()) != 0) { fatal("Could not figure out what device to send the packet out on with the source address you gave me! If you are trying to sp00f your scan, this is normal, just give the -e eth0 or -e ppp0 or whatever. Otherwise you can still use -e, but I find it kindof fishy."); } } if (o.af() == AF_INET && *o.device && !o.v4sourceip()) { struct sockaddr_in tmpsock; memset(&tmpsock, 0, sizeof(tmpsock)); if (devname2ipaddr(o.device, &(tmpsock.sin_addr)) == -1) { fatal("I cannot figure out what source address to use for device %s, does it even exist?", o.device); } tmpsock.sin_family = AF_INET;#if HAVE_SOCKADDR_SA_LEN tmpsock.sin_len = sizeof(tmpsock);#endif o.setSourceSockAddr((struct sockaddr_storage *) &tmpsock, sizeof(tmpsock)); } /* If he wants to bounce off of an ftp site, that site better damn well be reachable! */ if (o.bouncescan) { if (!inet_pton(AF_INET, ftp.server_name, &ftp.server)) { if ((target = gethostbyname(ftp.server_name))) memcpy(&ftp.server, target->h_addr_list[0], 4); else { fprintf(stderr, "Failed to resolve ftp bounce proxy hostname/IP: %s\n", ftp.server_name); exit(1); } } else if (o.verbose) log_write(LOG_STDOUT, "Resolved ftp bounce attack proxy to %s (%s).\n", ftp.server_name, inet_ntoa(ftp.server)); } fflush(stdout); fflush(stderr); timep = time(NULL); /* Brief info incase they forget what was scanned */ Strncpy(mytime, ctime(&timep), sizeof(mytime)); chomp(mytime); char *xslfname = o.XSLStyleSheet(); char xslline[1024]; if (xslfname) { char *p = xml_convert(xslfname); snprintf(xslline, sizeof(xslline), "<?xml-stylesheet href=\"%s\" type=\"text/xsl\"?>\n", p); free(p); } else xslline[0] = '\0'; log_write(LOG_XML, "<?xml version=\"1.0\" ?>\n%s<!-- ", xslline); log_write(LOG_NORMAL|LOG_MACHINE, "# "); log_write(LOG_NORMAL|LOG_MACHINE|LOG_XML, "%s %s scan initiated %s as: ", NMAP_NAME, NMAP_VERSION, mytime); for(i=0; i < argc; i++) { char *p = xml_convert(fakeargv[i]); log_write(LOG_XML,"%s ", p); free(p); log_write(LOG_NORMAL|LOG_MACHINE,"%s ", fakeargv[i]); } log_write(LOG_XML, "-->"); log_write(LOG_NORMAL|LOG_MACHINE|LOG_XML,"\n"); log_write(LOG_XML, "<nmaprun scanner=\"nmap\" args=\""); for(i=0; i < argc; i++) log_write(LOG_XML, (i == argc-1)? "%s\" " : "%s ", fakeargv[i]); log_write(LOG_XML, "start=\"%lu\" startstr=\"%s\" version=\"%s\" xmloutputversion=\"1.01\">\n", (unsigned long) timep, mytime, NMAP_VERSION); output_xml_scaninfo_records(ports); log_write(LOG_XML, "<verbose level=\"%d\" />\n<debugging level=\"%d\" />\n", o.verbose, o.debugging); /* Before we randomize the ports scanned, lets output them to machine parseable output */ if (o.verbose) output_ports_to_machine_parseable_output(ports, o.TCPScan(), o.udpscan, o.ipprotscan); /* more fakeargv junk, BTW malloc'ing extra space in argv[0] doesn't work */ if (quashargv) { size_t fakeargvlen = strlen(FAKE_ARGV), argvlen = strlen(argv[0]); if (argvlen < fakeargvlen) fatal("If you want me to fake your argv, you need to call the program with a longer name. Try the full pathname, or rename it fyodorssuperdedouperportscanner"); strncpy(argv[0], FAKE_ARGV, fakeargvlen); memset(&argv[0][fakeargvlen], '\0', strlen(&argv[0][fakeargvlen])); for(i=1; i < argc; i++) memset(argv[i], '\0', strlen(argv[i])); }#if defined(HAVE_SIGNAL) && defined(SIGPIPE) signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE so our program doesn't crash because of it, but we really shouldn't get an unsuspected SIGPIPE */#endif if (o.max_parallelism && (i = max_sd()) && i < o.max_parallelism) { fprintf(stderr, "WARNING: Your specified max_parallel_sockets of %d, but your system says it might only give us %d. Trying anyway\n", o.max_parallelism, i); } if (o.debugging > 1) log_write(LOG_STDOUT, "The max # of sockets we are using is: %d\n", o.max_parallelism); // At this point we should fully know our timing parameters if (o.debugging) { printf("--------------- Timing report ---------------\n"); printf(" hostgroups: min %d, max %d\n", o.minHostGroupSz(), o.maxHostGroupSz()); printf(" rtt-timeouts: init %d, min %d, max %d\n", o.initialRttTimeout(), o.minRttTimeout(), o.maxRttTimeout()); printf(" msx-scan-delay: TCP %d, UDP %d\n", o.maxTCPScanDelay(), o.maxUDPScanDelay()); printf(" parallelism: min %d, max %d\n", o.min_parallelism, o.max_parallelism); printf(" max-retries: %d, host-timeout: %ld\n", o.getMaxRetransmissions(), o.host_timeout); printf("---------------------------------------------\n"); } /* Before we randomize the ports scanned, we must initialize PortList class. */ if (o.ipprotscan) PortList::initializePortMap(IPPROTO_IP, ports->prots, ports->prot_count); if (o.TCPScan()) PortList::initializePortMap(IPPROTO_TCP, ports->tcp_ports, ports->tcp_count); if (o.UDPScan()) PortList::initializePortMap(IPPROTO_UDP, ports->udp_ports, ports->udp_count); if (randomize) { if (ports->tcp_count) { shortfry(ports->tcp_ports, ports->tcp_count); // move a few more common ports closer to the beginning to speed scan random_port_cheat(ports->tcp_ports, ports->tcp_count); } if (ports->udp_count) shortfry(ports->udp_ports, ports->udp_count); if (ports->prot_count) shortfry(ports->prots, ports->prot_count); } /* Time to create a hostgroup state object filled with all the requested machines */ host_exp_group = (char **) safe_malloc(o.ping_group_sz * sizeof(char *)); /* lets load our exclude list */ if ((NULL != excludefd) || (NULL != exclude_spec)) { exclude_group = load_exclude(excludefd, exclude_spec); if (o.debugging > 3) dumpExclude(exclude_group); if ((FILE *)NULL != excludefd) fclose(excludefd); if ((char *)NULL != exclude_spec) free(exclude_spec); } while(num_host_exp_groups < o.ping_group_sz && (host_spec = grab_next_host_spec(inputfd, argc, fakeargv))) { host_exp_group[num_host_exp_groups++] = strdup(host_spec); // For purposes of random scan if (o.max_ips_to_scan && o.max_ips_to_scan <= o.numhosts_scanned + num_host_exp_groups) break; } if (num_host_exp_groups == 0) fatal("No target machines/networks specified!"); hstate = new HostGroupState(o.ping_group_sz, o.randomize_hosts, host_exp_group, num_host_exp_groups); do { ideal_scan_group_sz = determineScanGroupSize(o.numhosts_scanned, ports); while(Targets.size() < ideal_scan_group_sz) { o.current_scantype = HOST_DISCOVERY; currenths = nexthost(hstate, exclude_group, ports, &(o.pingtype)); if (!currenths) { /* Try to refill with any remaining expressions */ /* First free the old ones */ for(i=0; i < num_host_exp_groups; i++) free(host_exp_group[i]); num_host_exp_groups = 0; /* Now grab any new expressions */ while(num_host_exp_groups < o.ping_group_sz && (!o.max_ips_to_scan || o.max_ips_to_scan > o.numhosts_scanned + num_host_exp_groups) && (host_spec = grab_next_host_spec(inputfd, argc, fakeargv))) { // For purposes of random scan host_exp_group[num_host_exp_groups++] = strdup(host_spec); } if (num_host_exp_groups == 0) break; delete hstate; hstate = new HostGroupState(o.ping_group_sz, o.randomize_hosts, host_exp_group, num_host_exp_groups); /* Try one last time -- with new expressions */ currenths = nexthost(hstate, exclude_group, ports, &(o.pingtype)); if (!currenths) break; } o.numhosts_scanned++; if (currenths->flags & HOST_UP && !o.listscan) o.numhosts_up++; if (o.pingscan || o.listscan) { /* We're done with the hosts */ log_write(LOG_XML, "<host>"); write_host_status(currenths, o.resolve_all); printmacinfo(currenths); // if (currenths->flags & HOST_UP) // log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT,"\n"); log_write(LOG_XML, "</host>\n"); log_flush_all(); delete currenths; continue; } if (o.spoofsource) { o.SourceSockAddr(&ss, &sslen); currenths->setSourceSockAddr(&ss, sslen); } /* I used to check that !currenths->wierd_responses, but in some rare cases, such IPs CAN be port successfully scanned and even connected to */ if (!(currenths->flags & HOST_UP)) { delete currenths; continue; } if (o.af() == AF_INET && o.RawScan()) { if (currenths->SourceSockAddr(NULL, NULL) != 0) { if (o.SourceSockAddr(&ss, &sslen) == 0) { currenths->setSourceSockAddr(&ss, sslen); } else { if (gethostname(myname, MAXHOSTNAMELEN) || resolve(myname, &ss, &sslen, o.af()) == 0) fatal("Cannot get hostname! Try using -S <my_IP_address> or -e <interface to scan through>\n"); o.setSourceSockAddr(&ss, sslen); currenths->setSourceSockAddr(&ss, sslen); if (! sourceaddrwarning) { fprintf(stderr, "WARNING: We could not determine for sure which interface to use, so we are guessing %s . If this is wrong, use -S <my_IP_address>.\n", inet_socktop(&ss)); sourceaddrwarning = 1; } } } if (!currenths->deviceName()) fatal("Do not have appropriate device name for target"); /* Groups should generally use the same device as properties change quite a bit between devices. Plus dealing with a multi-device group can be a pain programmatically. So if this Target has a different device the rest, we give it back. */ if (Targets.size() > 0 && strcmp(Targets[Targets.size() - 1]->deviceName(), currenths->deviceName())) { returnhost(hstate); o.numhosts_scanned--; o.numhosts_up--; break; } o.decoys[o.decoyturn] = currenths->v4source(); } Targets.push_back(currenths); } if (Targets.size() == 0) break; /* Couldn't find any more targets */ // Set the variable for status printing o.numhosts_scanning = Targets.size(); // Our source must be set in decoy list because nexthost() call can // change it (that issue really should be fixed when possible) if (o.af() == AF_INET && o.RawScan()) o.decoys[o.decoyturn] = Targets[0]->v4source(); /* I now have the group for scanning in the Targets vector */ // Ultra_scan sets o.scantype for us so we don't have to worry if (o.synscan) ultra_scan(Targets, ports, SYN_SCAN); if (o.ackscan) ultra_scan(Targets, ports, ACK_SCAN); if (o.windowscan) ultra_scan(Targets, ports, WINDOW_SCAN); if (o.finscan) ultra_scan(Targets, ports, FIN_SCAN); if (o.xmasscan) ultra_scan(Targets, ports, XMAS_SCAN); if (o.nullscan) ultra_scan(Targets, ports, NULL_SCAN); if (o.maimonscan) ultra_scan(Targets, ports, MAIMON_SCAN); if (o.udpscan) ultra_scan(Targets, ports, UDP_SCAN); if (o.connectscan) ultra_scan(Targets, ports, CONNECT_SCAN); if (o.ipprotscan) ultra_scan(Targets, ports, IPPROT_SCAN); /* These lame functions can only handle one target at a time */ for(targetno = 0; targetno < Targets.size(); targetno++) { currenths = Targets[tar
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?