Commit 59e5ccae authored by robertdavidgraham's avatar robertdavidgraham
Browse files

changes

parent 8d762cf0
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -41,11 +41,17 @@ LIBS = -lwpcap
endif


DEFINES = 
# this works on llvm or real gcc
CC = gcc

DEFINES = 
CFLAGS = -g $(INCLUDES) $(DEFINES) -Wall -O3 -Wno-format
.SUFFIXES: .c .cpp

# just compile everything in the 'src' directory. Using this technique
# means that include file dependencies are broken, so sometimes when
# the program crashes unexpectedly, 'make clean' then 'make' fixes the
# problem that a .h file was out of date
tmp/%.o: src/%.c
	$(CC) $(CFLAGS) -c $< -o $@

+61 −48
Original line number Diff line number Diff line
@@ -109,14 +109,14 @@ masscan_echo(struct Masscan *masscan, FILE *fp)
        (masscan->adapter_ip>> 8)&0xFF,
        (masscan->adapter_ip>> 0)&0xFF
        );
    fprintf(fp, "adapter.mac = %02x:%02x:%02x:%02x:%02x:%02x\n",
    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",
    fprintf(fp, "router-mac = %02x:%02x:%02x:%02x:%02x:%02x\n",
            masscan->router_mac[0],
            masscan->router_mac[1],
            masscan->router_mac[2],
@@ -130,18 +130,18 @@ masscan_echo(struct Masscan *masscan, FILE *fp)
    fprintf(fp, "# output\n");
    switch (masscan->nmap.format) {
    case Output_Interactive:
        fprintf(stderr, "output.format = interactive\n");
        fprintf(fp, "output-format = interactive\n");
        break;
    case Output_List:
        fprintf(stderr, "output.format = list\n");
        fprintf(fp, "output-format = list\n");
        break;
    default:
        fprintf(stderr, "output.format = unknown(%u)\n", masscan->nmap.format);
        fprintf(fp, "output-format = unknown(%u)\n", masscan->nmap.format);
        break;
    }
    fprintf(fp, "output.filename = %s\n", masscan->nmap.filename);
    fprintf(fp, "output-filename = %s\n", masscan->nmap.filename);
    if (masscan->nmap.append)
        fprintf(fp, "output.append = true\n");
        fprintf(fp, "output-append = true\n");


    /*
@@ -347,6 +347,22 @@ parseInt(const char *str)
}


int EQUALS(const char *lhs, const char *rhs)
{
    for (;;) {
        while (*lhs == '-' || *lhs == '.')
            lhs++;
        while (*rhs == '-' || *rhs == '.')
            rhs++;
        if (tolower(*lhs & 0xFF) != tolower(*rhs & 0xFF))
            return 0;
        if (*lhs == '\0')
            return 1;
        lhs++;
        rhs++;
    }
}

/***************************************************************************
 * Called either from the "command-line" parser when it sees a --parm,
 * or from the "config-file" parser for normal options.
@@ -354,7 +370,6 @@ parseInt(const char *str)
void
masscan_set_parameter(struct Masscan *masscan, const char *name, const char *value)
{
#define EQUALS(lhs, rhs) (strcmp(lhs, rhs)==0)

    if (EQUALS("conf", name) || EQUALS("config", name)) {
        masscan_read_config_file(masscan, value);
@@ -576,7 +591,7 @@ masscan_set_parameter(struct Masscan *masscan, const char *name, const char *val
        }
    } else if (EQUALS("output-filename", name)) {
        strcpy_s(masscan->nmap.filename, sizeof(masscan->nmap.filename), value);
    } else if (EQUALS("packet-trace", name)) {
    } else if (EQUALS("packet-trace", name) || EQUALS("trace-packet", name)) {
        masscan->nmap.packet_trace = 1;
    } else if (EQUALS("privileged", name) || EQUALS("unprivileged", name)) {
        fprintf(stderr, "nmap(%s): unsupported\n", name);
@@ -687,7 +702,7 @@ is_singleton(const char *name)
        "log-errors", "append-output", "webxml", "no-stylesheet",
        "no-stylesheet",
        "send-eth", "send-ip", "iflist", "randomize-hosts",
        "nmap",
        "nmap", "trace-packet",
        0};
    size_t i;

@@ -839,8 +854,6 @@ masscan_command_line(struct Masscan *masscan, int argc, char *argv[])
                switch (argv[i][2]) {
                case 'A':
                    masscan->nmap.format = Output_All;
                    fprintf(stderr, "nmap(%s): unsupported output format\n", argv[i]);
                    exit(1);
                    break;
                case 'N':
                    masscan->nmap.format = Output_Normal;
+7 −6
Original line number Diff line number Diff line
@@ -325,6 +325,8 @@ main_scan(struct Masscan *masscan)
    FILE *fpout = stdout;
    struct DedupTable *dedup;



    /*
     * Turn the adapter on, and get the running configuration
     */
@@ -418,6 +420,7 @@ main_scan(struct Masscan *masscan)
            perror(masscan->nmap.filename);
            exit(1);
        }
        fpout = fp;
    }
    dedup = dedup_create();

