QNAP Pre-Auth Root RCE Affecting ~312K Devices on the Internet

Henry Huang
May 18 · 3 min read

In 2019, I discovered multiple vulnerabilities in QNAP PhotoStation and CGI programs. These vulnerabilities can be chained into a pre-auth root RCE. All QNAP NAS models are vulnerable, and there are ~312K vulnerable QNAS NAS instances on the Internet (statistical prediction). These vulnerabilities have been responsibly reported, fixed and assigned CVE-2019–7192 (CVSS 9.8), CVE-2019–7193 (CVSS 9.8), CVE-2019–7194 (CVSS 9.8), CVE-2019–7195 (CVSS 9.8). This article is the first public disclosure, but only 3 of the vulnerabilities are disclosed, because they’re enough to achieve pre-auth root RCE.

Impact

Vulnerable Instances

The following Shodan search reveals 564K QNAP instances on the Internet. Among those, 590 of 1065 randomly chosen instances have Photo Station enabled. (checked via GET /photo/slideshow.php and see if it responds with Invalid album selection) Therefore, statistically speaking, with 95% confidence level, confidence interval 3, there should be ~312K instances having Photo Station enabled, and they were all vulnerable at the time (2019).

564K QNAP Instances Found on Shodan

Affected Photo Station Versions

All downloadable versions before the fixed ones (6.0.3, 5.2.11, 5.4.9) were affected.

Visit QNAP’s Security Advisory for details like version info.

Redaction

This article has been greatly redacted as requested by QNAP PSIRT to give more users extended lead time to get patched. Together we build a safer world.


Now, let’s look at the 3 vulnerabilities that will later be chained to make a pre-auth root RCE.

Vulnerability 1: Pre-Auth Local File Disclosure (Effectively a Login Bypass)

[Redacted]

Upgrading the Pre-Auth Local File Disclosure to Privilege Escalation (Login Bypass)

We can use this pre-auth local file disclosure to read a magic file that contains a login token, which we can use to authenticate as a valid builtin user appuser.

Magic file [redacted]:

[redacted]
  • the file content won’t change after factory reset
  • the file is generated when [redacted] succeeds for the first time
  • [redacted] is encrypted
  • PhotoStation caches a plaintext version of [redacted] in [redacted]
[redacted]

Therefore, we can use vulnerability 1 to read the cached plaintext token to bypass the login and authenticate as appuser:

[Redacted: picture of authentication bypass]

With this trick, vulnerability 1 is actually an authentication bypass.

Quick recap:

  • [redacted]
  • [redacted]
  • [redacted]

Vulnerability 2: Authenticated Session Tampering — Writing PHP Code to Session

Being authenticated as appuser gives us access to the SMTP setting, which has an improper filtering in the email string. An authenticated attacker can [redacted], and this can be chained in the next vulnerability, or other file inclusion vulnerabilities (e.g. [redacted]).

POC: Authenticated Session Tampering

[Redacted picture of session tampering]

Vulnerability 3: (Pre-Auth) Writing Session to Arbitrary Location

This section is redacted due to the request of QNAP PSIRT.

This vulnerability enables an unauthenticated attacker to write session contents ([redacted]) to arbitrary location on the server.

Vulnerable code:

[Redacted]

POC: Writing Session to Arbitrary Location

[Redacted]

Chaining for Pre-Auth Root RCE

  • Use vulnerability 1 to bypass authentication and authenticate as appuser
  • Use vulnerability 2 to put [redacted] code (via SMTP email) in [redacted] session ([redacted])
  • Use vulnerability 3 to write the polluted [redacted] session to Photo Station’s web directory to make a webshell

[redacted]

Disclosure

  • 2019/06/14: reported technical details to QNAP
  • 2019/12/16: vendor fixed all 4 vulnerabilities, offered to provide a bounty (the amount is concealed due to the bounty terms)
  • 2019/12/31: got bounty
  • 2020/05/19: public disclosure
  • 2020/06/09: details of vulnerability 1 is redacted due to vendor’s request
  • 2020/06/10: details of vulnerability 2 & 2 are redacted due to vendor’s request
  • 2020/06/19: more redaction due to vendor’s request

Conclusion

3 vulnerabilities are chained to get this pre-auth root RCE in QNAP PhotoStation, and it works on all QNAP’s NAS models. Several tricks for exploiting QNAP products are also disclosed. Hopefully QNAP fixes these tricks some day, otherwise I’m pretty sure there will be more high-CVSS CVEs coming up.

Key Takeaways:

  • UPGRADE YOUR QNAP NAS NOW, if you haven’t already
  • [redacted]
  • There is a way to decrypt [redacted], but I’ll leave it as homework for you
  • QNAP’s webserver runs as [redacted]
  • [redacted] might give you some more 0days

Vendor Advisory

https://www.qnap.com/zh-tw/security-advisory/nas-201911-25

InfoSec Write-ups

A collection of write-ups from the best hackers in the…

Henry Huang

Written by

Into programming, bug hunting

InfoSec Write-ups

A collection of write-ups from the best hackers in the world on topics ranging from bug bounties and CTFs to vulnhub machines, hardware challenges and real life encounters. In a nutshell, we are the largest InfoSec publication on Medium. Maintained by Hackrew

Henry Huang

Written by

Into programming, bug hunting

InfoSec Write-ups

A collection of write-ups from the best hackers in the world on topics ranging from bug bounties and CTFs to vulnhub machines, hardware challenges and real life encounters. In a nutshell, we are the largest InfoSec publication on Medium. Maintained by Hackrew

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store