Path traversal while uploading results in RCE
An unauthenticated log download endpoint leaked API keys that doubled as user passwords, and a file upload endpoint accepted a filename parameter that allowed path traversal, enabling arbitrary file creation. A normal user could overwrite an executable binary (phantomjs) with malicious code and trigger its execution via another endpoint, achieving remote code execution.
Published on blog.harshjaiswal.com
Hey guys, Today i’ll show you how i gained a Remote Code Execution on a HackerOne’s Private Program. This is chaining of multiple issues, which were addressed separately and all were marked as Critical/P1.
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
This is a chain of several issues found on a private HackerOne program, each of which was addressed separately and marked Critical/P1. The program name and endpoints have been replaced because the program is private.
The target was an enterprise installation of a product (referred to here as ABC Enterprise), hosted by the program for researchers, with admin credentials provided. A separate, normal installation with fewer features was available to download and host locally. The normal installation gives a basic idea of how the app is hosted, its structure and how the backend works, and it exposed some internal services and the web app structure. The enterprise installation had many more features and endpoints.
One feature of the application is the ability to download logs, which contain all kinds of logs from the service. Those logs leaked API keys, which were in fact the users' own passwords.
Setup
- Enterprise installation hosted by the program, with admin credentials given to researchers.
- Normal installation downloaded and hosted locally, used for recon: fewer features, lower bounty, but it reveals internal services and the web app structure.
- The upload endpoint also accepted normal user credentials, which turned out to matter later.
Finding it
Because the researcher was admin, the log download feature leaking API keys was not very useful on its own. The interesting question was whether logs could be downloaded while unauthenticated or with a low privileged user. The researcher tried downloading the log as a normal user and unauthenticated, and it got downloaded. An attacker could therefore download logs without authentication to the instance and then use the API key/password retrieved from the logs for further exploitation.
While going through the logs, the researcher noticed this request:
GET /api/v1/upload?username=xxx&api_key=xxx&dcid=xxx×tamp=xxx
Exploitation
Step 1: Reproduce the upload request
The request from the logs was replayed in this form:
GET /api/v1/upload?username=xxx&api_key=xxx&dcid=ggg×tamp=123
Content-Length: 0
Host: [redacted]
Connection: close
Accept-Encoding: gzip, deflate
The response was a 302 redirect to a location of this shape:
/api/v1/upload?id=ggg&filename=blah_ggg_timestamp.tar.gz
So a valid request with credentials produces an id and a filename.
Step 2: Probe the endpoint
The researcher started making requests to that URL. A POST of data did not work. Changing upload to download (in case a download endpoint existed too) did not work either. It looked like a dead end, so an OPTIONS request was sent:
OPTIONS /api/v1/upload?id=ggg&filename=blah_ggg_timestamp.tar.gz HTTP/1.1
Content-Length: 0
Host: [redacted]
Connection: close
Accept-Encoding: gzip, deflate
The response was:
HTTP/1.1 200 OK
Cache-Control: max-age=0
Content-Type: text/html; charset=UTF-8
Connection: Close
Content Length can't be 0
Raw data was then sent and the response was 200 OK. The researcher's reasoning was that the endpoint creates a file named blah_ggg_timestamp.tar.gz with the request body as its input, provided the id is valid, and the id is created when valid credentials are passed at /api/v1/upload?username=xxx&api_key=xxx&dcid=ggg×tamp=123. That looked promising.
Step 3: Path traversal in the filename
The filename was changed to a traversal path:
PUT /api/v1/upload?id=ggg&filename=../../../../../../../../../tmp/harsh.txt HTTP/1.1
Content-Length: 0
Host: [redacted]
Connection: close
Accept-Encoding: gzip, deflate
Testing
The response was 200 OK. The request was repeated with the filename set to ../../../../../../../../../etc/harsh.txt, which returned 500 Internal Server Error with:
Permission Denied (/opt/xxx/api/uploads/../../../../../../../../etc/harsh.txt)
This confirmed arbitrary file creation on the server. The etc directory was chosen deliberately: the web server in most cases will not have write access to /etc/, which is what caused the exception.
Step 4: Getting code execution
The error response made the researcher think about doing this without admin. The challenge was to use the endpoint independently and get code execution. Within a few minutes the researcher realised that normal user credentials were also accepted at /api/v1/upload?username=xxx&api_key=xxx&dcid=ggg×tamp=123, so a normal user can create files on the server.
Going back to the logs and the normal installation for information on how this could lead to code execution: the web app is started from /opt/xxx/bin/xlink. The simple approach would be to overwrite xlink with a reverse shell script and wait for the server admin to restart the server with sh xlink restart/stop/start, forcing a restart by making some files unusable. That is not reliable, so further research found a better way.
A phantomjs binary was in use and was executed from an endpoint on the web app. The researcher found an OS command injection using that endpoint (covered in a separate blog post) and had its full path saved from the recon on the log file. The file creation could overwrite files, and luckily the file was created with executable permission. So all a normal user needed to do was overwrite the phantomjs binary with malicious code and use the endpoint that executes it; the code uploaded by the normal user would then be executed.
Root cause
The error message shows the filename being appended to the uploads directory, so traversal sequences such as ../ reach outside it. The error message shows the path being resolved relative to /opt/xxx/api/uploads/, so the filename is appended to a base directory and the resulting path is used directly. The endpoint also accepts normal user credentials, not just admin, so any authenticated low privileged user can write files. The created file keeps executable permission, which turns arbitrary file creation into code execution once a file that the application executes (the phantomjs binary) is overwritten.
Impact
A normal user can create arbitrary files on the server, and an unauthenticated attacker can first download logs to obtain an API key/password. By overwriting the phantomjs binary that is executed from a web app endpoint, the attacker gets code execution as the user running that binary. The alternative route, overwriting /opt/xxx/bin/xlink and waiting for an admin restart, is possible but not reliable.
Fix
The report does not describe the fixes that were applied. The issues were addressed separately and all were marked Critical/P1. Based on the root cause, the filename should not be used to build a path directly: resolve it against the upload directory and reject anything that escapes it, and restrict the upload endpoint to the roles that need it.
Variants to test
- Look for log download endpoints that are reachable unauthenticated or as a low privileged user, and check whether the logs contain credentials or API keys. A positive result is a log file containing secrets that work against the API.
- Replay upload requests found in logs and probe the resulting URL with OPTIONS and PUT. A positive result is a
200 OKwith a file created from the request body. - Try traversal sequences in the
filenameparameter, first to a writable location such as/tmp, then to a protected one such as/etc. A positive result is200 OKfor the writable path and aPermission Deniederror showing the resolved path for the protected one. - Check whether normal user credentials are accepted by the upload endpoint. A positive result is a valid
idand a successful file creation as a non-admin user. - Identify binaries or scripts that the application executes (for example phantomjs) and check whether the created file keeps executable permission. A positive result is overwriting the binary and triggering its execution through the corresponding endpoint.
What happened, in plain words
A security researcher was given access to a private bug bounty program on HackerOne. A bug bounty program is a deal between a company and independent researchers: the company lets researchers test its systems, and pays for real problems they find. This one was private, meaning only invited researchers could take part, so the program name and the web addresses involved have been replaced in the write-up.
The product under test is called ABC Enterprise here. The company hosted an enterprise installation of it for researchers and handed out admin credentials. Admin means the most powerful kind of account, the one that can see and change everything. Separately, a normal installation with fewer features could be downloaded and run on the researcher's own computer. That local copy was used to learn how the product is put together.
The researcher found a chain of separate problems. Each was addressed on its own and each was marked Critical, or P1, the most serious level. In the end, a normal user could make the server run code of their choosing. That is called remote code execution, or RCE: an attacker gets their own instructions executed on someone else's machine, from a distance.
The two installations
The program offered two versions of the product, and the difference between them matters for the story.
- The enterprise installation was hosted by the program and had many more features and endpoints. An endpoint is one address on a web server that does one job, like a counter in a large building: you go to a specific counter for a specific service. Researchers were given admin credentials for it.
- The normal installation had fewer features and a lower bounty, but it could be downloaded and hosted locally. Running it yourself lets you look inside: you see how the app is started, how its files are arranged and how the backend works.
From the normal installation the researcher learned about some internal services and the web app structure. That knowledge was later used to turn a file-writing bug into code execution.
A log download that leaks passwords
The application has a feature to download logs. A log is a running diary that a service keeps of what happens to it: requests received, errors, actions taken. Logs are meant for troubleshooting, and they often contain more detail than anyone intended to expose.
These logs leaked API keys. An API key is a long secret string that a program uses to prove who it is when it talks to another program, much like a password but meant for software rather than a person. Here the API keys were in fact the users' own passwords.
Because the researcher was already admin, this leak was not very useful on its own: an admin can do almost anything anyway. The real question was whether the logs could be downloaded without logging in at all, or with a low privileged account. A low privileged account is a normal user account with only the basic permissions.
The researcher tried downloading the log as a normal user and while not logged in at all, and it worked both times. That means an attacker with no account on the instance could download the logs and then use the API key or password found inside for further attacks. This was one of the separately reported issues.
Reading the logs and finding an upload request
While going through the logs, the researcher noticed this request:
GET /api/v1/upload?username=xxx&api_key=xxx&dcid=xxx×tamp=xxx
Line by line:
GETis the method, the verb of the request. GET means "give me this thing". It is the same kind of request your browser makes when you open a page./api/v1/uploadis the path, the address of the endpoint on the server.apistands for application programming interface: a set of addresses a program exposes so that other programs, not just browsers, can talk to it.v1is a version number.- After the
?come parameters, the settings passed to the endpoint, written asname=valuepairs joined by&. Here they areusername,api_key,dcidandtimestamp. The values are shown asxxxbecause they were replaced. usernameidentifies the user,api_keyis the secret that proves the user's identity,dcidlooks like an identifier for something, andtimestampis a time value.
Seeing a working request in a log is valuable: it is a real example of how the endpoint is called, with real parameter names, that can be replayed.
Step 1: Replaying the upload request
The request from the logs was sent again in this form:
GET /api/v1/upload?username=xxx&api_key=xxx&dcid=ggg×tamp=123
Content-Length: 0
Host: [redacted]
Connection: close
Accept-Encoding: gzip, deflate
Line by line:
- The first line is the request itself: method
GET, path/api/v1/upload, and the four parameters.dcidis nowgggandtimestampis123, simple placeholder values. Content-Length: 0says the request carries no body, no extra data after the headers. Headers are the labelled lines that follow the first line and describe the request.Host: [redacted]names the server being contacted. It is hidden here because the program is private.Connection: closetells the server to close the network connection after answering, rather than keeping it open for more requests.Accept-Encoding: gzip, deflatetells the server which compression formats the client can handle for the response.
The response was a 302 redirect. A redirect is the server saying "the thing you want is over there", and it comes with a location, the address to go to instead. The location had this shape:
/api/v1/upload?id=ggg&filename=blah_ggg_timestamp.tar.gz
Line by line:
- The path is the same endpoint,
/api/v1/upload, but now with different parameters. id=gggis an identifier the server produced. It matches thedcidvalue that was sent.filename=blah_ggg_timestamp.tar.gzis a file name built from the pieces:blah, thegggid and the timestamp, ending in.tar.gz, which is a compressed archive format, a single file holding other files.
So a valid request with credentials produces an id and a filename. In plain terms: the server checks who you are, then hands you a ticket (id) and a place where a file will be stored.
Step 2: Probing the endpoint
The researcher started making requests to that URL. Sending data with POST did not work. POST is the method used to submit data to a server, for example when you send a form. Changing upload to download in the path, in case a download endpoint existed too, did not work either.
It looked like a dead end, so an OPTIONS request was sent:
OPTIONS /api/v1/upload?id=ggg&filename=blah_ggg_timestamp.tar.gz HTTP/1.1
Content-Length: 0
Host: [redacted]
Connection: close
Accept-Encoding: gzip, deflate
Line by line:
OPTIONSis a method that asks the server which methods it allows on this address. It is like asking a counter "what services do you offer here?" before queueing.- The path and parameters are the ones from the redirect location.
HTTP/1.1at the end of the first line states the version of the protocol being spoken.- The headers are the same as before: no body, hidden host, close the connection, accept compressed responses.
The response was:
HTTP/1.1 200 OK
Cache-Control: max-age=0
Content-Type: text/html; charset=UTF-8
Connection: Close
Content Length can't be 0
Line by line:
HTTP/1.1 200 OKis the status line. 200 means success. Status codes are three digit numbers: 2xx means fine, 3xx means redirect, 4xx means the client did something wrong, 5xx means the server hit an error.Cache-Control: max-age=0tells caches, the intermediate copies browsers and networks keep, not to store the response.Content-Type: text/html; charset=UTF-8says the body is HTML text encoded in UTF-8, a standard way of representing characters.Connection: Closerepeats that the connection will be closed.- After the blank line comes the body:
Content Length can't be 0. The server is complaining that the request had no content.
Raw data was then sent and the response was 200 OK. The researcher's reasoning was that the endpoint creates a file named blah_ggg_timestamp.tar.gz containing whatever was sent as the request body, provided the id is valid. The id is created when valid credentials are passed at /api/v1/upload?username=xxx&api_key=xxx&dcid=ggg×tamp=123. That looked promising: the server was writing the caller's data into a file whose name the caller could influence.
Step 3: Path traversal in the filename
The filename was changed to a traversal path:
PUT /api/v1/upload?id=ggg&filename=../../../../../../../../../tmp/harsh.txt HTTP/1.1
Content-Length: 0
Host: [redacted]
Connection: close
Accept-Encoding: gzip, deflate
Testing
Line by line:
PUTis a method that means "store this data at this address". It is the natural verb for writing a file.- The path is the upload endpoint with the same
idand afilenameparameter. The value is a long run of../followed bytmp/harsh.txt. ../means "go up one folder" in a file path. On a server, files live in folders inside folders, like rooms inside corridors inside a building. If the server builds a path by gluing a base folder and the filename together, each../climbs one level higher, out of the folder the server intended. This is called path traversal, or directory traversal: using relative steps to escape the intended location.tmpis a folder on Unix-like systems meant for temporary files, and it is normally writable by many programs.Content-Length: 0is present, but the request actually carries a body: the wordTestingafter the blank line. The server evidently used the body anyway.- The remaining headers are the same as before.
The response was 200 OK. The request was repeated with the filename set to ../../../../../../../../../etc/harsh.txt, which returned 500 Internal Server Error with:
Permission Denied (/opt/xxx/api/uploads/../../../../../../../../etc/harsh.txt)
Line by line:
500 Internal Server Errormeans the server itself failed while handling the request. It is the server's equivalent of a crash.Permission Deniedis the operating system refusing the write.- The path in brackets shows what the server tried: the base folder
/opt/xxx/api/uploads/, then the traversal steps, thenetc/harsh.txt. This reveals where uploads are stored and confirms the filename is simply appended to that base folder. etcis the folder on Unix-like systems that holds system configuration. It was chosen deliberately: the web server in most cases will not have write access to/etc/, which is what caused the exception. The failure was useful evidence, not a setback.
Together, the two results confirmed arbitrary file creation on the server: the ability to write a file with a name and contents of the attacker's choosing, anywhere the server process is allowed to write.
Step 4: Getting code execution
The error response made the researcher think about doing this without admin. The challenge was to use the endpoint independently and get code execution. Within a few minutes the researcher realised that normal user credentials were also accepted at /api/v1/upload?username=xxx&api_key=xxx&dcid=ggg×tamp=123, so a normal user can create files on the server. So a normal user can create files on the server.
Going back to the logs and the normal installation for information on how this could lead to code execution: the web app is started from /opt/xxx/bin/xlink. A binary is a program file that the computer can run. The simple approach would be to overwrite xlink with a reverse shell script and wait for the server admin to restart the server with sh xlink restart/stop/start, forcing a restart by making some files unusable. A reverse shell is a program that connects back out to the attacker and hands them a command prompt on the victim machine. That approach depends on a human restarting the service, so it is not reliable. Further research found a better way.
A phantomjs binary was in use and was executed from an endpoint on the web app. PhantomJS is a headless browser: a browser with no visible window, used by programs to load and render pages automatically. The researcher found an OS command injection using that endpoint, covered in a separate blog post, and had its full path saved from the recon on the log file. Command injection means tricking a program into running an operating system command it was not meant to run. The file creation could overwrite files, and luckily the file was created with executable permission. Executable permission is the flag on a file that tells the operating system "this may be run as a program".
So all a normal user needed to do was overwrite the phantomjs binary with malicious code and use the endpoint that executes it. The code uploaded by the normal user would then be executed. No restart, no waiting for an admin: the application itself would run the attacker's file the next time that endpoint was used.
Why it worked
The error message shows the filename being appended to the uploads folder, so traversal sequences such as ../ reach outside it. The error message shows the path being resolved relative to /opt/xxx/api/uploads/, so the filename is appended to a base folder and the resulting path is used directly. The endpoint also accepts normal user credentials, not just admin, so any authenticated low privileged user can write files. The created file keeps executable permission, which turns arbitrary file creation into code execution once a file that the application executes, the phantomjs binary, is overwritten.
What an attacker could do
A normal user can create arbitrary files on the server, and an unauthenticated attacker can first download logs to obtain an API key or password. By overwriting the phantomjs binary that is executed from a web app endpoint, the attacker gets code execution as the user running that binary. The alternative route, overwriting /opt/xxx/bin/xlink and waiting for an admin restart, is possible but not reliable.
How it was fixed
The write-up does not describe the fixes that were applied. The issues were addressed separately and all were marked Critical/P1. Based on the root cause, the filename should not be used to build a path directly: resolve it against the upload folder and reject anything that escapes it, and restrict the upload endpoint to the roles that need it.
Things to try on your own targets
- Look for log download endpoints that are reachable unauthenticated or as a low privileged user, and check whether the logs contain credentials or API keys. A positive result is a log file containing secrets that work against the API.
- Replay upload requests found in logs and probe the resulting URL with OPTIONS and PUT. A positive result is a
200 OKwith a file created from the request body. - Try traversal sequences in the
filenameparameter, first to a writable location such as/tmp, then to a protected one such as/etc. A positive result is200 OKfor the writable path and aPermission Deniederror showing the resolved path for the protected one. - Check whether normal user credentials are accepted by the upload endpoint. A positive result is a valid
idand a successful file creation as a non-admin user. - Identify binaries or scripts that the application executes, for example phantomjs, and check whether the created file keeps executable permission. A positive result is overwriting the binary and triggering its execution through the corresponding endpoint.
Glossary
- API: a set of addresses and rules a program exposes so other programs can talk to it, like a service counter for software.
- API key: a secret string a program uses to prove its identity to another program, similar to a password for software.
- Admin: the most powerful account type, able to see and change everything.
- Binary: a program file that the computer can execute.
- Body: the data carried in a request or response after the headers.
- Bug bounty program: an arrangement where a company invites researchers to test its systems and pays for real problems found.
- Cache: a stored copy of a response kept to avoid fetching it again.
- Command injection: tricking a program into running operating system commands it was not meant to run.
- Content-Length: a header stating how many bytes of body a request or response carries.
- Content-Type: a header stating what kind of data the body is, such as HTML text.
- Endpoint: one address on a server that performs one specific job.
- Executable permission: the flag on a file that allows the operating system to run it as a program.
- GET: the request method meaning "give me this thing".
- Header: a labelled line in a request or response describing it, such as
HostorContent-Type. - Host: the header naming which server is being contacted.
- HTTP: the protocol browsers and servers use to exchange requests and responses.
- id: an identifier produced by the server, used to refer to something it created, here an upload.
- Log: a running diary a service keeps of requests, errors and actions.
- Low privileged user: a normal account with only basic permissions.
- Method: the verb of a request, such as GET, POST, PUT or OPTIONS.
- OPTIONS: a request method asking which methods a server allows on an address.
- Parameter: a
name=valuesetting passed to an endpoint after the?in a URL. - Path: the part of a URL identifying the endpoint, such as
/api/v1/upload. - Path traversal: using
../steps in a file name to climb out of the folder the server intended, also called directory traversal. - PhantomJS: a headless browser, a browser with no visible window, used by programs to load pages automatically.
- POST: the request method used to submit data to a server.
- PUT: the request method meaning "store this data at this address".
- P1 / Critical: the most serious severity rating in a bug bounty program.
- Redirect: a response telling the client to go to a different address instead, usually with a 3xx status code.
- Remote code execution (RCE): getting your own instructions executed on someone else's machine from a distance.
- Reverse shell: a program that connects back to an attacker and gives them a command prompt on the victim machine.
- Status code: the three digit number in a response saying how it went: 2xx success, 3xx redirect, 4xx client error, 5xx server error.
- Timestamp: a value representing a point in time.
- Unauthenticated: without logging in, with no account or credentials.
- UTF-8: a standard way of encoding characters as bytes.