Comprueba si tu servidor Linux ha sido hackeado

Os voy a explicar como comprobar si tu servidor Linux ha sido hackeado por unos maleantes y se han puesto a minar un par de Bitcoins.

Hackers GIF - Find & Share on GIPHY

Lo primero y lo mas lógico que puedes hacer en el sistema, que es comprobar quien hay conectado al servidor con el comando who.

comandoit@comandoit:~# who
hackercillo tty7 2020-08-04 04:22

Ahora veremos que terminales tienen abiertos los usuarios y desde donde.
El siguiente comando w es muy parecido al who pero te indica desde donde esta conectado, por lo cual es mas interesante.

comandoit@comandoit:~# w
16:05:00 up 7 min,  2 users,  load average: 9.52, 9.58, 9.59
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root    tty7     :0               15:12    1:09   1.26s  0.01s /bin/sh
hackercillo  tty4 :0        04:22    2:09   2.26s  0.5s /bin/sh

El comando last muestra el nombre de usuario, terminal, IP, tiempo conectado de sesión y el total de la sesión.

comandoit@comandoit:~# last
hackercillo   pts/0 XX.red-XX-XX-XX  Sun Jun 16 21:00   still logged in
root   pts/1        localhost          Tue May 20 12:59   still logged in
hackercillo   pts/0      XX.red-XX-XX-XX  Sun Jun 16 19:00 - 20:00  (01:00)
root   pts/1        localhost          Thu May 20 19:30 - 19:07  (00:23)

El comando history que te chivara de todos los comandos que se han ejecutado en el servidor con el usuario actual, para probar con un usuario deseado debéis iniciar sesión con el.

comandoit@comandoit:~# su hackercillo
hackercillo@comandoit:~$ history
    1  reboot
    2  reboot
    3  rm -r /etc

También puedes ver el historial de comandos sin necesidad de iniciar sesión con otro usuario, ya que tienes permisos de root ¿verdad?.

Michael Scott Wink GIF - Find & Share on GIPHY

Todos los comandos que se van ejecutando, se guardan en un fichero en el siguiente directorio del home del usuario /home/<usuario>/.bash_history.

comandoit@comandoit:~# less /home/hackercillo/.bash_history
reboot
reboot
rm -r /etc

Vamos a comprobar el trafico que estamos recibiendo en nuestro servidor.
Para ello debemos instalar iftop en nuestro sistema.

comandoit@comandoit:~# iftop -i <interfaz de red>

Con ello podemos ver cuanto se esta consumiendo y hacia donde va todo el trafico.
En este punto deberías contrastar la información obtenida con la aplicación web de IP Wizcase.
https://es.wizcase.com/tools/whats-my-ip/
Si ves IPs como Russia, China… preocúpate y pon solución.

I Can'T Handle It Oh No GIF - Find & Share on GIPHY

El comando netstat es muy útil para hacer la misma función que con iftop.

comandoit@comandoit:~# netstat -la
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address    Foreign Address    State
tcp     1       501 comandoit:47260  78.107.156.105     LISTEN

La diferencia que con netstat podemos ver que puertos tenemos usándose y podríamos ver si nos han colado un meterpreter por ejemplo..
Así que deberéis hacer lo mismo comprobar IPs y ver si los puertos que aparecen en el apartado Foreign Address son de confianza… o no.

Ahora vamos a ver los procesos si han sido infectados o hay alguno que no debería estar ahí..

Computer GIF - Find & Share on GIPHY

El comando top podemos ver los procesos que mas consumen y podemos ver si hay alguno rarito.

comandoit@comandoit:~# top
top - 20:45:02 up  1:04,  0 users,  load average: 0.52, 0.58, 0.59
Tasks:   7 total,   1 running,   6 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   8007.9 total,   1290.6 free,   6493.3 used,    224.0 buff/cache
MiB Swap:  24576.0 total,  24504.6 free,     71.4 used.   1384.0 avail Mem

PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 1 root      20   0    8896    244    212 S   0.0   0.0   0:00.04 init
 5 root      20   0    8900    168    124 S   0.0   0.0   0:00.00 init
 6 hackercillo+  20   0   18076   3108   3000 S   0.0   0.0   0:00.07 bash

El comando lsof  nos permite ver que hacen los procesos y que archivos ejecutan y sus procesos asociados.
Una vez que tengamos la PID del comando top sobre un proceso un poco raro lo examinaremos con lsof.

comandoit@comandoit:~# lsof -p 6
COMMAND PID      USER   FD   TYPE DEVICE    SIZE              NODE NAME
bash      6 hackercillo  cwd    DIR    0,2     512  4503599627574999 /home/hackercillo
bash      6 hackercillo  rtd    DIR    0,2     512 17169973579399535 /
bash      6 hackercillo  txt    REG    0,2 1183448  1970324837452053 /usr/bin/bash
bash      6 hackercillo  mem    REG    0,0                    474298 /usr/lib/x86_64-linux-gnu/libnss_files-2.31.so (path dev=0,2, inode=1407374884027578)
bash      6 hackercillo  mem    REG    0,0                    442398 /usr/lib/locale/C.UTF-8/LC_CTYPE (path dev=0,2, inode=1125899907285022)
bash      6 hackercillo  mem    REG    0,0                    442397 /usr/lib/locale/C.UTF-8/LC_COLLATE (path dev=0,2, inode=1125899907285021)
bash      6 hackercillo  mem    REG    0,0                    442418 /usr/lib/locale/locale-archive (path dev=0,2, inode=3940649674391602)
bash      6 hackercillo  mem    REG    0,0                    474003 /usr/lib/x86_64-linux-gnu/libc-2.31.so (path dev=0,2, inode=1407374884027283)
bash      6 hackercillo  mem    REG    0,0                    474042 /usr/lib/x86_64-linux-gnu/libdl-2.31.so (path dev=0,2, inode=1407374884027322)
bash      6 hackercillo  mem    REG    0,0                    442411 /usr/lib/locale/C.UTF-8/LC_NUMERIC (path dev=0,2, inode=1125899907285035)
bash      6 hackercillo  mem    REG    0,0                    442417 /usr/lib/locale/C.UTF-8/LC_TIME (path dev=0,2, inode=7881299348340785)
bash      6 hackercillo  mem    REG    0,0                    442409 /usr/lib/locale/C.UTF-8/LC_MONETARY (path dev=0,2, inode=1125899907285033)
bash      6 hackercillo  mem    REG    0,0                    474436 /usr/lib/x86_64-linux-gnu/libtinfo.so.6.2 (path dev=0,2, inode=1407374884027716)
bash      6 hackercillo  mem    REG    0,0                    473886 /usr/lib/x86_64-linux-gnu/ld-2.31.so (path dev=0,2, inode=1407374884027166)
bash      6 hackercillo  mem    REG    0,0                    442408 /usr/lib/locale/C.UTF-8/LC_MESSAGES/SYS_LC_MESSAGES (path dev=0,2, inode=1125899907285032)
bash      6 hackercillo  mem    REG    0,0                    442414 /usr/lib/locale/C.UTF-8/LC_PAPER (path dev=0,2, inode=1125899907285038)
bash      6 hackercillo  mem    REG    0,0                    442410 /usr/lib/locale/C.UTF-8/LC_NAME (path dev=0,2, inode=1125899907285034)
bash      6 hackercillo  mem    REG    0,0                    442396 /usr/lib/locale/C.UTF-8/LC_ADDRESS (path dev=0,2, inode=1125899907285020)
bash      6 hackercillo  mem    REG    0,0                    442415 /usr/lib/locale/C.UTF-8/LC_TELEPHONE (path dev=0,2, inode=1125899907285039)
bash      6 hackercillo  mem    REG    0,0                    442400 /usr/lib/locale/C.UTF-8/LC_MEASUREMENT (path dev=0,2, inode=1125899907285024)
bash      6 hackercillo  mem    REG    0,0                    473849 /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache (path dev=0,2, inode=1407374884027129)
bash      6 hackercillo  mem    REG    0,0                    442399 /usr/lib/locale/C.UTF-8/LC_IDENTIFICATION (path dev=0,2, inode=1125899907285023)
bash      6 hackercillo  mem    REG    0,0                    477461 /usr/bin/bash (path dev=0,2, inode=1970324837452053)
bash      6 hackercillo    0u   CHR    4,1          3659174697740776 /dev/tty1
bash      6 hackercillo  255u   CHR    4,1          3659174697740776 /dev/tty1
bash      6 hackercillo    2u   CHR    4,1          3659174697740776 /dev/tty1
bash      6 hackercillo    1u   CHR    4,1          3659174697740776 /dev/tty1

