110 likes | 130 Views
Living With Detours. Why Use Detours?. You want to replace some target binary code with new detour code. Detours will: Replace the first instructions of a target function with JMP to the detour function. Preserve the original function callable through a trampoline function.
E N D
Why Use Detours? • You want to replace some target binary code with new detour code. • Detours will: • Replace the first instructions of a target function with JMP to the detour function. • Preserve the original function callable through a trampolinefunction. Detours Conference
Caller() { Target(args); } Target(args) { // Target Stuff } Trampoline(args) { goto Target; } Caller() { Target(args); } Target(args) { goto Detour; } Detour(args) { // your code here } Trampoline(args) { // Target Stuff } Inserting A Detour: Before: After: DetourFunctionWithTrampoline (Trampoline, Detour); Detours Conference
Target: push ebp [1 byte] mov ebp,esp [2 bytes] push ebx [1 bytes] push esi [1 byte] push edi .... Trampoline: jmp Target Target: jmp Detour [5 bytes] push edi .... Detour: ...Your Code... Trampoline: push ebp mov ebp,esp push ebx push esi jmp Target+5 In Detail: Before: After: Detours Conference
Detours Are Cheap: • Runtime library adds <18KB to binary. • << 1 microsecond per detoured call: • 6 cycles for Empty Function • 71 cycles for CoCreateInstance (5 Args.) • NB: Added cost is in call from Detour to Trampoline. • 1 Cache line per detour. Detours Conference
When can’t I use Detours? • Don’t know the address of the target code. • The target code is < 5 bytes (sizeof(JMP)). • First bytes of the target code contain the destination of a branch instruction. Target: ;;; code JNE Target + 2 • Not on x86 (no alpha or ia64 support). • Using .NET CLR (MSIL) code. Detours Conference
class CTarget { void Target(); }; class CDetour // add “: CTarget” to access class vars. { void Detour() { Trampoline(); } void Trampoline(); // No member variables or virtual funcs.! }; DETOUR_TRAMPOLINE_EMPTY (void CDetour::Trampoline()); void main() { void (CTarget::* pfTarget)() = CTarget::Target; void (CDetour::* pfDetour)() = CDetour::Detour; void (CDetour::* pfTrampoline)() = CDetour::Trampoline; DetourFunctionWithEmptyTrampoline( *(PBYTE*)&pfTrampoline, *(PBYTE*)&pfTarget, *(PBYTE*)&pfDetour); CTarget target; // Call Ctarget (w/ Detour): target.Target(); // Call CTarget (w/o Detour): ((CDetour*)&target)->Trampoline(); } How do I detour a member function? Detours Conference
HRESULT STDCALL (*pfSeekTrampoline)( IStream * This, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPos); HRESULT STDCALL SeekDetour( IStream * This, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPos) { return pfSeekTrampoline(This, dlibMove, dwOrgin, plibNewPos); } void detour_member_function(IStream *pi) { (*(PBYTE*)pfSeekTrampoline) = DetourFunction( (PBYTE)pi->lpVtbl->Seek, (PBYTE)SeekDetour); }; How do I detour a COM function? Detours Conference
Static Trampolines:DETOUR_TRAMPOLINE - Create a trampoline w/ known target DETOUR_TRAMPOLINE_EMPTY - Create a trampoline w/o target Detouring Functions: DetourFunction - Detour function and alloc trampoline. DetourFunctionWithTrampoline(Ex) - Detour function named in trampoline. DetourFunctionWithEmptyTrampoline(Ex) - Detour function & set trampoline. DetourRemove - Remove detour. Code Functions: DetourFindFunction - Find function in exports or symbols. DetourGetFinalCode - Skip over indirection JMP statements. DetourCopyInstruction(Ex) - Disassemble instruction (can copy). Which APIs manipulate code? Detours Conference
PE Image (Module) Enumeration Functions: DetourEnumerateModules - Find all PE images loaded into a process. DetourGetEntryPoint - Find the entry-point for an image. DetourEnumerateExportsForModule - Find all exports from an image. Payload Functions: DetourFindPayload - Find a specific payload. DetourGetSizeOfPayloads - Get size of all payloads in image. DLL Injection Functions: DetourCreateProcessWithDll - Create a new process and inject a DLL. DetourContinueProcessWithDll - Inject a DLL into an existing process. Exception Filter Functions: DetourFirstChanceExceptionFilter - Detour Win32 exception filtering. Which APIs help use Detours? Detours Conference
Persistent Binary Manipulation Functions:DetourBinaryOpen - Open a PE binary. DetourBinaryEnumeratePayloads - Enumerate the payloads in the binary. DetourBinaryFindPayload - Find a specific payload. DetourBinarySetPayload - Set/replace a specific payload. DetourBinaryDeletePayload - Delete a specific payload DetourBinaryPurgePayloads - Delete all payloads from the binary. DetourBinaryEditImports - Modify the PE binary import table. DetourBinaryResetImports - Reset the PE binary import table. DetourBinaryWrite - Write the PE binary to a file. DetourBinaryClose - Close the PE binary. DetourBinaryBind - Bind a PE binary with BindImage(). Which APIs change PE binary files? Detours Conference