Win32 Programming Lesson 22: DLL Magic Part Deux All your base are belong to us…

Slides:



Advertisements
Similar presentations
Process A process is usually defined as an instance of a running program and consists of two components: A kernel object that the operating system uses.
Advertisements

Operating System Security : David Phillips A Study of Windows Rootkits.
Win32 Programming Lesson 8: Processes. Where are we?  We’re starting to have some foundational understanding of Windows  But really, we don’t know how.
Disclaimer The Content, Demonstration, Source Code and Programs presented here is "AS IS" without any warranty or conditions.
Chapter 6 Limited Direct Execution
Itamargi at post.tau.ac.il Nirkrako at post.tau.ac.il.
ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
CSE 451 Section 4 Project 2 Design Considerations.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Malware Dynamic Analysis Part 3 Veronica Kovah vkovah.ost at gmail See notes for citation1
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Win32 Programming Lesson 9: Jobs & Thread Basics.
计算机系 信息处理实验室 Lecture 7 Processes, Threads, and Jobs (1)
Win32 Programming Lesson 24: More SEH That’s right… you’ll never generate an exception, will you?
Software attacks Software Attacks DLL injection & API patching.
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 Programming Lesson 13: Thread Pooling (Wow, Java is good for something…)
Win32 Programming Lesson 16: Virtual Memory. Where are we?  We’ve covered the theory of Windows memory, and poked around some  Now let’s use how to.
Win32 Programming Lesson 1: Why We’re All Here. Why We’re Here…  Okay, maybe that’s too grandiose  Windows – in particular Win32 Thirty-what?  What.
Win32 Programming Lesson 10: Thread Scheduling and Priorities.
Win32 Programming Lesson 20: Advanced DLL Techniques.
Saves memory and reduces swapping No recompilation on changes A DLL can provide after-market support Programs written in different languages can call the.
Lecture 11 Dynamic link libraries. Differences between static libraries and DLLs In static library code is added to the executable. In DLL, the code is.
Win32 Programming Lesson 7: Kernel Objects. Abstract  Many of the concepts we’ll look at today won’t make complete sense until you use them  However,
CRT State Stuff Dana Robinson The HDF Group. In the next slide, I show a single executable linked to three dlls. Two dlls and the executable were built.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
Win32 Programming Lesson 18: More Memory Mapped Files and the HEAP (Finally, cool stuff!)
Dr. Richard Ford  Szor 12  Virus Scanners – why they need to scan memory and what issues there are in this area.
Briefing for: Hacking Windows Internals Cesar Cerrudo Argeniss.
Win32 Programming Lesson 25: Unhandled Exceptions Bet you’ve never encountered one of those, eh?
Module 6: Debugging a Windows CE Image.  Overview Debug Zones IDE Debug Setup IDE Debug Commands Platform Builder Integrated Kernel Debugger Other Debugging.
Win32 Programming Lesson 8a: Jobs. Where are we?  Grouping processes into logical groups: Jobs  However, processes don’t retain a parent- child relationship.
System calls for Process management
© 2004, D. J. Foreman 1 Implementing Processes and Threads.
Memory Management II CS Spring Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address.
Dynamic Link Libraries: Inside Out. Dynamic Link Libraries  About Dynamic-Link Libraries  Dynamic-Link Libraries Hands On  Dynamic Link Library Reference.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
CSE 451: Operating Systems Winter 2011 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura.
Win32 Programming Lesson 17: Memory Mapped Files (Finally, cool stuff again, all this work is getting tedious!)
Win32 Programming Lesson 11: User-mode Thread Sync (aka: How to crash your machine without really trying…)
Application Remediation
Win32 Programming Lesson 15: Practical Windows Memory (If you can read this you have good vision)
Win32 Programming Lesson 19: Introduction to DLLs.
13/July/1999Third USENIX Windows NT Symposium1 Detours: Binary Interception of Win32 Functions Galen Hunt and Doug Brubacher Systems and Networking Group.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
System calls for Process management Process creation, termination, waiting.
Lecture 11 Example Rootkit. Intel internship Intel CTG (Corporate Technology Group) –Advanced research & development –System integrity services using.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
Memory Mapped I/O Gregory Mortensen CSIS 4330, Advanced Windows Programming – UVSC.
Operating System Debugging Support and How Win32 Debuggers Work
Processes and threads.
Protecting Memory What is there to protect in memory?
Scheduler activations
Mechanism: Limited Direct Execution
Concurrency, Processes and Threads
Implementing Processes and Threads
CSI 400/500 Operating Systems Spring 2009
Windows CE Memory Management
CSE 451: Operating Systems Winter 2010 Module 4 Processes
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Module 6: Debugging a Windows CE Image
Implementing Processes and Threads
Malware and the Windows API
Why we have Counterintuitive Memory Models
Implementing Processes and Threads
Following Malware Execution in IDA
Dirty COW Race Condition Attack
Presentation transcript:

