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