/*************************************************************************** * This thread spews packets as fast as it can * * THIS IS WHERE ALL THE EXCITEMENT HAPPENS!!!! * 90% of CPU cycles are in the function. * ***************************************************************************/ static void transmit_thread(void *v) /*aka. scanning_thread() */ { struct ThreadPair *parms = (struct ThreadPair *)v; uint64_t i; 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 range; struct BlackRock blackrock; uint64_t count_ips = rangelist_count(&masscan->targets); struct Throttler *throttler = parms->throttler; struct TemplateSet pkt_template = templ_copy(parms->tmplset); unsigned *picker = parms->picker; struct Adapter *adapter = parms->adapter; uint64_t packets_sent = 0; unsigned increment = (masscan->shard.of-1) + masscan->nic_count; unsigned src_ip; unsigned src_ip_mask; unsigned src_port; unsigned src_port_mask; get_sources(masscan, parms->nic_index, &src_ip, &src_ip_mask, &src_port, &src_port_mask); LOG(1, "xmit: starting transmit thread #%u\n", parms->nic_index); /* Create the shuffler/randomizer. This creates the 'range' variable, * which is simply the number of IP addresses times the number of * ports */ range = rangelist_count(&masscan->targets) * rangelist_count(&masscan->ports); blackrock_init(&blackrock, range, masscan->seed); /* Calculate the 'start' and 'end' of a scan. One reason to do this is * to support --shard, so that multiple machines can co-operate on * the same scan. Another reason to do this is so that we can bleed * a little bit past the end when we have --retries. Yet another * thing to do here is deal with multiple network adapters, which * is essentially the same logic as shards. */ start = masscan->resume.index + (masscan->shard.one-1) + parms->nic_index; end = range; if (masscan->resume.count && end > start + masscan->resume.count) end = start + masscan->resume.count; end += retries * rate; /* "THROTTLER" rate-limits how fast we transmit, set with the * --max-rate parameter */ throttler_start(throttler, masscan->max_rate/masscan->nic_count); /* ----------------- * the main loop * -----------------*/ LOG(3, "xmit: starting main loop: [%llu..%llu]\n", start, end); for (i=start; i<end; ) { uint64_t batch_size; /* * Do a batch of many packets at a time. That because per-packet * throttling is expensive at 10-million pps, so we reduce the * per-packet cost by doing batches. At slower rates, the batch * size will always be one. (--max-rate) */ batch_size = throttler_next_batch(throttler, packets_sent); /* * Transmit packets from other thread, when doing --banners. This * takes priority over sending SYN packets. If there is so much * activity grabbing banners that we cannot transmit more SYN packets, * then "batch_size" will get decremented to zero, and we won't be * able to transmit SYN packets. */ flush_packets(adapter, parms->packet_buffers, parms->transmit_queue, &packets_sent, &batch_size); /* * Transmit a bunch of packets. At any rate slower than 100,000 * packets/second, the 'batch_size' is likely to be 1 */ while (batch_size && i < end) { uint64_t xXx; unsigned ip_them; unsigned port_them; unsigned ip_me; unsigned port_me; uint64_t cookie; /* * RANDOMIZE THE TARGET: * This is kinda a tricky bit that picks a random IP and port * number in order to scan. We monotonically increment the * index 'i' from [0..range]. We then shuffle (randomly transmog) * that index into some other, but unique/1-to-1, number in the * same range. That way we visit all targets, but in a random * order. Then, once we've shuffled the index, we "pick" the * the IP address and port that the index refers to. */ xXx = (i + (r--) * rate); while (xXx >= range) xXx -= range; xXx = blackrock_shuffle(&blackrock, xXx); ip_them = rangelist_pick2(&masscan->targets, xXx % count_ips, picker); port_them = rangelist_pick(&masscan->ports, xXx / count_ips); /* * SYN-COOKIE LOGIC */ ip_me = src_ip + (i & src_ip_mask); port_me = src_port + (xXx & src_port_mask); cookie = syn_cookie(ip_them, port_them, ip_me, port_me); /* * SEND THE PROBE * This is sorta the entire point of the program, but little * exciting happens here. The thing to note that this may * be a "raw" transmit that bypasses the kernel, meaning * we can call this function millions of times a second. */ rawsock_send_probe( adapter, ip_them, port_them, ip_me, port_me, (unsigned)cookie, !batch_size, /* flush queue on last packet in batch */ &pkt_template ); batch_size--; packets_sent++; foo_count++; /* TODO: debug thing, will be removed*/ /* * SEQUENTIALLY INCREMENT THROUGH THE RANGE * Yea, I know this is a puny 'i++' here, but it's a core feature * of the system that is linearly increments through the range, * but produces from that a shuffled sequence of targets (as * described above). Because we are linearly incrementing this * number, we can do lots of creative stuff, like doing clever * retransmits and sharding. */ if (r == 0) { i += increment; /* <------ increment by 1 normally, more with shards/nics */ r = retries + 1; } } /* end of batch */ /* If the user pressed <ctrl-c>, then we need to exit. but, in case * the user wants to --resume the scan later, we save the current * state in a file */ if (control_c_pressed) { break; } /* save our current location for resuming, if the user pressed * <ctrl-c> to exit early */ parms->my_index = i; } /* * We are done transmitting. However, response packets will take several * seconds to arrive. Therefore, sit in short loop waiting for those * packets to arrive. Pressing <ctrl-c> a second time will exit this * prematurely. */ while (!control_c_pressed_again) { unsigned k; 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 */ batch_size = throttler_next_batch(throttler, packets_sent); /* Transmit packets from the receive thread */ flush_packets( adapter, parms->packet_buffers, parms->transmit_queue, &packets_sent, &batch_size); pixie_usleep(1000); } } /* Thread is about to exit */ parms->done_transmitting = 1; LOG(1, "xmit: stopping transmit thread #%u\n", parms->nic_index); }
/*************************************************************************** * * Asynchronous receive thread * * The transmit and receive threads run independently of each other. There * is no record what was transmitted. Instead, the transmit thread sets a * "SYN-cookie" in transmitted packets, which the receive thread will then * use to match up requests with responses. ***************************************************************************/ static void receive_thread(void *v) { struct ThreadPair *parms = (struct ThreadPair *)v; const struct Masscan *masscan = parms->masscan; struct Output *out; struct DedupTable *dedup; struct PcapFile *pcapfile = NULL; struct TCP_ConnectionTable *tcpcon = 0; LOG(1, "recv: start receive 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) { unsigned cpu_count = pixie_cpu_get_count(); unsigned cpu = parms->nic_index * 2 + 1; while (cpu >= cpu_count) { cpu -= cpu_count; cpu++; } pixie_cpu_set_affinity(cpu); } /* * If configured, open a --pcap file for saving raw packets. This is * so that we can debug scans, but also so that we can look at the * strange things people send us. Note that we don't record transmitted * packets, just the packets we've received. */ /*if (masscan->pcap_filename[0]) pcapfile = pcapfile_openwrite(masscan->pcap_filename, 1);*/ /* * Open output. This is where results are reported when saving * the --output-format to the --output-filename */ out = output_create(masscan); /* * Create deduplication table. This is so when somebody sends us * multiple responses, we only record the first one. */ dedup = dedup_create(); /* * Create a TCP connection table for interacting with live * connections when doing --banners */ if (masscan->is_banners) { tcpcon = tcpcon_create_table( (size_t)((masscan->max_rate/5) / masscan->nic_count), parms->transmit_queue, parms->packet_buffers, &parms->tmplset->pkts[Proto_TCP], output_report_banner, out, masscan->tcb.timeout ); if (masscan->http_user_agent_length) tcpcon_set_parameter( tcpcon, "http-user-agent", masscan->http_user_agent_length, masscan->http_user_agent); } /* * In "offline" mode, we don't have any receive threads, so simply * wait until transmitter thread is done then go to the end */ if (masscan->is_offline) { while (!control_c_pressed_again) pixie_usleep(10000); parms->done_receiving = 1; goto end; } /* * Receive packets. This is where we catch any responses and print * them to the terminal. */ LOG(1, "begin receive thread\n"); while (!control_c_pressed_again) { int status; unsigned length; unsigned secs; unsigned usecs; const unsigned char *px; int err; unsigned x; struct PreprocessedInfo parsed; unsigned ip_me; unsigned port_me; unsigned ip_them; unsigned port_them; unsigned seqno_me; unsigned seqno_them; unsigned cookie; /* * RECIEVE * * This is the boring part of actually receiving a packet */ err = rawsock_recv_packet( parms->adapter, &length, &secs, &usecs, &px); if (err != 0) continue; /* * Do any TCP event timeouts based on the current timestamp from * the packet. For example, if the connection has been open for * around 10 seconds, we'll close the connection. (--banners) */ if (tcpcon) { tcpcon_timeouts(tcpcon, secs, usecs); } if (length > 1514) continue; /* * "Preprocess" the response packet. This means to go through and * figure out where the TCP/IP headers are and the locations of * some fields, like IP address and port numbers. */ x = preprocess_frame(px, length, 1, &parsed); if (!x) continue; /* corrupt packet */ ip_me = parsed.ip_dst[0]<<24 | parsed.ip_dst[1]<<16 | parsed.ip_dst[2]<< 8 | parsed.ip_dst[3]<<0; ip_them = parsed.ip_src[0]<<24 | parsed.ip_src[1]<<16 | parsed.ip_src[2]<< 8 | parsed.ip_src[3]<<0; port_me = parsed.port_dst; port_them = parsed.port_src; seqno_them = TCP_SEQNO(px, parsed.transport_offset); seqno_me = TCP_ACKNO(px, parsed.transport_offset); cookie = syn_cookie(ip_them, port_them, ip_me, port_me) & 0xFFFFFFFF; /* verify: my IP address */ if (!is_my_ip(&parms->src, ip_me)) continue; /* * Handle non-TCP protocols */ switch (parsed.found) { case FOUND_ARP: LOGip(2, ip_them, 0, "-> ARP [%u] \n", px[parsed.found_offset]); switch (px[parsed.found_offset + 6]<<8 | px[parsed.found_offset+7]) { case 1: /* request */ /* This function will transmit a "reply" to somebody's ARP request * for our IP address (as part of our user-mode TCP/IP). * Since we completely bypass the TCP/IP stack, we have to handle ARPs * ourself, or the router will lose track of us.*/ arp_response( ip_me, parms->adapter_mac, px, length, parms->packet_buffers, parms->transmit_queue); break; case 2: /* response */ /* This is for "arp scan" mode, where we are ARPing targets rather * than port scanning them */ /* If we aren't doing an ARP scan, then ignore ARP responses */ if (!masscan->is_arp) break; /* If this response isn't in our range, then ignore it */ if (!rangelist_is_contains(&masscan->targets, ip_them)) break; /* Ignore duplicates */ if (dedup_is_duplicate(dedup, ip_them, 0)) continue; /* ...everything good, so now report this response */ handle_arp(out, px, length, &parsed); break; } continue; case FOUND_UDP: case FOUND_DNS: if (!is_nic_port(masscan, port_me)) continue; if (parms->masscan->nmap.packet_trace) packet_trace(stdout, px, length, 0); handle_udp(out, px, length, &parsed); continue; case FOUND_ICMP: handle_icmp(out, px, length, &parsed); continue; case FOUND_TCP: /* fall down to below */ break; default: continue; } /* verify: my port number */ if (!is_my_port(&parms->src, port_me)) continue; if (parms->masscan->nmap.packet_trace) packet_trace(stdout, px, length, 0); /* Save raw packet in --pcap file */ if (pcapfile) { pcapfile_writeframe( pcapfile, px, length, length, secs, usecs); } { char buf[64]; LOGip(5, ip_them, port_them, "-> TCP ackno=0x%08x flags=0x%02x(%s)\n", seqno_me, TCP_FLAGS(px, parsed.transport_offset), reason_string(TCP_FLAGS(px, parsed.transport_offset), buf, sizeof(buf))); } /* If recording --banners, create a new "TCP Control Block (TCB)" */ if (tcpcon) { struct TCP_Control_Block *tcb; /* does a TCB already exist for this connection? */ tcb = tcpcon_lookup_tcb(tcpcon, ip_me, ip_them, port_me, port_them); if (TCP_IS_SYNACK(px, parsed.transport_offset)) { if (cookie != seqno_me - 1) { LOG(2, "%u.%u.%u.%u - bad cookie: ackno=0x%08x expected=0x%08x\n", (ip_them>>24)&0xff, (ip_them>>16)&0xff, (ip_them>>8)&0xff, (ip_them>>0)&0xff, seqno_me-1, cookie); continue; } if (tcb == NULL) { tcb = tcpcon_create_tcb(tcpcon, ip_me, ip_them, port_me, port_them, seqno_me, seqno_them+1); } tcpcon_handle(tcpcon, tcb, TCP_WHAT_SYNACK, 0, 0, secs, usecs, seqno_them+1); } else if (tcb) { /* If this is an ACK, then handle that first */ if (TCP_IS_ACK(px, parsed.transport_offset)) { tcpcon_handle(tcpcon, tcb, TCP_WHAT_ACK, 0, seqno_me, secs, usecs, seqno_them); } /* If this contains payload, handle that */ if (parsed.app_length) { tcpcon_handle(tcpcon, tcb, TCP_WHAT_DATA, px + parsed.app_offset, parsed.app_length, secs, usecs, seqno_them); } /* If this is a FIN, handle that. Note that ACK + * payload + FIN can come together */ if (TCP_IS_FIN(px, parsed.transport_offset) && !TCP_IS_RST(px, parsed.transport_offset)) { tcpcon_handle(tcpcon, tcb, TCP_WHAT_FIN, 0, 0, secs, usecs, seqno_them); } /* If this is a RST, then we'll be closing the connection */ if (TCP_IS_RST(px, parsed.transport_offset)) { tcpcon_handle(tcpcon, tcb, TCP_WHAT_RST, 0, 0, secs, usecs, seqno_them); } } else if (TCP_IS_FIN(px, parsed.transport_offset)) { /* * NO TCB! * This happens when we've sent a FIN, deleted our connection, * but the other side didn't get the packet. */ if (!TCP_IS_RST(px, parsed.transport_offset)) tcpcon_send_FIN( tcpcon, ip_me, ip_them, port_me, port_them, seqno_them, seqno_me); } }
/*************************************************************************** * This is where we handle all incoming ICMP packets. Some of these packets * will be due to scans we are doing, like pings (echoes). Some will * be inadvertent, such as "destination unreachable" messages. ***************************************************************************/ void handle_icmp(struct Output *out, time_t timestamp, const unsigned char *px, unsigned length, struct PreprocessedInfo *parsed) { unsigned type = parsed->port_src; unsigned code = parsed->port_dst; unsigned seqno_me; unsigned ip_me; unsigned ip_them; unsigned cookie; ip_me = parsed->ip_dst[0]<<24 | parsed->ip_dst[1]<<16 | parsed->ip_dst[2]<< 8 | parsed->ip_dst[3]<<0; ip_them = parsed->ip_src[0]<<24 | parsed->ip_src[1]<<16 | parsed->ip_src[2]<< 8 | parsed->ip_src[3]<<0; seqno_me = px[parsed->transport_offset+4]<<24 | px[parsed->transport_offset+5]<<16 | px[parsed->transport_offset+6]<<8 | px[parsed->transport_offset+7]<<0; switch (type) { case 0: /* ICMP echo reply */ cookie = (unsigned)syn_cookie(ip_them, Templ_ICMP_echo, ip_me, 0); if ((cookie & 0xFFFFFFFF) != seqno_me) return; /* not my response */ //if (syn_hash(ip_them, Templ_ICMP_echo) != seqno_me) // return; /* not my response */ /* * Report "open" or "existence" of host */ output_report_status( out, timestamp, PortStatus_Open, ip_them, 1, /* ip proto */ 0, 0, 0); break; case 3: /* destination unreachable */ switch (code) { case 0: /* net unreachable */ /* We get these a lot while port scanning, often a flood coming * back from broken/misconfigured networks */ break; case 1: /* host unreachable */ /* This means the router doesn't exist */ break; case 2: /* protocol unreachable */ /* The host exists, but it doesn't support SCTP */ break; case 3: /* port unreachable */ if (length - parsed->transport_offset > 8) { unsigned ip_me2, ip_them2, port_me2, port_them2; unsigned ip_proto; int err; err = parse_port_unreachable( px + parsed->transport_offset + 8, length - parsed->transport_offset + 8, &ip_me2, &ip_them2, &port_me2, &port_them2, &ip_proto); if (err) return; if (!matches_me(out, ip_me2, port_me2)) return; switch (ip_proto) { case 6: output_report_status( out, timestamp, PortStatus_Closed, ip_them2, ip_proto, port_them2, 0, px[parsed->ip_offset + 8]); break; case 17: output_report_status( out, timestamp, PortStatus_Closed, ip_them2, ip_proto, port_them2, 0, px[parsed->ip_offset + 8]); break; case 132: output_report_status( out, timestamp, PortStatus_Closed, ip_them2, ip_proto, port_them2, 0, px[parsed->ip_offset + 8]); break; } } } break; default: ; } }