2.4.8 From SQLi to Server Takeover
From SQLi to Server Takeover
In this section, advanced features provided by MS SQL Server and MySQL are explored, revealing how these features can be exploited to gain access to the DBMS server machine.
Advanced SQL Server Exploitation
xp_cmdshell:
xp_cmdshellis a stored procedure in SQL Server that provides advanced features.To run an OS command:
EXEC master..xp_cmdshell '<command>'Not enabled by default, requires sa privileges.
Enable and disable:
EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE; -- After tests EXEC sp_configure 'xp_cmdshell', 0; EXEC sp_configure 'show advanced options', 0; RECONFIGURE;
Internal Network Host Enumeration
Use
xp_cmdshellto launch commands.Ping command:
EXEC master.dbo.xp_cmdshell 'ping <target IP address>'Use execution time to infer ping result.
Port Scanning (OPENROWSET)
Use
OPENROWSETfor remote server table access.Exploit for port scanning:
Detect open/closed ports based on error messages.
Reading the File System
Use
EXEC master..xp_cmdshell 'dir <target directory>'to list directory contents.Save output to a web-accessible folder or read a file and put content into a table for extraction.
Uploading Files
Insert file into a table in the MS SQL database.
Retrieve it from our server:
Storing Command Results into a Temporary Table
Create a temporary table to hold stored procedure output:
Craft argument for
xp_cmdshell.Execute command and store results:
Conclusion:
Advanced SQL Server exploitation involves leveraging features like
xp_cmdshellfor OS command execution, internal network host enumeration, port scanning, reading the file system, uploading and downloading files, and storing command results for further analysis.
Advanced MySQL Exploitation
Reading the File System:
Use
LOAD_FILE('<text file path>')to read files.For binary files:
SELECT HEX(LOAD_FILE('<text file path>')).
Uploading Files:
Use
SELECT ... INTO DUMPFILEto write query results to a file.Convert binary file to hex-string and upload.
Executing Shell Commands:
MySQL doesn't provide a direct method for running shell commands.
Use User Defined Functions (UDF) for custom functions like
sys_evalandsys_exec.Upload shared objects (SO) or dynamic-link library (DLL) files to the target server.
Execute commands:
Conclusion:
Advanced MySQL exploitation includes reading the file system, uploading files, and executing shell commands using User Defined Functions (UDFs).