Commit 19a91b29 authored by robertdavidgraham's avatar robertdavidgraham
Browse files

multi-adapter

parent b3553322
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ endif
# intended environment, so it make break in the future.
ifneq (, $(findstring mingw, $(SYS)))
INCLUDES = -I. -Ivs10/include
LIBS = -L vs10/lib -lwpcap -lIPHLPAPI
LIBS = -L vs10/lib -lwpcap -lIPHLPAPI -lWs2_32
FLAGS2 = -march=i686
endif

+80 −30
Original line number Diff line number Diff line
@@ -86,6 +86,46 @@ print_nmap_help(void)
}


/***************************************************************************
 * Echoes the configuration for one nic
 ***************************************************************************/
static void
masscan_echo_nic(struct Masscan *masscan, FILE *fp, unsigned i)
{
    char zzz[64];

    /* If we have only one adapter, then don't print the array indexes.
     * Otherwise, we need to print the array indexes to distinguish
     * the NICs from each other */
    if (masscan->nic_count <= 1)
        zzz[0] = '\0';
    else
        sprintf_s(zzz, sizeof(zzz), "[%u]", i);

    fprintf(fp, "adapter%s = %s\n", zzz, masscan->nic[i].ifname);
    fprintf(fp, "adapter-ip%s = %u.%u.%u.%u\n", zzz,
        (masscan->nic[i].adapter_ip>>24)&0xFF,
        (masscan->nic[i].adapter_ip>>16)&0xFF,
        (masscan->nic[i].adapter_ip>> 8)&0xFF,
        (masscan->nic[i].adapter_ip>> 0)&0xFF
        );
    fprintf(fp, "adapter-mac = %02x:%02x:%02x:%02x:%02x:%02x\n", zzz,
            masscan->nic[i].adapter_mac[0],
            masscan->nic[i].adapter_mac[1],
            masscan->nic[i].adapter_mac[2],
            masscan->nic[i].adapter_mac[3],
            masscan->nic[i].adapter_mac[4],
            masscan->nic[i].adapter_mac[5]);
    fprintf(fp, "router-mac = %02x:%02x:%02x:%02x:%02x:%02x\n", zzz,
            masscan->nic[i].router_mac[0],
            masscan->nic[i].router_mac[1],
            masscan->nic[i].router_mac[2],
            masscan->nic[i].router_mac[3],
            masscan->nic[i].router_mac[4],
            masscan->nic[i].router_mac[5]);

}

/***************************************************************************
 * Prints the current configuration to the command-line then exits.
 * Use#1: create a template file of all setable parameters.
@@ -104,27 +144,13 @@ masscan_echo(struct Masscan *masscan, FILE *fp)
        fprintf(fp, "banners = true\n");

    fprintf(fp, "# ADAPTER SETTINGS\n");
    fprintf(fp, "adapter = %s\n", masscan->ifname);
    fprintf(fp, "adapter-ip = %u.%u.%u.%u\n",
        (masscan->adapter_ip>>24)&0xFF,
        (masscan->adapter_ip>>16)&0xFF,
        (masscan->adapter_ip>> 8)&0xFF,
        (masscan->adapter_ip>> 0)&0xFF
        );
    fprintf(fp, "adapter-mac = %02x:%02x:%02x:%02x:%02x:%02x\n",
            masscan->adapter_mac[0],
            masscan->adapter_mac[1],
            masscan->adapter_mac[2],
            masscan->adapter_mac[3],
            masscan->adapter_mac[4],
            masscan->adapter_mac[5]);
    fprintf(fp, "router-mac = %02x:%02x:%02x:%02x:%02x:%02x\n",
            masscan->router_mac[0],
            masscan->router_mac[1],
            masscan->router_mac[2],
            masscan->router_mac[3],
            masscan->router_mac[4],
            masscan->router_mac[5]);
    if (masscan->nic_count == 0)
        masscan_echo_nic(masscan, fp, 0);
    else {
        for (i=0; i<masscan->nic_count; i++)
            masscan_echo_nic(masscan, fp, i);
    }


    /*
     * Output information
@@ -458,10 +484,12 @@ static int
EQUALS(const char *lhs, const char *rhs)
{
    for (;;) {
        while (*lhs == '-' || *lhs == '.')
        while (*lhs == '-' || *lhs == '.' || *lhs == '_')
            lhs++;
        while (*rhs == '-' || *rhs == '.')
        while (*rhs == '-' || *rhs == '.' || *rhs == '_')
            rhs++;
        if (*lhs == '\0' && *rhs == '[')
            return 1; /*arrays*/
        if (tolower(*lhs & 0xFF) != tolower(*rhs & 0xFF))
            return 0;
        if (*lhs == '\0')
