Skip to content
Path traversal Write-up Advanced

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.

Harsh Jaiswal Follow

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.

Harsh Jaiswal (@rootxharsh), on blog.harshjaiswal.com

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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

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:

HTTP
GET /api/v1/upload?username=xxx&api_key=xxx&dcid=xxx&timestamp=xxx

Exploitation

Step 1: Reproduce the upload request

The request from the logs was replayed in this form:

HTTP
GET /api/v1/upload?username=xxx&api_key=xxx&dcid=ggg&timestamp=123
Content-Length: 0
Host: [redacted]
Connection: close
Accept-Encoding: gzip, deflate

The response was a 302 redirect to a location of this shape:

Text
/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:

HTTP
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
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&timestamp=123. That looked promising.

Step 3: Path traversal in the filename

The filename was changed to a traversal path:

HTTP
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:

Text
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&timestamp=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

  1. 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.
  2. Replay upload requests found in logs and probe the resulting URL with OPTIONS and PUT. A positive result is a 200 OK with a file created from the request body.
  3. Try traversal sequences in the filename parameter, first to a writable location such as /tmp, then to a protected one such as /etc. A positive result is 200 OK for the writable path and a Permission Denied error showing the resolved path for the protected one.
  4. Check whether normal user credentials are accepted by the upload endpoint. A positive result is a valid id and a successful file creation as a non-admin user.
  5. 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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 14 min read.

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:

HTTP
GET /api/v1/upload?username=xxx&api_key=xxx&dcid=xxx&timestamp=xxx

Line by line:

  • GET is 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/upload is the path, the address of the endpoint on the server. api stands for application programming interface: a set of addresses a program exposes so that other programs, not just browsers, can talk to it. v1 is a version number.
  • After the ? come parameters, the settings passed to the endpoint, written as name=value pairs joined by &. Here they are username, api_key, dcid and timestamp. The values are shown as xxx because they were replaced.
  • username identifies the user, api_key is the secret that proves the user's identity, dcid looks like an identifier for something, and timestamp is 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:

HTTP
GET /api/v1/upload?username=xxx&api_key=xxx&dcid=ggg&timestamp=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. dcid is now ggg and timestamp is 123, simple placeholder values.
  • Content-Length: 0 says 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: close tells the server to close the network connection after answering, rather than keeping it open for more requests.
  • Accept-Encoding: gzip, deflate tells 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:

Text
/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=ggg is an identifier the server produced. It matches the dcid value that was sent.
  • filename=blah_ggg_timestamp.tar.gz is a file name built from the pieces: blah, the ggg id 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:

HTTP
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:

  • OPTIONS is 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.1 at 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
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 OK is 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=0 tells caches, the intermediate copies browsers and networks keep, not to store the response.
  • Content-Type: text/html; charset=UTF-8 says the body is HTML text encoded in UTF-8, a standard way of representing characters.
  • Connection: Close repeats 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&timestamp=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:

HTTP
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:

  • PUT is 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 id and a filename parameter. The value is a long run of ../ followed by tmp/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.
  • tmp is a folder on Unix-like systems meant for temporary files, and it is normally writable by many programs.
  • Content-Length: 0 is present, but the request actually carries a body: the word Testing after 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:

Text
Permission Denied (/opt/xxx/api/uploads/../../../../../../../../etc/harsh.txt)

Line by line:

  • 500 Internal Server Error means the server itself failed while handling the request. It is the server's equivalent of a crash.
  • Permission Denied is 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, then etc/harsh.txt. This reveals where uploads are stored and confirms the filename is simply appended to that base folder.
  • etc is 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&timestamp=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

  1. 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.
  2. Replay upload requests found in logs and probe the resulting URL with OPTIONS and PUT. A positive result is a 200 OK with a file created from the request body.
  3. Try traversal sequences in the filename parameter, first to a writable location such as /tmp, then to a protected one such as /etc. A positive result is 200 OK for the writable path and a Permission Denied error showing the resolved path for the protected one.
  4. Check whether normal user credentials are accepted by the upload endpoint. A positive result is a valid id and a successful file creation as a non-admin user.
  5. 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 Host or Content-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=value setting 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.

More by Harsh Jaiswal

View all

4 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Path traversal and file inclusion

The lesson behind this report.

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.