In 2019, I discovered multiple vulnerabilities in QNAP Photo Station and CGI programs. These vulnerabilities can be chained into a pre-auth root RCE, which means an attacker could run code as root remotely without logging in. CyCraft was able to find this bug by giving its researchers 10% of their work time to bug hunting and bounties to keep their skills sharp and relevant.
All QNAP NAS models are vulnerable, and there are ~312K vulnerable QNAS NAS instances on the Internet (see statistical prediction below). These vulnerabilities have been responsibly reported, fixed, and assigned the following CVEs:
This article is the first public disclosure, focusing on only three of the vulnerabilities as they’re enough to achieve pre-auth root RCE.
The following Shodan search reveals 564,000 QNAP instances on the Internet. Among those, 590 of 1065 randomly chosen instances have Photo Station enabled. (Checked via GET /photo/slideshow.php responding with Invalid album selection) Statistically speaking, with 95% confidence and a 3% margin of error, there should be approx. 312K instances with Photo Station enabled, which were all vulnerable at the time of discovery (2019).
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 concerning version info and how to fix the vulnerabilities.
This article has been greatly redacted as requested by QNAP PSIRT to give more users extended lead time to get patched.
Now, let’s take a look at the 3 vulnerabilities that will later be chained to make a pre-auth root RCE.
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]
[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:
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]).
[Redacted picture of session tampering]
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]
[Redacted]
appuser
[redacted]
Three vulnerabilities are chained to get this pre-auth root RCE in QNAP Photo Station, and it works on all QNAP’s NAS models. Several tricks for exploiting QNAP products are also disclosed. Hopefully, QNAP fixes running the web server as root; otherwise, I’m pretty sure there will be more high-CVSS CVEs coming up.
QNAP probably needs to conduct a thorough security auditing of their code base and configuration, as their CGI programs and PHP code contain lots of instances of string concatenation without proper sanitization, which is a textbook example of injection that every hacker learns the first day they start hacking.
PHP is an excellent language full of features that make programming easier, but it can sometimes cause unexpected behavior of which many PHP users aren’t aware of, e.g. file stream wrappers, tsrm_realpath, etc. The latter part is what made PHP notorious for security issues: Convenience often contradicts security.
Our research team jokingly refer to their 10% bug hunting time as our “Bug Bounty Hunting Happy Hour.” Other vulnerabilities discovered during “happy hour” include:
Writer: Henry Huang, CyCraft Senior Security Researcher
CyCraft is a cybersecurity company founded in 2017, focusing on autonomous AI technology. Headquartered in Taiwan, it has subsidiaries in Japan and Singapore. CyCraft provides professional cybersecurity services to government agencies, police and defense forces, banks, and high-tech manufacturers throughout the Asia-Pacific region. It has received strong backing from the CID Group and Pavilion Capital, a Temasek Holdings Private Limited subsidiary.