Chain of hacks leading to Database Compromise!
A PHP download handler was vulnerable to local file inclusion and server-side request forgery, allowing an attacker to read system files and access the AWS instance metadata service. This led to the theft of AWS credentials, which granted access to S3 buckets containing database credentials and customer data.
Published on medium.com
#### Avinash Jain (@logicbomb1)
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 write-up describes a chain of vulnerabilities found in an Indian e-commerce company's application, starting from a local file inclusion (LFI) bug and ending in a database compromise. The researcher states the work was done with the explicit permission of the company concerned.
The entry point was an ordinary feature: a page offering the application for download through the "Android Google play" and "iPhone App store" links. Clicking one of those links did not deliver a file directly; it redirected to an intermediate page that handled the download.

Setup
The report does not describe any accounts, roles or tool versions used during the discovery phase. The only tooling mentioned is the AWS command line interface, configured later in the chain to test the credentials that had been obtained. The report shows the page behaving differently in an incognito window, where there is no referring page.
Finding it
The researcher was deliberately hunting for LFI, so the focus was on functionality that interacts with files. The app download links were exactly that kind of functionality.
Clicking the link redirected to a page with a particular URL.

That page then immediately redirected back to the previously referred page. Opening the same thing in an incognito window, where there is no referring page, produced a "404 Page not found" response. That behaviour indicated the page was evaluating some condition on parameters and following simple if/else logic: with a referrer it went one way, without one it fell through to the 404.
To find which parameters were missing, the researcher read the HTML source of the page.

The logic was visible in the source. The interesting part, highlighted in the source, was a PHP file called download_handler.php that was absent from the URL. That handler requires a parameter path, used as the final download link, and a parameter name for the name of the URL. Because those were not present, nothing was downloaded.
Exploitation
Step 1: Reconstructing the handler URL and testing LFI
Following the code in the page source, the final URL came out as:
downloadcallback/download_handler.php?path=
The path parameter was then given a directory traversal payload pointing at the password file:
../../../../etc/passwd
The files on the host had the maximum permissions set, which the researcher calls a common mistake, and the traversal succeeded. The contents of /etc/passwd were returned.

Other sensitive files could be read the same way.

Through this LFI the researcher read Linux system files, configuration files and access logs. The access logs exposed a user access token being passed in GET parameters, along with other sensitive information.
Step 2: Reading the handler source
The cause of the whole loophole was download_handler.php itself.

The file simply took the file named in the input and read it back to the client, with no restriction on which file that could be.
Step 3: Turning the LFI into SSRF
Because the handler fetched whatever the input pointed at, it was also vulnerable to server-side request forgery. The researcher could make the server issue requests rather than only read local files.
The same target was then requested through different URL schemes: file:///, dict://, ftp:// and gopher://. The file:/// scheme worked and returned /etc/passwd again.
Step 4: Identifying the hosting platform
While pulling sensitive files through the LFI, the researcher read /etc/motd. That file indicated the application was deployed on AWS ElasticBeanstalk.
That was enough to go looking for the AWS instance metadata and user data through the SSRF.
The AWS account ID and region were retrieved from the instance identity document at:
http://[redacted]/latest/dynamic/instance-identity/document
Step 5: Stealing the instance role credentials
Reading about AWS Elastic Beanstalk led the researcher to an API call that returns the AWS access key, secret access key and token for the instance role:
http://[redacted]/latest/meta-data/iam/security-credentials/aws-elasticbeanstalk-ec2-role
That call was made through the SSRF, and it returned the AWS access key, ID and token. Combined with the account ID obtained earlier, this is where the finding became significantly more critical.
Step 6: Authenticating to AWS and pulling S3 data
To check whether the credentials were still valid, the researcher configured the AWS CLI and listed and downloaded S3 bucket data to the local machine. Both worked.
The bucket contents were then copied down recursively.
Step 7: Mining the buckets and reaching the database
Reviewing the buckets turned up critical files, including database.js, config.js, app.js and payment.config. These contained a payment hash key and salt, which the researcher notes could be used to tamper with an order's payment, several sets of database credentials, and usernames and passwords for internal tools.
The config files also held, in plain text, the credentials for a running MongoDB instance. Connecting to that instance with those credentials exposed customer data.

