fix your broken applications: the black art of shims

98

Upload: truongnga

Post on 01-Jan-2017

254 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Fix Your Broken Applications: The Black Art of Shims
Page 2: Fix Your Broken Applications: The Black Art of Shims

Fix your Broken Applications: The Black Art of Shims

Chris JacksonThe App Compat GuyMicrosoft CorporationWCL304

Page 3: Fix Your Broken Applications: The Black Art of Shims

Application Windows

How Shims Work

Shim DLL

ImportFunction

ExportFunction

ImportFunction

ExportFunction

Page 4: Fix Your Broken Applications: The Black Art of Shims

Shims for User Account Control

Page 5: Fix Your Broken Applications: The Black Art of Shims

Standard User Analyzer

Application

Windows

AppVerifier Logs XMLLuaPriv

Page 6: Fix Your Broken Applications: The Black Art of Shims

SUA MitigationsSUA can recommend:

ElevateCreateProcessForceAdminAccessLocalMappedObjectVirtualizeDeleteFileVirtualizeHKCRLiteCorrectFilePathsVirtualRegistry

Page 7: Fix Your Broken Applications: The Black Art of Shims

ElevateCreateProcessSymptomsERROR_ELEVATION_REQUIRED

Fix descriptionTries again, requesting elevation

Page 8: Fix Your Broken Applications: The Black Art of Shims

ForceAdminAccess

SymptomsFails explicit administrator check

Fix descriptionLies

Page 9: Fix Your Broken Applications: The Black Art of Shims

ForceAdminAccess Shim for IsUserAnAdmin:

return TRUE;

Page 10: Fix Your Broken Applications: The Black Art of Shims

LocalMappedObject

SymptomsCan’t create in Global namespace

Fix descriptionCreates in Local namespace

Page 11: Fix Your Broken Applications: The Black Art of Shims

VirtualizeDeleteFile

SymptomsCan’t delete files

Fix descriptionPretends to delete files

Page 12: Fix Your Broken Applications: The Black Art of Shims

VirtualizeHKCRLite

SymptomsCan’t register COM components

Fix descriptionRegisters them per-user

Page 13: Fix Your Broken Applications: The Black Art of Shims

SUA Mitigationsdemo

Page 14: Fix Your Broken Applications: The Black Art of Shims

UAC Manifests

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity type="win32" processorArchitecture="*" version="1.0.0.0" name="MyApplication.exe"/> <description>My totally sweet Vista application</description> <ms_asmv2:trustInfo xmlns:ms_asmv2="urn:schemas-microsoft-

com:asm.v2"> <ms_asmv2:security> <ms_asmv2:requestedPrivileges> <ms_asmv2:requestedExecutionLevel level="asInvoker || highestAvailable || requireAdministrator"/> </ms_asmv2:requestedPrivileges> </ms_asmv2:security> </ms_asmv2:trustInfo></assembly>

Page 15: Fix Your Broken Applications: The Black Art of Shims

Installer Detection

Legacy installers / updaters

SpecificInstaller

GenericInstaller

SpecificNonInstaller

Page 16: Fix Your Broken Applications: The Black Art of Shims

RunAsAdmin

SymptomsRequires admin

Fix descriptionPrompts for elevation

Page 17: Fix Your Broken Applications: The Black Art of Shims

RunAsHighest

SymptomsHad both admin and standard user views

Fix descriptionProvides most powerful token

Page 18: Fix Your Broken Applications: The Black Art of Shims

RunAsInvoker

SymptomsPrompting unnecessarily

Fix descriptionNo more prompt

Page 19: Fix Your Broken Applications: The Black Art of Shims

SpecificInstaller

SymptomsNot fixed as a legacy setup

Fix descriptionFlags it as a legacy setup

Page 20: Fix Your Broken Applications: The Black Art of Shims

SpecificNonInstaller

SymptomsFlagged as a legacy setup inappropriately

Fix descriptionNo longer flagged as a legacy setup

Page 21: Fix Your Broken Applications: The Black Art of Shims

Run Level Specificationdemo

Page 22: Fix Your Broken Applications: The Black Art of Shims

VirtualizeRegisterTypeLib

SymptomsRegistering type library fails

Fix descriptionRegisters type library per-user

Page 23: Fix Your Broken Applications: The Black Art of Shims