@@ -510,9 +513,8 @@ main_scan(struct Masscan *masscan)
         * our sending port numbers, and having the right seqno/ackno
         * fields set.
         */
        switch (masscan->nmap.format) {
        case Output_Interactive:
            fprintf(fpout, "Discovered %s port %u/tcp on %u.%u.%u.%u                          \n",
        if (masscan->nmap.format == Output_Interactive || masscan->nmap.format == Output_All) {
            fprintf(stdout, "Discovered %s port %u/tcp on %u.%u.%u.%u                          \n",
                status_string(status),
                parsed.port_src,
                (src>>24)&0xFF,
@@ -520,8 +522,8 @@ main_scan(struct Masscan *masscan)
                (src>> 8)&0xFF,
                (src>> 0)&0xFF
                );
            break;
        case Output_List:
        }
        if (masscan->nmap.format == Output_List || masscan->nmap.format == Output_All) {
            fprintf(fpout, "%s tcp %u %u.%u.%u.%u\n",
                status_string(status),
                parsed.port_src,
@@ -530,7 +532,6 @@ main_scan(struct Masscan *masscan)
                (src>> 8)&0xFF,
                (src>> 0)&0xFF
                );
            break;
        }
    }

+79 −9
Original line number Diff line number Diff line
@@ -116,6 +116,17 @@ rangelist_add_range(struct RangeList *task, unsigned begin, unsigned end)
    task->count++;
}

/***************************************************************************
 ***************************************************************************/
void
rangelist_free(struct RangeList *list)
{
    if (list->list) {
        free(list->list);
        memset(list, 0, sizeof(*list));
    }
}

/***************************************************************************
 ***************************************************************************/
void
@@ -187,8 +198,8 @@ rangelist_remove_range2(struct RangeList *task, struct Range range)
 * Parse an IPv4 address from a line of text, moving the offset forward
 * to the first non-IPv4 character
 ***************************************************************************/
static unsigned
parse_ipv4(const char *line, unsigned *inout_offset, unsigned max)
static int
parse_ipv4(const char *line, unsigned *inout_offset, unsigned max, unsigned *ipv4)
{
    unsigned offset = *inout_offset;
    unsigned result = 0;
@@ -196,18 +207,39 @@ parse_ipv4(const char *line, unsigned *inout_offset, unsigned max)

    for (i=0; i<4; i++) {
        unsigned x = 0;
        unsigned digits = 0;

        if (offset >= max)
            return -4;
        if (!isdigit(line[offset]&0xFF))
            return -1;

        /* clear leading zeros */
        while (offset < max && line[offset] == '0')
            offset++;

        /* parse maximum of 3 digits */
        while (offset < max && isdigit(line[offset]&0xFF)) {
            x = x * 10 + (line[offset] - '0');
            offset++;
            if (++digits > 3)
                return -2;
        }
        if (x > 255)
            return -5;
        result = result * 256 + (x & 0xFF);
        if (offset >= max || line[offset] != '.')
        if (i == 3)
            break;

        if (line[offset] != '.')
            return -3;
        offset++; /* skip dot */
    }

    *inout_offset = offset;
    return result;
    *ipv4 = result;

    return 0; /* parse ok */
}

