SSD Advisory – TP-LINK NCXXX Authentication Bypass
An authentication bypass in TP-Link NCXXX cameras allows an unauthenticated attacker to read arbitrary files by manipulating the DOCUMENT_ROOT parameter, including a password file. This can be chained with a post-authentication stack overflow to potentially achieve remote code execution.
A vulnerability exists in TP-Link NCXXX family of devices, the vulnerability allows accessing the device without credentials – this chained with well known and currently unpatched post-auth vulnerabilities allow for the complete compromise of the device.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
The target is the TP-Link NCXXX family of IP cameras. The advisory lists the affected models as TP-Link NC200, NC210, NC220, NC230, NC250, NC260 and NC450. The devices are out of support, some since 2015 and others since 2017, and the vendor response states that patches are unlikely. The advisory was published to alert users of devices still reachable on the Internet that they should disconnect them before they are compromised.
The feature involved is the camera's embedded web server, implemented in a binary called ipcamera. The root URL (/) is handled by the httpLoginRpm method, which builds the path to login.html and serves it to the browser. The advisory describes two separate bugs: a logical authentication bypass that lets an unauthenticated user read arbitrary files, and a post-authentication stack overflow in the SD-card file deletion handler. Chained together, the advisory says they allow complete compromise of the device.
Setup
The advisory does not describe any accounts, roles or tooling beyond the proof-of-concept script. The first bug needs no credentials at all: it is reachable on the root URL before authentication. The second bug is only exploitable after authentication, and the advisory notes it can be reached by leveraging the first vulnerability. The proof-of-concept script is written for Python 3 and uses the requests library, and it targets a host on the local network.
Finding it
The researcher reviewed the ipcamera binary and looked at how the root URL handler, httpLoginRpm, constructs the path of the login page. The handler takes user input through the call httpGetEnv(param_1,"DOCUMENT_ROOT") before any authentication is performed, and then uses that value when generating the path for login.html:
snprintf(full_path, 0x40, "%s%s", uVar1, "login.html");
Here full_path is a buffer of at most 64 bytes (0x40 in hexadecimal), and uVar1 holds the value returned by httpGetEnv(param_1,"DOCUMENT_ROOT"), which the user can control. The problem is that the value is trusted before the user has authenticated, and that it is concatenated directly in front of the fixed login.html suffix.
Exploitation
Step 1: Manipulate DOCUMENT_ROOT to read an arbitrary file
By manipulating the input given to httpGetEnv, an attacker can change uVar1 so that it points to a different file path, effectively sidestepping the concatenation with login.html. The attacker can then specify a path to any file they wish to access, which results in a file disclosure. When the path construction is successfully manipulated, the designated file is opened and its contents are relayed back to the attacker, potentially exposing sensitive information stored on the system.
The advisory gives a simple proof of concept as a GET request:
http://[redacted]/?DOCUMENT_ROOT=////////////////////usr/local/config/ipcamera/.lighttpdpassword
The DOCUMENT_ROOT parameter carries a long run of forward slashes followed by the path /usr/local/config/ipcamera/.lighttpdpassword. The slashes pad the value so that the fixed login.html suffix no longer matters, and the path to the hidden file is the one that gets opened.
Step 2: Use the disclosed file to bypass authentication
To bypass authentication it is enough to use this vulnerability to read the hidden file /usr/local/config/ipcamera/.lighttpdpassword on the filesystem. The advisory is explicit that unauthenticated users should not be able to modify DOCUMENT_ROOT.
Step 3: Send the post-authentication request
The advisory's proof-of-concept script reads /usr/local/config/ipcamera/user.conf and sends a Cookie header:
#!/usr/bin/python3
import requests
host = "[redacted]"
filename = "/usr/local/config/ipcamera/user.conf"
url = "http://" + host + "/?DOCUMENT_ROOT=" + filename.rjust(0x40 - 1, "/")
headers = {"Cookie": "sess=ok"}
response = requests.get(url, headers=headers, timeout=1).text
print(f"{response=}")
The script builds the URL by right-justifying the target filename to 63 characters (0x40 - 1) with / as the fill character. It sends a Cookie header with the value sess=ok and prints the response body. The advisory does not show the output of this script.
Step 4: Overflow the stack through the SD-card delete handler
The second vulnerability targets the /sddelfile.fcgi endpoint and becomes exploitable after authentication. The function in the ipcamera binary is named httpSDDelFile, and the advisory says it can be reached by leveraging the first vulnerability. An attacker sends a POST request with an excessively long filepath parameter. The function swSdDelFile handles that parameter improperly, which leads to a stack overflow: it concatenates the attacker-provided filepath data byte by byte onto the stack without adequately checking the length of the input.
The vulnerable code snippet is:
undefined4 swSdDelFile(char *filepath)
{
...
attacker_path = filepath;
...
i = 0;
while (*attacker_path != '*' && *attacker_path != '\0')
{
strncat((char *)&stack_parameter, attacker_path, 1);
i++;
attacker_path++;
}
...
}
The loop copies one byte at a time from attacker_path into stack_parameter on the stack, stopping at a * character or at the end of the string. There is no check on the size of attacker_path, so a long value overflows stack_parameter. Because the binary has no security protections at all, the advisory states the overflow can be exploited. An attacker could use it to cause a denial of service by crashing the device, or potentially to execute arbitrary shellcode by manipulating the program's execution flow to jump to a location on the stack where the shellcode is written.
Root cause
The authentication bypass is a trust problem in the path construction. The handler for the root URL reads DOCUMENT_ROOT from user-controlled input through httpGetEnv before authentication, and then passes it as the first %s argument of snprintf in front of the literal login.html. Nothing constrains the value to a real document root, so the attacker controls the prefix of the path that is opened and returned. The advisory frames the fix expectation simply: unauthenticated users should not be able to modify DOCUMENT_ROOT.
The stack overflow is a bounds problem in swSdDelFile. The function walks the filepath string byte by byte and appends each byte with strncat((char *)&stack_parameter, attacker_path, 1), with no check on how much has already been written to stack_parameter. The advisory attributes exploitability to the absence of all security protections in the binary.
Impact
The first vulnerability lets an unauthenticated attacker read files on the device, including /usr/local/config/ipcamera/.lighttpdpassword, which is enough to bypass authentication. The second, once authenticated, lets an attacker crash the device for a denial of service or potentially execute arbitrary shellcode. The advisory's summary states that the chain allows complete compromise of the device. The affected devices are out of support, some since 2015 and others since 2017, and are unlikely to receive patches; the advisory recommends that users immediately disconnect devices that are still present on the Internet.
Fix
The advisory does not describe a patch. The vendor response states that the devices are out of support and unlikely to get any patches, and the publication is intended to alert users so they can disconnect the devices. The design change the advisory points to is that unauthenticated users should not be able to modify DOCUMENT_ROOT.
Variants to test
- Try other parameters read through
httpGetEnvin the same handler and see whether they are also trusted before authentication. A positive result is a request without credentials that returns a file other than the login page. - Try the
DOCUMENT_ROOTpadding trick against other paths on the filesystem, starting with configuration files under/usr/local/config/ipcamera/. A positive result is a response body containing configuration data rather than HTML. - Try the same padding trick on other endpoints that build paths from environment values. A positive result is any endpoint that serves a file outside its intended directory.
- Send a long
filepathvalue to/sddelfile.fcgiafter authenticating. A positive result is a crash or a controlled change in execution flow, which the advisory says is possible because the binary lacks security protections. - Test whether the
*terminator in theswSdDelFileloop can be used to truncate the copied data at a chosen point. A positive result is a request whose effect differs from the full string being copied.
What happened, in plain words
TP-Link made a family of small home security cameras, the NC series. Each camera runs a tiny web server inside it, so you can open a browser, type the camera's address on your home network, and get a login page. That little web server is a program called ipcamera that lives on the camera itself.
A security researcher looked inside that program and found two separate mistakes. The first mistake lets anyone who can reach the camera read files on it without logging in at all. One of those files is enough to get past the login when read. The second mistake is in the part of the program that deletes files from the camera's SD card. Once you are logged in, you can send it a very long file name and it will write past the end of a small area of memory, which can crash the camera or, in the worst case, let an attacker run their own code on it.
Put the two together and an attacker can take over the whole device. The cameras are no longer supported, some since 2015 and others since 2017, so the vendor says patches are unlikely. The advisory was published to warn people who still have these cameras connected to the Internet that they should unplug them before someone compromises them.
The product and the feature involved
An IP camera is a camera with its own small computer inside, connected to your network. It has a web interface so you can watch the video and change settings from a browser. That web interface is served by a web server, which is just a program that listens for requests from browsers and sends back pages.
On these cameras the web server is part of a single program called ipcamera. When your browser asks for the front page, the address it asks for is just /, which is called the root URL. The part of ipcamera that answers that request is a function named httpLoginRpm. Its job is to work out where the login page file, login.html, lives on the camera's storage and then send it back to you.
A function is a named block of code that does one job. When we say the root URL is "handled by" httpLoginRpm, we mean that when a request for / arrives, this is the code that runs.
The affected models
The advisory lists these cameras as affected:
- TP-Link NC200
- TP-Link NC210
- TP-Link NC220
- TP-Link NC230
- TP-Link NC250
- TP-Link NC260
- TP-Link NC450
Finding the first bug
The researcher read through the ipcamera program and looked at how httpLoginRpm builds the path to the login page. Before checking whether you are logged in, the code asks the web server for a setting called DOCUMENT_ROOT using a call that looks like this:
httpGetEnv(param_1,"DOCUMENT_ROOT")
httpGetEnv is a helper that looks up a value by name, a bit like asking someone "what is the value of DOCUMENT_ROOT?". DOCUMENT_ROOT is normally a setting that says which folder on disk holds the website's files. The problem is that on these cameras the value can be influenced by whoever is making the request, and it is read before any login check happens.
The code then glues that value together with the fixed file name login.html using this line:
snprintf(full_path, 0x40, "%s%s", uVar1, "login.html");
snprintf is a function that builds a piece of text from a template. The template here is "%s%s", which means "put one piece of text here, then another piece of text here". The two pieces are uVar1 (the value that came back from DOCUMENT_ROOT) and the literal text login.html. The result is written into a buffer called full_path, and 0x40 says that buffer can hold at most 64 bytes. 0x40 is just the hexadecimal way of writing 64. A buffer is a fixed-size area of memory set aside to hold data, like a small box with a lid; if you try to put more in than it holds, things go wrong.
So the code is doing: take whatever DOCUMENT_ROOT says, stick login.html on the end, and open that file. If an attacker can make DOCUMENT_ROOT say something else, they control the start of the path that gets opened.
Exploiting it: reading any file without logging in
By changing the value that goes into httpGetEnv, an attacker can make uVar1 point at a different file path. Because the code simply glues login.html onto the end, the attacker can arrange things so that the login.html part no longer matters, and the path they chose is the one that gets opened. The camera then opens that file and sends its contents back to whoever asked. This is called file disclosure: a file that should be private is handed out to someone who should not see it.
The advisory gives a simple proof of concept, a single GET request:
http://[redacted]/?DOCUMENT_ROOT=////////////////////usr/local/config/ipcamera/.lighttpdpassword
Line by line:
http://is the scheme, telling the browser this is a normal web request.[redacted]is the address of the camera on the local network. Addresses starting with192.168.are private addresses used inside a home or office network, not on the public Internet./is the root URL, the front page request thathttpLoginRpmhandles.?starts the query string, the part of a URL that carries extra values to the server.DOCUMENT_ROOT=says we are supplying a value for the setting calledDOCUMENT_ROOT.////////////////////is a long run of forward slashes. On a filesystem, a slash separates folder names, and a path that starts with a slash is an absolute path, meaning it starts from the very top of the disk. A long run of slashes is still just a path that starts at the top, but it is long enough to push the fixedlogin.htmlsuffix out of the way so it no longer affects which file is opened.usr/local/config/ipcamera/.lighttpdpasswordis the file the attacker wants to read. The leading dot in.lighttpdpasswordis a convention for hidden files on Unix-like systems; they do not show up in a normal folder listing, but they can still be opened if you know the name.
The request asks the camera to open that hidden file instead of the login page.
Using the file to get past the login
To bypass authentication it is enough to use this vulnerability to read the hidden file /usr/local/config/ipcamera/.lighttpdpassword on the camera's filesystem. The advisory is explicit that unauthenticated users should not be able to modify DOCUMENT_ROOT.
The proof-of-concept script
The advisory also gives a small script that reads a different file, /usr/local/config/ipcamera/user.conf, and sends a cookie:
#!/usr/bin/python3
import requests
host = "[redacted]"
filename = "/usr/local/config/ipcamera/user.conf"
url = "http://" + host + "/?DOCUMENT_ROOT=" + filename.rjust(0x40 - 1, "/")
headers = {"Cookie": "sess=ok"}
response = requests.get(url, headers=headers, timeout=1).text
print(f"{response=}")
Line by line:
#!/usr/bin/python3is a shebang line. It tells a Unix-like system which program should run this file, here Python 3.import requestsloads a library calledrequests, which makes it easy to send web requests from Python. A library is a bundle of ready-made code you can use instead of writing everything yourself.host = "[redacted]"stores the camera's address in a variable calledhost. A variable is a named box that holds a value.filename = "/usr/local/config/ipcamera/user.conf"stores the path of the file we want to read.user.confis a configuration file, a file that holds settings.url = "http://" + host + "/?DOCUMENT_ROOT=" + filename.rjust(0x40 - 1, "/")builds the full URL. The+joins pieces of text together.filename.rjust(0x40 - 1, "/")takes the file path and pads it on the left with/characters until it is 63 characters long.0x40 - 1is 64 minus 1, so 63. This pads the file path with/characters on the left until it is 63 characters long.headers = {"Cookie": "sess=ok"}builds a set of extra headers to send with the request. A header is a small labelled piece of information sent alongside a request, like the label on a parcel. A cookie is a small piece of data a website asks your browser to store and send back on every request, usually to remember who you are. Here the script sends a cookie namedsesswith the valueok.response = requests.get(url, headers=headers, timeout=1).textsends a GET request to the URL with those headers, waits at most one second, and stores the response body as text inresponse. A GET request is the kind a browser makes when you type an address or click a link.print(f"{response=}")prints the response. Thefbefore the string means it is an f-string, a Python feature that lets you put variable names inside text;{response=}prints both the nameresponseand its value.
The advisory does not show the output of this script.
The second bug: overflowing the stack
The second vulnerability is in a different part of the camera: the code that deletes files from the SD card. An SD card is the small memory card some cameras use to store recordings. The endpoint involved is /sddelfile.fcgi. An endpoint is a specific address on a server that does a specific job; think of it as a particular counter in a shop rather than the front door. .fcgi is a naming convention for programs that a web server hands requests to.
This bug only works after you are logged in, and the advisory says it can be reached by using the first vulnerability to get past the login. The function in ipcamera that handles this endpoint is named httpSDDelFile. An attacker sends a POST request with an excessively long filepath parameter. A POST request is the kind a browser makes when you submit a form; it carries data in the body of the request rather than in the URL. A parameter is a named value passed to a program.
The function swSdDelFile handles that filepath value badly, which leads to a stack overflow. The stack is a region of memory a program uses for short-lived data, including the local variables of a function while it runs. It is laid out like a stack of plates: each function call puts a new plate on top, and when the function returns the plate is taken off. If you write more data into a local variable than it can hold, you spill over into whatever is next to it on the stack, which is what a stack overflow means.
The vulnerable code snippet is:
undefined4 swSdDelFile(char *filepath)
{
...
attacker_path = filepath;
...
i = 0;
while (*attacker_path != '*' && *attacker_path != '\0')
{
strncat((char *)&stack_parameter, attacker_path, 1);
i++;
attacker_path++;
}
...
}
Line by line:
undefined4 swSdDelFile(char *filepath)declares a function calledswSdDelFilethat takes one argument,filepath, which is a pointer to text. A pointer is a value that says where in memory something lives, like a house number rather than the house itself.undefined4is the type of value the function returns; it is a name used by the tool that decompiled the program, meaning "four bytes of unknown type".{and}mark the start and end of the function body....stands for code the advisory has left out because it is not relevant to the bug.attacker_path = filepath;copies the pointer so the function can walk along the text without losing the original starting point.i = 0;sets a counter to zero. The counter is incremented in the loop but the advisory does not say what it is used for.while (*attacker_path != '*' && *attacker_path != '\0')is the loop condition.*attacker_pathmeans "the character currently pointed at".'*'is the asterisk character and'\0'is the null character, a special character that marks the end of a piece of text in C.&&means "and". So the loop keeps going while the current character is neither an asterisk nor the end of the string.strncat((char *)&stack_parameter, attacker_path, 1);appends one character fromattacker_pathontostack_parameter.strncatis a standard function for joining text, and the1says "copy at most one character this time".stack_parameteris a local variable, so it lives on the stack.(char *)&stack_parametertakes the address of that variable and treats it as text.i++;adds one to the counter.attacker_path++;moves the pointer to the next character.
The loop copies one byte at a time from attacker_path into stack_parameter on the stack, stopping only at a * character or at the end of the string. There is no check on how much has already been written to stack_parameter, so a long value overflows it. Because the binary has no security protections at all, the advisory states the overflow can be exploited. An attacker could use it to cause a denial of service by crashing the camera, or potentially to execute arbitrary shellcode by manipulating the program's execution flow to jump to a location on the stack where the shellcode is written. Shellcode is a small piece of machine code an attacker wants the program to run; execution flow means the order in which the program runs its instructions.
Why these bugs exist
The authentication bypass is a trust problem in the path construction. The handler for the root URL reads DOCUMENT_ROOT from user-controlled input through httpGetEnv before authentication, and then passes it as the first %s argument of snprintf in front of the literal login.html. Nothing constrains the value to a real document root, so the attacker controls the prefix of the path that is opened and returned. The advisory frames the fix expectation simply: unauthenticated users should not be able to modify DOCUMENT_ROOT.
The stack overflow is a bounds problem in swSdDelFile. The function walks the filepath string byte by byte and appends each byte with strncat((char *)&stack_parameter, attacker_path, 1), with no check on how much has already been written to stack_parameter. The advisory attributes exploitability to the absence of all security protections in the binary.
What an attacker can do with this
The first vulnerability lets an unauthenticated attacker read files on the camera, including /usr/local/config/ipcamera/.lighttpdpassword, which is enough to bypass authentication. The second, once authenticated, lets an attacker crash the camera for a denial of service or potentially execute arbitrary shellcode. The advisory's summary states that the chain allows complete compromise of the device. The affected cameras are out of support, some since 2015 and others since 2017, and are unlikely to receive patches; the advisory recommends that users immediately disconnect devices that are still present on the Internet.
Is there a fix?
The advisory does not describe a patch. The vendor response states that the devices are out of support and unlikely to get any patches, and the publication is intended to alert users so they can disconnect the devices. The design change the advisory points to is that unauthenticated users should not be able to modify DOCUMENT_ROOT.
Other things worth testing
- Try other parameters read through
httpGetEnvin the same handler and see whether they are also trusted before authentication. A positive result is a request without credentials that returns a file other than the login page. - Try the
DOCUMENT_ROOTpadding trick against other paths on the filesystem, starting with configuration files under/usr/local/config/ipcamera/. A positive result is a response body containing configuration data rather than HTML. - Try the same padding trick on other endpoints that build paths from environment values. A positive result is any endpoint that serves a file outside its intended directory.
- Send a long
filepathvalue to/sddelfile.fcgiafter authenticating. A positive result is a crash or a controlled change in execution flow, which the advisory says is possible because the binary lacks security protections. - Test whether the
*terminator in theswSdDelFileloop can be used to truncate the copied data at a chosen point. A positive result is a request whose effect differs from the full string being copied.
Glossary
- Advisory: a public notice describing a security vulnerability, who is affected and what to do about it.
- Authentication: proving who you are, usually by logging in with a username and password.
- Authentication bypass: getting past a login check without valid credentials.
- Binary: a compiled program, the file the computer actually runs.
- Buffer: a fixed-size area of memory set aside to hold data.
- Byte: a unit of data, roughly one character of text.
- Configuration file: a file that holds settings for a program.
- Cookie: a small piece of data a website asks your browser to store and send back on later requests, often used to remember who you are.
- Credential material: data a system uses to check passwords or logins, such as a stored password file.
- Denial of service (DoS): making a service unavailable, for example by crashing it.
- DOCUMENT_ROOT: a setting that normally names the folder holding a website's files.
- Endpoint: a specific address on a server that performs a specific job.
- Execution flow: the order in which a program runs its instructions.
- f-string: a Python string that lets you insert variable values inside text.
- File disclosure: a bug that lets someone read a file they should not be able to see.
- Function: a named block of code that does one job.
- GET request: the kind of web request a browser makes when you type an address or click a link.
- Header: a small labelled piece of information sent alongside a web request or response.
- Hexadecimal: a way of writing numbers using base 16, where
0x40means 64. - Hidden file: a file whose name starts with a dot on Unix-like systems, so it does not appear in a normal folder listing.
- IP camera: a camera with its own small computer and network connection.
- Library: a bundle of ready-made code you can use in your own program.
- Local network: the private network inside a home or office, using addresses like
192.168.x.x. - Null character: a special character (
\0) that marks the end of a piece of text in C. - Parameter: a named value passed to a program or included in a request.
- Pointer: a value that says where in memory something lives.
- POST request: the kind of web request a browser makes when you submit a form, carrying data in the body.
- Query string: the part of a URL after
?that carries extra values to the server. - Root URL: the address
/, the front page of a site. - SD card: a small memory card used to store data, such as recordings.
- Shellcode: a small piece of machine code an attacker wants a program to run.
- snprintf: a C function that builds text from a template into a buffer of limited size.
- Stack: a region of memory a program uses for short-lived data, including a function's local variables.
- Stack overflow: writing more data into a stack variable than it can hold, spilling into neighbouring memory.
- strncat: a C function that joins text, copying at most a given number of characters.
- Variable: a named box in a program that holds a value.
- Web server: a program that listens for requests from browsers and sends back pages.