Skip to content
SQL injection Write-up Intermediate

SQLi2RCE: another discovery ️ (part 2)

The author was testing whether a UNION-based SQL injection could be combined with INTO OUTFILE to write a file to disk. The phpMyAdmin client silently appended a LIMIT clause to the query, which broke the statement until a trailing comment was added, after which the write succeeded.

medium.com

Published on medium.com

Cybersecurity

Sevada797, on medium.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. 4 min read.

Context

This is a short note about a syntax detail that matters when you try to turn a SQL injection into file write, and from there into remote code execution. The work was done inside phpMyAdmin (PMA), used here as the web SQL client through which the researcher runs queries against a MySQL server. The researcher describes the injection as authorised and says explicitly that the PMA behaviour is not the point of the article: the point is what the behaviour teaches about INTO OUTFILE combined with UNION.

The feature under examination is SELECT ... INTO OUTFILE, which writes the result of a query to a file on the database server. Chained after a UNION SELECT, it is the classic route from SQL injection to writing a file the web server can execute.

Setup

The researcher works in phpMyAdmin, and separately in the mysql command line client on a shell, to compare behaviour between the two. No versions, accounts or roles are given. The target table used throughout is users, and the output path used in the examples is /tmp/lol.

Finding it

While checking whether INTO OUTFILE works together with UNION, the query appeared to fail. Looking more closely at what PMA had actually sent, the researcher noticed the query was not exactly what had been typed.

PMA had run:

SQL
SELECT * FROM `users` UNION SELECT * FROM `users` INTO OUTFILE '/tmp/lol' LIMIT 0, 25

while the researcher had written:

SQL
SELECT * FROM `users` UNION SELECT * FROM `users` INTO OUTFILE '/tmp/lol'
From the original on medium.com

Unsure what to make of it, the researcher passed the problem to an AI assistant, saying it did not work, and got back the answer that it should work.

From the original on medium.com

Exploitation

Step 1: add a comment line

The researcher tried again, this time appending a comment line to the payload.

From the original on medium.com

With the comment in place the query worked. The researcher notes that this was an authorised SQL injection in PMA, and that the real lesson is how important it is to read errors and understand them.

The conclusion drawn is that PMA appends the LIMIT clause for its own UI purposes, because the SQL is being run from PMA, which is the web SQL client. The comment absorbs the clause PMA appends after the statement.

Step 2: confirm the cause is PMA, not MySQL

To confirm that the problem exists only on the PMA side, the researcher ran the same test first in the mysql shell client.

From the original on medium.com

Step 3: select the text to write, keeping the column count

The next idea was to stop selecting from the table and instead select the text to be written, keeping the same number of columns so the UNION does not error.

From the original on medium.com

This worked. The researcher also notes that a WHERE statement placed in the first SELECT does not interfere, which is described as useful for real case escalations.

Root cause

The behaviour that caused the initial failure is on the client side: PMA rewrites the statement it is given, appending LIMIT 0, 25 for its own UI. A payload that ends at INTO OUTFILE '...' therefore reaches the server with an extra clause attached, and the statement no longer behaves as written. Terminating the payload with a comment removes the appended clause from the executed statement.

The underlying capability is MySQL's own: INTO OUTFILE can follow a UNION SELECT, and the columns of the union can be filled with literal text rather than table data, as long as the column count matches. The report does not discuss any server-side defence that was or was not in place.

Impact

Where a SQL injection exists, this technique lets an attacker write attacker-chosen content to a file on the database server. Writing a script into a web-served directory is the step from SQL injection to code execution. The report frames the whole exercise as motivation to look for SQL injection in real targets, and does not describe a specific production target or its configuration.

Fix

No fix is described. The PMA behaviour is presented as a client-side quirk to work around, not as a vulnerability to patch.

Variants to test

  1. Append a comment to any INTO OUTFILE payload and see whether a client that rewrites queries (PMA or similar) now executes it as written. A positive result is the file appearing at the chosen path.
  2. Compare the same payload in a web SQL client and in the mysql shell. A difference between the two points at client-side rewriting rather than at the server.
  3. Replace the table in the second SELECT with literal strings, keeping the column count identical to the first SELECT. A positive result is the chosen text landing in the output file.
  4. Put a WHERE clause in the first SELECT and check that the write still happens. A positive result is the file written with the filtered rows.
  5. Read the exact statement the client executed whenever a payload fails, rather than trusting what was typed. A positive result is spotting an appended clause such as a LIMIT that was never written.

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. 10 min read.

What happened, in plain words

This is a short story about a small detail that can decide whether a database attack works or fails. The author was testing a technique that lets an attacker who has found a way into a database also write a file onto the database server. Writing a file is a big deal, because if you can write a file into a folder the web server hands out to visitors, you can often write a program and get it to run. That chain, from breaking into the database to running your own code, is what the title means by SQLi2RCE: SQL injection to remote code execution.