/****************************************************************************
@@ -233,6 +265,9 @@ range_parse_ipv4(const char *line, unsigned *inout_offset, unsigned max)
{
    unsigned offset;
    struct Range result;
    static const struct Range badrange = {0xFFFFFFFF, 0};
    int err;


    if (inout_offset == NULL) {
         inout_offset = &offset;
@@ -247,7 +282,10 @@ range_parse_ipv4(const char *line, unsigned *inout_offset, unsigned max)
        offset++;

    /* get the first IP address */
    result.begin = parse_ipv4(line, &offset, max);
    err = parse_ipv4(line, &offset, max, &result.begin);
    if (err) {
        return badrange;
    }
    result.end = result.begin;

    /* trim whitespace */
@@ -264,13 +302,27 @@ range_parse_ipv4(const char *line, unsigned *inout_offset, unsigned max)
    if (line[offset] == '/') {
        unsigned prefix = 0;
        uint64_t mask = 0;
        unsigned digits = 0;

		/* skip slash */
        offset++;

        if (!isdigit(line[offset]&0xFF)) {
            return badrange;
        }

        /* strip leading zeroes */
        while (offset<max && line[offset] == '0')
            offset++;

		/* parse decimal integer */
        while (offset<max && isdigit(line[offset]&0xFF))
        while (offset<max && isdigit(line[offset]&0xFF)) {
            prefix = prefix * 10 + (line[offset++] - '0');
            if (++digits > 2)
                return badrange;
        }
        if (prefix > 32)
            return badrange;

		/* Create the mask from the prefix */
        mask = 0xFFFFFFFF00000000UL >> prefix;
@@ -292,7 +344,9 @@ range_parse_ipv4(const char *line, unsigned *inout_offset, unsigned max)
		unsigned ip;

        offset++;
		ip = parse_ipv4(line, &offset, max);
		err = parse_ipv4(line, &offset, max, &ip);
        if (err)
            return badrange;
		if (ip < result.begin) {
            result.begin = 0xFFFFFFFF;
            result.end = 0x00000000;
@@ -341,7 +395,7 @@ rangelist_pick(struct RangeList *targets, uint64_t index)
	unsigned i;

	for (i=0; i<targets->count; i++) {
		uint64_t range = targets->list[i].end - targets->list[i].begin + 1;
		uint64_t range = (uint64_t)targets->list[i].end - (uint64_t)targets->list[i].begin + 1UL;
		if (index < range)
			return (unsigned)(targets->list[i].begin + index);
		else
@@ -397,9 +451,25 @@ ranges_selftest()
    struct RangeList task[1];

    memset(task, 0, sizeof(task[0]));
#define ASSURT(x) if (!(x)) return (fprintf(stderr, "regression failed %s:%u\n", __FILE__, __LINE__)|1)
#define ERROR() fprintf(stderr, "selftest: failed %s:%u\n", __FILE__, __LINE__);

    /* test for the /0 CIDR block, since we'll be using that a lot to scan the entire
     * Internet */
    r = range_parse_ipv4("0.0.0.0/0", 0, 0);
    ASSURT(r.begin == 0 && r.end == 0xFFFFFFFF);

#define ERROR() fprintf(stderr, "selftest: failed %s:%u\n", __FILE__, __LINE__);
    r = range_parse_ipv4("0.0.0./0", 0, 0);
    ASSURT(r.begin > r.end);

    r = range_parse_ipv4("75.748.86.91", 0, 0);
    ASSURT(r.begin > r.end);

    r = range_parse_ipv4("23.75.345.200", 0, 0);
    ASSURT(r.begin > r.end);

    r = range_parse_ipv4("192.1083.0.1", 0, 0);
    ASSURT(r.begin > r.end);

    r = range_parse_ipv4("192.168.1.3", 0, 0);
    if (r.begin != 0xc0a80103 || r.end != 0xc0a80103) {
+1 −0
Original line number Diff line number Diff line
@@ -29,4 +29,5 @@ uint64_t rangelist_count(struct RangeList *targets);
unsigned rangelist_pick(struct RangeList *targets, uint64_t i);
void rangelist_parse_ports(struct RangeList *ports, const char *string);

void rangelist_free(struct RangeList *list);
#endif
Loading