DLL Injection
CONTACT@ADAMFURMANEK.PL HTTP://BLOG.ADAMFURMANEK.PL FURMANEKADAM
DLL INJECTION - ADAM FURMANEK 18.07.2020
1
DLL Injection CONTACT@ADAMFURMANEK.PL HTTP://BLOG.ADAMFURMANEK.PL - - PowerPoint PPT Presentation
DLL Injection CONTACT@ADAMFURMANEK.PL HTTP://BLOG.ADAMFURMANEK.PL FURMANEKADAM 1 18.07.2020 DLL INJECTION - ADAM FURMANEK About me Experienced with backend, frontend, mobile, desktop, ML, databases. Blogger, public speaker. Author of .NET
CONTACT@ADAMFURMANEK.PL HTTP://BLOG.ADAMFURMANEK.PL FURMANEKADAM
DLL INJECTION - ADAM FURMANEK 18.07.2020
1
Experienced with backend, frontend, mobile, desktop, ML, databases. Blogger, public speaker. Author of .NET Internals Cookbook. http://blog.adamfurmanek.pl contact@adamfurmanek.pl furmanekadam
18.07.2020 DLL INJECTION - ADAM FURMANEK
2
What and why Preliminaries How + Demos Summary
18.07.2020 DLL INJECTION - ADAM FURMANEK
3
18.07.2020 DLL INJECTION - ADAM FURMANEK
4
Our process Target Our DLL
1 – inject DLL 2 – execute code
18.07.2020 DLL INJECTION - ADAM FURMANEK
5
We want to execute our code in different (target) process. This means:
process
We want to do it by injecting DLL We are not modifying the target process’ source code (especially, we are not recompiling the target) We control the machine (however, we might not be administrators) We want the whole process to be clean, safe, and reliable
18.07.2020 DLL INJECTION - ADAM FURMANEK
6
REAL LIFE USAGES
18.07.2020 DLL INJECTION - ADAM FURMANEK
7
18.07.2020 DLL INJECTION - ADAM FURMANEK
8
Every proces has its own address space.
18.07.2020 DLL INJECTION - ADAM FURMANEK
9
Every memory address is translated by CPU. Every proces has its own memory page table.
18.07.2020 DLL INJECTION - ADAM FURMANEK
10
18.07.2020 DLL INJECTION - ADAM FURMANEK
11
18.07.2020 DLL INJECTION - ADAM FURMANEK
12
Cornerstone of Microsoft Windows All functions in the API are contained in DLLs Three most important:
How many DLLs does notepad have?
18.07.2020 DLL INJECTION - ADAM FURMANEK
13
Before application can call functions in a DLL, the DLL’s file image must be mapped into the calling process’ address space Two methods:
Once an image is mapped into the address space, it is in fact no longer library
18.07.2020 DLL INJECTION - ADAM FURMANEK
14
Im Impli licit it lo loading
When application’s source code reference symbols contained in the DLL Loader implicitly loads and links the required library during startup
Exp xplic icit lo loadin ing
Application can load library in runtime Requires call to LoadLibrary or LoadLibraryEx Flexible – allows to load library as a datafile or change search path
18.07.2020 DLL INJECTION - ADAM FURMANEK
15
1. The directory containing the executable image file 2. The Windows system directory returned by GetWindowsDirectory function 3. The 16-bit system directory (System subfolder under the Windows directory) 4. The Windows directory returned by GetSystemDirectory 5. The process’ current directory 6. The directories listed in the PATH environment variable
Can be changed!
18.07.2020 DLL INJECTION - ADAM FURMANEK
16
Every executable and DLL module has a preferred base address This address identifies the ideal memory address where the module should get mapped into a process’ address space.
Why is this so important?
18.07.2020 DLL INJECTION - ADAM FURMANEK
17
DLL can have a relocation section
When a DLL cannot be loaded at its preferred address loader can modify relocation section and adjust offsets We can do it using Rebase + Bind utilities
18.07.2020 DLL INJECTION - ADAM FURMANEK
18
Security technique involved in protection from buffer overflow attacks ASLR randomly arranges the address space positions of key data areas of a process:
18.07.2020 DLL INJECTION - ADAM FURMANEK
19
DLL can have a single entry-point function The system calls this function at various Times These calls are informational – DLL is notified when it’s attached to process or thread
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch (fdwReason) { case DLL_THREAD_DETACH: EnterCriticalSection(&g_csGlobal); } }
18.07.2020 DLL INJECTION - ADAM FURMANEK
20
Windows holds a loader loack during DLL initialization This is required to block other threads from calling DLL’s functions before the library is initialized This often causes deadlock
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch (fdwReason) { case DLL_THREAD_DETACH: EnterCriticalSection(&g_csGlobal); } } BAD IDEA!
18.07.2020 DLL INJECTION - ADAM FURMANEK
21
REGISTRY, HOOKS, REMOTE THREADS
18.07.2020 DLL INJECTION - ADAM FURMANEK
22
18.07.2020 DLL INJECTION - ADAM FURMANEK
23
18.07.2020 DLL INJECTION - ADAM FURMANEK
24
Target Our DLL (on disk) User32.dll
1 – process starts 2 – process loads user32.dll 3 – user32.dll loads our dll
18.07.2020 DLL INJECTION - ADAM FURMANEK
25
18.07.2020 DLL INJECTION - ADAM FURMANEK
26
Target Our DLL (on disk)
1 – process starts 2 – we press some key 3 – windows loads our dll and executes hook function
18.07.2020 DLL INJECTION - ADAM FURMANEK
27
18.07.2020 DLL INJECTION - ADAM FURMANEK
28
Target
1 – target starts 2 – our process starts 3 – our process allocates memory in target
Our process Our DLL (on disk)
4 – our process writes memory in target 5 – our process creates thread in target 6 –thread loads our dll
C:\...
18.07.2020 DLL INJECTION - ADAM FURMANEK
29
18.07.2020 DLL INJECTION - ADAM FURMANEK
30
Target
1 – target starts 2 – our process starts 3 – our process allocates memory in target
Our process Native DLL (on disk)
4 – our process writes memory in target 5 – our process creates thread in target 6 – thread loads our dll
C:\...
7 – our process create another thread to run function inside native dll
Managed DLL
8 – our function loads and starts .NET
18.07.2020 DLL INJECTION - ADAM FURMANEK
31
custom one having the same methods
(APC)
Windows
CreateRemoteThread equivalent in Linux
18.07.2020 DLL INJECTION - ADAM FURMANEK
32
18.07.2020 DLL INJECTION - ADAM FURMANEK
33
Jeffrey Richter - „CLR via C#” Jeffrey Richter, Christophe Nasarre - „Windows via C/C++” Mark Russinovich, David A. Solomon, Alex Ionescu - „Windows Internals” Penny Orwick – „Developing drivers with the Microsoft Windows Driver Foundation” Mario Hewardt, Daniel Pravat - „Advanced Windows Debugging” Mario Hewardt - „Advanced .NET Debugging” Steven Pratschner - „Customizing the Microsoft .NET Framework Common Language Runtime” Serge Lidin - „Expert .NET 2.0 IL Assembler” Joel Pobar, Ted Neward — „Shared Source CLI 2.0 Internals” Adam Furmanek – „.NET Internals Cookbook” https://github.com/dotnet/coreclr/blob/master/Documentation/botr/README.md — „Book of the Runtime” https://blogs.msdn.microsoft.com/oldnewthing/ — Raymond Chen „The Old New Thing”
18.07.2020 DLL INJECTION - ADAM FURMANEK
34
18.07.2020 DLL INJECTION - ADAM FURMANEK
35
CONTACT@ADAMFURMANEK.PL HTTP://BLOG.ADAMFURMANEK.PL FURMANEKADAM
18.07.2020 DLL INJECTION - ADAM FURMANEK
36