Skip to content

Commit

Permalink
clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdavidgraham committed Apr 21, 2019
1 parent 09ab9c3 commit fd5e917
Show file tree
Hide file tree
Showing 33 changed files with 1,111 additions and 1,048 deletions.
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

# MASSCAN: Mass IP port scanner

This is the fastest Internet port scanner. It can scan the entire Internet
in under 6 minutes, transmitting 10 million packets per second.
This is an Internet-scale port scanner. It can scan the entire Internet
in under 6 minutes, transmitting 10 million packets per second,
from a single machine.

It produces results similar to `nmap`, the most famous port scanner.
Internally, it operates more like `scanrand`, `unicornscan`, and `ZMap`, using
asynchronous transmission. The major difference is that it's faster than these
other scanners. In addition, it's more flexible, allowing arbitrary address
ranges and port ranges.
It's input/output is similar to `nmap`, the most famous port scanner.
When in doubt, try one of those features.

NOTE: masscan uses a **custom TCP/IP stack**. Anything other than simple port
scans will cause conflict with the local TCP/IP stack. This means you need to
either use the `-S` option to use a separate IP address, or configure your
operating system to firewall the ports that masscan uses.
Internally, it uses asynchronous tranmissions, similar to port scanners
like `scanrand`, `unicornscan`, and `ZMap`. It's more flexible, allowing
arbitrary port and address ranges.

This tool is free, but consider funding it here:
NOTE: masscan uses a its own **custom TCP/IP stack**. Anything other than
simple port scans may cause conflict with the local TCP/IP stack. This means you
need to either the `--src-ip` option to run from a different IP address, or
use `--src-port` to configure which source ports masscan uses, then also
configure the internal firewall (like `pf` or `iptables`) to firewall those ports
from the rest of the operating system.

This tool is free, but consider contributing money to its developement:
Bitcoin wallet address: 1MASSCANaHUiyTtR3bJ2sLGuMw5kDBaj4T


Expand All @@ -40,13 +44,14 @@ by using the multi-threaded build:

While Linux is the primary target platform, the code runs well on many other
systems. Here's some additional build info:
* Windows w/ Visual Studio: use the VS10 project
* Windows w/ MingGW: just type `make`
* Windows w/ cygwin: won't work
* Mac OS X /w XCode: use the XCode4 project
* Mac OS X /w cmdline: just type `make`
* FreeBSD: type `gmake`
* other: I don't know, don't care

* Windows w/ Visual Studio: use the VS10 project
* Windows w/ MingGW: just type `make`
* Windows w/ cygwin: won't work
* Mac OS X /w XCode: use the XCode4 project
* Mac OS X /w cmdline: just type `make`
* FreeBSD: type `gmake`
* other: try just compiling all the files together


