Elevation of Privilege (EoP) using Vim

Elevation of Privilege (EoP) using Vim

January 4, 2024
4 min read
Table of Contents
index

Elevation of Privilege (EoP) in Linux

Privilege escalation, also known as Elevation of Privilege (EoP), is when a user gains privileges they are not authorized to have. On Linux this usually means a regular user obtaining root, which allows them to change system configuration, read any data, or hide their tracks. EoP can come from software vulnerabilities, but very often it comes from configuration mistakes.

This lab demonstrates the second kind: a sudo misconfiguration. No vulnerability in Vim is involved. Vim works exactly as designed, and that is the problem.

Lab Setup

The target is a deliberately vulnerable lab machine (a Docker container in Azure), accessed over SSH from Kali Linux as the unprivileged user HNUser. The sudoers configuration allows this user to run Vim as root.

Demonstration

1. Enumerate sudo rights

The first step after getting a shell is checking what the user may run with sudo:

Terminal window
sudo -l
sudo -l showing that HNUser may run /usr/bin/vim as root

Figure 1. HNUser may run /usr/bin/vim as any user, including root.

2. Start Vim with sudo and spawn a shell

Terminal window
sudo vim

Inside Vim, in normal mode, run a shell command:

:!/bin/bash
Vim started with sudo, with :!/bin/bash typed on the command line

Figure 2. Launching a shell from Vim’s command line.

3. Result

The shell inherits Vim’s privileges, so the user is now root:

id output showing uid=0(root) after escaping from Vim

Figure 3. id confirms uid=0(root).

Why It Works

sudo vim starts the Vim process as root. Vim can run external commands (:!cmd), open a shell (:shell), and read or write any file. Any child process it starts runs as root as well. Granting sudo on an interactive program like this is effectively granting full root access.

Vim is far from the only such binary. Editors, pagers (less, more), interpreters (python, perl), find, tar, awk and many others can spawn a shell or write arbitrary files. GTFOBins catalogues them, and it’s the first place an attacker checks after running sudo -l.

Mitigation

  1. Don’t grant sudo on interactive programs. If a user needs to edit a root-owned file, use sudoedit. It copies the file, opens the editor as the user, and writes the result back as root, so the editor never runs privileged:

    /etc/sudoers.d/webadmins
    %webadmins ALL=(root) sudoedit /etc/nginx/nginx.conf
  2. Use NOEXEC where an exec-capable binary is unavoidable. It blocks the program from starting child processes on most systems. Treat it as defence in depth, not a fix, since it can be bypassed by binaries that can write files.

  3. Least privilege in sudoers. Allow specific commands with fixed arguments instead of whole programs, avoid wildcards, and review sudoers.d regularly. sudo -l -U <user> shows what an account can actually do.

  4. Log and monitor sudo usage. sudo logs every invocation to the auth log/journal. Forwarding those logs to central monitoring makes an unexpected sudo vim followed by a root shell easy to spot.

Note

Related, but different: CVE-2019-12735. Vim and Neovim versions before 8.1.1365 / 0.3.6 could execute arbitrary commands when merely opening a crafted file, through modelines. That is a real software vulnerability, fixed by patching (and mitigated by set nomodeline). It’s not what this lab demonstrates: the Vim on the target is 8.1.2269, already patched, and the escalation works on any version because it relies on a sudo rule, not a bug.

Conclusion

The lab escalated from an unprivileged user to root with two commands, without exploiting any vulnerability. A single overly permissive sudoers line was enough. The takeaway for administrators: check every sudo rule against GTFOBins, and give editing rights through sudoedit, not through the editor itself. This technique should only be used on systems you are authorized to test.