Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:22823
HistoryNov 24, 2009 - 12:00 a.m.

Code to mitigate IE STYLE zero-day

2009-11-2400:00:00
vulners.com
31

/*

This code is for a DLL that loads into Internet Explorer as a BHO and
modifies MSHTML.DLL in memory to render attempts to exploit this new
IE vulnerability inert. It does that by forcing a "controlled crash"
at a high address, instead of letting EIP reach an MSHTML-dependent
address that could fall within the heap-sprayable zone. It's not a
patch, or a "fix" in any pure sense – it's just a mitigation.

The vulnerability details I've figured out are that
MSHTML!CDispNode::SetExpandedClipRect ORs a CDispScroller instance's
vtable pointer by 2, then MSHTML!CLayout::GetFirstContentDispNode
tries to call a function (at +2Ch on IE 6, +30h on IE 7) from the
vtable. This makes exploitability completely dependent on the
system's version of MSHTML.DLL, and all but rules out successful
exploitation in 64-bit Internet Explorer.

The mitigation works by replacing one function pointer in the vtable
with a pointer for which the low 2 bytes are 0xCCCC, but at which the
code is functionally equivalent. Legitimate virtual function calls
work will as usual, while exploitation attempts will arrive at EIP =
0xCCCCxxxx (not exploitable) rather than 0xyyyyxxxx (exploitable for
some yyyy).

The following snippet is a pared-down, harmless proof-of-concept to
illustrate the fundamental elements of the vulnerability. < and >
have been replaced by # to avoid setting off alarms.

#!DOCTYPE#
#STYLE#* { margin: 0; overflow: scroll }#/STYLE#
#BODY ONLOAD="document.getElementsByTagName('STYLE')[0].outerHTML++"#

The !DOCTYPE tag is necessary to cause
MSHTML!CFlowLayout::CalcSizeCore to call
CFlowLayout::CalcSizeCoreCSS1Strict (the vulnerable code path) instead
of CFlowLayout::CalcSizeCoreCompat. The STYLE needs to apply to the
BODY, but the * illustrates that "body" appearing there shouldn't be
relied upon when constructing any detection signatures. The ++ works
as well as anything to modify 'outerHTML'.

This code has received minimal testing and is not guaranteed to stop
all attacks. Use it at your own risk.

Thanks to MMM for the sacrificial system. Greets to the November birthday crew.

– Derek

*/

////////////////////////////////////////////////////////////////
// iebsfix1.cpp
//==============================================================
// Dirty mitigation for the Internet Explorer 6/7
// getElementsByTagName Body Style zero-day. Downgrades an
// exploitation attempt to a harmless crash.
//
// This mitigation is for 32-bit (x86) Windows only – it does
// not work on 64-bit Windows, even though 64-bit Internet
// Explorer is technically affected.
//
// To build:
//
// 1. Start Visual Studio 2008 (2005 should also work)
// 2. File -> New -> Project
// 3. Choose Visual C++: Win32: Win32 Project
// 4. Enter "iebsfix1" for the name
// 5. In the Win32 Application Wizard, choose an
// "Application type" of "DLL", and under "Additional
// options", check "Empty project"
// 6. In the Solution Explorer, right-click on "Source Files",
// Add -> New Item
// 7. Choose "C++ File (.cpp)" and enter "iebsfix1.cpp" for
// the name
// 8. Paste all of this source code into the new .cpp file
// 9. In the Solution Explorer, right-click again on "Source
// Files", Add -> New Item
// 10. Choose "Module-Definition File (.def)" and enter
// "iebsfix1.def" for the name
// 11. Paste everything in the block comment below (between the
// rows of ***'s) into the new .def file
// 12. Build -> Configuration Manager; for "Active solution
// configuration", choose "Release"
// 13. For maximum portability, Project -> Properties,
// Configuration Properties: C/C++: Code Generation: set
// "Runtime Library" to "Multi-threaded (/MT)"; this will
// keep iebsfix1.dll from requiring MSVCR
.DLL
// 14. (While you're in there, Project -> Properties,
// Configuration Properties: Linker: Input, and make sure
// that "Module Definition File" contains "iebsfix1.def")
// 15. Build -> Build Solution
//
// To use, copy "iebsfix1.dll" to the Windows SYSTEM32
// directory and run "regsvr32 iebsfix1.dll" as an
// administrator.
//
// To uninstall, run "regsvr32 /u iebsfix1.dll".
//
// The DLL self-registers as a Browser Helper Object, but it
// doesn't actually do anything BHO-like – it just hooks
// MSHTML.DLL during DllGetClassObject, then "fails." Being a
// BHO is a convenient way to get loaded into Internet Explorer.
// (Note that it may also load into Explorer.) If it can't
// hook the system's MSHTML.DLL, it will display a message box
// informing the user of the failure.
//
// NO WARRANTIES. Use at your own risk. Redistribution of this
// source code in its original, unmodified form is permitted.
//
// Derek Soeder - 11/22/2009
////////////////////////////////////////////////////////////////

