Skip to content

Commit

Permalink
oproto
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdavidgraham committed May 16, 2019
1 parent 0e17953 commit ae4b908
Show file tree
Hide file tree
Showing 20 changed files with 310 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
paused.conf
.Makefile.swp

.vscode
27 changes: 21 additions & 6 deletions src/main-conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,13 @@ masscan_set_parameter(struct Masscan *masscan,
if (masscan->op == 0)
masscan->op = Operation_Scan;
}
else if (EQUALS("oprotos", name) || EQUALS("oproto", name)) {
unsigned is_error = 0;
masscan->scan_type.oproto = 1;
rangelist_parse_ports(&masscan->ports, value, &is_error, Templ_Oproto_first);
if (masscan->op == 0)
masscan->op = Operation_Scan;
}
else if (EQUALS("tcp-ports", name) || EQUALS("tcp-port", name)) {
unsigned is_error = 0;
masscan->scan_type.tcp = 1;
Expand Down Expand Up @@ -2300,8 +2307,10 @@ masscan_load_database_files(struct Masscan *masscan)
if (filename) {
if (masscan->payloads.udp == NULL)
masscan->payloads.udp = payloads_udp_create();

payloads_read_pcap(filename, masscan->payloads.udp);
if (masscan->payloads.oproto == NULL)
masscan->payloads.oproto = payloads_udp_create();

payloads_read_pcap(filename, masscan->payloads.udp, masscan->payloads.oproto);
}

/*
Expand Down Expand Up @@ -2626,9 +2635,9 @@ masscan_command_line(struct Masscan *masscan, int argc, char *argv[])
case 'N':
fprintf(stderr, "nmap(%s): NULL scan not yet supported\n", argv[i]);
exit(1);
case 'O':
fprintf(stderr, "nmap(%s): IP proto scan not yet supported\n", argv[i]);
exit(1);
case 'O': /* Other IP protocols (not ICMP, UDP, TCP, or SCTP) */
masscan->scan_type.oproto = 1;
break;
case 'S': /* TCP SYN scan - THIS IS WHAT WE DO! */
masscan->scan_type.tcp = 1;
break;
Expand Down Expand Up @@ -2720,7 +2729,8 @@ masscan_command_line(struct Masscan *masscan, int argc, char *argv[])
* If no other "scan type" found, then default to TCP
*/
if (masscan->scan_type.udp == 0 && masscan->scan_type.sctp == 0
&& masscan->scan_type.ping == 0 && masscan->scan_type.arp == 0)
&& masscan->scan_type.ping == 0 && masscan->scan_type.arp == 0
&& masscan->scan_type.oproto == 0)
masscan->scan_type.tcp = 1;

/*
Expand Down Expand Up @@ -2798,6 +2808,11 @@ masscan_echo(struct Masscan *masscan, FILE *fp, unsigned is_echo_all)
rrange.end -= Templ_UDP;
fprintf(fp,"U:");
range.begin = Templ_SCTP;
} else if (Templ_Oproto_first <= rrange.begin && rrange.begin <= Templ_Oproto_last) {
rrange.begin -= Templ_Oproto_first;
rrange.end -= Templ_Oproto_first;
fprintf(fp, "O:");
range.begin = Templ_Oproto_first;
} else
range.begin = Templ_UDP;
rrange.end = min(rrange.end, 65535);
Expand Down
7 changes: 7 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "crypto-base64.h" /* base64 encode/decode */
#include "pixie-backtrace.h"
#include "proto-sctp.h"
#include "proto-oproto.h" /* Other protocols on top of IP */
#include "vulncheck.h" /* checking vulns like monlist, poodle, heartblee */
#include "main-readrange.h"
#include "scripting.h"
Expand Down Expand Up @@ -829,6 +830,9 @@ receive_thread(void *v)
case FOUND_SCTP:
handle_sctp(out, secs, px, length, cookie, &parsed, entropy);
break;
case FOUND_OPROTO: /* other IP proto */
handle_oproto(out, secs, px, length, &parsed, entropy);
break;
case FOUND_TCP:
/* fall down to below */
break;
Expand Down Expand Up @@ -1141,6 +1145,7 @@ main_scan(struct Masscan *masscan)
* makes lookups faster at high packet rates.
*/
payloads_udp_trim(masscan->payloads.udp, &masscan->ports);
payloads_oproto_trim(masscan->payloads.oproto, &masscan->ports);

