Table of Contents
Requirements
This attack vector usually requires you to be able to:
- Start and stop given service - Windows services
- Restart the service - Windows services
- Restart the whole host - Rebooting a Windows host
- Or you have a suspicion that someone will run it for you
Additionally, you’d also need enough permissions to either modify (for a swap) or write (for an include) on the parent folders - check for M, W or F rights or their other equivlants with icacls.
Tips
- To start our
.dllenumeration we should simply look for existing files, see if we know any.dllthat they might want to use and our permissions for them. We could also look online to see if a given software version doesn’t have a CVE regarding dll hijacking. - You can test your write rights by just simply writing into directories - stuff like
echo "test" > 'C:\Users\dave'is valid play afterall. - Process Monitor software (Procmon) can be only lunched if you have administrator privileges. Normal pentesters usually download needed files locally and run such enumeration on their hardware and virtual machines.
Types of attacks
In DLL hijacking, swapping and attaching payloads usually describe two different approaches.
- Swap: Replace the legitimate DLL with your malicious DLL. The vulnerable program loads it instead and if it runs as an elevated user, you will get an elevated shell. The service or application may not work as expected, but it generally still results in code execution for us.
- Attach payload: Put a malicious DLL somewhere Windows will search first, so the program loads your malicious dll instead of the legitimate one. For that, it’s best to try and follow the DLL search order itself. The search order is defined by Microsoft and determines what to inspect first when searching for DLLs. By default, all current Windows versions have safe DLL search mode enabled. You can find more about it in Dynamic Link Libraries (DLL)
Besides swapping dll files as in the first method, we could use this method in case there as actually a missing .dll which the problem requires. In such scenerio, we could upload our malicious .dll to a given directory (with safe seach off it’s way easier to do) and run the program or wait for someone to execute it for us.
To figure out which directory is used by the application to fetch the .dll we can:
- Use ProcMon, run the program and filter it out via “Create File” operation so that you can see the locations that it looks for .dll’s - ProcMon of course requires administrative privilages.
- Another way it to manually enumerate where are given .dlls either change them or add them in directories where they should be in the first place.
- Additionally, you could just download the service binary locally and run ProcMon there on your own VM (best if it mimicks the target VM) and see it behavior that way.
Search modes
By default, all current Windows versions have safe DLL search mode enabled.
The standard search order (safe search on):
1. The directory from which the application loaded.
2. The system directory. (C:\Windows\System32)
3. The 16-bit system directory. (C:\Windows\System)
4. The Windows directory. (C:\Windows)
5. **The current directory.**
6. The directories that are listed in the PATH environment variable.
And this is the old one (safe search off)
1. The directory from which the application loaded.
2. **The current directory.**
3. The system directory.
4. The 16-bit system directory.
5. The Windows directory.
6. The directories that are listed in the PATH environment variable.
Scenerio
If you have required privileges to write or modify the directory or the files residing inside of it, as well as some rights to restart either the service or the host, you can start with crafting your payload.
You can find a ready payload with some configuration options in DLL hijacking payload.
After the payload creation we then cross-compile it with x86_64-w64-mingw32-gcc TextShaping.cpp --shared -o TextShaping.dll. You can find output about this tool in Compilers.
Important note: Remember to include the --shared flag, otherwise your payload will be compiled as a normal executable and not a shared library!
After that we simply need to upload our file and place it in the destined place for our vulnerable program to yoink it and execute our code.
Important note: DLLs run on permissions based on the person that executed them. Meaning we need to wait for someone with higher privileges to use the program for us to be able to create a user with the code above - or in a case of a revshell, elevate our privs.
This attack will work for both Attach and Swap variants, they really just change where you put the file.