@@ -471,6 +499,17 @@ EQUALS(const char *lhs, const char *rhs)
    }
}

static unsigned
ARRAY(const char *rhs)
{
    const char *p = strchr(rhs, '[');
    if (p == NULL)
        return 0;
    else
        p++;
    return (unsigned)parseInt(p);
}

/***************************************************************************
 * Called either from the "command-line" parser when it sees a --parm,
 * or from the "config-file" parser for normal options.
@@ -479,14 +518,25 @@ void
masscan_set_parameter(struct Masscan *masscan, 
                      const char *name, const char *value)
{
    unsigned index = ARRAY(name);
    if (index >= 8) {
        fprintf(stderr, "%s: bad index\n", name);
        exit(1);
    }

    if (EQUALS("conf", name) || EQUALS("config", name)) {
        masscan_read_config_file(masscan, value);
    } else if (EQUALS("adapter", name) || EQUALS("if", name) || EQUALS("interface", name)) {
        if (masscan->ifname[0]) {
            fprintf(stderr, "CONF: overwriting \"adapter=%s\"\n", masscan->ifname);
        }
        sprintf_s(masscan->ifname, sizeof(masscan->ifname), "%s", value);
        if (masscan->nic[index].ifname[0]) {
            fprintf(stderr, "CONF: overwriting \"adapter=%s\"\n", masscan->nic[index].ifname);
        }
        if (masscan->nic_count < index + 1)
            masscan->nic_count = index + 1;
        sprintf_s(  masscan->nic[index].ifname, 
                    sizeof(masscan->nic[index].ifname), 
                    "%s",
                    value);

    }
    else if (EQUALS("adapter-ip", name) || EQUALS("source-ip", name) 
             || EQUALS("source-address", name) || EQUALS("spoof-ip", name)
@@ -501,7 +551,7 @@ masscan_set_parameter(struct Masscan *masscan,
                return;
            }

            masscan->adapter_ip = range.begin;
            masscan->nic[index].adapter_ip = range.begin;
    } else if (EQUALS("adapter-port", name) || EQUALS("source-port", name)) {
        /* Send packets FROM this port number */
        unsigned x = strtoul(value, 0, 0);