Shims for File and Registry Paths

Page 24: Fix Your Broken Applications: The Black Art of Shims

VirtualRegistry

SymptomsProblem reading/writing registry value

Fix descriptionReturns a different registry value

Command line parameters...

Page 25: Fix Your Broken Applications: The Black Art of Shims

VirtualRegistry Generic Fix

AddRedirect ( HKLM\Key ^ HKCU\Key ^ HKLM\Key2 ^ HKCU\Key2)

Page 26: Fix Your Broken Applications: The Black Art of Shims

VirtualRegistrydemo

Page 27: Fix Your Broken Applications: The Black Art of Shims

CorrectFilePaths

SymptomsProblem reading/writing a file

Fix descriptionRedirects to a different file

Page 28: Fix Your Broken Applications: The Black Art of Shims

CorrectFilePathsdemo

Page 29: Fix Your Broken Applications: The Black Art of Shims

Shims for User Interface Process Isolation

Page 30: Fix Your Broken Applications: The Black Art of Shims

UIPIEnableStandardMessages

SymptomsWindow messages not delivered

Fix descriptionAdds the message to the filter

ParametersMessageID1 MessageID2 MessageID3

Example:1055 1056 1057 1058 1069

Page 31: Fix Your Broken Applications: The Black Art of Shims

UIPIEnableCustomMessages

SymptomsCustom window messages not delivered

Fix descriptionAdds the custom window message to the allowed filter

ParametersMessageString1 MessageString2

Page 32: Fix Your Broken Applications: The Black Art of Shims

User Interface Process Isolationdemo

Page 33: Fix Your Broken Applications: The Black Art of Shims

Shims for Windows Resource Protection

Shims for

Page 34: Fix Your Broken Applications: The Black Art of Shims

WRPMitigation

SymptomsWriting protected operating system file / registry key

Fix descriptionLies

Page 35: Fix Your Broken Applications: The Black Art of Shims

WRPDllRegister

SymptomsRegistering protected OS component

Fix descriptionNew DllRegisterServer

Parameterscomponet1.dll;component2.dll

Example:hhctrl.ocx;itircl.dll;itss.dll

Page 36: Fix Your Broken Applications: The Black Art of Shims

WRPRegDeleteKey

SymptomsCan’t delete protected OS registry key

Fix descriptionLies

Page 37: Fix Your Broken Applications: The Black Art of Shims

Windows Resource Protectiondemo

Page 38: Fix Your Broken Applications: The Black Art of Shims

Shims for Operating System Version

Page 39: Fix Your Broken Applications: The Black Art of Shims

I Can't Find the > Key…Operating System Version Number Release Date

Windows 1.0 1.04 1985Windows 2.0 2.11 1987Windows 3.0 3.00 1990Windows NT 3.1 3.10.528 1993Windows for Workgroups 3.11 3.11 1993Windows NT Workstation 3.5 3.5.807 1994Windows NT Workstation 3.51 3.51.1057 1995Windows 95 4.0.950 1995Windows NT Workstation 4.0 4.0.1381 1996Windows 98 4.1.1998 1998Windows 98 Second Edition 4.1.2222 1999Windows Me 4.90.3000 2000Windows 2000 Professional 5.0.2195 2000Windows XP 5.1.2600 2001Windows Vista 6.0.6000 2006

Page 40: Fix Your Broken Applications: The Black Art of Shims

xxxVersionLie

Symptoms“Unsupported operating system”

Fix descriptionLies

Page 41: Fix Your Broken Applications: The Black Art of Shims

Version Lie ShimsWin95VersionLieWinNT4SP5VersionLieWin98VersionLieWin2000VersionLieWin2000SP1VersionLieWin2000SP2VersionLie

Win2000SP3VersionLieWinXPVersionLieWinXPSP1VersionLieWinXPSP2VersionLieWin2K3RTMVersionLieWin2K3SP1VersionLieVistaRTMVersionLie

Page 42: Fix Your Broken Applications: The Black Art of Shims

Version Lie LayersWin95NT4SP5Win98Win2000Win2000SP2Win2000SP3

WinXPWinXPSP1WinXPSP2WinXPSP2VersionLieWinSrv03WinSrv03SP1VistaRTM

Page 43: Fix Your Broken Applications: The Black Art of Shims

