TimeBased SQL Injection to Dumping the Database
A JSON parameter in an Android app's search request was vulnerable to time-based SQL injection. An attacker could use it to extract the entire database.
Naveen J
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 covers a SQL injection found in an Android asset tracking application. The application name is not disclosed, so the package and class names in the report are anonymised as com.abc.xyz. The feature involved is the application's search screen, which sends its criteria to the backend as a JSON request body and returns matching asset records.
The researcher is Naveen J (@thevillagehackr), and the write-up was published on 19 May 2021.
Setup
The work was done against the installed Android application. The tools used were:
- The application itself, installed normally.
- A reverse engineering step on the APK to look for hardcoded secrets.
- objection to bypass SSL certificate pinning so that traffic could be intercepted.
- MobSF to enumerate the activities exported by the application.
- sqlmap to automate detection and extraction once a candidate parameter was identified.
No account details, application version or backend version are given in the report.
Finding it
The reverse engineering pass for hardcoded secrets produced nothing. After certificate pinning was bypassed with objection, the traffic showed that the application uses:
Content-Type: application/json; charset=UTF-8
That is, actions are performed through JSON request bodies rather than form encoding.
The next idea was that an activity which could be triggered without authorisation would have a large impact, so all activities were pulled from MobSF. Two dashboard activities stood out:
com.abc.xyz.view.activity.DashboardActivity => activity 1
com.abc.xyz.view.activity.DashboardActivitySecond => activity 2
Triggering the first one opened the dashboard with its default pages. A further activity, com.abc.xyz.view.activity.SearchActivity, was also triggered successfully.
The search screen requires a lot of information to locate a specific record, and all of it was entered. The JSON body of the resulting request drew attention, and the decision was made to test it for injection. The object abc_serial_no was the attack surface chosen.
Exploitation
Step 1: Confirm the parameter is injectable
A single quote was appended to the value of abc_serial_no. The server responded with 500 INTERNAL SERVER ERROR, which suggested the object was vulnerable to SQL injection.
Step 2: Automate detection with sqlmap
The POST request was copied to a file and passed to sqlmap to identify the backend database. sqlmap reported that the JSON object abc_serial_no is vulnerable to time-based SQL injection, and the backend was identified as PostgreSQL.
Step 3: Verify the time-based payload manually
The payload below was applied and the response was delayed by 5,044ms, matching the intended sleep:
abc_serial_no":"1' AND 9520=(SELECT 9520 FROM PG_SLEEP(5)) AND 'GRTs'='GRTs"
A delay of roughly five seconds confirms that the injected SQL is executed.
Step 4: Dump the database
sqlmap was then run against the same request to retrieve data. The report states that sqlmap automated the entire process and returned the whole database. The exact command line and the extracted output are not included in the write-up.
Root cause
The value of abc_serial_no from the JSON body appears to reach a SQL query unsafely, so a quote in the value changes the structure of the statement. The 500 error on a single quote and the successful PG_SLEEP delay both point to this. The report does not describe any server-side validation, allow-listing or escaping that would have prevented it, and it does not say how the query is constructed in the application code; the concatenation explanation is the likely cause rather than something shown in source.
Impact
An attacker who can reach the search endpoint can read data from the backend database through time-based blind injection. The report does not enumerate which tables or records were exposed, and it does not state how the endpoint is authorised or whether authentication is required to reach it. The activity triggering described earlier was exploratory and is not presented as the vector for the injection itself.
Fix
The appropriate remediation is to use parameterised queries or prepared statements for the search criteria, including abc_serial_no, so that user input is never parsed as SQL, and to return a generic error rather than a 500 when input is malformed.
Variants to test
- Other JSON fields in the same search body: append a single quote to each in turn and look for a 500 or a change in response time.
- Numeric-looking fields: send a value such as
1 AND 1=1and1 AND 1=2and compare responses; a difference indicates injection. - Time-based confirmation on PostgreSQL: use a
PG_SLEEPexpression in the same shape as above and check for a consistent delay of the requested length. - Other database engines: substitute the equivalent sleep function for the backend in use and confirm with a measured delay.
- Other activities exported by the application: trigger each one from MobSF's list and inspect the requests it produces for parameters that reach the backend.
- Endpoints behind certificate pinning: repeat the interception with pinning bypassed to make sure no request bodies are missed.
What happened, in plain words
A security researcher tested an Android app used for tracking assets (things like equipment or stock that a company owns and wants to keep track of). The app's name is not disclosed, so the package and class names in the write-up are anonymised as com.abc.xyz. The researcher found a way to make the app's search screen send a specially crafted value to the server, and that value was inserted directly into a database command. By measuring how long the server took to reply, the researcher could ask the database yes/no questions and slowly read out its contents. This kind of flaw is called SQL injection.
The researcher is Naveen J (@thevillagehackr), and the write-up was published on 19 May 2021.
To follow this, you only need to know a few everyday ideas:
- An app and its server. The Android app on the phone is the front end. It talks over the internet to a server, which is a computer that stores and processes data. Think of the app as a waiter and the server as the kitchen: you tell the waiter what you want, and the kitchen prepares it.
- A request. When the app wants something from the server, it sends a message called a request. The request has a body, which is the content of the message, like the order written on a slip of paper.
- JSON. A common format for that body. It looks like a list of labels and values, for example
"name": "value". It is just structured text that both sides agree to read the same way. - A database. A large organised store of information, like a very big spreadsheet with many tables. The server asks the database questions using a language called SQL. A question is called a query.
- SQL injection. If the server builds a database query by gluing together text that came from the user, a clever user can include extra SQL words in their input. The database then treats those words as part of the query instead of as plain data. It is like a form that says "write your name here" and someone writes "Alice, and also delete all the files".
Setup
The work was done against the installed Android application. The tools used were:
- The application itself, installed normally.
- A reverse engineering step on the APK to look for hardcoded secrets. An APK is the installable file of an Android app. Reverse engineering means unpacking it to read what is inside, the way you might open a parcel to see its contents. Hardcoded secrets are passwords or keys written directly into the app's code, which anyone who unpacks the app could read.
- objection to bypass SSL certificate pinning so that traffic could be intercepted. SSL certificate pinning is a protection where the app only trusts one specific certificate (an identity document for a server) and refuses to talk to anything else. Bypassing it lets a tester sit in the middle and read the app's internet traffic, like opening a sealed letter to read it before passing it on.
- MobSF to enumerate the activities exported by the application. An activity is a single screen in an Android app, such as a dashboard or a search page. Exported means other apps on the phone are allowed to open it. MobSF is a tool that lists these screens for you.
- sqlmap to automate detection and extraction once a candidate parameter was identified. A parameter is one named field in a request, such as a serial number. sqlmap is a program that tries many injection tricks automatically and reports what works.
No account details, application version or backend version are given in the report.
Finding it
The reverse engineering pass for hardcoded secrets produced nothing. After certificate pinning was bypassed with objection, the traffic showed that the application uses:
Content-Type: application/json; charset=UTF-8
This line is a header, a small label attached to a request that tells the server what kind of content is inside. Content-Type names the format of the body. application/json means the body is JSON, and charset=UTF-8 says which character encoding is used so that letters and symbols are read correctly. In plain words: the app sends its orders as JSON text, not as a traditional web form. Knowing this matters because it tells the tester where to look for input fields: inside the JSON body.
The next idea was that an activity which could be triggered without authorisation would have a large impact, so all activities were pulled from MobSF. Authorisation here means permission to open a screen. If a screen can be opened by anyone, without logging in or being allowed, that is a weakness in itself. Two dashboard activities stood out:
com.abc.xyz.view.activity.DashboardActivity => activity 1
com.abc.xyz.view.activity.DashboardActivitySecond => activity 2
Each line names a screen by its internal identifier. com.abc.xyz is the anonymised package name, the app's unique address on the phone. view.activity is a folder-like grouping, and DashboardActivity and DashboardActivitySecond are the two screen names. The arrow simply says which one the researcher called activity 1 and which one activity 2.
Triggering the first one opened the dashboard with its default pages. A further activity, com.abc.xyz.view.activity.SearchActivity, was also triggered successfully. This screen is the search page, where a user types criteria to find a specific asset record.
The search screen requires a lot of information to locate a specific record, and all of it was entered. The JSON body of the resulting request drew attention, and the decision was made to test it for injection. The object abc_serial_no was the attack surface chosen. An object here is just one labelled field inside the JSON body. Attack surface means the parts of a system that accept input from outside, and therefore the parts worth testing.
Exploitation
Step 1: Confirm the parameter is injectable
A single quote was appended to the value of abc_serial_no. The server responded with 500 INTERNAL SERVER ERROR, which suggested the object was vulnerable to SQL injection.
Why this works: in SQL, text values are wrapped in single quotes, like 'ABC123'. If the server pastes the user's value between quotes without care, adding your own quote closes the text early and leaves the rest of the query malformed. The database then refuses to run it and the server reports a 500 error, which is the generic "something went wrong on our side" message. A well-built server would treat the quote as an ordinary character and still answer normally.
Step 2: Automate detection with sqlmap
The POST request was copied to a file and passed to sqlmap to identify the backend database. POST is the type of request used to send data to the server, as opposed to GET, which is used to ask for a page. Copying the request to a file lets sqlmap replay it exactly, with all its headers and body, while changing one field at a time. sqlmap reported that the JSON object abc_serial_no is vulnerable to time-based SQL injection, and the backend was identified as PostgreSQL. PostgreSQL is a popular database program; knowing which one it is matters because each database has its own functions and syntax.
Step 3: Verify the time-based payload manually
The payload below was applied and the response was delayed by 5,044ms, matching the intended sleep:
abc_serial_no":"1' AND 9520=(SELECT 9520 FROM PG_SLEEP(5)) AND 'GRTs'='GRTs"
This is a fragment of the JSON body, not a full request. Reading it piece by piece:
abc_serial_no":"is the field name and the opening of its value, as it appears in the JSON text.1'is the value the tester supplies, followed by a single quote. That quote closes the text value early, exactly as in step 1, and lets the rest be read as SQL.ANDjoins conditions. In SQL, a query only returns a result if all the joined conditions are true.9520=(SELECT 9520 FROM PG_SLEEP(5))is a comparison.PG_SLEEP(5)is a PostgreSQL function that makes the database wait for five seconds.SELECT 9520produces the number 9520 after that wait. So the left side, 9520, equals the right side only once the sleep has finished. The comparison is true, but it takes five seconds to become true.AND 'GRTs'='GRTs'is a comparison of a piece of text with itself, which is always true. It is there to keep the rest of the original query syntactically valid, so the database does not complain about a broken statement.- The final
"closes the JSON value.
A delay of roughly five seconds confirms that the injected SQL is executed. This is called time-based blind injection: the tester cannot see the data directly, so they ask questions whose answers change how long the server takes to reply. It is like asking a locked room "is the light on?" and getting only a knock after a pause, with the length of the pause carrying the answer.
Step 4: Dump the database
sqlmap was then run against the same request to retrieve data. The report states that sqlmap automated the entire process and returned the whole database. The exact command line and the extracted output are not included in the write-up.
Root cause
The value of abc_serial_no from the JSON body appears to reach a SQL query unsafely, so a quote in the value changes the structure of the statement. Concatenation means gluing text together, like building a sentence by sticking words end to end. Parameterisation, also called a prepared statement, means giving the database the query and the values separately, so the database always treats the values as data and never as instructions. The 500 error on a single quote and the successful PG_SLEEP delay both point to this. The report does not describe any server-side validation, allow-listing or escaping that would have prevented it, and it does not say how the query is constructed in the application code; the concatenation explanation is the likely cause rather than something shown in source.
Impact
An attacker who can reach the search endpoint can read data from the backend database through time-based blind injection. An endpoint is a specific address on the server that accepts requests. The report does not enumerate which tables or records were exposed, and it does not state how the endpoint is authorised or whether authentication is required to reach it. The activity triggering described earlier was exploratory and is not presented as the vector for the injection itself.
Fix
The appropriate remediation is to use parameterised queries or prepared statements for the search criteria, including abc_serial_no, so that user input is never parsed as SQL, and to return a generic error rather than a 500 when input is malformed.
Variants to test
- Other JSON fields in the same search body: append a single quote to each in turn and look for a 500 or a change in response time.
- Numeric-looking fields: send a value such as
1 AND 1=1and1 AND 1=2and compare responses; a difference indicates injection. The first condition is always true and the second always false, so if the server behaves differently for each, it is evaluating your words as SQL. - Time-based confirmation on PostgreSQL: use a
PG_SLEEPexpression in the same shape as above and check for a consistent delay of the requested length. - Other database engines: substitute the equivalent sleep function for the backend in use and confirm with a measured delay.
- Other activities exported by the application: trigger each one from MobSF's list and inspect the requests it produces for parameters that reach the backend.
- Endpoints behind certificate pinning: repeat the interception with pinning bypassed to make sure no request bodies are missed.
Glossary
- Activity: a single screen in an Android app, such as a dashboard or a search page.
- APK: the installable file of an Android app.
- Attack surface: the parts of a system that accept input from outside, and therefore the parts worth testing.
- Authorisation: permission to do something, such as open a screen or reach an endpoint.
- Certificate pinning: a protection where an app only trusts one specific server certificate and refuses to talk to anything else.
- Concatenation: gluing pieces of text together to build a larger string.
- Content-Type: a request header that names the format of the body, such as JSON.
- Database: an organised store of information that the server queries using SQL.
- Endpoint: a specific address on the server that accepts requests.
- Exported activity: an Android screen that other apps on the phone are allowed to open.
- Hardcoded secret: a password or key written directly into an app's code.
- Header: a small label attached to a request that describes it, such as its content type.
- JSON: a structured text format of labels and values used to send data between apps and servers.
- MobSF: a tool that lists the screens and other components of an Android app.
- Objection: a tool used to bypass protections such as certificate pinning so traffic can be intercepted.
- Parameter: one named field in a request, such as a serial number.
- Parameterisation (prepared statement): giving the database the query and the values separately so values are never treated as instructions.
- Payload: the crafted value a tester sends to try to trigger a vulnerability.
- POST request: a request type used to send data to the server.
- PostgreSQL: a popular database program.
- Query: a question asked of a database in SQL.
- Reverse engineering: unpacking an app to read what is inside.
- SQL: the language used to ask questions of a database.
- SQL injection: a flaw where user input is glued into a SQL query and can change its meaning.
- sqlmap: a tool that automates detecting and exploiting SQL injection.
- Time-based blind injection: a technique where the tester cannot see data directly and instead measures how long the server takes to reply to learn yes/no answers.
- 500 INTERNAL SERVER ERROR: a generic server response meaning something went wrong on the server side.