Writing Files
Last updated
Last updated
The final SQL query is the following:
So, similar to other UNION
injection queries, we can get the above query result with the following payload. Remember to add two more columns 1
& 4
as junk data to have a total of 4 columns':
And the result shows that the secure_file_priv
value is empty, meaning that we can read/write files to any location.
To use it, we can add INTO OUTFILE '...'
after our query to export the results into the file we specified. The below example saves the output of the users
table into the /tmp/credentials
file:
If we go to the back-end server and cat
the file, we see that table's content:
It is also possible to directly SELECT
strings into files, allowing us to write arbitrary files to the back-end server.
When we cat
the file, we see that text:
Let's try writing a text file to the webroot and verify if we have write permissions. The below query should write file written successfully!
to the /var/www/html/proof.txt
file, which we can then access on the web application:
To read the server configuration, like Apache's configuration found at
/etc/apache2/apache2.conf
, Nginx's configuration at/etc/nginx/nginx.conf
, or IIS configuration at%WinDir%\System32\Inetsrv\Config\ApplicationHost.config.
The UNION
injection payload would be as follows:
We don’t see any errors on the page, which indicates that the query succeeded. Checking for the file proof.txt
in the webroot, we see that it indeed exists: http://SERVER_IP:PORT/proof.txt
We can reuse our previous UNION
injection payload, and change the string to the above, and the file name to shell.php
:
Once again, we don't see any errors, which means the file write probably worked. This can be verified by browsing to the /shell.php
file and executing commands via the 0
parameter, with ?0=id
in our URL: http://SERVER_IP:PORT/shell.php?0=id
Now target to http://83.136.252.13:50193/shell.php?0=id
Or we can search online for other possible configuration locations. Furthermore, we may run a fuzzing scan and try to write files to different possible web roots, using or .