/**** Paste the following into a new .def file: *************

LIBRARY "iebsfix1.dll"

EXPORTS
DllCanUnloadNow PRIVATE
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE

***************************************************************/

#define IEBSFIX1_CLSID_W L"{802af903-a984-4481-8376-c103ade582e6}"

#define WIN32_LEAN_AND_MEAN
#define _CRT_NON_CONFORMING_SWPRINTFS
#define _CRT_SECURE_NO_WARNINGS

#include <windows.h>
#include <olectl.h>
#include <stdio.h>

////////////////////////////////////////////////////////////////
// MSHTML!CDispScroller vtable hooking
////////////////////////////////////////////////////////////////

PVOID * find_vtable_slot(
HMODULE hmMSHTML )
{
PIMAGE_DOS_HEADER pmz;
PIMAGE_NT_HEADERS32 ppe;
UINT_PTR codestart;
PBYTE pbcode;
SIZE_T cbremain;
UINT_PTR ptr;
size_t i;
PVOID * ppfn;

    pmz = &#40;PIMAGE_DOS_HEADER&#41;
            &#40;&#40;UINT_PTR&#41;hmMSHTML &amp; ~&#40;UINT_PTR&#41;0xFFFFU&#41;;
    if &#40;pmz-&gt;e_magic != IMAGE_DOS_SIGNATURE || pmz-&gt;e_lfanew &lt;= 0&#41;
            return NULL;

    ppe = &#40;PIMAGE_NT_HEADERS32&#41;
            &#40;&#40;LONG_PTR&#41;pmz + pmz-&gt;e_lfanew&#41;;
    if &#40; ppe-&gt;Signature != IMAGE_NT_SIGNATURE ||
         ppe-&gt;FileHeader.Machine != IMAGE_FILE_MACHINE_I386 ||
         ppe-&gt;OptionalHeader.Magic !=
            IMAGE_NT_OPTIONAL_HDR32_MAGIC &#41;
    {
            return NULL;
    }

    codestart = &#40;UINT_PTR&#41;pmz + ppe-&gt;OptionalHeader.BaseOfCode;
    pbcode = &#40;PBYTE&#41;codestart;

    // find instructions that assign to memory at [reg] a pointer
    // to constant data stored in the code section; vtable
    // pointer initialization instructions are a subset of these

    for &#40; cbremain = ppe-&gt;OptionalHeader.SizeOfCode;
          cbremain &gt;= 7; pbcode++, cbremain-- &#41;
    {       // C7/0x/vtableptr -- MOV [reg], vtableptr
            if &#40;pbcode[0] != 0xC7U&#41; continue;
            if &#40; pbcode[1] &lt;= 0x03 ||       // [EAX/ECX/EDX/EBX]
                 pbcode[1] == 0x06 ||       // [ESI]
                 pbcode[1] == 0x07 &#41;        // [EDI]
            {
                    ptr = *&#40;DWORD *&#41;&#40;pbcode + 2&#41;;
            }
            // C7/45/00/vtableptr -- MOV [EBP+0], vtableptr
            else if &#40;pbcode[1] == 0x45 &amp;&amp; pbcode[2] == 0x00&#41;
                    ptr = *&#40;DWORD *&#41;&#40;pbcode + 3&#41;;
            else    continue;

            // pointer to pointers, must be machine word aligned

            if &#40;&#40;ptr &amp; 3&#41; != 0&#41; continue;

            // if it doesn&#39;t point to at least 25 code-section
            // pointers, we&#39;re not interested

            for &#40;i = 0; i &lt; 25; i++&#41;
            {
                    if &#40; ptr &lt; codestart || &#40;ptr - codestart&#41; &gt;=
                         ppe-&gt;OptionalHeader.SizeOfCode &#41;
                    {
                            break;
                    }
            }

            if &#40;i &lt; 25&#41; continue;

            ppfn = &#40;PVOID *&#41;ptr;

            // IE 6: [11], [12], and [14] return 1; [13] returns 0
            // IE 7: [12], [13], and [15] return 1; [14] returns 0
            //      &#40;CalcDispInfoForViewport was inserted at [11]&#41;

            if &#40; ppfn[11] == ppfn[12] &amp;&amp; ppfn[11] != ppfn[13] &amp;&amp;
                 ppfn[11] == ppfn[14] &#41;
            {
                    ppfn += 11;
            }
            else if &#40; ppfn[12] == ppfn[13] &amp;&amp;
                    ppfn[12] != ppfn[14] &amp;&amp; ppfn[12] == ppfn[15] &#41;
            {
                    ppfn += 12;
            }
            else    continue;

            // 33/C0/40/C3 -- XOR EAX, EAX / INC EAX / RET
            // 6A/01/58/C3 -- PUSH 1 / POP EAX / RET
            if &#40; *&#40;DWORD *&#41;*ppfn == 0xC340C033U ||
                 *&#40;DWORD *&#41;*ppfn == 0xC358016AU &#41;
            {
                    return ppfn;
            }
    } //for&#40;cbremain&gt;=7&#41;

    return NULL;

} //find_vtable_slot