## PF_RING
Expand All @@ -55,17 +60,16 @@ To get beyond 2 million packets/second, you need an Intel 10-gbps Ethernet
adapter and a special driver known as ["PF_RING ZC" from ntop](http://www.ntop.org/products/packet-capture/pf_ring/pf_ring-zc-zero-copy/). Masscan doesn't need to be rebuilt in order to use PF_RING. To use PF_RING,
you need to build the following components:

* `libpfring.so` (installed in /usr/lib/libpfring.so)
* `pf_ring.ko` (their kernel driver)
* `ixgbe.ko` (their version of the Intel 10-gbps Ethernet driver)
* `libpfring.so` (installed in /usr/lib/libpfring.so)
* `pf_ring.ko` (their kernel driver)
* `ixgbe.ko` (their version of the Intel 10-gbps Ethernet driver)

You don't need to build their version of `libpcap.so`.

When Masscan detects that an adapter is named something like `zc:enp1s0` instead
of something like `enp1s0`, it'll automatically switch to PF_RING ZC mode.

## Regression testing

The project contains a built-in self-test:

$ make regress
Expand Down
2 changes: 1 addition & 1 deletion src/crypto-base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ base64_selftest(void)
char buf3[100];
size_t buf_len;
size_t buf2_len;
size_t buf3_len;
unsigned i;
unsigned seed = (unsigned)time(0);

Expand All @@ -201,6 +200,7 @@ base64_selftest(void)
*/
for (i=0; i<100; i++) {
unsigned j;
size_t buf3_len;

/* create a string of random bytes */
buf_len = r_rand(&seed) % 50;
Expand Down
2 changes: 1 addition & 1 deletion src/in-binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ parse_file(struct Output *out, const char *filename,

/* get the remainder fo the record */
bytes_read = fread(buf, 1, length, fp);
if (bytes_read < (int)length)
if (bytes_read < length)
break; /* eof */

/* Depending on record type, do something different */
Expand Down
2 changes: 1 addition & 1 deletion src/in-report.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ cndb_add(unsigned ip, const unsigned char *name, size_t name_length)
{
struct CNDB_Entry *entry;

if (name_length <= 0)
if (name_length == 0)
return;

if (db == NULL) {
Expand Down
68 changes: 35 additions & 33 deletions src/main-conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,15 @@ ranges_from_file(struct RangeList *ranges, const char *filename)
i = 1;
while (!feof(fp)) {
c = getc(fp);
if (c == EOF)
break;
line_number += (c == '\n');
if (isspace(c&0xFF) || c == ',') {
break;
}
if (i+1 >= sizeof(address)) {
LOG(0, "%s:%u:%u: bad address spec: \"%.*s\"\n",
filename, line_number, offset, i, address);
filename, line_number, offset, (int)i, address);
exit(1);
} else
address[i] = (char)c;
Expand All @@ -414,7 +416,7 @@ ranges_from_file(struct RangeList *ranges, const char *filename)
range = range_parse_ipv4(address, &offset, (unsigned)i);
if (range.begin == 0xFFFFFFFF && range.end == 0) {
LOG(0, "%s:%u:%u: bad range spec: \"%.*s\"\n",
filename, line_number, offset, i, address);
filename, line_number, offset, (int)i, address);
exit(1);
} else {
rangelist_add_range(ranges, range.begin, range.end);
Expand Down Expand Up @@ -1573,41 +1575,41 @@ struct ConfigParameter {
};
enum {F_NONE, F_BOOL};
struct ConfigParameter config_parameters[] = {
{"resume-index", SET_resume_index},
{"resume-count", SET_resume_count},
{"seed", SET_seed},
{"resume-index", SET_resume_index, 0, {0}},
{"resume-count", SET_resume_count, 0, {0}},
{"seed", SET_seed, 0, {0}},
{"arpscan", SET_arpscan, F_BOOL, {"arp",0}},
{"randomize-hosts", SET_randomize_hosts, F_BOOL},
{"rate", SET_rate, 0, {"max-rate",0}},
{"shard", SET_shard, 0, {"shards",0}},
{"randomize-hosts", SET_randomize_hosts, F_BOOL, {0}},
{"rate", SET_rate, 0, {"max-rate",0}},
{"shard", SET_shard, 0, {"shards",0}},
{"banners", SET_banners, F_BOOL, {"banner",0}},
{"nobanners", SET_nobanners, F_BOOL, {"nobanner",0}},
{"retries", SET_retries, 0, {"retry", "max-retries", "max-retry", 0}},
{"noreset", SET_noreset, F_BOOL},
{"nmap-payloads", SET_nmap_payloads, 0, {"nmap-payload",0}},
{"nmap-service-probes",SET_nmap_service_probes, 0, {"nmap-service-probe",0}},
{"pcap-filename", SET_pcap_filename, 0, {"pcap",0}},
{"pcap-payloads", SET_pcap_payloads, 0, {"pcap-payload",0}},
{"hello", SET_hello},
{"hello-file", SET_hello_file, 0, {"hello-filename",0}},
{"hello-string", SET_hello_string},
{"hello-timeout", SET_hello_timeout},
{"min-packet", SET_min_packet, 0, {"min-pkt",0}},
{"capture", SET_capture},
{"SPACE", SET_space},
{"output-filename", SET_output_filename, 0, {"output-file",0}},
{"output-format", SET_output_format},
{"output-show", SET_output_show, 0, {"output-status", "show",0}},
{"output-noshow", SET_output_noshow, 0, {"noshow",0}},
{"retries", SET_retries, 0, {"retry", "max-retries", "max-retry", 0}},
{"noreset", SET_noreset, F_BOOL, {0}},
{"nmap-payloads", SET_nmap_payloads, 0, {"nmap-payload",0}},
{"nmap-service-probes",SET_nmap_service_probes, 0, {"nmap-service-probe",0}},
{"pcap-filename", SET_pcap_filename, 0, {"pcap",0}},
{"pcap-payloads", SET_pcap_payloads, 0, {"pcap-payload",0}},
{"hello", SET_hello, 0, {0}},
{"hello-file", SET_hello_file, 0, {"hello-filename",0}},
{"hello-string", SET_hello_string, 0, {0}},
{"hello-timeout", SET_hello_timeout, 0, {0}},
{"min-packet", SET_min_packet, 0, {"min-pkt",0}},
{"capture", SET_capture, 0, {0}},
{"SPACE", SET_space, 0, {0}},
{"output-filename", SET_output_filename, 0, {"output-file",0}},
{"output-format", SET_output_format, 0, {0}},
{"output-show", SET_output_show, 0, {"output-status", "show",0}},
{"output-noshow", SET_output_noshow, 0, {"noshow",0}},
{"output-show-open",SET_output_show_open, F_BOOL, {"open", "open-only", 0}},
{"output-append", SET_output_append, 0, {"append-output",0}},
{"rotate", SET_rotate_time, 0, {"output-rotate", "rotate-output", "rotate-time", 0}},
{"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},
{"script", SET_script},
{"SPACE", SET_space},
{"output-append", SET_output_append, 0, {"append-output",0}},
{"rotate", SET_rotate_time, 0, {"output-rotate", "rotate-output", "rotate-time", 0}},
{"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, 0, {0}},
{"script", SET_script, 0, {0}},
{"SPACE", SET_space, 0, {0}},
{0}
};

Expand Down
8 changes: 4 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ transmit_thread(void *v) /*aka. scanning_thread() */
uint64_t start;
uint64_t end;
const struct Masscan *masscan = parms->masscan;
unsigned retries = masscan->retries;
unsigned rate = (unsigned)masscan->max_rate;
unsigned r = retries + 1;
uint64_t retries = masscan->retries;
uint64_t rate = masscan->max_rate;
unsigned r = (unsigned)retries + 1;
uint64_t range;
struct BlackRock blackrock;
uint64_t count_ips = rangelist_count(&masscan->targets);
Expand Down Expand Up @@ -426,7 +426,7 @@ transmit_thread(void *v) /*aka. scanning_thread() */
*/
if (r == 0) {
i += increment; /* <------ increment by 1 normally, more with shards/nics */
r = retries + 1;
r = (unsigned)retries + 1;
}

} /* end of batch */
Expand Down
3 changes: 0 additions & 3 deletions src/masscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ enum Operation {
* be "--interactive", meaning that we'll print to the command-line live as
* results come in. Only one output format can be specified, except that
* "--interactive" can be specified alongside any of the other ones.
* FIXME: eventually we'll support multiple file formats and "all"
* outputing simultaneously.
*/
enum OutputFormat {
Output_Default = 0x0000,
Expand Down Expand Up @@ -121,7 +119,6 @@ struct Masscan
* One or more network adapters that we'll use for scanning. Each adapter
* should have a separate set of IP source addresses, except in the case
* of PF_RING dnaX:Y adapters.
* FIXME: add support for link aggregation across adapters
*/
struct {
char ifname[256];
Expand Down
48 changes: 26 additions & 22 deletions src/out-redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ parse_state_machine(struct Output *out, const unsigned char *px, size_t length)
state = NUMBER;
break;
default:
LOG(0, "redis: unexpected data: %.*s\n", length-i, px+i);
LOG(0, "redis: unexpected data: %.*s\n", (int)(length-i), px+i);
exit(1);
break;
}
Expand All @@ -73,7 +73,7 @@ parse_state_machine(struct Output *out, const unsigned char *px, size_t length)
}
out->redis.outstanding--;
} else {
LOG(0, "redis: unexpected data: %.*s\n", length-i, px+i);
LOG(0, "redis: unexpected data: %.*s\n", (int)(length-i), px+i);
exit(1);
}
break;
Expand All @@ -89,7 +89,7 @@ parse_state_machine(struct Output *out, const unsigned char *px, size_t length)
return 1;
}
} else {
LOG(0, "redis: unexpected data: %.*s\n", length-i, px+i);
LOG(0, "redis: unexpected data: %.*s\n", (int)(length-i), px+i);
exit(1);
}
default:
Expand Down Expand Up @@ -200,17 +200,21 @@ redis_out_status(struct Output *out, FILE *fp, time_t timestamp,
{
ptrdiff_t fd = (ptrdiff_t)fp;
char line[1024];
int line_length;
char ip_string[16];
char port_string[10];
int ip_string_length;
int port_string_length;
size_t count;
char values[64];
int values_length;

sprintf_s(ip_string, sizeof(ip_string), "%u.%u.%u.%u",
ip_string_length = sprintf_s(ip_string, sizeof(ip_string), "%u.%u.%u.%u",
(unsigned char)(ip>>24),
(unsigned char)(ip>>16),
(unsigned char)(ip>> 8),
(unsigned char)(ip>> 0));
sprintf_s(port_string, sizeof(port_string), "%u/%s", port, name_from_ip_proto(ip_proto));
port_string_length = sprintf_s(port_string, sizeof(port_string), "%u/%s", port, name_from_ip_proto(ip_proto));

/**3
$3
Expand All @@ -228,11 +232,11 @@ myvalue
sprintf_s(line, sizeof(line),
"*3\r\n"
"$4\r\nSADD\r\n"
"$%u\r\n%s\r\n"
"$%u\r\n%s\r\n"
"$%d\r\n%s\r\n"
"$%d\r\n%s\r\n"
,
(unsigned)strlen("host"), "host",
(unsigned)strlen(ip_string), ip_string
4, "host",
ip_string_length, ip_string
);

count = send((SOCKET)fd, line, (int)strlen(line), 0);
Expand All @@ -249,11 +253,11 @@ myvalue
sprintf_s(line, sizeof(line),
"*3\r\n"
"$4\r\nSADD\r\n"
"$%u\r\n%s\r\n"
"$%u\r\n%s\r\n"
"$%d\r\n%s\r\n"
"$%d\r\n%s\r\n"
,
(unsigned)strlen(ip_string), ip_string,
(unsigned)strlen(port_string), port_string);
ip_string_length, ip_string,
port_string_length, port_string);

count = send((SOCKET)fd, line, (int)strlen(line), 0);
if (count != strlen(line)) {
Expand All @@ -267,21 +271,21 @@ myvalue
* KEY: ip:port
* VALUE: timestamp:status:reason:ttl
*/
sprintf_s(values, sizeof(values), "%u:%u:%u:%u",
values_length = sprintf_s(values, sizeof(values), "%u:%u:%u:%u",
(unsigned)timestamp, status, reason, ttl);
sprintf_s(line, sizeof(line),
line_length = sprintf_s(line, sizeof(line),
"*3\r\n"
"$4\r\nSADD\r\n"
"$%u\r\n%s:%s\r\n"
"$%u\r\n%s\r\n"
"$%d\r\n%s:%s\r\n"
"$%d\r\n%s\r\n"
,
(unsigned)(strlen(ip_string) + 1 + strlen(port_string)),
ip_string,port_string,
(unsigned)strlen(values), values
ip_string_length + 1 + port_string_length,
ip_string, port_string,
values_length, values
);

count = send((SOCKET)fd, line, (int)strlen(line), 0);
if (count != strlen(line)) {
count = send((SOCKET)fd, line, (int)line_length, 0);
if (count != line_length) {
LOG(0, "redis: error sending data\n");
exit(1);
}
Expand Down
3 changes: 1 addition & 2 deletions src/pixie-backtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void printStack( void )
for( i = 0; i < frames; i++ ) {
Dbg.SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );

printf( "%i: %s - 0x%0X\n", frames - i - 1, symbol->Name, symbol->Address );
printf( "%u: %s - 0x%0X\n", frames - i - 1, symbol->Name, symbol->Address );
}

free( symbol );
Expand All @@ -179,7 +179,6 @@ handle_segfault(int sig)
printf(" https://github.com/robertdavidgraham/masscan/issues\n");
printf("======================================================================");
exit(1);
return;
}


Expand Down
2 changes: 1 addition & 1 deletion src/pixie-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
int
pixie_fopen_shareable(FILE **in_fp, const char *filename, unsigned is_append)
{
FILE *fp = NULL;
FILE *fp;

*in_fp = NULL;

Expand Down
Loading

0 comments on commit fd5e917

Please sign in to comment.