The database did not hold every user detail, but it contained more than 10K records.
Root cause
The immediate defect was in download_handler.php: it accepted a caller-supplied path and read the file back to the client without validating or restricting it, which gave both arbitrary local file read and server-side request forgery. The report shows the handler source, which simply read the requested file back to the client.
Two conditions made the impact far worse. First, the files on the host had the maximum permissions set, so the LFI could reach anything the process could read, including /etc/motd and access logs. Second, the application ran on Elastic Beanstalk with an instance role whose credentials were reachable from the instance metadata service at [redacted], and the SSRF could reach that address. Nothing in the report indicates that the metadata endpoint was blocked or that the role was scoped narrowly enough to prevent S3 access.
The final link in the chain was configuration hygiene: secrets that should never have been in bucket objects, namely the payment hash key and salt, database credentials and internal tool credentials, were stored in plain text in files such as database.js, config.js, app.js and payment.config, and the S3 access obtained was read/write.
Impact
An unauthenticated attacker could read arbitrary files from the server, including system files, configuration and access logs, and the logs leaked a user access token passed in GET parameters. The same handler allowed SSRF, which reached the AWS instance metadata service and yielded the Elastic Beanstalk instance role's access key, secret key and token, plus the account ID and region.
With those credentials the attacker had read/write access to the S3 buckets, from which payment material (hash key and salt, usable to tamper with an order's payment), database credentials and internal tool credentials were obtained. The MongoDB credentials in the config files gave access to customer data, more than 10K records, though not all user details were present.
Fix
The report states that the company patched the vulnerability quickly and rotated all affected credentials and keys. It does not describe the code change made to download_handler.php. Beyond that, the lessons the chain illustrates are to validate and restrict the path accepted by a download handler, to avoid running with maximum file permissions, to block access from the application to the instance metadata service or restrict the instance role to what it needs, and to keep secrets out of S3 objects and config files.
Variants to test
- Download and export handlers that take a file path or URL parameter. A positive result is reading a known file such as
/etc/passwdor fetching an external URL you control. - Handlers that behave differently with and without a referrer, as here. A positive result is a parameter visible in the page source that is required for the handler to run.
- URL schemes other than
httpin any server-side fetch. A positive result isfile:///,dict://,ftp://orgopher://being accepted. - SSRF against
http://[redacted]/latest/dynamic/instance-identity/documentand the IAM security credentials path. A positive result is account ID, region or role credentials coming back. - Cloud storage reachable with any credentials you obtain. A positive result is listing and downloading bucket objects, and finding config or secret files in them.
- Database credentials found in configuration files. A positive result is a successful connection and readable data.
What happened, in plain words
A security researcher was hunting for a specific kind of bug called local file inclusion, or LFI. In plain terms, LFI is when a website lets you ask it to open a file on its own server, and it does not check carefully which file you asked for. Imagine a hotel receptionist who will fetch any room key you name, including the master key, without checking whether you are allowed to have it. That is the shape of the bug.
The researcher reached this bug through an ordinary feature of an Indian e-commerce company's website: the page that offers the mobile app for download through the "Android Google play" and "iPhone App store" links. From that one small flaw, the researcher was able to make the server fetch web addresses of their choosing (a second bug called SSRF), learn that the site ran on Amazon's cloud hosting service called Elastic Beanstalk, steal the cloud account's access keys, download the company's cloud storage files, find database passwords sitting in plain text inside those files, and finally connect to a database holding more than 10,000 customer records.
The researcher states clearly that all of this was done with the explicit permission of the company concerned. The company patched the problem quickly and replaced all the affected credentials and keys.
This article walks through the same chain step by step, explaining each concept from scratch along the way.
The starting point: a normal download page
The researcher was not browsing randomly. They had decided in advance to look for LFI, so they focused on any part of the site that deals with files. A download button is exactly that kind of feature: somewhere behind it, the server has to pick a file and send it to your browser.

The image shows the app download page with the "Android Google play" and "iPhone App store" options. Nothing about it looks unusual. That is the point: the interesting bugs often sit behind the most ordinary buttons.
Following the redirect
Clicking one of those links did not hand over a file directly. Instead it sent the browser to an intermediate page, a page whose only job is to handle the download. The browser arrived at a particular URL.

That page then immediately sent the browser back to the page it had come from. To understand what was going on, the researcher opened the same link in an incognito window. Incognito mode is useful here because it starts with a clean slate: no saved logins, no history, and crucially no record of which page you came from. In that window, the intermediate page responded with "404 Page not found".
That difference told the researcher something important. The page was checking a condition: if a certain piece of information was present, it did one thing; if not, it fell through to the 404. Programmers call this if/else logic. The researcher's next question was: which pieces of information (parameters) were missing?
A parameter is a small labelled value attached to a web address, usually after a question mark. In shop.example/page?colour=red, the parameter is colour and its value is red. Websites use parameters constantly to pass instructions from the browser to the server.
Reading the page source
Every web page your browser displays is built from text: HTML, plus often small programs written in languages like PHP that run on the server. You can see that text yourself by right-clicking a page and choosing to view its source. The researcher did exactly that to find the missing parameters.

The source revealed the logic. Highlighted in the page was a reference to a PHP file called download_handler.php. PHP is a programming language commonly used to build websites; a .php file is a small program that runs on the server when a browser asks for it. This handler was not present in the URL the browser had used, and it needed two parameters to work: path, which served as the final download link, and name, for the name of the URL. Because neither was supplied, nothing was downloaded. That explained the odd redirect behaviour.
Step 1: Rebuilding the address and testing for LFI
Following the code in the page source, the researcher worked out what the full address should look like:
downloadcallback/download_handler.php?path=
Line by line, this is a relative web address. downloadcallback/ is a folder on the site. download_handler.php is the small server program inside it. The ? marks the start of the parameters, and path= is the parameter named path, waiting for a value after the equals sign. Nothing else is supplied yet.
The researcher then put a directory traversal payload into path:
../../../../etc/passwd
This needs unpacking. On a Linux server, files are arranged in folders inside folders, like a filing cabinet. .. is a special name meaning "the folder above this one". So ../../../../ means: go up one folder, then up again, four times in total, climbing out of whatever folder the program started in. /etc/passwd is a well-known Linux file that lists user accounts on the machine. It does not contain passwords in the modern sense, despite the name, but it is a standard target for testing because reading it proves you can reach files outside the intended folder. This technique is called directory traversal, and when it lets you read files on the server itself, the bug is called local file inclusion.
The attempt worked. The researcher notes that the files on the host had the maximum permissions set, which they call a common mistake. Permissions are the rules that decide who may read or change a file; maximum permissions means essentially everyone can read everything. The contents of /etc/passwd came back.

The image shows the /etc/passwd file returned by the server, confirming the traversal worked.
Other sensitive files could be read the same way.

This image shows further sensitive files being read through the same LFI.
Through this technique the researcher read Linux system files, configuration files and access logs. Access logs are records the server keeps of every request it receives. They are valuable to an attacker because they can contain things users sent to the site, and in this case the logs exposed a user access token being passed in GET parameters, along with other sensitive information.
A token is a short string of characters that acts like a temporary pass, proving who you are or what you are allowed to do, without repeating a password every time. GET parameters are simply the parameters attached to a web address, the ?name=value part described earlier. A token appearing in a web address is risky because addresses get written into logs, browser history and shared links.
Step 2: Reading the handler's own code
The cause of the whole problem was download_handler.php itself.

The image shows the source of download_handler.php. The program simply took the file named in the input and read it back to the client, with no restriction on which file that could be. Think of a librarian who fetches any book you name, including the staff ledger and the safe combination, because nobody told them some shelves are off limits. The fix for this class of bug is to check the requested name against an approved list, or to build the file path yourself from a fixed folder rather than trusting what the visitor typed.
Step 3: Turning file reading into SSRF
Because the handler fetched whatever the input pointed at, it was also vulnerable to server-side request forgery, usually shortened to SSRF. Here is the idea from zero. When your browser visits a web address, your computer makes a request across the internet. SSRF is when you trick the server into making that request instead, on your behalf. The server sits inside the company's network, so it can reach addresses that you, sitting outside, cannot. It is like persuading a member of staff to go into the back office and read out whatever is written on a particular shelf.
The image shows the SSRF behaviour: the handler fetching a remote address rather than a local file.
The researcher then requested the same target through different URL schemes. A scheme is the first part of a web address, the bit before ://, which tells the software how to communicate. The schemes tried were file:///, dict://, ftp:// and gopher://. The file:/// scheme, which means "read a file on this machine", worked and returned /etc/passwd again.
The image shows /etc/passwd being retrieved through the file:/// scheme, confirming the SSRF.
Step 4: Working out where the site was hosted
While pulling sensitive files through the LFI, the researcher read /etc/motd. The name stands for "message of the day": a short text file that a Linux system shows to people who log in, often used by hosting providers to display a welcome banner. That file indicated the application was deployed on AWS ElasticBeanstalk.
AWS is Amazon Web Services, a large cloud provider where companies rent computers instead of buying their own. Elastic Beanstalk is one of its services for running web applications. Knowing the hosting platform matters because each platform has its own well-known internal addresses, and an SSRF bug lets you reach them.
The image shows the message of the day revealing Elastic Beanstalk.
That was enough to go looking for the AWS instance metadata and user data through the SSRF. Metadata is information the cloud platform makes available to the running server about itself: its identity, its network details, and, importantly, the credentials it has been given. User data is a startup script the customer supplied when creating the server. Both are served from a special internal address, [redacted], which only the server itself can normally reach. That is exactly why SSRF is dangerous here: the attacker cannot reach that address from outside, but the server can.
This image shows the AWS instance user data retrieved through the SSRF.
This image shows the AWS instance metadata retrieved the same way.
The AWS account ID and region were retrieved from the instance identity document at:
http://[redacted]/latest/dynamic/instance-identity/document
Line by line: http:// is the scheme for ordinary web requests. [redacted] is the internal metadata address. /latest/ selects the current version of the metadata service. /dynamic/instance-identity/document asks for a document describing the server's identity. The account ID identifies which AWS account the server belongs to, and the region is the geographic area where the cloud resources run.
The image shows the account ID and region returned by that address.
Step 5: Stealing the server's cloud credentials
Reading about AWS Elastic Beanstalk led the researcher to an API call that returns the AWS access key, secret access key and token for the instance role.
An API is a defined way for one piece of software to ask another for something, like a service counter with a fixed list of requests you can make. An instance role is a set of permissions attached to the server itself, so the server can use other AWS services without a human logging in. The credentials for that role are exactly what an attacker wants, because they inherit the server's powers.
The address was:
http://[redacted]/latest/meta-data/iam/security-credentials/aws-elasticbeanstalk-ec2-role
Line by line: http:// is the scheme. [redacted] is the internal metadata address again. /latest/meta-data/ is the part of the service that describes the server. /iam/ refers to identity and access management, the AWS system that controls who can do what. /security-credentials/ is where temporary credentials live. aws-elasticbeanstalk-ec2-role is the name of the role attached to this server. Asking for this address returns the access key, secret key and session token for that role.
That call was made through the SSRF, and it returned the AWS access key, ID and token. Combined with the account ID obtained earlier, this is where the finding became significantly more critical.
The image shows the AWS account access ID and access key returned by the metadata service.
Step 6: Logging in to AWS and downloading storage
Now it was time to authenticate into the AWS account. To check whether the credentials were still valid, the researcher configured the AWS CLI and listed and downloaded S3 bucket data to the local machine. Both worked.
The AWS CLI is a command line interface: a program you run by typing commands in a terminal window instead of clicking buttons. S3 is Amazon's cloud storage service, and a bucket is a named container inside it, like a folder in the cloud. Configuring the CLI means telling it which credentials to use.
The image shows the AWS CLI being configured with the stolen credentials.
The bucket contents were then copied down recursively. Recursively means copying a folder and everything inside it, including subfolders and their contents, all in one go.
The image shows all the S3 bucket content being copied to the local machine.
Step 7: Searching the files and reaching the database
Reviewing the buckets turned up critical files, including database.js, config.js, app.js and payment.config. These are configuration files: files that tell a program how to connect to other systems and which secrets to use. They contained a payment hash key and salt, which the researcher notes could be used to tamper with an order's payment, several sets of database credentials, and usernames and passwords for internal tools.
A hash key and salt are pieces of secret data used when scrambling information so it cannot be read or altered by outsiders. If an attacker learns them, they may be able to forge or change data that the system trusts, which is why the payment material mattered.
The config files also held, in plain text, the credentials for a running MongoDB instance. MongoDB is a type of database, a system for storing and looking up large amounts of structured information. Connecting to that instance with those credentials exposed customer data.

The image shows the MongoDB instance and the customer data found inside it.
The database did not hold every user detail, but it contained more than 10K records.
Why it worked, and what to learn
The immediate defect was in download_handler.php: it accepted a caller-supplied path and read the file back to the client without validating or restricting it. That single weakness gave both arbitrary local file read and server-side request forgery. The report shows the handler source, which simply read the requested file back to the client.
Two conditions made the impact far worse. First, the files on the host had the maximum permissions set, so the LFI could reach anything the process could read, including /etc/motd and access logs. Second, the application ran on Elastic Beanstalk with an instance role whose credentials were reachable from the metadata service at [redacted], and the SSRF could reach that address. Nothing in the report indicates that the metadata endpoint was blocked or that the role was scoped narrowly enough to prevent S3 access.
The final link in the chain was configuration hygiene: secrets that should never have been in bucket objects, namely the payment hash key and salt, database credentials and internal tool credentials, were stored in plain text in files such as database.js, config.js, app.js and payment.config, and the S3 access obtained was read/write.
What the company did
The report states that the company patched the vulnerability quickly and rotated all affected credentials and keys. Rotating means replacing old credentials with new ones so the stolen ones stop working. The report does not describe the code change made to download_handler.php.
Things worth testing on your own targets
- Download and export handlers that take a file path or URL parameter. A positive result is reading a known file such as
/etc/passwdor fetching an external URL you control. - Handlers that behave differently with and without a referrer, as here. A positive result is a parameter visible in the page source that is required for the handler to run.
- URL schemes other than
httpin any server-side fetch. A positive result isfile:///,dict://,ftp://orgopher://being accepted. - SSRF against
http://[redacted]/latest/dynamic/instance-identity/documentand the IAM security credentials path. A positive result is account ID, region or role credentials coming back. - Cloud storage reachable with any credentials you obtain. A positive result is listing and downloading bucket objects, and finding config or secret files in them.
- Database credentials found in configuration files. A positive result is a successful connection and readable data.
Glossary
- LFI (local file inclusion): a bug where a website lets a visitor make the server open a file on its own machine, without properly checking which file.
- Directory traversal: using
..in a file path to climb out of the intended folder and reach other folders. - SSRF (server-side request forgery): a bug where a visitor can make the server send a request to an address of the visitor's choosing.
- Parameter: a labelled value attached to a web address after a question mark, used to pass instructions to the server.
- GET parameters: parameters carried in the web address itself.
- HTML: the text format that describes the structure of a web page.
- PHP: a programming language commonly used to build websites;
.phpfiles run on the server. - Page source: the underlying text of a web page, viewable in the browser.
- Incognito window: a browser window that starts with no history, logins or referring page.
- Referrer: the page a browser came from when following a link.
- 404: the standard response meaning the requested page was not found.
- If/else logic: a program's decision to do one thing when a condition is true and another when it is false.
- Permissions: rules deciding who may read or change a file.
/etc/passwd: a standard Linux file listing user accounts, often used to prove file read access./etc/motd: a Linux "message of the day" file, often a welcome banner from the hosting provider.- Access log: a server's record of requests it has received.
- Token: a short string acting as a temporary pass, proving identity or permission.
- URL scheme: the first part of an address, before
://, telling software how to communicate (for examplehttp,file,ftp). - AWS: Amazon Web Services, a cloud provider where companies rent computers and storage.
- Elastic Beanstalk: an AWS service for running web applications.
- Instance metadata: information a cloud platform exposes to a running server about itself, including credentials.
- User data: a startup script supplied by the customer when creating a cloud server.
[redacted]: the internal address where cloud instance metadata is served.- API: a defined way for one piece of software to request something from another.
- Instance role: a set of cloud permissions attached to a server so it can use other services.
- Access key / secret access key: the identifier and secret half of a cloud credential pair.
- AWS CLI: a program for controlling AWS by typing commands in a terminal.
- S3: Amazon's cloud storage service.
- Bucket: a named container for files in cloud storage.
- Recursive copy: copying a folder together with everything inside it.
- Configuration file: a file that tells a program how to connect to systems and which secrets to use.
- Hash key and salt: secret values used when scrambling data so it cannot be read or altered by outsiders.
- MongoDB: a type of database for storing and looking up structured information.
- Credential rotation: replacing old passwords and keys with new ones so stolen ones stop working.