Win32 Programming Lesson 21: DLL Magic. Where are we? We’ve looked at DLLs from a...

15
Win32 Programming Lesson 21: DLL Magic

Transcript of Win32 Programming Lesson 21: DLL Magic. Where are we? We’ve looked at DLLs from a...

Page 1: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Win32 ProgrammingLesson 21: DLL Magic

Page 2: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Where are we? We’ve looked at DLLs from a

build/link/execute perspective, as well as some more advanced techniques

Today, start looking at Thread Local Storage and DLL interception >:)

Page 3: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Thread Local Storage (TLS) What does the strtok function do? How does it work? What happens in a multithreaded

environment?

Page 4: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

TLS Provides simple method for storing variables

on a per-thread basis Two types: dynamic and static; we’ll be

looking at both.

Page 5: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Supporting Structures

Page 6: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

So… We call:

DWORD TlsAlloc(); Returns TLS_OUT_OF_INDEXES if no storage is

available Else, returns an index number which can be used to store

a DWORD BOOL TlsSetValue(

   DWORD dwTlsIndex,    PVOID pvTlsValue);

Page 7: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Cleaning Up It’s C++, so there’s not a lot of cleaning up

done for us… PVOID TlsGetValue(DWORD dwTlsIndex); BOOL TlsFree(DWORD dwTlsIndex);

Page 8: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Using Static TLS Can also do this:

__declspec(thread) DWORD gt_dwStartTime = 0; Creates a .tls section Allocates the necessary storage automatically

Page 9: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

DLL Injection So, life can be interesting Windows provides limited process isolation But sometimes we want to “hook” into

another process One way to do this is by leveraging DLLs

Page 10: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Danger, Will Robinson Some of these techniques will make global

changes to how your computer functions. You need to carefully decide whether to do this on your main machine, or if a VM is a better option. You have been warned!

(That said, I do this all on my own laptop…)

Page 11: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

The Trick What are we actually trying to do?

Page 12: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Registry HKEY_LOCAL_MACHINE\Software\

Microsoft    \Windows NT\CurrentVersion\Windows\AppInit_DLLs

Hmmm. Advantages? Drawbacks?

Page 13: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Drawbacks… You must restart your computer Only mapped into processes which use

User32.dll You’re in *every* GUI app… … for it’s entire lifetime

Page 14: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Better… SetWindowsHookEx

E.g: HHOOK hHook = SetWindowsHookEx(

WH_GETMESSAGE, GetMsgProc,     hinstDll, 0

); Why hinstDll?

Page 15: Win32 Programming Lesson 21: DLL Magic. Where are we?  We’ve looked at DLLs from a build/link/execute perspective, as well as some more advanced techniques.

Walkthrough DIPS