BOOL apply_mitigation(
PVOID * ppfnVTableSlot )
{
PBYTE pbhook;
DWORD dwprot;

    // we &quot;hook&quot; the next vtable slot and make sure the two low
    // bytes of the function pointer are unusably high, so the
    // call to [ppfnVTableSlot | 2] will always crash

    pbhook = &#40;PBYTE&#41; VirtualAlloc&#40; NULL, 0x10000,
            MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE &#41;;

    if &#40;pbhook == NULL&#41; return FALSE;

    memset&#40; pbhook, 0xF4U, 0x10000 &#41;;       // F4 -- HLT

    // 33/C0/40/C3 -- XOR EAX, EAX / INC EAX / RET
    *&#40;DWORD *&#41;&#40;pbhook + 0xCCCCU&#41; = 0xC340C033U;

    // see?  now the virtual method does its &quot;return 1&quot; at address
    // xxxxCCCC instead of at whatever address inside MSHTML.DLL;
    // it&#39;ll still work fine, but those two low bytes of CCCC will
    // &quot;poison&quot; the exploit

    VirtualProtect&#40; pbhook, 0x10000, PAGE_EXECUTE_READ, &amp;dwprot &#41;;

    FlushInstructionCache&#40; GetCurrentProcess&#40;&#41;, pbhook, 0x10000 &#41;;

    // set the hook

    if &#40; !VirtualProtect&#40; ppfnVTableSlot + 1,
            sizeof&#40;ppfnVTableSlot[1]&#41;, PAGE_EXECUTE_READWRITE,
            &amp;dwprot &#41; &#41;
    {
            VirtualFree&#40; pbhook, 0, MEM_RELEASE &#41;;
            return FALSE;
    }

    ppfnVTableSlot[1] = &#40;pbhook + 0xCCCCU&#41;;

    VirtualProtect&#40; ppfnVTableSlot + 1, sizeof&#40;ppfnVTableSlot[1]&#41;,
            dwprot, &amp;dwprot &#41;;

    FlushInstructionCache&#40; GetCurrentProcess&#40;&#41;,
            ppfnVTableSlot + 1, sizeof&#40;ppfnVTableSlot[1]&#41; &#41;;

    return TRUE;

} //apply_mitigation

////////////////////////////////////////////////////////////////
// Browser Helper Object DLL
////////////////////////////////////////////////////////////////

HINSTANCE g_hinstMyself;
BOOL g_fInitialized;
CRITICAL_SECTION g_csInit;

HMODULE g_hmMSHTML;

STDAPI DllUnregisterServer()
{
HKEY hkey, hkey2, hkey3;

    if &#40; RegOpenKeyW&#40; HKEY_LOCAL_MACHINE, L&quot;SOFTWARE&#92;&#92;&quot;
            L&quot;Classes&#92;&#92;CLSID&quot;, &amp;hkey &#41; == ERROR_SUCCESS &#41;
    {
            if &#40; RegOpenKeyW&#40; hkey, IEBSFIX1_CLSID_W,
                    &amp;hkey2 &#41; == ERROR_SUCCESS &#41;
            {
                    if &#40; RegOpenKeyW&#40; hkey2, L&quot;InprocServer32&quot;,
                            &amp;hkey3 &#41; == ERROR_SUCCESS &#41;
                    {
                            RegDeleteValueW&#40; hkey3, NULL &#41;;
                            RegCloseKey&#40; hkey3 &#41;;
                            RegDeleteKeyW&#40; hkey2,
                                    L&quot;InprocServer32&quot; &#41;;
                    }

                    RegCloseKey&#40; hkey2 &#41;;
                    RegDeleteKeyW&#40; hkey, IEBSFIX1_CLSID_W &#41;;
            }

            RegCloseKey&#40; hkey &#41;;
    }

    if &#40; RegOpenKeyW&#40; HKEY_LOCAL_MACHINE, L&quot;SOFTWARE&#92;&#92;Microsoft&#92;&#92;&quot;
            L&quot;Windows&#92;&#92;CurrentVersion&#92;&#92;Explorer&quot;,
            &amp;hkey &#41; == ERROR_SUCCESS &#41;
    {
            if &#40; RegOpenKeyW&#40; hkey, L&quot;Browser Helper Objects&quot;,
                    &amp;hkey2 &#41; == ERROR_SUCCESS &#41;
            {
                    RegDeleteKeyW&#40; hkey2, IEBSFIX1_CLSID_W &#41;;
                    RegCloseKey&#40; hkey2 &#41;;
                    RegDeleteKeyW&#40; hkey,
                            L&quot;Browser Helper Objects&quot; &#41;;
            }

            RegCloseKey&#40; hkey &#41;;
    }

    return S_OK;

} //DllUnregisterServer