Win32 Programming Lesson 22: DLL Magic Part Deux All your base are belong to us…

Where are we?  We’ve looked at injection techniques in general  But now it’s time to get specific

Remind Me…  What’s the trick we’re trying to pull off?

Injecting DLLs using a Remote Thread  Remember, we’re trying to get a DLL into another process’ address space  One way to do this is to create a thread in a remote process  And Windows provides an easy way to do this…

So…  HANDLE CreateRemoteThread( HANDLE hProcess, PSECURITY_ATTRIBUTES psa, DWORD dwStackSize, PTHREAD_START_ROUTINE pfnStartAddr, PVOID pvParam, DWORD fdwCreate, PDWORD pdwThreadId);  Look familiar…?

Problems?  Troy, does this solve all our problems?  Casey?  Chris?  Invisible Dan? ……

Bingo!  This only works if we have code in the remote thread that we want to execute to load our Trojan DLL…  Hmmm… can we fix it?  How do you load a DLL during program execution?

Right: LoadLibrary  But LoadLibrary doesn’t exist (someone tell me why?)  So basically, we want to execute a line of code which looks like this: HANDLE hThread = CreateRemoteThread( hProcessRemote, NULL, 0, LoadLibraryA, "C:\\MyLib.dll", 0, NULL ); BUT this is still wrong… why?

Right: Real address  The address of LoadLibraryA as a function won’t be the same because of relocation  But the *real* address of LoadLibraryA in KERNEL32.DLL will be the same (provided it loads in the same location)  So, how do we get that?

Right: GetProcAddress…  GetProcAddress(GetModuleHandle(TEXT(" Kernel32")), "LoadLibraryA");  Easy eh?  Predicated on the identical loading of Kernel32… but that’s quite common  But it still doesn’t work right… there’s still one string attached to this idea…

Right: The STRING “c:\\mylib.dll”  The String exists in Process A’s address space, but we need it to exist in Process B’s  Hmmm  How can we fix that?

Right: VirtualAllocEx  Bingo!  PVOID VirtualAllocEx( HANDLE hProcess, PVOID pvAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect );

But we’re still not done  Processes boundaries stop one process writing to another…  So we need a function which can read and write Process memory  And Windows gives us such a function…

Memory…  BOOL ReadProcessMemory( HANDLE hProcess, PVOID pvAddressRemote, PVOID pvBufferLocal, DWORD dwSize, PDWORD pdwNumBytesRead ); BOOL WriteProcessMemory( HANDLE hProcess, PVOID pvAddressRemote, PVOID pvBufferLocal, DWORD dwSize, PDWORD pdwNumBytesWritten );

7 Step Process  Use the VirtualAllocEx function to allocate memory in the remote process’s address space.  Use the WriteProcessMemory function to copy the DLL's pathname to the memory allocated in step 1.  Use the GetProcAddress function to get the real address (inside Kernel32.dll) of the LoadLibraryA or LoadLibraryW function.  Use the CreateRemoteThread function to create a thread in the remote process that calls the proper LoadLibrary function, passing it the address of the memory allocated in step 1.

Continued…  Use the VirtualFreeEx function to free the memory allocated in step 1.  Use the GetProcAddress function to get the real address (inside Kernel32.dll) of the FreeLibrary function.  Use the CreateRemoteThread function to create a thread in the remote process that calls LoadLibrary function, passing the remote DLL's HINSTANCE.

Lastly…  hProcess = OpenProcess( PROCESS_CREATE_THREAD | // For CreateRemoteThread PROCESS_VM_OPERATION | // For VirtualAllocEx/VirtualFreeEx PROCESS_VM_WRITE, // For WriteProcessMemory FALSE, dwProcessId );

Next: The Trojan DLL  Replace some DLL you want…  But you have to export all the same functions

Injecting a DLL via a Debugger  Tricky and processor-specific  But can work well, as Debuggers have special privileges

Injecting Via CreateProcess  Have your process spawn the child process suspended.  Retrieve the primary thread's starting memory address from the.exe module's file header.  Save the machine instructions at this memory address.  Force some hand-coded machine instructions at this address. The instructions should call LoadLibrary to load a DLL.  Resume the child process’s primary thread so that this code executes.  Restore the original instructions back into the starting address.  Let the process continue execution from the starting address as if nothing had happened