Skip to content

Instantly share code, notes, and snippets.

@angad
Created June 26, 2011 19:17
Show Gist options
  • Save angad/1047880 to your computer and use it in GitHub Desktop.
Save angad/1047880 to your computer and use it in GitHub Desktop.
SYN Packet
uint16_t csum (uint16_t *addr, int len) {
//RFC 1071
register long sum = 0;
int count = len;
uint16_t temp;
while (count > 1) {
temp = htons(*addr++);
sum += temp;
count -= 2;
}
/* Add left-over byte, if any */
if(count > 0)
sum += *(unsigned char *)addr;
/* Fold 32-bit sum to 16 bits */
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
uint16_t checksum = ~sum;
return checksum;
}
void syn()
{
char src_ip[17];
char dst_ip[17];
short dst_port = 80;
short th_sport = 1234;
short tcp_flags = TH_SYN;
//Headers
struct ip *iph = (struct ip *) datagram;
struct tcpheader *tcph = (struct tcpheader *) (datagram + sizeof (struct ip));
struct sockaddr_in servaddr;
snprintf(src_ip,16,"%s", getLocalIP()); //src ip
snprintf(dst_ip,16,"%s","209.85.175.104"); //google's ip
printf("Source IP %s\nDestination IP %s\n", src_ip, dst_ip);
memset(datagram, 0, 4096); //clearing the buffer
int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, dst_ip, &servaddr.sin_addr);
int tcpheader_size = sizeof(struct tcpheader);
printf("TCP Header Size %d\n", tcpheader_size);
iph->ip_hl = 5; //header length 5
iph->ip_v = 4; //version 4
iph->ip_tos = 0; //type of service
iph->ip_len = sizeof(struct ip) + sizeof(struct tcpheader); //no data
iph->ip_id = htons(31337); //id
iph->ip_off = 0; //no fragmentation
iph->ip_ttl = 250; //time to live
iph->ip_p = IPPROTO_TCP; //6
iph->ip_sum = 0; //let kernel fill the checksum
printf("IP len %d\n", iph->ip_len);
inet_pton(AF_INET, src_ip, &(iph->ip_src)); //local device ip
iph->ip_dst.s_addr = servaddr.sin_addr.s_addr; //destination address
tcph->th_sport = htons(th_sport); //any port
tcph->th_dport = htons(dst_port); //destination port
tcph->th_seq = htonl(31337); //random
tcph->th_ack = htonl(0); //ACK not needed
tcph->th_x2 = 0; //
tcph->th_off = 0x50; //data offset
tcph->th_flags = tcp_flags; //SYN flag
tcph->th_win = htons(65535); //window size
tcph->th_sum = 0; //later
tcph->th_urp = 0; //no urgent pointer
struct pseudo_hdr *phdr = (struct pseudo_hdr *) (datagram + sizeof(struct ipheader) + sizeof(struct tcpheader));
memset(phdr, 0, sizeof(phdr));
phdr->src = iph->ip_src.s_addr;
phdr->dst = iph->ip_dst.s_addr;
phdr->mbz = 0;
phdr->proto = IPPROTO_TCP;
phdr->len = ntohs(0x14);
tcph->th_sum = htons(csum((unsigned short *)tcph, sizeof(struct pseudo_hdr)+ sizeof(struct tcpheader)));
int one = 1;
const int *val = &one;
if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
printf("Cannot set HDRINCL for port %d", th_sport);
if (sendto(s, datagram, iph->ip_len, 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
{
printf("Error in sending");
}
else printf("Sent\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment