While investigating the NVRMini2, a network video recorder developed by NUUO, Tenable discovered a couple of vulnerabilities in the HTTP interface.
CVE-2018-1149: Unauthenticated Remote Stack Buffer Overflow
The HTTP interface exposes the binary cgi_system through the http://<target>/cgi-bin/cgi_system endpoint. Much of the functionality of cgi_system requires the remote user to be authenticated. One of the ways the system verifies authentication is by comparing the session identifier in the user's PHPSESSID cookie against the session file stored in /tmp/. In the following screenshot, you can see how the system builds up the filename of the tmp file.
The value returned from sub_534a4 is the session identifier string. The string has not been limited to any size. Similarly there is no bounds check when the string is passed into sprintf to build the tmp filename. As such a remote unauthenticated attacker can overflow the stack buffer passed into sprintf. A one line proof of concept looks like this:
curl -v --cookie "PHPSESSID=982e6c010064b3878a4b793bfab8d2d2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;" "http://192.168.1.85/cgi-bin/cgi_system?cmd=portCheck"
Furthermore, we found that this overflow is exploitable to achieve remote code execution. A couple of proof of concept exploits can be found on our Github repository. This is particularly devastating because not only is an attacker able to control the NVR but the credentials for all the cameras connected to the NVR are stored in plaintext on disk.
A common idiom in the NVRMini2's PHP code base is to first:
- Check that the current PHP session is valid.
- Verify the session has appropriate permissions for the page being accessed (ie. admin, poweruser, user, root, guest).
However, the following clause in check_session_is_valid() is quite curious:
if (file_exists(constant("MOSES_FILE"))) //back door
Note that we didn't insert the "back door" comment. It appears just like that in the code base. The constant("MOSES_FILE") turns out to be /tmp/moses. What does this logic allow an attacker to do? If the file /tmp/moses/ exists on the file system then an unauthenticated remote attacker can list all of the non-admin users and change their passwords:
[email protected]:~$ curl http://192.168.1.85/users_xml.php
[email protected]:~$ curl 'http://192.168.1.85/users_xml.php?cmd=changepwd&username=testuser&newpwd=pwned'
change password: testuser ok!
This affectively allows unauthenticated remote users to take over accounts.