/* Optimize target selection so it's a quick binary search instead
* of walking large memory tables. When we scan the entire Internet
Expand Down Expand Up @@ -1204,6 +1209,7 @@ main_scan(struct Masscan *masscan)
parms->adapter_mac,
parms->router_mac,
masscan->payloads.udp,
masscan->payloads.oproto,
rawsock_datalink(masscan->nic[index].adapter),
masscan->seed);

Expand Down Expand Up @@ -1503,6 +1509,7 @@ int main(int argc, char *argv[])
masscan->shard.of = 1;
masscan->min_packet_size = 60;
masscan->payloads.udp = payloads_udp_create();
masscan->payloads.oproto = payloads_oproto_create();
strcpy_s( masscan->output.rotate.directory,
sizeof(masscan->output.rotate.directory),
".");
Expand Down
8 changes: 5 additions & 3 deletions src/masscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ struct Masscan

struct {
unsigned tcp:1;
unsigned udp:1;
unsigned udp:1; /* -sU */
unsigned sctp:1;
unsigned ping:1; /* --ping, ICMP echo */
unsigned arp:1; /* --arp, local ARP scan */
unsigned ping:1; /* --ping, ICMP echo */
unsigned arp:1; /* --arp, local ARP scan */
unsigned oproto:1; /* -sO */
} scan_type;

/**
Expand Down Expand Up @@ -369,6 +370,7 @@ struct Masscan
char *nmap_service_probes_filename;

struct PayloadsUDP *udp;
struct PayloadsUDP *oproto;
struct TcpCfgPayloads *tcp;
struct NmapServiceProbeList *probes;
} payloads;
Expand Down
24 changes: 15 additions & 9 deletions src/out-grepable.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
/****************************************************************************
****************************************************************************/
static unsigned
count_type(const struct RangeList *ports, int type)
count_type(const struct RangeList *ports, int start_type, int end_type)
{
unsigned min_port = type;
unsigned max_port = type + 65535;
unsigned min_port = start_type;
unsigned max_port = end_type;
unsigned i;
unsigned result = 0;

Expand Down Expand Up @@ -84,22 +84,28 @@ grepable_out_open(struct Output *out, FILE *fp)
fprintf(fp, "# Masscan " MASSCAN_VERSION " scan initiated %s\n",
timestamp);

count = count_type(&out->masscan->ports, Templ_TCP);
count = count_type(&out->masscan->ports, Templ_TCP, Templ_TCP_last);
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);
count = count_type(&out->masscan->ports, Templ_UDP, Templ_UDP_last);
fprintf(fp, ") UDP(%u;", count);
if (count)
print_port_list(&out->masscan->ports, Templ_UDP, fp);

count = count_type(&out->masscan->ports, Templ_SCTP);


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

fprintf(fp, ") PROTOCOLS(0;)\n");
count = count_type(&out->masscan->ports, Templ_Oproto_first, Templ_Oproto_last);
fprintf(fp, ") PROTOCOLS(%u;", count);
if (count)
print_port_list(&out->masscan->ports, Templ_Oproto_first, fp);

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

