text
stringlengths
0
1.99k
a valid account on the system to exploit it. It is triggered in a very
specific situation: when an FTP control connection is closed while there is
an FTP data transfer taking place on another TCP port. This data transfer
could be a directory listing, a file download, an upload, or anything else
that depends on an active FTP data connection.
However, since we want to have great control over the payload, the only
option I could see was using the FTP upload functionality. This brings us
more limitations, since we need write permissions on a directory in the
remote server to issue the FTP STOR or STOU commands. Maybe if you find a
file with write permissions, you could try using the APPE command to append
data to it, but then you'd need to calculate the offset to your shellcode
and RET into it after the initial ROP phase. We'll get into this later.
Another problem is that chroot should not be enabled on the target,
otherwise we won't be able to download the /proc/self/maps file required to
understand the server's memory layout. We'll see more on this in chapter 4.
Initially this would not be necessary, but since the target might have ASLR
and NX enabled, it becomes mandatory to exploit it successfully. I created
another version of this exploit that does not depend on mod_copy or maps
download, but the downside is that it needs to brute-force the memory
addresses and offsets, which is a bad idea because an IPS could block you.
The compiler flags play an important role here too. Using gcc, adding flags
like -O1 or -O2 completely change the memory layout. When I compiled with
clang, I noticed that some variables migrated from the heap to another
unnamed area. Some default gcc compilation parameters may vary from OS to
OS. It also depends on the kernel, because ASLR has a huge impact here.
Summarizing:
- you must have an account on the FTP target server;
- the account must have write permissions to a directory;
- the kernel and processor protections plays a huge impact on the
memory layout of your target (ASLR, NX, etc.);
- the compiler flags and compilation options can also impact the
memory layout;
- mod_copy must have been enabled (not mandatory for old systems
without ASLR and NX bit).
These limitations probably makes the vulnerability less attractive to some
people. If you have a valid account you could SSH into the server, why
bother exploiting a FTP service?
Well, I might have some reasons for you to give a try:
- your target has no SSH service running (duh);
- your company uses a vulnerable version of ProFTPd and you want to
prove your point on upgrading it;
- This bug turned out to be hard to exploit, so it's a good opportunity
for learning;
- grab root or other users cryptogram password for cracking - this is
something interesting I noticed in memory due to how libc getspnam()
function works, let's see on chapter 6.
All the research related to this exploit was done based on Ubuntu 20.04.2
and libc 2.31.1. This is important for the offsets that we'll be using.
However, some months before I finished this article, I updated my machine
to the PopOS distro. Later on, I went back to Ubuntu 22.04.1, so the
offsets of the final exploit and also the ROP functions will be different
(I took too long to finish and publish this article, excuse me for that).
----[ 3.1 - Notes on ProFTPd compilation
During my analysis I noticed that the vulnerability can also be triggered
after timeout is reached. But the default value is too long (1 hour) so you
can shorten it by adding the following compilation flags (but not required
for the exploitation):
--enable-timeout-idle=60 \
--enable-timeout-no-transfer=90 \
--enable-timeout-stalled=120
Also, as we'll see, we'll need mod_copy module. I prefer to build ProFTPd
with this module builtin instead of loading it via DSO.
Finally, we need to include debug flag "-g" so we can have debug symbols
in gdb:
$ cd proftpd-1.3.7rc2/
$ CFLAGS="-g" CXXFLAGS="-g" LDFLAGS="-g" ./configure --prefix=/usr/local \
--with-modules=mod_copy
$ CFLAGS="-g" CXXFLAGS="-g" LDFLAGS="-g" make -j4
You may see "Program received signal SIGALRM, Alarm clock" more often if
you decreased the timeouts, but that's harmless. However, this signal is
enough to kill your shell when you get RCE! So don't do it :) or use
`trap '' ALRM` as soon as you get a shell.
The default compiler used is gcc. It's important to keep it due to the
behavior I mentioned earlier.
--[ 4 - Analysis of ProFTPd internals
ProFTPd allocates a buffer for each command sent to the FTP control port.
The commands are processed in "categories". There's a function called
pr_cmd_dispatch_phase() which performs some logging and prepares the
commands to be dispatched into the PRE_CMD, CMD, and POST_CMD phases.
These phases are preconfigured in static table arrays, and each command
has its own configuration.