@@ -509,7 +559,7 @@ masscan_set_parameter(struct Masscan *masscan,
            fprintf(stderr, "error: %s=<n>: expected number less than 1000\n", 
                    name);
        } else {
            masscan->adapter_port = x;
            masscan->nic[index].adapter_port = x;
        }
    } else if (EQUALS("adapter-mac", name) || EQUALS("spoof-mac", name)
               || EQUALS("source-mac", name)) {
@@ -521,7 +571,7 @@ masscan_set_parameter(struct Masscan *masscan,
            return;
        }

        memcpy(masscan->adapter_mac, mac, 6);
        memcpy(masscan->nic[index].adapter_mac, mac, 6);
    }
    else if (EQUALS("router-mac", name) || EQUALS("router", name)) {
        unsigned char mac[6];
@@ -531,7 +581,7 @@ masscan_set_parameter(struct Masscan *masscan,
            return;
        }

        memcpy(masscan->router_mac, mac, 6);
        memcpy(masscan->nic[index].router_mac, mac, 6);
    }
    else if (EQUALS("rate", name) || EQUALS("max-rate", name) ) {
        double rate = 0.0;
+11 −9
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
int
masscan_initialize_adapter(
    struct Masscan *masscan,
    unsigned index,
    unsigned *r_adapter_ip,
    unsigned char *adapter_mac,
    unsigned char *router_mac)
@@ -33,8 +34,8 @@ masscan_initialize_adapter(
     * the best Interface to use. We do this by choosing the first
     * interface with a "default route" (aka. "gateway") defined
     */
    if (masscan->ifname && masscan->ifname[0])
        ifname = masscan->ifname;
    if (masscan->nic[index].ifname && masscan->nic[index].ifname[0])
        ifname = masscan->nic[index].ifname;
    else {
        /* no adapter specified, so find a default one */
        int err;
@@ -57,7 +58,7 @@ masscan_initialize_adapter(
     * is done by queryin the adapter (or configured by user). If the
     * adapter doesn't have one, then the user must configure one.
     */
    *r_adapter_ip = masscan->adapter_ip;
    *r_adapter_ip = masscan->nic[index].adapter_ip;
    if (*r_adapter_ip == 0) {
        *r_adapter_ip = rawsock_get_adapter_ip(ifname);
        LOG(2, "auto-detected: adapter-ip=%u.%u.%u.%u\n",
@@ -81,7 +82,7 @@ masscan_initialize_adapter(
     * matter what this address is, but to be a "responsible" citizen we
     * try to use the hardware address in the network card.
     */
    memcpy(adapter_mac, masscan->adapter_mac, 6);
    memcpy(adapter_mac, masscan->nic[index].adapter_mac, 6);
    if (memcmp(adapter_mac, "\0\0\0\0\0\0", 6) == 0) {
        rawsock_get_adapter_mac(ifname, adapter_mac);
        LOG(2, "auto-detected: adapter-mac=%02x-%02x-%02x-%02x-%02x-%02x\n",
@@ -105,17 +106,18 @@ masscan_initialize_adapter(
     * Once we've figured out which adapter to use, we now need to
     * turn it on.
     */
    masscan->adapter = rawsock_init_adapter(    ifname, 
    masscan->nic[index].adapter = rawsock_init_adapter(   
                                            ifname, 
                                            masscan->is_pfring, 
                                            masscan->is_sendq,
                                            masscan->nmap.packet_trace,
                                            masscan->is_offline);
    if (masscan->adapter == 0) {
    if (masscan->nic[index].adapter == 0) {
        fprintf(stderr, "adapter[%s].init: failed\n", ifname);
        return -1;
    }
    LOG(3, "rawsock: ignoring transmits\n");
    rawsock_ignore_transmits(masscan->adapter, adapter_mac);
    rawsock_ignore_transmits(masscan->nic[index].adapter, adapter_mac);
    LOG(3, "rawsock: initialization done\n");


@@ -129,7 +131,7 @@ masscan_initialize_adapter(
     * Note: in order to ARP the router, we need to first enable the libpcap
     * code above.
     */
    memcpy(router_mac, masscan->router_mac, 6);
    memcpy(router_mac, masscan->nic[index].router_mac, 6);
    if (masscan->is_offline) {
        memcpy(router_mac, "\x66\x55\x44\x33\x22\x11", 6);
    } else if (memcmp(router_mac, "\0\0\0\0\0\0", 6) == 0) {
@@ -148,7 +150,7 @@ masscan_initialize_adapter(
                );

            arp_resolve_sync(
                    masscan->adapter,
                    masscan->nic[index].adapter,
                    *r_adapter_ip,
                    adapter_mac,
                    router_ipv4,
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ packet_trace(FILE *fp, const unsigned char *px, size_t length, unsigned is_sent)
        direction = "RCVD";

    /* parse the packet */
    x = preprocess_frame(px, length, 1, &parsed);
    x = preprocess_frame(px, (unsigned)length, 1, &parsed);
    if (!x)
        return;
    offset = parsed.found_offset;
+71 −59
Original line number Diff line number Diff line
/*
    for printing the status to the command-line roughly once per second
    prints "status" message once per second to the commandline

    the complication is that we cann't afford a "time" check for each
    packet, since it's a system call, so we try to keep a rough
    approximation of when to print a status.
    The status message indicates:
    - the rate in packets-per-second
    - %done
    - estimated time remaining of the scan
    - number of 'tcbs' (TCP control blocks) of active TCP connections

*/
#include "main-status.h"
#include "pixie-timer.h"
#include "unusedparm.h"
#include <stdio.h>
#include <string.h>

#ifndef UNUSEDPARM
#ifdef _MSC_VER
#define UNUSEDPARM(x) x
#else
#define UNUSEDPARM(x)
#endif
#endif

extern time_t global_now;
extern uint64_t global_tcb_count;
@@ -28,10 +24,17 @@ extern uint64_t global_tcb_count;
 * packet is slow.
 ***************************************************************************/
void
status_print(struct Status *status, uint64_t count, uint64_t max_count)
status_print(
    struct Status *status, 
    uint64_t count, 
    uint64_t max_count, 
    double x)
{
    double elapsed;
    uint64_t now;
    double elapsed_time;
    double rate;
    double now;
    double percent_done;
    double time_remaining;

    /*
     * ####  FUGGLY TIME HACK  ####
@@ -44,42 +47,28 @@ status_print(struct Status *status, uint64_t count, uint64_t max_count)
     */
    global_now = time(0);

    /* speed up or slow down how often we report so that we get about
     * 1-second between reports */
    {
        time_t t = time(0);
        if ((int)t == (int)status->last.time) {
            status->timer <<= 1;
            status->timer |= 1;
        } else {
            status->timer >>= 1;
            status->timer |= 1;
        }
        status->last.time = t;
    }

    /* If nothing's changed, then stop here, because otherwise we'll
     * be dividing by zero or something */
    if (count <= status->last.count)
        return;


    /* Get the time. NOTE: this is CLOCK_MONOTONIC_RAW on Linux, not
     * wall-clock time. */
    now = pixie_gettime();
    elapsed = ((double)now - (double)status->last.clock)/(double)1000000.0;
    if (elapsed == 0)
    now = (double)pixie_gettime();

    /* Figure how many SECONDS have elapsed, in a floating point value.
     * Since the above timestamp is in microseconds, we need to 
     * shift it by 1-million
     */
    elapsed_time = (now - status->last.clock)/1000000.0;
    if (elapsed_time == 0)
        return;
    status->last.clock = now;

    /* Figure out the "packets-per-second" number, which is just:
     *
     *  rate = packets_sent / elapsed_time;
     */
    rate = (count - status->last.count)*1.0/elapsed_time;

    /*
     * Print the message to <stderr> so that <stdout> can be redirected
     * to a file (<stdout> reports what systems were found).
     * Smooth the number by averaging over the last 8 seconds 
     */
    {
        double rate = ((double)(count - status->last.count)*1.0/elapsed);
        double percent_done = (double)(count*100.0/max_count);
        double finished = 0;
     status->last_rates[status->last_count++ & 0x7] = rate;
     rate =     status->last_rates[0]
                + status->last_rates[1]
@@ -91,20 +80,41 @@ status_print(struct Status *status, uint64_t count, uint64_t max_count)
                + status->last_rates[7]
                ;
    rate /= 8;
        if (rate)
            finished  = (1.0 - percent_done/100.0) * (max_count / rate);
        /* (%u-days %02u:%02u:%02u remaining) */
        fprintf(stderr, "rate:%6.2f-kpps, %5.2f%% done, %u:%02u:%02u remaining, %llu-tcbs             \r",
                        rate/1000.0,
    if (rate == 0)
        return;

    /*
     * Calculate "percent-done", which is just the total number of
     * packets sent divided by the number we need to send.
     */
    percent_done = (double)(count*100.0/max_count);


    /*
     * Calulate the time remaining in the scan
     */
    time_remaining  = (1.0 - percent_done/100.0) * (max_count / rate);


    /*
     * Print the message to <stderr> so that <stdout> can be redirected
     * to a file (<stdout> reports what systems were found).
     */
    fprintf(stderr, "rate:%6.2f-kpps, %5.2f%% done,%4u:%02u:%02u remaining, %llu-tcbs,     \r",
                    x/1000.0,
                    percent_done,
            (unsigned)(finished/60/60),
            (unsigned)(finished/60)%60,
            (unsigned)(finished)%60,
                    (unsigned)(time_remaining/60/60),
                    (unsigned)(time_remaining/60)%60,
                    (unsigned)(time_remaining)%60,
                    global_tcb_count
                    //(unsigned)rate
                    );
    fflush(stderr);
    }

    /*
     * Remember the values to be diffed against the next time around
     */
    status->last.clock = now;
    status->last.count = count;
}

@@ -114,6 +124,8 @@ void
status_finish(struct Status *status)
{
    UNUSEDPARM(status);
    fprintf(stderr, 
"                                                                             \r");
}

/***************************************************************************
Loading