Commit 11a1547b authored by robertdavidgraham's avatar robertdavidgraham
Browse files

grepable

parent 33e5c5b8
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -204,6 +204,7 @@ masscan_echo(struct Masscan *masscan, FILE *fp)
    case Output_List:       fprintf(fp, "output-format = list\n"); break;
    case Output_XML:        fprintf(fp, "output-format = xml\n"); break;
    case Output_Binary:     fprintf(fp, "output-format = binary\n"); break;
    case Output_Grepable:   fprintf(fp, "output-format = grepable\n"); break;
    case Output_JSON:       fprintf(fp, "output-format = json\n"); break;
    case Output_None:       fprintf(fp, "output-format = none\n"); break;
    case Output_Redis:
@@ -1104,6 +1105,8 @@ masscan_set_parameter(struct Masscan *masscan,
        else if (EQUALS("interactive", value))  masscan->nmap.format = Output_Interactive;
        else if (EQUALS("xml", value))          masscan->nmap.format = Output_XML;
        else if (EQUALS("binary", value))       masscan->nmap.format = Output_Binary;
        else if (EQUALS("greppable", value))    masscan->nmap.format = Output_Grepable;
        else if (EQUALS("grepable", value))     masscan->nmap.format = Output_Grepable;
        else if (EQUALS("json", value))         masscan->nmap.format = Output_JSON;
        else if (EQUALS("none", value))         masscan->nmap.format = Output_None;
        else if (EQUALS("redis", value))        masscan->nmap.format = Output_Redis;
@@ -1539,8 +1542,6 @@ masscan_command_line(struct Masscan *masscan, int argc, char *argv[])
                    break;
                case 'G':
                    masscan->nmap.format = Output_Grepable;
                    fprintf(stderr, "nmap(%s): unsupported output format\n", argv[i]);
                    exit(1);
                    break;
                case 'L':
                    masscan_set_parameter(masscan, "output-format", "list");
@@ -1551,7 +1552,7 @@ masscan_command_line(struct Masscan *masscan, int argc, char *argv[])
                }

                ++i;
                if (i >= argc || argv[i][0] == '-') {
                if (i >= argc || (argv[i][0] == '-' && argv[i][1] != '\0')) {
                    fprintf(stderr, "missing output filename\n");
                    exit(1);
                }
+2 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
    to make this file relative "flat" this way so that everything is visible.
*/
#include "masscan.h"
#include "masscan-version.h"
#include "masscan-status.h"     /* open or closed */
#include "rand-blackrock.h"     /* the BlackRock shuffling func */
#include "rand-lcg.h"           /* the LCG randomization func */
@@ -1180,7 +1181,7 @@ main_scan(struct Masscan *masscan)
        now = time(0);
        gmtime_s(&x, &now);
        strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S GMT", &x);
        LOG(0, "\nStarting masscan 1.0.1 (http://bit.ly/14GZzcT) at %s\n", buffer);
        LOG(0, "\nStarting masscan " MASSCAN_VERSION " (http://bit.ly/14GZzcT) at %s\n", buffer);
        LOG(0, " -- forced options: -sS -Pn -n --randomize-hosts -v --send-eth\n");
        LOG(0, "Initiating SYN Stealth Scan\n");
        LOG(0, "Scanning %u hosts [%u port%s/host]\n",

src/masscan-version.h

0 → 100644
+5 −0
Original line number Diff line number Diff line
#ifndef MASSCAN_VERSION

#define MASSCAN_VERSION "1.0.2"

#endif
 No newline at end of file

src/out-grepable.c

0 → 100644
+197 −0
Original line number Diff line number Diff line
#include "output.h"
#include "masscan.h"
#include "masscan-version.h"
#include "templ-port.h"
#include "string_s.h"


/****************************************************************************
 ****************************************************************************/
static unsigned
count_type(const struct RangeList *ports, int type)
{
    unsigned min_port = type;
    unsigned max_port = type + 65535;
    unsigned i;
    unsigned result = 0;

    for (i=0; i<ports->count; ports++) {
        struct Range r = ports->list[i];
        if (r.begin > max_port)
            continue;
        if (r.end < min_port)
            continue;

        if (r.begin < min_port)
            r.begin = min_port;
        if (r.end > max_port)
            r.end = max_port;


        result += r.end - r.begin + 1;
    }

    return result;
}

/****************************************************************************
 ****************************************************************************/
static void
print_port_list(const struct RangeList *ports, int type, FILE *fp)
{
    unsigned min_port = type;
    unsigned max_port = type + 65535;
    unsigned i;

    for (i=0; i<ports->count; ports++) {
        struct Range r = ports->list[i];
        if (r.begin > max_port)
            continue;
        if (r.end < min_port)
            continue;

        if (r.begin < min_port)
            r.begin = min_port;
        if (r.end > max_port)
            r.end = max_port;

        fprintf(fp, "%u-%u%s", r.begin, r.end, (i+1<ports->count)?",":"");
    }
}

/****************************************************************************
 * This function doesn't really "open" the file. Instead, the purpose of
 * this function is to initialize the file by printing header information.
 ****************************************************************************/
static void
grepable_out_open(struct Output *out, FILE *fp)
{
    char timestamp[64];
    struct tm tm;
    unsigned count;

    gmtime_s(&tm, &out->when_scan_started);

    //Tue Jan 21 20:23:22 2014
    //%a %b %d %H:%M:%S %Y
    strftime(timestamp, sizeof(timestamp), "%c", &tm);

    fprintf(fp, "# Masscan " MASSCAN_VERSION " scan initiated %s\n", 
                timestamp);

    count = count_type(&out->masscan->ports, Templ_TCP);
    fprintf(fp, "# Ports scanned: TCP(%u;", count);
    if (count)
        print_port_list(&out->masscan->ports, Templ_TCP, fp);

    count = count_type(&out->masscan->ports, Templ_UDP);
    fprintf(fp, ") UDP(%u;", count);
    if (count)
        print_port_list(&out->masscan->ports, Templ_UDP, fp);

    count = count_type(&out->masscan->ports, Templ_SCTP);
    fprintf(fp, ") SCTP(%u;", count);
    if (count)
        print_port_list(&out->masscan->ports, Templ_SCTP, fp);

    fprintf(fp, ") PROTOCOLS(0;)\n");
}

/****************************************************************************
 * This function doesn't really "close" the file. Instead, it's purpose
 * is to print trailing information to the file. This is pretty much only
 * a concern for XML files that need stuff appeneded to the end.
 ****************************************************************************/
static void
grepable_out_close(struct Output *out, FILE *fp)
{
    time_t now = time(0);
    char timestamp[64];
    struct tm tm;

    UNUSEDPARM(out);

    gmtime_s(&tm, &now);

    //Tue Jan 21 20:23:22 2014
    //%a %b %d %H:%M:%S %Y
    strftime(timestamp, sizeof(timestamp), "%c", &tm);

    fprintf(fp, "# Masscan done at %s\n", 
                timestamp);
}

/****************************************************************************
 * Prints out the status of a port, which is almost always just "open"
 * or "closed".
 ****************************************************************************/
static void
grepable_out_status(struct Output *out, FILE *fp, time_t timestamp,
    int status, unsigned ip, unsigned port, unsigned reason, unsigned ttl)
{
    UNUSEDPARM(timestamp);
    UNUSEDPARM(out);
    UNUSEDPARM(reason);
    UNUSEDPARM(ttl);

    fprintf(fp, "Host: %u.%u.%u.%u ()",
                    (unsigned char)(ip>>24),
                    (unsigned char)(ip>>16),
                    (unsigned char)(ip>> 8),
                    (unsigned char)(ip>> 0)
                    );
    fprintf(fp, "\tPorts: %u/%s/%s/%s/%s/%s/%s\n",
                port,
                status_string(status),      //"open", "closed"
                proto_from_status(status),  //"tcp", "udp", "sctp"
                "", //owner
                "", //service
                "", //SunRPC info
                "" //Version info
                );
}

/****************************************************************************
 * Prints out "banner" information for a port. This is done when there is
 * a protocol defined for a port, and we do some interaction to find out
 * more information about which protocol is running on a port, it's version,
 * and other useful information.
 ****************************************************************************/
static void
grepable_out_banner(struct Output *out, FILE *fp, time_t timestamp,
        unsigned ip, unsigned ip_proto, unsigned port,
        enum ApplicationProtocol proto, const unsigned char *px, unsigned length)
{
    char banner_buffer[4096];

    UNUSEDPARM(timestamp);
    UNUSEDPARM(out);
    UNUSEDPARM(ip_proto);
    
    fprintf(fp, "Host: %u.%u.%u.%u ()",
                    (unsigned char)(ip>>24),
                    (unsigned char)(ip>>16),
                    (unsigned char)(ip>> 8),
                    (unsigned char)(ip>> 0)
                    );
    fprintf(fp, "\tApp: %s", masscan_app_to_string(proto));

    normalize_string(px, length, banner_buffer, sizeof(banner_buffer));

    fprintf(fp, "\tBanner: %s\n", banner_buffer);

}


/****************************************************************************
 * This is the only structure exposed to the rest of the system. Everything
 * else in the file is defined 'static' or 'private'.
 ****************************************************************************/
const struct OutputType grepable_output = {
    "grepable",
    0,
    grepable_out_open,
    grepable_out_close,
    grepable_out_status,
    grepable_out_banner
};
+21 −10
Original line number Diff line number Diff line
@@ -71,9 +71,9 @@ proto_from_status(unsigned status)
 * string based on the narrow variable.
 *****************************************************************************/
const char *
status_string(int x)
status_string(int status)
{
    switch (x) {
    switch (status) {
        case Port_Open: return "open";
        case Port_Closed: return "closed";
        case Port_UdpOpen: return "open";
@@ -155,7 +155,7 @@ normalize_string(const unsigned char *px, size_t length,
static FILE *
open_rotate(struct Output *out, const char *filename)
{
    FILE *fp;
    FILE *fp = 0;
    unsigned is_append = out->is_append;
    int x;

@@ -186,9 +186,14 @@ open_rotate(struct Output *out, const char *filename)
        return (FILE*)fd;
    }

    /* Do something special for the "-" filename */
    if (filename[0] == '-' && filename[1] == '\0')
        fp = stdout;

    /* open a "shareable" file. On Windows, by default files can't be renamed
     * while they are open, so we need a special function that takes care
     * of this. */
    if (fp == 0) {
        x = pixie_fopen_shareable(&fp, filename, is_append);
        if (x != 0 || fp == NULL) {
            fprintf(stderr, "out: could not open file for %s\n",
@@ -197,6 +202,7 @@ open_rotate(struct Output *out, const char *filename)
            control_c_pressed = 1;
            return NULL;
        }
    }

    /*
     * Write the format-specific headers, like <xml>
@@ -369,6 +375,8 @@ output_create(const struct Masscan *masscan, unsigned thread_index)
    if (out == NULL)
        return NULL;
    memset(out, 0, sizeof(*out));
    out->masscan = masscan;
    out->when_scan_started = time(0);

    /*
     * Copy the configuration information from the 'masscan' structure.
@@ -408,6 +416,9 @@ output_create(const struct Masscan *masscan, unsigned thread_index)
    case Output_Binary:
        out->funcs = &binary_output;
        break;
    case Output_Grepable:
        out->funcs = &grepable_output;
        break;
    case Output_Redis:
        out->funcs = &redis_output;
        break;
Loading