STDAPI DllRegisterServer()
{
HKEY hkey, hkey2;
WCHAR wszmod[1024];
LSTATUS lret;

    if &#40; RegCreateKeyW&#40; HKEY_LOCAL_MACHINE,
            L&quot;SOFTWARE&#92;&#92;Classes&#92;&#92;CLSID&#92;&#92;&quot; IEBSFIX1_CLSID_W
            L&quot;&#92;&#92;InprocServer32&quot;, &amp;hkey &#41; != ERROR_SUCCESS &#41;
    {

_fail:
DllUnregisterServer();
return SELFREG_E_CLASS;
}

    GetModuleFileNameW&#40; g_hinstMyself, wszmod,
            &#40;sizeof&#40;wszmod&#41; / sizeof&#40;wszmod[0]&#41;&#41; &#41;;

    lret = RegSetValueW&#40; hkey, NULL, REG_SZ, wszmod,
            &#40;wcslen&#40; wszmod &#41; + 1&#41; * sizeof&#40;wszmod[0]&#41; &#41;;

    RegCloseKey&#40; hkey &#41;;

    if &#40;lret != ERROR_SUCCESS&#41; goto _fail;

    if &#40; RegCreateKeyW&#40; HKEY_LOCAL_MACHINE, L&quot;SOFTWARE&#92;&#92;&quot;
            L&quot;Microsoft&#92;&#92;Windows&#92;&#92;CurrentVersion&#92;&#92;Explorer&#92;&#92;&quot;
            L&quot;Browser Helper Objects&quot;, &amp;hkey &#41; != ERROR_SUCCESS &#41;
    {
            goto _fail;
    }

    lret = RegCreateKeyW&#40; hkey, IEBSFIX1_CLSID_W, &amp;hkey2 &#41;;

    RegCloseKey&#40; hkey &#41;;

    if &#40;lret != ERROR_SUCCESS &#41; goto _fail;

    RegCloseKey&#40; hkey2 &#41;;

    return S_OK;

} //DllRegisterServer

STDAPI DllCanUnloadNow()
{
return S_OK;
}

STDAPI DllGetClassObject(
REFCLSID rclsid,
REFIID riid,
LPVOID * ppv )
{
PVOID * ppfn;
WCHAR wszbuf[256];

    EnterCriticalSection&#40; &amp;g_csInit &#41;;

__try
{
    if &#40;!g_fInitialized&#41;
    {
            // MSHTML should already be loaded; this extra
            // reference will keep it from ever unloading
            g_hmMSHTML = LoadLibraryW&#40; L&quot;mshtml.dll&quot; &#41;;

            ppfn = find_vtable_slot&#40; g_hmMSHTML &#41;;

            if &#40;ppfn != NULL&#41;
            {
                    swprintf&#40; wszbuf,

L"IEBSFix1: Found vtable slot at %p in MSHTML_%p\r\n",
ppfn, g_hmMSHTML );
OutputDebugStringW( wszbuf );

                    apply_mitigation&#40; ppfn &#41;;
            }
            else
            {
                    swprintf&#40; wszbuf,

L"IEBSFix1: FAILED to find vtable slot in MSHTML_%p\r\n",
g_hmMSHTML );
OutputDebugStringW( wszbuf );

                    MessageBoxW&#40; NULL,

L"The Internet Explorer 6/7 getElementsByTagName Body Style zero-day "
L"mitigation, also known as IEBSFix1, is not protecting your system "
L"because it is incompatible with this version of Internet Explorer."
L"\n\nTo remove IEBSFix1, run \"regsvr32 /u iebsfix1.dll\" as an "
L"administrator.",
L"IEBSFix1", MB_ICONWARNING|MB_OK );
}

            g_fInitialized = TRUE;
    }
}
__finally
{
    LeaveCriticalSection&#40; &amp;g_csInit &#41;;
}

    return CLASS_E_CLASSNOTAVAILABLE;

} //DllGetClassObject

BOOL WINAPI DllMain(
HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved )
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
g_hinstMyself = hinstDLL;
g_fInitialized = FALSE;
InitializeCriticalSection( &g_csInit );
}

    return TRUE;

} //DllMain