Before the details, three ideas need explaining, because everything below depends on them.

  • Database: a program that stores information in tables, like a very large, very strict spreadsheet. Each table has rows (records) and columns (fields). A query is an instruction you send to the database, written in a language called SQL, asking it to read, change or write data.
  • SQL injection (SQLi): a bug where a website builds a database query out of something a user typed, without keeping the two separate. If the site pastes your input straight into the query, you can type SQL of your own and the database will run it as if the site had asked for it. It is like a form that says "write your name here" and then reads your name out loud to a clerk as an instruction; if you write "and also open the safe", the clerk may obey.
  • phpMyAdmin (PMA): a popular web page for managing a MySQL database. Instead of typing commands in a terminal, you open it in a browser, type SQL into a box and press a button. It is a web SQL client: a friendly front end that passes your SQL to the database for you. The author uses it here as the place where the queries are run.

The author was checking one specific feature: whether the SQL command INTO OUTFILE works when it is attached to a UNION SELECT. INTO OUTFILE tells the database "take the result of this query and save it as a file at this path". UNION SELECT glues the results of two queries together into one list of rows. Chained together, they are the classic way to go from SQL injection to writing a file on the server.

The query seemed to fail. Then the author looked more carefully at what PMA had actually sent to the database, and saw it was not exactly what had been typed. PMA had quietly added a piece to the end. Once the author understood that, a small change made the query work, and a second experiment showed the same trick can write text the attacker chooses, not just data from a table.

The author is clear that the injection was authorised and that the PMA behaviour is not the point. The point is what it teaches about combining INTO OUTFILE with UNION, and how important it is to read error messages instead of assuming the database is at fault.

The setup

The author works in two places and compares them:

  1. phpMyAdmin, in a browser, where SQL is typed into a box and run through the web interface.
  2. The mysql command line client, in a terminal, where SQL is typed directly and sent to the database with no web page in between.

The table used in every example is called users, and the file path used in the examples is /tmp/lol. On Linux and similar systems, /tmp is a scratch folder for temporary files. No versions, accounts or permissions are given.

Finding it

While testing whether INTO OUTFILE works together with UNION, the query appeared to fail. Looking closer at what PMA had actually run, the author noticed the query was not exactly what had been typed.

PMA had run:

SQL
SELECT * FROM `users` UNION SELECT * FROM `users` INTO OUTFILE '/tmp/lol' LIMIT 0, 25

while the author had written:

SQL
SELECT * FROM `users` UNION SELECT * FROM `users` INTO OUTFILE '/tmp/lol'
From the original on medium.com

Line by line, in plain words:

  • SELECT * FROM users means "give me every column (*) of every row in the table called users". The backticks around the table name are just quoting, so the database knows users is a name and not a keyword.
  • UNION means "and also add the rows from the next query to the same result".
  • SELECT * FROM users again is the second query, producing the same columns as the first. A UNION only works if both sides produce the same number of columns, so repeating the same query is a simple way to guarantee that.
  • INTO OUTFILE '/tmp/lol' means "instead of showing me the result, save it as a file at /tmp/lol on the database server".
  • LIMIT 0, 25 means "show at most 25 rows, starting from the first one". This is the part the author never typed. PMA added it, presumably so its results page does not try to display an enormous table.

That extra clause is the whole problem. The database receives a statement that ends with LIMIT 0, 25 instead of ending at the file path, and a statement that has been changed is not the statement the author meant to test. It is like handing a shop assistant a note saying "put this in the box" and the assistant adding "and then count twenty-five of them" before acting on it.

Unsure what to make of it, the author passed the problem to an AI assistant, saying it did not work, and got back the answer that it should work.

From the original on medium.com

Making it work

Step 1: add a comment line

The author tried again, this time appending a comment line to the payload.

From the original on medium.com

With the comment in place the query worked. The author notes that this was an authorised SQL injection in PMA, and that the real lesson is how important it is to read errors and understand them.

Why a comment fixes it: SQL has a way of marking the rest of a line as a note to humans, which the database ignores. In MySQL, -- (two dashes followed by a space) starts such a comment, and # does the same. If the payload ends with a comment marker, anything the client appends after it, including the LIMIT 0, 25, becomes part of the comment and is never executed. The statement the database sees now ends where the author intended. It is the programming equivalent of writing "ignore everything after this line" at the bottom of a form.

The conclusion drawn is that PMA appends the LIMIT clause for its own interface purposes, because the SQL is being run from PMA, which is the web SQL client. The comment absorbs the clause PMA appends after the statement.

Step 2: confirm the cause is PMA, not MySQL

To confirm that the problem exists only on the PMA side, the author ran the same test first in the mysql shell client.

From the original on medium.com