En este caso he seleccionado el PID del bash que esta usando el usuario hackercillo.

También podemos utilizar el comando ps -axu que nos mostrara que procesos están ejecutándose y nos indica quien y cuantos recursos utiliza.

comandoit@comandoit:~# ps -axu
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.5  0.0   8896   312 ?        Ssl  17:02   0:00 /init
root         5  0.0  0.0   8900   220 tty1     Ss   17:02   0:00 /init
hackercillo+     6  0.5  0.0  18076  3600 tty1     S 17:02   0:00 -bash
root        19  0.1  0.0  19168  2856 tty1     S    17:02   0:00 sudo su
root        20  0.7  0.0  18016  2024 tty1     S    17:03   0:00 su
root        21  0.3  0.0  17008  2392 tty1     S    17:03   0:00 bash
root        28  0.0  0.0  18648  1888 tty1     R    17:03   0:00 ps -axu

Vamos a buscar Rootkits en el sistema, ya que es una de las amenazas mas común y peligrosas. La gran mayoría de las veces para eliminarlo correctamente nos tocaría reinstalar el sistema operativo y no estas por la labor ¿verdad?

The Office No GIF - Find & Share on GIPHY

Debemos instalar el paquete chkrootkit en nuestro sistema.

comandoit@comandoit:~# chkrootkit
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu libbinutils libctf-nobfd0 libctf0
Suggested packages:
  binutils-doc
The following NEW packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu chkrootkit libbinutils libctf-nobfd0 libctf0
0 upgraded, 7 newly installed, 0 to remove and 4 not upgraded.
Need to get 2707 kB of archives.
After this operation, 14.8 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Una vez instalado ejecutamos el comando chkrootkit.

comandoit@comandoit:~# chkrootkit
ROOTDIR is `/'
Checking `amd'...                                           not found
Checking `basename'...                                      not infected
Checking `biff'...                                          not found
Checking `chfn'...                                          not infected
Checking `chsh'...                                          not infected
Checking `cron'...                                          not infected
Checking `crontab'...                                       not infected
Checking `date'...                                          not infected
Checking `du'...                                            not infected
Checking `dirname'...                                       not infected
Checking `echo'...                                          not infected
Checking `egrep'...                                         not infected
Checking `env'...                                           not infected
Checking `find'...                                          not infected
Checking `fingerd'...                                       not found
Checking `gpm'...                                           not found
Checking `grep'...                                          not infected
Checking `hdparm'...                                        not infected
Checking `su'...                                            not infected
Checking `ifconfig'...                                      not infected
Checking `inetd'...                                         not infected
Checking `inetdconf'...                                     not found
Checking `identd'...                                        not found
Checking `init'...                                          not infected
Checking `killall'...                                       not infected

Como podéis ver, os hace un chequeo bastante bueno de vuestro sistema, buscando los rootkits mas conocidos.
Recordatorio no dejéis de examinar vuestros servidores continuamente y así evitar los ataques innecesarios.

Comando IT