/****************************************************************************
Expand Down Expand Up @@ -145,7 +151,7 @@ grepable_out_status(struct Output *out, FILE *fp, time_t timestamp,
else if (ip_proto == 17)
service = udp_service_name(port);
else
service = "";
service = oproto_service_name(ip_proto);

fprintf(fp, "Timestamp: %lu", timestamp);

Expand Down
19 changes: 18 additions & 1 deletion src/out-tcp-services.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

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

static char *oproto_services[256];


const char *
Expand Down Expand Up @@ -82,3 +82,20 @@ udp_service_name(int port)
}
#endif
}

const char *
oproto_service_name(int port)
{
if (oproto_services[port])
return oproto_services[port];
{
struct protoent *result;

result = getprotobynumber(port);

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

return oproto_services[port] = strdup(result->p_name);
}
}
1 change: 1 addition & 0 deletions src/out-tcp-services.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

#endif

3 changes: 3 additions & 0 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,9 @@ output_report_status(struct Output *out, time_t timestamp, int status,
case 132:
out->counts.sctp.open++;
break;
default:
out->counts.oproto.open++;
break;
}
if (!out->is_show_open)
return;
Expand Down
4 changes: 4 additions & 0 deletions src/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ struct Output
struct {
uint64_t open;
} arp;
struct {
uint64_t open;
uint64_t closed;
} oproto;
} counts;

struct {
Expand Down
10 changes: 10 additions & 0 deletions src/proto-oproto.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "proto-oproto.h"

void
handle_oproto(struct Output *out, time_t timestamp,
const unsigned char *px, unsigned length,
struct PreprocessedInfo *parsed,
uint64_t entropy)
{

}
25 changes: 25 additions & 0 deletions src/proto-oproto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Other IP protocol (not TCP, UDP, TCP, ICMP
Specificaly for scanning things like GRE.
*/
#ifndef PROTO_OPROTO_H
#define PROTO_OPROTO_H
#include <stdint.h>
#include <time.h>
struct Output;
struct PreprocessedInfo;


/**
* Parse an incoming response.
* @param entropy
* The random seed, used in calculating syn-cookies.
*/
void
handle_oproto(struct Output *out, time_t timestamp,
const unsigned char *px, unsigned length,
struct PreprocessedInfo *parsed,
uint64_t entropy);

#endif

4 changes: 3 additions & 1 deletion src/proto-preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ preprocess_frame(const unsigned char *px, unsigned length, unsigned link_type,
case 6: goto parse_tcp;
case 17: goto parse_udp;
case 132: goto parse_sctp;
default: return 0; /* todo: should add more protocols, like ICMP */
default:
VERIFY_REMAINING(0, FOUND_OPROTO);
return 0; /* todo: should add more protocols, like ICMP */
}
}

Expand Down
1 change: 1 addition & 0 deletions src/proto-preprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum {
FOUND_LLC,
FOUND_ARP,
FOUND_SLL, /* Linux SLL */
FOUND_OPROTO, /* some other IP protocol */
};
struct PreprocessedInfo {
const unsigned char *mac_src;
Expand Down
9 changes: 8 additions & 1 deletion src/ranges.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,9 @@ rangelist_parse_ports(struct RangeList *ports, const char *string, unsigned *is_
case 'S': case 's':
proto_offset = Templ_SCTP;
break;
case 'O': case 'o':
proto_offset = Templ_Oproto_first;
break;
case 'I': case 'i':
proto_offset = Templ_ICMP_echo;
break;
Expand All @@ -805,7 +808,11 @@ rangelist_parse_ports(struct RangeList *ports, const char *string, unsigned *is_
end = (unsigned)strtoul(p, &p, 0);
}

if (port > 0xFFFF || end > 0xFFFF || end < port) {
if (port > 0xFF && proto_offset == Templ_Oproto_first) {
fprintf(stderr, "bad ports: %u-%u\n", port, end);
*is_error = 2;
return p;
} else if (port > 0xFFFF || end > 0xFFFF || end < port) {
fprintf(stderr, "bad ports: %u-%u\n", port, end);
*is_error = 2;
return p;
Expand Down
Loading

0 comments on commit ae4b908

Please sign in to comment.