This step matters because there are two suspects: the database itself, or the web page in front of it. Running the same statement in a client that does not rewrite queries removes the middleman. If it behaves as written there, the database is fine and the web client is the one changing things. This is a general debugging habit: when something behaves strangely, change one part of the setup at a time and see which change makes the difference.

Step 3: select the text to write, keeping the column count

The next idea was to stop selecting from the table and instead select the text to be written, keeping the same number of columns so the UNION does not error.

From the original on medium.com

This worked. The author also notes that a WHERE statement placed in the first SELECT does not interfere, which is described as useful for real case escalations.

Why this matters: in a real attack, you usually do not want to dump a table into a file. You want to write content you control, such as a small script. A UNION requires both sides to produce the same number of columns, so if the first query returns, say, three columns, the second query must also produce three. You can do that by selecting literal values instead of table columns, for example three quoted strings. The database then writes those strings into the file. The note about WHERE means you can still filter the first query, for example to pick one specific row, and the write still happens, which is handy when you need the output to depend on data already in the database.

Why it happened

The behaviour that caused the initial failure is on the client side: PMA rewrites the statement it is given, appending LIMIT 0, 25 for its own interface. A payload that ends at INTO OUTFILE '...' therefore reaches the server with an extra clause attached, and the statement no longer behaves as written. Ending the payload with a comment removes the appended clause from the executed statement.

The underlying capability belongs to MySQL itself: INTO OUTFILE can follow a UNION SELECT, and the columns of the union can be filled with literal text rather than table data, as long as the column count matches. The report does not discuss any server-side defence that was or was not in place.

What an attacker could do with it

Where a SQL injection exists, this technique lets an attacker write attacker-chosen content to a file on the database server. The FILE privilege is a permission that allows a database account to read and write files on the machine, and most accounts do not have it. Writing a script into a folder the web server serves to visitors is the step from SQL injection to code execution: the attacker writes a small program, then asks the web server for it in a browser, and the server runs it.

The report frames the whole exercise as motivation to look for SQL injection in real targets, and does not describe a specific production target or its configuration.

How to defend against it

No fix is described. The PMA behaviour is presented as a client-side quirk to work around, not as a vulnerability to patch. secure_file_priv is a MySQL setting that limits which folders file operations are allowed to touch; pointing it at a folder the web server does not serve, or leaving it empty in a way that disables file writes, removes the path from database to code execution.

Things worth trying yourself

  1. Append a comment to any INTO OUTFILE payload and see whether a client that rewrites queries (PMA or similar) now executes it as written. A positive result is the file appearing at the chosen path.
  2. Compare the same payload in a web SQL client and in the mysql shell. A difference between the two points at client-side rewriting rather than at the server.
  3. Replace the table in the second SELECT with literal strings, keeping the column count identical to the first SELECT. A positive result is the chosen text landing in the output file.
  4. Put a WHERE clause in the first SELECT and check that the write still happens. A positive result is the file written with the filtered rows.
  5. Read the exact statement the client executed whenever a payload fails, rather than trusting what was typed. A positive result is spotting an appended clause such as a LIMIT that was never written.

The author ends by saying that probing is how you learn syntax, that no guide will tell you every detail of a language, and that this is another reason to go looking for SQL injection in real targets.

Glossary

  • Database: a program that stores data in tables of rows and columns and answers queries about it.
  • Query: an instruction sent to a database, written in SQL.
  • SQL: Structured Query Language, the language used to talk to relational databases.
  • SQL injection (SQLi): a bug where user input is pasted into a database query, letting the user change the query itself.
  • phpMyAdmin (PMA): a web page for managing a MySQL database from a browser.
  • Web SQL client: a browser-based tool that sends the SQL you type to a database for you.
  • MySQL: a widely used relational database system.
  • mysql command line client: a terminal program that sends SQL directly to a MySQL database, with no web page in between.
  • SELECT: the SQL command that reads data.
  • UNION: an SQL operator that joins the results of two queries into one result; both sides must have the same number of columns.
  • INTO OUTFILE: an SQL clause that saves a query's result to a file on the database server instead of returning it.
  • LIMIT: an SQL clause that caps how many rows are returned.
  • Comment (SQL): text the database ignores, started in MySQL by -- or #; used here to swallow anything appended after the payload.
  • WHERE: an SQL clause that filters which rows a query acts on.
  • FILE privilege: a database permission that allows an account to read and write files on the server.
  • secure_file_priv: a MySQL setting that restricts which folders file operations may use.
  • Remote code execution (RCE): getting your own program to run on someone else's server.
  • Payload: the piece of input an attacker sends to trigger or exploit a bug.
  • Privilege: a permission granted to a database account.
  • Web server: the program that answers browser requests and hands out pages and files.

Similar reports

View all

Same bug class and techniques, clearest first.

SQL injection

The lesson behind this report.

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.