5.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_cmdshell
is 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:
Internal Network Host Enumeration
Use
xp_cmdshell
to launch commands.Ping command:
EXEC master.dbo.xp_cmdshell 'ping <target IP address>'
Use execution time to infer ping result.
Port Scanning (OPENROWSET)
Use
OPENROWSET
for 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_cmdshell
for 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 DUMPFILE
to 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_eval
andsys_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).
Last updated