Skip to content

Commit

Permalink
Set MSS to default Linux MSS in synscan modules (zmap#673)
Browse files Browse the repository at this point in the history
* Set MSS to default Linux MSS in synscan modules

For both synscan and synackscan, set MSS to 1450. This is the default
Linux MSS, and the option is required to be set for certain hosts to
respond.

Fixes zmap#601

* set_tcp_options to set_mss_option
  • Loading branch information
dadrian authored Aug 2, 2021
1 parent 967c589 commit 6819c65
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/probe_modules/module_tcp_synackscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#include "packet.h"
#include "module_tcp_synscan.h"

#define ZMAP_TCP_SYNACKSCAN_PACKET_LEN 54
#define ZMAP_TCP_SYNACKSCAN_TCP_HEADER_LEN 24
#define ZMAP_TCP_SYNACKSCAN_PACKET_LEN 58

probe_module_t module_tcp_synackscan;
static uint32_t num_ports;
Expand All @@ -40,10 +41,11 @@ static int synackscan_init_perthread(void *buf, macaddr_t *src, macaddr_t *gw,
struct ether_header *eth_header = (struct ether_header *)buf;
make_eth_header(eth_header, src, gw);
struct ip *ip_header = (struct ip *)(&eth_header[1]);
uint16_t len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
uint16_t len = htons(sizeof(struct ip) + ZMAP_TCP_SYNACKSCAN_TCP_HEADER_LEN);
make_ip_header(ip_header, IPPROTO_TCP, len);
struct tcphdr *tcp_header = (struct tcphdr *)(&ip_header[1]);
make_tcp_header(tcp_header, dst_port, TH_SYN | TH_ACK);
set_mss_option(tcp_header);
return EXIT_SUCCESS;
}

Expand All @@ -69,7 +71,7 @@ static int synackscan_make_packet(void *buf, UNUSED size_t *buf_len,
tcp_header->th_ack = tcp_ack;
tcp_header->th_sum = 0;
tcp_header->th_sum =
tcp_checksum(sizeof(struct tcphdr), ip_header->ip_src.s_addr,
tcp_checksum(ZMAP_TCP_SYNACKSCAN_TCP_HEADER_LEN, ip_header->ip_src.s_addr,
ip_header->ip_dst.s_addr, tcp_header);

ip_header->ip_sum = 0;
Expand Down
8 changes: 5 additions & 3 deletions src/probe_modules/module_tcp_synscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#include "probe_modules.h"
#include "packet.h"

#define ZMAP_TCP_SYNSCAN_PACKET_LEN 54
#define ZMAP_TCP_SYNSCAN_TCP_HEADER_LEN 24
#define ZMAP_TCP_SYNSCAN_PACKET_LEN 58

probe_module_t module_tcp_synscan;
static uint32_t num_ports;
Expand All @@ -39,10 +40,11 @@ static int synscan_init_perthread(void *buf, macaddr_t *src, macaddr_t *gw,
struct ether_header *eth_header = (struct ether_header *)buf;
make_eth_header(eth_header, src, gw);
struct ip *ip_header = (struct ip *)(&eth_header[1]);
uint16_t len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
uint16_t len = htons(sizeof(struct ip) + ZMAP_TCP_SYNSCAN_TCP_HEADER_LEN);
make_ip_header(ip_header, IPPROTO_TCP, len);
struct tcphdr *tcp_header = (struct tcphdr *)(&ip_header[1]);
make_tcp_header(tcp_header, dst_port, TH_SYN);
set_mss_option(tcp_header);
return EXIT_SUCCESS;
}

Expand All @@ -65,7 +67,7 @@ static int synscan_make_packet(void *buf, size_t *buf_len,
tcp_header->th_seq = tcp_seq;
tcp_header->th_sum = 0;
tcp_header->th_sum =
tcp_checksum(sizeof(struct tcphdr), ip_header->ip_src.s_addr,
tcp_checksum(ZMAP_TCP_SYNSCAN_TCP_HEADER_LEN, ip_header->ip_src.s_addr,
ip_header->ip_dst.s_addr, tcp_header);

ip_header->ip_sum = 0;
Expand Down
18 changes: 18 additions & 0 deletions src/probe_modules/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ void make_tcp_header(struct tcphdr *tcp_header, port_h_t dest_port,
tcp_header->th_dport = htons(dest_port);
}

size_t set_mss_option(struct tcphdr *tcp_header) {
// This only sets MSS, which is a single-word option.
size_t header_size = tcp_header->th_off * 4;
uint8_t *base = (uint8_t *) tcp_header;
uint8_t *last_opt = (uint8_t*) base + header_size;

// TCP Option "header"
last_opt[0] = 2; // MSS
last_opt[1] = 4; // MSS is 4 bytes long

// Default Linux MSS is 1460, which 0x05b4
last_opt[2] = 0x05;
last_opt[3] = 0xb4;

tcp_header->th_off += 1;
return tcp_header->th_off*4;
}

void make_udp_header(struct udphdr *udp_header, port_h_t dest_port,
uint16_t len)
{
Expand Down
1 change: 1 addition & 0 deletions src/probe_modules/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void make_eth_header(struct ether_header *ethh, macaddr_t *src, macaddr_t *dst);

void make_ip_header(struct ip *iph, uint8_t, uint16_t);
void make_tcp_header(struct tcphdr *, port_h_t, uint16_t);
size_t set_mss_option(struct tcphdr *tcp_header);
void make_icmp_header(struct icmp *);
void make_udp_header(struct udphdr *udp_header, port_h_t dest_port,
uint16_t len);
Expand Down

0 comments on commit 6819c65

Please sign in to comment.