nmap_dns.cc
来自「Ubuntu packages of security software。 相」· CC 代码 · 共 1,261 行 · 第 1/3 页
CC
1,261 行
/*************************************************************************** * nmap_dns.cc -- Handles parallel reverse DNS resolution for target IPs * * * ***********************IMPORTANT NMAP LICENSE TERMS************************ * * * The Nmap Security Scanner is (C) 1996-2006 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-fingerprints 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 just meant to * * clarify our interpretation of derived works with some common examples. * * These restrictions only apply when you actually redistribute Nmap. For * * example, nothing stops you from writing and selling a proprietary * * front-end to Nmap. Just distribute it by itself, and point people to * * http://insecure.org/nmap/ to download Nmap. * * * * We don't consider these to be added restrictions on top of the GPL, but * * just a clarification of how we interpret "derived works" as it applies * * to our GPL-licensed Nmap product. This is similar to the way Linus * * Torvalds has announced his interpretation of how "derived works" * * applies to Linux kernel modules. Our interpretation refers only to * * Nmap - we don't speak for any other GPL products. * * * * 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 Copying.OpenSSL 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 fyodor@insecure.org for possible incorporation into the main * * distribution. By sending these changes to Fyodor or one the * * Insecure.Org development mailing lists, it is assumed that you are * * offering Fyodor and 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 for more details at * * http://www.gnu.org/copyleft/gpl.html , or in the COPYING file included * * with Nmap. * * * ***************************************************************************/// mass_rdns - Parallel Asynchronous Reverse DNS Resolution//// One of Nmap's features is to perform reverse DNS queries// on large number of IP addresses. Nmap supports 2 different// methods of accomplishing this://// System Resolver (specified using --system-dns):// Performs sequential getnameinfo() calls on all the IPs.// As reliable as your system resolver, almost guaranteed// to be portable, but intolerably slow for scans of hundreds// or more because the result from each query needs to be// received before the next one can be sent.//// Mass/Async DNS (default):// Attempts to resolve host names in parallel using a set// of DNS servers. DNS servers are found here://// --dns-servers <serv1[,serv2],...> (all platforms - overrides everything else)//// /etc/resolv.conf (only on unix)//// These registry keys: (only on windows)//// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NameServer// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DhcpNameServer// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\*\NameServer// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\*\DhcpNameServer////// Also, most systems maintain a file "/etc/hosts" that contains// IP to hostname mappings. We also try to consult these files. Here// is where we look for the files://// Unix: /etc/hosts//// Windows:// for 95/98/Me: WINDOWS_DIR\hosts// for NT/2000/XP Pro: WINDOWS_DIR\system32\drivers\etc\hosts// for XP Home: WINDOWS_DIR\system32\drivers\etc\hosts// --see http://accs-net.com/hosts/how_to_use_hosts.html////// Created by Doug Hoyte// doug at hcsw.org// http://www.hcsw.org// TODO://// * Tune performance parameters//// * Figure out best way to estimate completion time// and display it in a ScanProgressMeter #ifdef WIN32#include "nmap_winconfig.h"#endif#include <stdlib.h>#include <limits.h>#include <list>#include <vector>#include "nmap.h"#include "NmapOps.h"#include "nmap_dns.h"#include "nsock.h"#include "utils.h"#include "nmap_tty.h"extern NmapOps o;//------------------- Performance Parameters ---------------------// Algorithm://// A batch of num_targets hosts is passed to nmap_mass_rdns():// void nmap_mass_rdns(Target **targets, int num_targets)//// mass_dns sends out CAPACITY_MIN of these hosts to the DNS// servers detected, alternating in sequence.// When a request is fulfilled (either a resolved domain, NXDomain,// or confirmed ServFail) CAPACITY_UP_STEP is added to the current// capacity of the server the request was found by.// When a request times out and retries on the same server,// the server's capacity is scaled by CAPACITY_MINOR_DOWN_STEP.// When a request times out and moves to the next server in// sequence, the server's capacity is scaled by CAPACITY_MAJOR_DOWN_STEP.// mass_dns tries to maintain the current number of "outstanding// queries" on each server to that of its current capacity. The// packet is dropped if it cycles through all specified DNS// servers.// Since multiple DNS servers can be specified, different sequences// of timers are maintained. These are the various retransmission// intervals for each server before we move on to the next DNS server:// In milliseconds// Each row MUST be terminated with -1static int read_timeouts[][4] = { { 4000, 4000, 5000, -1 }, // 1 server { 2500, 4000, -1, -1 }, // 2 servers { 2500, 3000, -1, -1 }, // 3+ servers};#define CAPACITY_MIN 10#define CAPACITY_MAX 200#define CAPACITY_UP_STEP 2#define CAPACITY_MINOR_DOWN_SCALE 0.9#define CAPACITY_MAJOR_DOWN_SCALE 0.7// Each request will try to resolve on at most this many servers:#define SERVERS_TO_TRY 3//------------------- Other Parameters ---------------------// How often to display a short debugging summary if debugging is// specified. Lower numbers means it's displayed more often.#define SUMMARY_DELAY 50// Minimum debugging level to display packet trace#define TRACE_DEBUG_LEVEL 4// The amount of time we wait for nsock_write() to complete before// retransmission. This should almost never happen. (in milliseconds)#define WRITE_TIMEOUT 100// Size of hash table used to hold the hosts from /etc/hosts#define HASH_TABLE_SIZE 256//------------------- Internal Structures ---------------------typedef struct dns_server_s dns_server;typedef struct request_s request;typedef struct host_elem_s host_elem;struct dns_server_s { char *hostname; sockaddr_in addr; nsock_iod nsd; int connected; int reqs_on_wire; int capacity; int write_busy; std::list<request *> to_process; std::list<request *> in_process;};struct request_s { Target *targ; struct timeval timeout; int tries; int servers_tried; dns_server *first_server; dns_server *curr_server; u16 id;};struct host_elem_s { char *name; u32 addr;};//------------------- Globals ---------------------static std::list<dns_server *> servs;static std::list<request *> new_reqs;static std::list<request *> cname_reqs;static int total_reqs;static nsock_pool dnspool=NULL;static std::list<host_elem *> etchosts[HASH_TABLE_SIZE];static int stat_actual, stat_ok, stat_nx, stat_sf, stat_trans, stat_dropped, stat_cname;static struct timeval starttv;static int read_timeout_index;static u16 id_counter;static int firstrun=1;static ScanProgressMeter *SPM;//------------------- Prototypes and macros ---------------------static void put_dns_packet_on_wire(request *req);#define ACTION_FINISHED 0#define ACTION_CNAME_LIST 1#define ACTION_TIMEOUT 2//------------------- Misc code --------------------- static void output_summary() { int tp = stat_ok + stat_nx + stat_dropped; struct timeval now; memcpy(&now, nsock_gettimeofday(), sizeof(struct timeval)); if (o.debugging && (tp%SUMMARY_DELAY == 0)) log_write(LOG_STDOUT, "mass_rdns: %.2fs %d/%d [#: %lu, OK: %d, NX: %d, DR: %d, SF: %d, TR: %d]\n", TIMEVAL_MSEC_SUBTRACT(now, starttv) / 1000.0, tp, stat_actual, (unsigned long) servs.size(), stat_ok, stat_nx, stat_dropped, stat_sf, stat_trans);}static void check_capacities(dns_server *tpserv) { if (tpserv->capacity < CAPACITY_MIN) tpserv->capacity = CAPACITY_MIN; if (tpserv->capacity > CAPACITY_MAX) tpserv->capacity = CAPACITY_MAX; if (o.debugging >= TRACE_DEBUG_LEVEL) log_write(LOG_STDOUT, "CAPACITY <%s> = %d\n", tpserv->hostname, tpserv->capacity);}// Closes all nsis created in connect_dns_servers()static void close_dns_servers() { std::list<dns_server *>::iterator serverI; for(serverI = servs.begin(); serverI != servs.end(); serverI++) { if ((*serverI)->connected) { nsi_delete((*serverI)->nsd, NSOCK_PENDING_SILENT); (*serverI)->connected = 0; (*serverI)->to_process.clear(); (*serverI)->in_process.clear(); } }}// Inserts an integer (endian non-specifically) into a DNS packet.// Returns number of bytes writtenstatic int add_integer_to_dns_packet(char *packet, int c) { char tpnum[4]; int tplen; sprintf(tpnum, "%d", c); tplen = strlen(tpnum); packet[0] = (char) tplen; memcpy(packet+1, tpnum, tplen); return tplen+1;}// Puts as many packets on the line as capacity will allowstatic void do_possible_writes() { std::list<dns_server *>::iterator servI; dns_server *tpserv; request *tpreq; for(servI = servs.begin(); servI != servs.end(); servI++) { tpserv = *servI; if (tpserv->write_busy == 0 && tpserv->reqs_on_wire < tpserv->capacity) { tpreq = NULL; if (!tpserv->to_process.empty()) { tpreq = tpserv->to_process.front(); tpserv->to_process.pop_front(); } else if (!new_reqs.empty()) { tpreq = new_reqs.front(); tpreq->first_server = tpreq->curr_server = tpserv; new_reqs.pop_front(); } if (tpreq) { if (o.debugging >= TRACE_DEBUG_LEVEL) log_write(LOG_STDOUT, "mass_rdns: TRANSMITTING for <%s> (server <%s>)\n", tpreq->targ->targetipstr() , tpserv->hostname); stat_trans++; put_dns_packet_on_wire(tpreq); } } }}// nsock write handlerstatic void write_evt_handler(nsock_pool nsp, nsock_event evt, void *req_v) { request *req = (request *) req_v; req->curr_server->write_busy = 0; req->curr_server->in_process.push_front(req); do_possible_writes();}// Takes a DNS request structure and actually puts it on the wire// (calls nsock_write()). Does various other tasks like recording// the time for the timeout.static void put_dns_packet_on_wire(request *req) { char packet[512]; int plen=0; u32 ip; struct timeval now, timeout; ip = (u32) ntohl(req->targ->v4host().s_addr); packet[0] = (req->id >> 8) & 0xFF; packet[1] = req->id & 0xFF; plen += 2; memcpy(packet+plen, "\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00", 10); plen += 10; plen += add_integer_to_dns_packet(packet+plen, ip & 0xFF); plen += add_integer_to_dns_packet(packet+plen, (ip>>8) & 0xFF); plen += add_integer_to_dns_packet(packet+plen, (ip>>16) & 0xFF); plen += add_integer_to_dns_packet(packet+plen, (ip>>24) & 0xFF); memcpy(packet+plen, "\x07in-addr\004arpa\x00\x00\x0c\x00\x01", 18); plen += 18; req->curr_server->write_busy = 1; req->curr_server->reqs_on_wire++; memcpy(&now, nsock_gettimeofday(), sizeof(struct timeval)); TIMEVAL_MSEC_ADD(timeout, now, read_timeouts[read_timeout_index][req->tries]);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?