traceroute.cc

来自「Ubuntu packages of security software。 相」· CC 代码 · 共 1,501 行 · 第 1/4 页

CC
1,501
字号
/*************************************************************************** * traceroute.cc -- Parallel multi-protocol traceroute feature             * *                                                                         * ***********************IMPORTANT NMAP LICENSE TERMS************************ *                                                                         * * The Nmap Security Scanner is (C) 1996-2008 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.                                                              * *                                                                         * ***************************************************************************//* * Written by Eddie Bell <ejlbell@gmail.com> as part of SoC2006 * A multi-protocol parallel traceroute implementation for nmap. * * For more information on how traceroutes work: * http://en.wikipedia.org/wiki/Traceroute * * Traceroute takes in a list of scanned targets and determines a valid * responsive port to trace to based on the scan results, scan protocol and  * various pieces of protocol data. * * Nmap first sends a probe to the target port, from the reply traceroute is able * to infer how many hops away the target is. Nmap starts the trace by sending  * a packet with a TTL equal to that of the hop distance guess. If it gets an  * ICMP_TTL_EXCEEDED message back it know the hop distance guess was under so * nmap will continue sending probes with incremental TTLs until it receives a * reply from the target host. * * Once a reply from the host is received nmap sets the TTL to one below the  * hop guess and continues to send probes with decremental TTLs until it reaches * TTL 0. Then we have a complete trace to the target. If nmap does not get a  * hop distance probe reply, the trace TTL starts at one and is incremented  * until it hits the target host. * * Forwards/Backwards tracing example *  hop guess:20 *  send:20  --> ICMP_TTL_EXCEEDED *  send:21  --> ICMP_TTL_EXCEEDED *  send:22  --> Reply from host *  send:19  --> ICMP_TTL_EXCEEDED *  .... *  send:1   --> ICMP_TTL_EXCEEDED * * The forward/backwards tracing method seems a little convoluted at first but  * there is a reason for it. The first host traced in a Target group is * designated as the reference trace. All other traces  * (once they have reached their destination host)  are compared against the * reference trace. If a match is found the trace is ended prematurely and the  * remaining hops are assumed to be the same as the reference trace. This  * normally only happens in the lower TTls, which rarely change. On average nmap * sends 5 less packets per host. If nmap is tracing related hosts  * (EG. 1.2.3.0/24) it will send a lot less packets. Depending on the network  * topology it may only have to send a single packet to each host. *    * Nmap's traceroute employs a dynamic timing model similar to nmap's scanning engine * but a little more light weight. It keeps track of sent, received and dropped * packet, then adjusts timing parameters accordingly. The parameters are; number of * retransmissions, delay between each sent packet and the amount of time to wait  * for a reply. They are initially based on the timing level (-T0 to -T5).  * Traceroute also has to watch out for rate-limiting of ICMP TTL EXCEEDED * messages, sometimes there is nothing we can do and just have to settle with a * timedout hop. * * The output from each trace is consolidated to save space, XML logging and debug * mode ignore consolidation. There are two type of consolidation time-out and  * reference trace. * * Timed out *  23  ... 24 no response * * Reference trace *   Hops 1-10 are the same as for X.X.X.X * * Traceroute does not work with connect scans or idle scans and has trouble * with ICMP_TIMESTAMP and ICMP_ADDRESSMASK scans because so many host filter  * them out. The quickest seems to be SYN scan. * * Bugs * ---- *  o The code, currently, only works with ipv4. *  o Should send both UDP and TCP hop distance probes no matter what the *    scan protocol */#include "traceroute.h"#include "NmapOps.h"#include "NmapOutputTable.h"#include "nmap_tty.h"#include "nmap_dns.h"#include "osscan2.h"#include "protocols.h"#include "timing.h"#include "utils.h"#include <algorithm>#include <stdlib.h>using namespace std;extern NmapOps o;static void enforce_scan_delay (struct timeval *, int);static char *hostStr (u32 ip);/* Each target group has a single reference trace. All * other traces are compared to it and if a match is * found the trace is ended prematurely and the  * remaining hops are assumed to match the reference * trace */unsigned long commonPath[MAX_TTL + 1];Traceroute::Traceroute (const char *device_name, devtype type) {    fd = -1;    ethsd = NULL;    hops = NULL;    pd = NULL;    total_size = 0;    memset(&ref_ipaddr, '\0', sizeof(struct in_addr));    cp_flag = 0;    if(type == devt_loopback)         return;    /* open various socks to send and read from on windows and      * unix */    if ((o.sendpref & PACKET_SEND_ETH) && type == devt_ethernet) {        /* We'll send ethernet packets with dnet */        ethsd = eth_open_cached (device_name);        if (ethsd == NULL)            fatal ("dnet: Failed to open device %s", device_name);    } else {        if ((fd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)            pfatal ("Traceroute: socket troubles");        broadcast_socket (fd);#ifndef WIN32        sethdrinclude (fd);#endif    }    /* rely on each group using the same device */    pd = my_pcap_open_live (device_name, 100, o.spoofsource ? 1 : 0, 2);    scaninfo.initial_proto = IPPROTO_IP;    scaninfo.open_response = 0;    scaninfo.open_state = PORT_OPEN;    scaninfo.closed_state = PORT_CLOSED;    /* Set up which protocols, tcp flags and responsive     * states to use with the current scan type.     *     * Horribly messy but it is better then peppering     * the nmap source code with references to traceroute */    if (o.synscan) {        scaninfo.scan_flags = TH_SYN;        scaninfo.open_response = TH_SYN | TH_ACK;        scaninfo.closed_response = TH_RST;    } else if (o.ackscan) {        scaninfo.scan_flags = TH_ACK;        scaninfo.open_response = TH_RST;        scaninfo.closed_response = TH_RST;        scaninfo.open_state = PORT_UNFILTERED;        scaninfo.closed_state = PORT_UNFILTERED;    } else if (o.finscan) {        scaninfo.scan_flags = TH_FIN;        scaninfo.closed_response = TH_RST;    } else if (o.xmasscan) {        scaninfo.scan_flags = TH_FIN | TH_URG | TH_PUSH;        scaninfo.closed_response = TH_RST;    } else if (o.nullscan) {        scaninfo.scan_flags = 0;        scaninfo.closed_response = TH_RST;    } else if (o.windowscan) {        scaninfo.scan_flags = TH_ACK;        scaninfo.open_response = TH_RST;        scaninfo.closed_response = TH_RST;    } else if (o.maimonscan) {        scaninfo.scan_flags = TH_FIN | TH_ACK;        scaninfo.open_response = TH_RST;        scaninfo.closed_response = TH_RST;    }    if (o.udpscan)        scaninfo.initial_proto = IPPROTO_UDP;    if (o.synscan || o.finscan || o.xmasscan || o.nullscan ||        o.ackscan || o.windowscan || o.maimonscan)        scaninfo.initial_proto = IPPROTO_TCP;    if(o.pingscan) {        scaninfo.open_state = HOST_UP;        if (o.pingtype & PINGTYPE_TCP_USE_SYN) {            scaninfo.scan_flags = TH_SYN;            scaninfo.open_response = TH_SYN | TH_ACK;            scaninfo.closed_response = TH_RST;            scaninfo.initial_proto = IPPROTO_TCP;        } else if (o.pingtype & PINGTYPE_TCP_USE_ACK) {            scaninfo.scan_flags = TH_ACK;            scaninfo.open_response = TH_RST;            scaninfo.closed_response = TH_RST;            scaninfo.initial_proto = IPPROTO_TCP;        } else if (o.pingtype & PINGTYPE_UDP) {            scaninfo.initial_proto = IPPROTO_UDP;        } else if (o.pingtype & PINGTYPE_ICMP_PING) {            scaninfo.initial_proto = IPPROTO_ICMP;            scaninfo.icmp_type = ICMP_ECHO;        } else if (o.pingtype & PINGTYPE_ICMP_TS) {            scaninfo.initial_proto = IPPROTO_ICMP;            scaninfo.icmp_type = ICMP_TIMESTAMP;        } else if(o.pingtype & PINGTYPE_ICMP_MASK) {            scaninfo.initial_proto = IPPROTO_ICMP;            scaninfo.icmp_type = ICMP_ADDRESS;        }    }    if (o.scanflags != -1)        scaninfo.scan_flags = o.scanflags;    memset (commonPath, 0, sizeof (commonPath));}Traceroute::~Traceroute () {    map < u32, TraceGroup * >::iterator it = TraceGroups.begin ();    while ((--total_size) >= 0)         delete(hops[total_size]);    if(hops)        free(hops);    for (; it != TraceGroups.end (); ++it)        delete (it->second);    if (ethsd)        ethsd = NULL;    if (fd != -1)        close (fd);    if(pd)        pcap_close (pd);}/* get an open or closed port from the portlist. Traceroute requires a positive response,  * positive responses are generated by different port states depending on the type of scan */inline intTraceroute::getTracePort (u8 proto, Target * t) {    u16 open_port = 1;    u16 closed_port = 1;    u16 filtered_port = 1;    u16 state = 0;    u16 port = 0;    struct Port *np;    /* Use the first specified port for ping traceroutes */    if (o.pingscan) {        if (o.pingtype & PINGTYPE_TCP_USE_SYN)            return o.ping_synprobes[0];        else if (o.pingtype & PINGTYPE_TCP_USE_ACK)            return o.ping_ackprobes[0];        else if (o.pingtype & PINGTYPE_UDP)            return o.ping_udpprobes[0];        else            return 0;    }    if (proto == IPPROTO_TCP) {        /* can't use filtered ports for tcp */        filtered_port = 0;        open_port = (!scaninfo.open_response) ? 0 : 1;    }    /* First we try to find an open port, if not we try to find a closed     * port and lastly we try to find a filtered port */    if (open_port && t->ports.getStateCounts (proto, scaninfo.open_state))        state = scaninfo.open_state;    else if (closed_port && t->ports.getStateCounts (proto, scaninfo.closed_state))        state = scaninfo.closed_state;    else if (filtered_port && t->ports.getStateCounts (proto, PORT_FILTERED)) {        state = PORT_FILTERED;        if (o.verbose)            log_write (LOG_PLAIN, "%s: only filtered %s available, results may be incorrect\n",                       t->targetipstr (), o.ipprotscan ? "protocols" : "ports");    } else {        return -1;    }    np = t->ports.nextPort (NULL, proto, state);    if (!np)      return -1;    port = np->portno;    /* If this is a protocol scan traceroute and we are using     * one of the major protocols, set up the required information     * so we include the correct protocol headers */    if (proto == IPPROTO_IP) {        if (port == IPPROTO_TCP) {            scaninfo.initial_proto = IPPROTO_TCP;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?