Shims and Layers

Windows

Shim

Application Child Application

Layer

Page 44: Fix Your Broken Applications: The Black Art of Shims

Layers: More Than Version LiesXPSP2 Layer:

DirectXVersionLieElevateCreateProcessEmulateSortingEnableLegacyExceptionHandlingInOLEFailObsoleteShellAPIsGlobalMemoryStatus2GB

HandleBadPtrHardwareAudioMixerLoadLibraryCWDNoGhostRedirectMP3CodecVirtualRegistryWinXPSP2VersionLie

Page 45: Fix Your Broken Applications: The Black Art of Shims

Operating System Versiondemo

Page 46: Fix Your Broken Applications: The Black Art of Shims

How to INSTALL Custom Shim Databases

Page 47: Fix Your Broken Applications: The Black Art of Shims

sdbinst.exe

%windir%\system32\sdbinst.exe

Page 48: Fix Your Broken Applications: The Black Art of Shims

sdbinst Command Line-q Quiet mode-u Uninstall-g Guid (uninstall only)-n Internal name (uninstall only)

Page 49: Fix Your Broken Applications: The Black Art of Shims

Deployment Options

Per enterprise

Per application

Page 50: Fix Your Broken Applications: The Black Art of Shims

Installing Custom Shim Databasesdemo

Page 51: Fix Your Broken Applications: The Black Art of Shims

API Hooking Option #1

Overwriting code

Locate the address of the function

Save the first few bytes

Overwrite with JMP

Page 52: Fix Your Broken Applications: The Black Art of Shims

Overwriting Code: Hooking

USER32!MessageBoxA:mov edi,edi ;nop for hot patchingpush ebp ;set up stack framemov ebp,esp ;””

Page 53: Fix Your Broken Applications: The Black Art of Shims

Overwriting Code: Hooking

USER32!MessageBoxA:jmp rel8 ;short jump to hookpush ebp ;we never get heremov ebp,esp ;””

Page 54: Fix Your Broken Applications: The Black Art of Shims

Overwriting Code: Challenges

CPU dependent

JUMP instructions vary between x86, x64, IA-64

Not thread safe

“…it is the programmer’s responsibility to ensure that no other threads are executing in the address space while a detour is inserted or removed.”

Page 55: Fix Your Broken Applications: The Black Art of Shims

API Hooking Option #2

Module import section

Modify address

No CPU dependency

No thread synchronization

Page 56: Fix Your Broken Applications: The Black Art of Shims

IAT Modification: Data Structures

MS-DOS Header

NT Headers

Section Headers

Section Images

PE Signature

File Headers

Optional Headers

Data Directories

Export Table

Import Table

Resource Table

Exception Table

...

Page 57: Fix Your Broken Applications: The Black Art of Shims

Original First Thunk

Time Date Stamp

Forwarder Chain

Imported DLL Name

First Thunk

...

IAT Modification: Import Table

0x1034

0x1047

...

GetModuleHandleA

LoadLibrary

...

kernel32.dll

user32.dll

advapi32.dll

...

Page 58: Fix Your Broken Applications: The Black Art of Shims

IAT Modification: Sample CodeRichter & Nasarre, 2008ULONG ulSize;PIMAGE_IMPORT_DESCRIPTOR pImportDesc = NULL;__try { pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData(hmodCaller, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &ulSize);} __except (InvalidReadExceptionFilter(GetExceptionInformation())) {}if (pImportDesc == NULL) return;for (; pImportDesc->Name; pImportDesc++) { PSTR pszModName=(PSTR)((PBYTE)hmodCaller + pImportDesc->Name); if (lstrcmpiA(pszModName, pszCalleeModName) == 0) { PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)((PBYTE)hmodCaller + pImportDesc->FirstThunk); for (; pThunk->u1.Function; pThunk++) { PROC* ppfn = (PROC*)&pThunk->u1.Function; BOOL bFound = (*ppfn == pfnCurrent); if (bFound) { if (!WriteProcessMemory(GetCurrentProcess(), ppfn, &pfnNew, sizeof(pfnNew), NULL) && (ERROR_NOACCESS == GetLastError())) { DWORD dwOldProtect; if (VirtualProtect(ppfn, sizeof(pfnNew), PAGE_WRITECOPY, &dwOldProtect)) { WriteProcessMemory(GetCurrentProcess(), ppfn, &pfnNew, sizeof(pfnNew), NULL); VirtualProtect(ppfn, sizeof(pfnNew), dwOldProtect, &dwOldProtect); } } return; } } }}

