SQLi to Remote Code Execution
SQL injection can escalate to OS command execution when the database user has FILE privilege or sysadmin rights. MySQL INTO OUTFILE writes a web shell to the web root. MSSQL xp_cmdshell executes OS commands directly from SQL. Both require specific privilege conditions confirmed in vulnerability discovery.
MySQL — Check FILE Privilege
FILE privilege is required for INTO OUTFILE. Confirm the current user has it:
Y
File_priv Y and an empty secure_file_priv confirm unrestricted file write. Find the web root path before writing:
/var/lib/mysql/
MySQL — Write a Web Shell
Write a PHP web shell to the web root using INTO OUTFILE:
uid=33(www-data) gid=33(www-data) groups=33(www-data)
Command execution confirmed. Get a reverse shell:
MSSQL — Enable and Abuse xp_cmdshell
When SQLi hits MSSQL with sysadmin rights, enable xp_cmdshell through stacked queries:
nt authority\system
References
-
MySQL — SELECT INTO OUTFILEdev.mysql.com/doc/refman/8.0/en/select-into.html (opens in new tab)
INTO OUTFILE syntax and secure_file_priv interaction
-
Microsoft — xp_cmdshelllearn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/xp-cmdshell-transact-sql (opens in new tab)
xp_cmdshell enable procedure and permission requirements
Was this helpful?
Your feedback helps improve this page.