Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/DDOS Scripts/AMP Methods/DAYTIME Amplification/dtscan.c
4607 views
1
#include <pcap.h>
2
#include <stdio.h>
3
#include <stdlib.h> // for exit()
4
#include <string.h> //for memset
5
#include <sys/ioctl.h>
6
#include <net/if.h>
7
#include <sys/socket.h>
8
#include <arpa/inet.h> // for inet_ntoa()
9
#include <net/ethernet.h>
10
#include <netinet/udp.h> //Provides declarations for udp header
11
#include <netinet/ip.h> //Provides declarations for ip header
12
#include <pthread.h>
13
#include <semaphore.h>
14
#include <signal.h>
15
#include <sys/resource.h>
16
#include <unistd.h>
17
18
void process_packet(void *args, struct pcap_pkthdr *header, void *buffer);
19
20
struct buffer
21
{
22
void *data;
23
int size;
24
struct buffer *next;
25
struct buffer *prev;
26
};
27
struct buffer *head;
28
29
char *ipv4;
30
int processed,over,total,i,j;
31
struct sockaddr_in dest;
32
pthread_mutex_t buf_mutex = PTHREAD_MUTEX_INITIALIZER;
33
sem_t loop_sem;
34
int running_threads = 0;
35
volatile int found_srvs = 0;
36
volatile unsigned long per_thread = 0;
37
volatile unsigned long start = 0;
38
volatile unsigned long scanned = 0;
39
int sleep_between = 0;
40
volatile int bytes_sent = 0;
41
volatile unsigned long hosts_done = 0;
42
FILE *fd;
43
44
void *readthread()
45
{
46
struct buffer *ourhead = head;
47
struct sockaddr_in saddr;
48
while(1)
49
{
50
sem_wait(&loop_sem);
51
while(ourhead->data == NULL){ ourhead = ourhead->next; }
52
pthread_mutex_lock(&buf_mutex);
53
void *buf = malloc(ourhead->size);
54
int size = ourhead->size;
55
memcpy(buf, ourhead->data, ourhead->size);
56
free(ourhead->data);
57
ourhead->data = NULL;
58
ourhead->size = 0;
59
pthread_mutex_unlock(&buf_mutex);
60
memset(&saddr, 0, sizeof(saddr));
61
struct iphdr *iph = (struct iphdr*)(buf + sizeof(struct ethhdr));
62
saddr.sin_addr.s_addr = iph->saddr;
63
struct udphdr *udph = (struct udphdr *)(buf + sizeof(struct ethhdr) + sizeof(struct iphdr));
64
if(ntohs(udph->source) == 13)
65
{
66
int body_length = size - sizeof(struct ethhdr) - sizeof(struct iphdr) - sizeof(struct udphdr);
67
fprintf(fd,"%s %d\n",inet_ntoa(saddr.sin_addr),body_length);
68
fflush(fd);
69
found_srvs++;
70
}
71
free(buf);
72
processed++;
73
ourhead = ourhead->next;
74
}
75
}
76
77
void *flood(void *par1)
78
{
79
running_threads++;
80
int thread_id = (int)par1;
81
unsigned long start_ip = htonl(ntohl(start)+(per_thread*thread_id));
82
unsigned long end = htonl(ntohl(start)+(per_thread*(thread_id+1)));
83
unsigned long w;
84
int y;
85
unsigned char buf[65536];
86
memcpy(buf, " ", 1);
87
int sizeofpayload = 1;
88
int sock;
89
if((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))<0) {
90
perror("cant open socket");
91
exit(-1);
92
}
93
for(w=ntohl(start_ip);w<htonl(end);w++)
94
{
95
struct sockaddr_in servaddr;
96
bzero(&servaddr, sizeof(servaddr));
97
servaddr.sin_family = AF_INET;
98
servaddr.sin_addr.s_addr=htonl(w);
99
servaddr.sin_port=htons(13);
100
sendto(sock,(char *)buf,sizeofpayload,0, (struct sockaddr *)&servaddr,sizeof(servaddr));
101
bytes_sent+=sizeofpayload;
102
scanned++;
103
hosts_done++;
104
usleep(sleep_between*1000);
105
}
106
close(sock);
107
running_threads--;
108
return;
109
}
110
111
void sighandler(int sig)
112
{
113
fclose(fd);
114
printf("\n");
115
exit(0);
116
}
117
118
void *printthread(void *argvs)
119
{
120
char **argv = (char **)argvs;
121
int threads = atoi(argv[4]);
122
pthread_t thread;
123
sleep(1);
124
char *str_start = malloc(18);
125
memset(str_start, 0, 18);
126
str_start = argv[1];
127
char *str_end = malloc(18);
128
memset(str_end, 0, 18);
129
str_end = argv[2];
130
start = inet_addr(str_start);
131
per_thread = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start))) / threads;
132
unsigned long toscan = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start)));
133
int i;
134
for(i = 0;i<threads;i++){
135
pthread_create( &thread, NULL, &flood, (void *) i);
136
}
137
sleep(1);
138
char *temp = (char *)malloc(17);
139
memset(temp, 0, 17);
140
sprintf(temp, "Found");
141
printf("%-16s", temp);
142
memset(temp, 0, 17);
143
sprintf(temp, "Host/s");
144
printf("%-16s", temp);
145
memset(temp, 0, 17);
146
sprintf(temp, "B/s");
147
printf("%-16s", temp);
148
memset(temp, 0, 17);
149
sprintf(temp, "Running Thrds");
150
printf("%-16s", temp);
151
memset(temp, 0, 17);
152
sprintf(temp, "Done");
153
printf("%s", temp);
154
printf("\n");
155
156
char *new;
157
new = (char *)malloc(16*6);
158
while (running_threads > 0)
159
{
160
printf("\r");
161
memset(new, '\0', 16*6);
162
sprintf(new, "%s|%-15lu", new, found_srvs);
163
sprintf(new, "%s|%-15d", new, scanned);
164
sprintf(new, "%s|%-15d", new, bytes_sent);
165
sprintf(new, "%s|%-15d", new, running_threads);
166
memset(temp, 0, 17);
167
int percent_done=((double)(hosts_done)/(double)(toscan))*100;
168
sprintf(temp, "%d%%", percent_done);
169
sprintf(new, "%s|%s", new, temp);
170
printf("%s", new);
171
fflush(stdout);
172
bytes_sent=0;
173
scanned = 0;
174
sleep(1);
175
}
176
printf("\n");
177
fclose(fd);
178
exit(0);
179
}
180
181
int main(int argc, char *argv[ ])
182
{
183
if(argc < 6){
184
fprintf(stderr, "Invalid parameters!\n");
185
fprintf(stdout, "Usage: %s <ip range start (192.168.0.0)> <ip range end (192.168.255.255)> <outfile> <threads> <scan delay in ms>\n", argv[0]);
186
exit(-1);
187
}
188
fd = fopen(argv[3], "a");
189
sleep_between = atoi(argv[5]);
190
int num_threads = atoi(argv[4]);
191
192
const rlim_t kOpenFD = 1024 + (num_threads * 2);
193
struct rlimit rl;
194
int result;
195
rl.rlim_cur = kOpenFD;
196
rl.rlim_max = kOpenFD;
197
result = setrlimit(RLIMIT_NOFILE, &rl);
198
if (result != 0)
199
{
200
perror("setrlimit_nofile");
201
fprintf(stderr, "setrlimit_nofile returned result = %d\n", result);
202
}
203
bzero(&rl, sizeof(struct rlimit));
204
rl.rlim_cur = 256 * 1024;
205
rl.rlim_max = 4096 * 1024;
206
result = setrlimit(RLIMIT_STACK, &rl);
207
if (result != 0)
208
{
209
perror("setrlimit_stack");
210
fprintf(stderr, "setrlimit_stack returned result = %d\n", result);
211
}
212
213
signal(SIGINT, &sighandler);
214
215
pcap_if_t *alldevsp;
216
pcap_t *handle; //Handle of the device that shall be sniffed
217
218
char errbuf[100] , *devname , devs[100][100];
219
int count = 1 , n;
220
221
if( pcap_findalldevs( &alldevsp , errbuf) )
222
{
223
exit(1);
224
}
225
226
devname = alldevsp->name;
227
ipv4 = malloc(16);
228
bzero(ipv4, 16);
229
struct ifreq ifc;
230
int res;
231
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
232
233
if(sockfd < 0) exit(-1);
234
strcpy(ifc.ifr_name, devname);
235
res = ioctl(sockfd, SIOCGIFADDR, &ifc);
236
close(sockfd);
237
if(res < 0) exit(-1);
238
strcpy(ipv4, inet_ntoa(((struct sockaddr_in*)&ifc.ifr_addr)->sin_addr));
239
printf("Opening device %s for sniffing ... " , devname);
240
handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);
241
242
if (handle == NULL)
243
{
244
fprintf(stderr, "Couldn't open device %s : %s\n" , devname , errbuf);
245
exit(1);
246
}
247
printf("Done\n");
248
249
sem_init(&loop_sem, 0, -1);
250
i = 1024*1000;
251
while(i--)
252
{
253
if(head == NULL)
254
{
255
head = (struct buffer *)malloc(sizeof(struct buffer));
256
bzero(head, sizeof(struct buffer));
257
head->data = NULL;
258
head->size = 0;
259
head->next = head;
260
head->prev = head;
261
} else {
262
struct buffer *new_node = (struct buffer *)malloc(sizeof(struct buffer));
263
bzero(new_node, sizeof(struct buffer));
264
new_node->data = NULL;
265
new_node->size = 0;
266
new_node->prev = head;
267
new_node->next = head->next;
268
head->next = new_node;
269
}
270
}
271
272
pthread_t prnthread;
273
pthread_create( &prnthread, NULL, &printthread, (void *)argv);
274
pthread_t redthread;
275
pthread_create( &redthread, NULL, &readthread, NULL);
276
277
pcap_loop(handle , -1 , process_packet , NULL);
278
279
return 0;
280
}
281
282
void process_packet(void *args, struct pcap_pkthdr *header, void *buffer)
283
{
284
int size = header->len;
285
286
//Get the IP Header part of this packet , excluding the ethernet header
287
struct iphdr *iph = (struct iphdr*)(buffer + sizeof(struct ethhdr));
288
memset(&dest, 0, sizeof(dest));
289
dest.sin_addr.s_addr = iph->daddr;
290
291
if(iph->protocol == 17 && strcmp(inet_ntoa(dest.sin_addr), ipv4) == 0)
292
{
293
//toss into buffer
294
if(head->data != NULL) over++;
295
pthread_mutex_lock(&buf_mutex);
296
void *temp = malloc(size);
297
memcpy(temp, buffer, size);
298
head->data = temp;
299
head->size = size;
300
head = head->next;
301
pthread_mutex_unlock(&buf_mutex);
302
sem_post(&loop_sem);
303
total++;
304
}
305
}
306