Page 59: Fix Your Broken Applications: The Black Art of Shims

Security: the Good

Application

Shim

Page 60: Fix Your Broken Applications: The Black Art of Shims

Security: Something to Think About

Crack resources to locate dialog item IDImplement hook for GetDlgItemTextTrick user into installing itYour user-mode API interception can now harvest passwords

Page 61: Fix Your Broken Applications: The Black Art of Shims

API Hookingdemo

Page 62: Fix Your Broken Applications: The Black Art of Shims

Compatibility Administrator

/x switch

SearchNot perfect

Query

Page 63: Fix Your Broken Applications: The Black Art of Shims

Shim Debug Spew

[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags]

"ShowDebugInfo"=dword:00000009

Debugger, DebugView, etc.

Page 64: Fix Your Broken Applications: The Black Art of Shims

Shim Logging

Environment variables:reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v SHIM_DEBUG_LEVEL /t REG_SZ /d 9 /f

reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v SHIM_FILE_LOG /t REG_SZ /d logfile.txt /f

%appdata%\logfile.txt

Page 65: Fix Your Broken Applications: The Black Art of Shims

Shim Diagnostic Toolsdemo

Page 66: Fix Your Broken Applications: The Black Art of Shims

When to Use Shims

Define standards:

Vender no longer in business

Internal applications

Support negotiable

Page 67: Fix Your Broken Applications: The Black Art of Shims

Custom SDBs Management

Custom SDBs defined by GUID

Affects updates

Define and enforce policy / workflow

Test / mitigation deployment

Centralized resources

Page 68: Fix Your Broken Applications: The Black Art of Shims

Custom SDB Deployment

Package with installer

Centralized enterprise SDB

Page 69: Fix Your Broken Applications: The Black Art of Shims

Managing Shims in the Enterprisehttp://go.microsoft.com/fwlink/?LinkId=107072

Page 70: Fix Your Broken Applications: The Black Art of Shims

Custom SDB Deploymentdemo

Page 71: Fix Your Broken Applications: The Black Art of Shims

(More) Shims for User Account Control

Page 72: Fix Your Broken Applications: The Black Art of Shims

Symptom

Taskbar flasher

CorrectShellExecuteHWND

Fix description

Use the currently active HWND

Page 73: Fix Your Broken Applications: The Black Art of Shims

RetryOpenServiceWithReadAccess

Symptoms

Access denied opening service

Fix description

Try again requesting fewer rights

Page 74: Fix Your Broken Applications: The Black Art of Shims

RetryOpenSCManagerWithReadAccess

Symptoms

Access denied opening Service Control Manager

Fix description

Try again requesting fewer rights

Page 75: Fix Your Broken Applications: The Black Art of Shims

(More) UAC Shimsdemo

Page 76: Fix Your Broken Applications: The Black Art of Shims

Shims for User Interface Issues

Page 77: Fix Your Broken Applications: The Black Art of Shims

DisableDWM

Symptoms

Not rendering correctly

Fix description

Turn off DWM when the app is running

Page 78: Fix Your Broken Applications: The Black Art of Shims

FakeLunaTheme

Symptoms

Hard to read

Fix description

Use XP Luna theme colors

Page 79: Fix Your Broken Applications: The Black Art of Shims

User Interface Shimsdemo

Page 80: Fix Your Broken Applications: The Black Art of Shims

Shims for Miscellaneous Issues

Page 81: Fix Your Broken Applications: The Black Art of Shims

EmulateSorting

Symptoms

Search functions fail

Fix description

Use legacy sorting tables

Page 82: Fix Your Broken Applications: The Black Art of Shims

SessionShim

Symptoms

Unable to talk to service

Fix description

Global Local original API

Parameters\\ delimited list of object names, otherwise all objects shimmed

Page 83: Fix Your Broken Applications: The Black Art of Shims

IgnoreAltTab

Symptoms

Alt-Tab or other special keys fail

Fix description

Filters out special keys

ParametersNOKEYS – doesn’t disable Filter Keys / Sticky Keys

