Skip to content

Commit

Permalink
tcp service names in greppable output
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdavidgraham committed Jul 23, 2018
1 parent 1107def commit b869603
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 80 deletions.
33 changes: 25 additions & 8 deletions src/main-conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,18 @@ masscan_save_state(struct Masscan *masscan)
}


void
/*****************************************************************************
* Just a wrapper around rangelist_add_range() since in theory they can
* be different functions even though their prototypes are almost
* identical.
*****************************************************************************/
static void
rangelist_add_callback(void *v, unsigned begin, unsigned end)
{
rangelist_add_range(v, begin, end);
rangelist_add_range((struct RangeList *)v, begin, end);
}


/*****************************************************************************
* Read in ranges from a file
*
Expand Down Expand Up @@ -1475,6 +1481,21 @@ static int SET_shard(struct Masscan *masscan, const char *name, const char *valu
return CONF_OK;
}

static int SET_output_stylesheet(struct Masscan *masscan, const char *name, const char *value)
{
UNUSEDPARM(name);
if (masscan->echo) {
if (masscan->output.stylesheet[0] || masscan->echo_all)
fprintf(masscan->echo, "stylesheet = %s\n", masscan->output.stylesheet);
return 0;
}

if (masscan->output.format == 0)
masscan->output.format = Output_XML;

strcpy_s(masscan->output.stylesheet, sizeof(masscan->output.stylesheet), value);
return CONF_OK;
}



Expand Down Expand Up @@ -1515,7 +1536,7 @@ struct ConfigParameter config_parameters[] = {
{"rotate-dir", SET_rotate_directory, 0, {"output-rotate-dir", "rotate-directory", 0}},
{"rotate-offset", SET_rotate_offset, 0, {"output-rotate-offset", 0}},
{"rotate-size", SET_rotate_filesize, 0, {"output-rotate-filesize", "rotate-filesize", 0}},

{"stylesheet", SET_output_stylesheet},
{"SPACE", SET_space},
{0}
};
Expand Down Expand Up @@ -2070,10 +2091,6 @@ masscan_set_parameter(struct Masscan *masscan,
;
} else if (EQUALS("no-stylesheet", name)) {
masscan->output.stylesheet[0] = '\0';
} else if (EQUALS("stylesheet", name)) {
strcpy_s(masscan->output.stylesheet,
sizeof(masscan->output.stylesheet),
value);
} else if (EQUALS("system-dns", name)) {
fprintf(stderr, "nmap(%s): DNS lookups will never be supported by this code\n", name);
exit(1);
Expand Down Expand Up @@ -2146,7 +2163,7 @@ is_singleton(const char *name)
"osscan-limit", "osscan-guess",
"badsum", "reason", "open", "open-only",
"packet-trace", "release-memory",
"log-errors", "append-output", "webxml", "no-stylesheet",
"log-errors", "append-output", "webxml",
"no-stylesheet", "heartbleed", "ticketbleed",
"send-eth", "send-ip", "iflist",
"nmap", "trace-packet", "pfring", "sendq",
Expand Down
4 changes: 3 additions & 1 deletion src/main-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,15 @@ status_print(
);
} else {
if (is_tx_done) {

fprintf(stderr,
"rate:%6.2f-kpps, %5.2f%% done, waiting %d-secs, found=%" PRIu64 " \r",
"rate:%6.2f-kpps, %5.2f%% done, waiting %d-secs, found=%" PRIu64 " \r",
x/1000.0,
percent_done,
(int)exiting,
total_synacks
);

} else {
fprintf(stderr,
"rate:%6.2f-kpps, %5.2f%% done,%4u:%02u:%02u remaining, found=%" PRIu64 " \r",
Expand Down
26 changes: 17 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ time_t global_now;

uint64_t usec_start;


/***************************************************************************
* We create a pair of transmit/receive threads for each network adapter.
* This structure contains the parameters we send to each pair.
Expand Down Expand Up @@ -305,7 +306,7 @@ transmit_thread(void *v) /*aka. scanning_thread() */
throttler_start(throttler, masscan->max_rate/masscan->nic_count);

infinite:

/* Create the shuffler/randomizer. This creates the 'range' variable,
* which is simply the number of IP addresses times the number of
* ports */
Expand Down Expand Up @@ -484,6 +485,7 @@ transmit_thread(void *v) /*aka. scanning_thread() */
uint64_t batch_size;

for (k=0; k<1000; k++) {

/*
* Only send a few packets at a time, throttled according to the max
* --max-rate set by the user
Expand Down Expand Up @@ -558,7 +560,7 @@ receive_thread(void *v)
parms->total_tcbs = status_tcb_count;

LOG(1, "THREAD: recv: starting thread #%u\n", parms->nic_index);

/* Lock this thread to a CPU. Transmit threads are on even CPUs,
* receive threads on odd CPUs */
if (pixie_cpu_get_count() > 1) {
Expand Down Expand Up @@ -718,13 +720,12 @@ receive_thread(void *v)
&secs,
&usecs,
&px);

if (err != 0) {
if (tcpcon)
tcpcon_timeouts(tcpcon, (unsigned)time(0), 0);
continue;
}


/*
* Do any TCP event timeouts based on the current timestamp from
Expand Down Expand Up @@ -968,6 +969,7 @@ receive_thread(void *v)
parsed.ip_ttl,
parsed.mac_src
);


/*
* Send RST so other side isn't left hanging (only doing this in
Expand All @@ -987,7 +989,7 @@ receive_thread(void *v)


LOG(1, "THREAD: recv: stopping thread #%u\n", parms->nic_index);

/*
* cleanup
*/
Expand All @@ -1011,7 +1013,6 @@ receive_thread(void *v)

/* Thread is about to exit */
parms->done_receiving = 1;

}


Expand All @@ -1033,8 +1034,14 @@ static void control_c_handler(int x)
control_c_pressed = 1+x;
is_tx_done = control_c_pressed;
} else {
control_c_pressed_again = 1;
is_rx_done = control_c_pressed_again;
if (is_rx_done) {
fprintf(stderr, "\nERROR: threads not exiting %d\n", is_rx_done);
if (is_rx_done++ > 1)
exit(1);
} else {
control_c_pressed_again = 1;
is_rx_done = control_c_pressed_again;
}
}

}
Expand Down Expand Up @@ -1388,8 +1395,9 @@ main_scan(struct Masscan *masscan)



if (time(0) - now >= masscan->wait)
if (time(0) - now >= masscan->wait) {
is_rx_done = 1;
}

if (masscan->output.is_status_updates) {
status_print(&status, min_index, range, rate,
Expand Down
14 changes: 13 additions & 1 deletion src/out-grepable.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "masscan.h"
#include "masscan-version.h"
#include "masscan-status.h"
#include "out-tcp-services.h"
#include "templ-port.h"
#include "string_s.h"

Expand Down Expand Up @@ -60,6 +61,8 @@ print_port_list(const struct RangeList *ports, int type, FILE *fp)
}
}

extern const char *debug_recv_status;

/****************************************************************************
* This function doesn't really "open" the file. Instead, the purpose of
* this function is to initialize the file by printing header information.
Expand All @@ -71,6 +74,7 @@ grepable_out_open(struct Output *out, FILE *fp)
struct tm tm;
unsigned count;


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

//Tue Jan 21 20:23:22 2014
Expand Down Expand Up @@ -130,11 +134,19 @@ static void
grepable_out_status(struct Output *out, FILE *fp, time_t timestamp,
int status, unsigned ip, unsigned ip_proto, unsigned port, unsigned reason, unsigned ttl)
{
const char *service;
UNUSEDPARM(timestamp);
UNUSEDPARM(out);
UNUSEDPARM(reason);
UNUSEDPARM(ttl);

if (ip_proto == 6)
service = tcp_service_name(port);
else if (ip_proto == 17)
service = udp_service_name(port);
else
service = "";

fprintf(fp, "Host: %u.%u.%u.%u ()",
(unsigned char)(ip>>24),
(unsigned char)(ip>>16),
Expand All @@ -146,7 +158,7 @@ grepable_out_status(struct Output *out, FILE *fp, time_t timestamp,
status_string(status), //"open", "closed"
name_from_ip_proto(ip_proto), //"tcp", "udp", "sctp"
"", //owner
"", //service
service, //service
"", //SunRPC info
"" //Version info
);
Expand Down
80 changes: 80 additions & 0 deletions src/out-tcp-services.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "out-tcp-services.h"
#include <string.h>
#include <stdlib.h>

#ifndef WIN32
#include <netdb.h>
#else
#include <WinSock2.h>
#endif
#include <ctype.h>

#if _MSC_VER
#define strdup _strdup
#endif

static char *tcp_services[65536];
static char *udp_services[65536];



const char *
tcp_service_name(int port)
{
if (tcp_services[port])
return tcp_services[port];

#ifdef __linux__
int r;
struct servent result_buf;
struct servent *result;
char buf[2048];

r = getservbyport_r(htons(port), "tcp", &result_buf,buf, sizeof(buf), &result);

/* ignore ERANGE - if the result can't fit in 2k, just return unknown */
if (r != 0 || result == NULL)
return "unknown";

return tcp_services[port] = strdup(result_buf.s_name);
#else
struct servent *result;

result = getservbyport(htons((unsigned short)port), "tcp");

if (result == 0)
return "unknown";

return tcp_services[port] = strdup(result->s_name);
#endif
}

const char *
udp_service_name(int port)
{
if (udp_services[port])
return udp_services[port];
#ifdef __linux__
int r;
struct servent result_buf;
struct servent *result;
char buf[2048];

r = getservbyport_r(htons(port), "udp", &result_buf,buf, sizeof(buf), &result);

/* ignore ERANGE - if the result can't fit in 2k, just return unknown */
if (r != 0 || result == NULL)
return "unknown";

return udp_services[port] = strdup(result_buf.s_name);
#else
struct servent *result;

result = getservbyport(htons((unsigned short)port), "udp");

if (result == 0)
return "unknown";

return udp_services[port] = strdup(result->s_name);
#endif
}
8 changes: 8 additions & 0 deletions src/out-tcp-services.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef OUT_TCP_SERVICES_H
#define OUT_TCP_SERVICES_H

const char *tcp_service_name(int port);
const char *udp_service_name(int port);

#endif

Loading

0 comments on commit b869603

Please sign in to comment.