OPENGL – do not shim if running in OpenGL

Page 84: Fix Your Broken Applications: The Black Art of Shims

IgnoreException

Symptoms

Unhandled exception (typically crashes)

Fix description

Ignores the exceptionYes, this should make you nervous…

Parameters...

Page 85: Fix Your Broken Applications: The Black Art of Shims

IgnoreException Parameters 1/2

EXCEPTION1_NAME:EXCEPTION1_LEVEL;EXCEPTION2_NAME:EXCEPTION2_LEVEL;…Exception names can be:

ACCESS_VIOLATION_READACCESS_VIOLATION_WRITEARRAY_BOUNDS_EXCEEDEDBREAKPOINTDATATYPE_MISALIGNMENTFLT_DENORMAL_OPERANDFLT_DIVIDE_BY_ZEROFLT_INEXACT_RESULTFLT_INVALID_OPERATIONFLT_OVERFLOWFLT_STACK_CHECK

FLT_UNDERFLOWILLEGAL_INSTRUCTIONIN_PAGE_ERRORINT_DIVIDE_BY_ZEROINT_OVERFLOWINVALID_DISPOSITIONNONCONTINUABLE_EXCEPTIONPRIV_INSTRUCTIONSINGLE_STEPSTACK_OVERFLOWINVALID_HANDLE

Page 86: Fix Your Broken Applications: The Black Art of Shims

IgnoreException Parameters 2/2

Exception levels can be:0 - Don't ignore the exception1 - Ignore first chance exception2 - Ignore second chance exception3 - Exit process on second chance exception

Page 87: Fix Your Broken Applications: The Black Art of Shims

IgnoreMessageBox

Symptoms

Extraneous message box

Fix descriptionIgnores the message box

ParametersMessageBoxText1,MessageBoxCaption1;MessageBoxText2,MessageBoxCaption2

Empty arguments match anythingSupports * and ? wildcardsEscape these characters: ? * , ; \

Page 88: Fix Your Broken Applications: The Black Art of Shims

Miscellaneous Shimsdemo

Page 89: Fix Your Broken Applications: The Black Art of Shims

Frequently Asked Questions

Managed code?

VB6 code?

msvbvm60.dll

16-bit code?

Page 90: Fix Your Broken Applications: The Black Art of Shims

Top 10 System SDB Shims1. GameUX2. VirtualRegistry3. CorrectFilePaths4. Win95VersionLie5. WinXPSP2VersionLie6. IgnoreAltTab7. ShimViaEAT8. AOLFindBundledInstaller9. ElevateCreateProcess10.OpenDirectoryACL

Page 92: Fix Your Broken Applications: The Black Art of Shims

www.microsoft.com/teched

Sessions On-Demand & Community

http://microsoft.com/technet

Resources for IT Professionals

http://microsoft.com/msdn

Resources for Developers

www.microsoft.com/learningMicrosoft Certification and Training Resources

www.microsoft.com/learning

Microsoft Certification & Training Resources

Resources

Page 93: Fix Your Broken Applications: The Black Art of Shims

Related Content

Breakout Sessions (session codes and titles)WCL302 – Are You Breaking my Stuff Again? The Windows 7 App Compat StoryWCL304 – Fix Your Broken Applications: The Black Art of ShimsWCL401 – Not for the Faint of Heart: Hard Core App Compat Debugging

Page 94: Fix Your Broken Applications: The Black Art of Shims

Track Resources→Want to find out which Windows Client sessions are best suited to help you in your deployment lifecycle? →Want to talk face-to-face with folks from the Windows Product Team?

Meet us today at the

Springboard Series Lounge, or visit us at www.microsoft.com/springboard

Springboard SeriesThe Springboard Series empowers you to select the right resources, at the right technical

level, at the right point in your Windows® Client adoption and management process. Come see why Springboard Series is your destination for Windows 7.

Page 95: Fix Your Broken Applications: The Black Art of Shims

Complete an evaluation on CommNet and enter to win!

Page 96: Fix Your Broken Applications: The Black Art of Shims

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,

IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 97: Fix Your Broken Applications: The Black Art of Shims

Complete an evaluation on CommNet and enter to win!

Page 98: Fix Your Broken Applications: The Black Art of Shims

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,

IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.