Windows
DLL Search Order Hijacking
👑 - require w permissions for
Windows
andWindows\System32
.
Detection:
- 💭 unusual locations for new DLL created or saved during the attack (especially unsigned)
- 💭 see RAM for DLLs loaded from the wrong locations
- 💭 multiple locations for one DLL
- 💭 behavioural analysis (C2C or other)
- 🛠️ https://github.com/adamkramer/dll_hijack_detect/releases
- IDS/IPS
- Integrity controls?
Example:
eplorer.exe
loadingntshrui.dll
.
Following is the search order for modern Windows systems:
- Manifest (if the application provides an absolute path to the
dll
) - RAM
- SxS
KnowDLLs
listHKLM\SYSTEM\CurrentControlSet\Control\WOW
orHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs
([docs](KnowDLLslist
HKLM\SYSTEM\CurrentControlSet\Control\WOW`)). So, if the dll is in the list, the link from the registry is used, and there is no search conducted.- Application’s loading directory
C:\Windows\System32
C:\Windows\system
C:\Windows
- Application’s registered App Paths directories ❓/ Current directory
%PATH%
env variable
❗️ If a DLL has dependencies, the system searches for the dependent DLLs as if they were loaded with just their module names. This is true even if the first DLL was loaded by specifying a full path. [1] So, that means that we can make a side-loading attack on “a side-loaded
dll
”!
❓Not sure about the different paths specified for the KnowDlls entries. This might be due to the different OS versions. See here:
For 16-bit apps, Windows NT uses KnownDLLs to implicitly and explicitly load DLLs. The value is at
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\WOW
. At this key,KnownDLLs
is a typeREG_SZ
value which lists the 8.3 DLL names, separated by spaces. Without aKnownDLLs
entry,WOW
searches:
- The current directory.
- The
%SystemRoot%
directory.- The
%SystemRoot%\SYSTEM
directory.- The
%SystemRoot%\SYSTEM32
directory.- The
.exe
file directory.- The directories in your
Path
environment variable.
With the KnownDLLs entry, WOW only searches the %SystemRoot%\SYSTEM32 directory.
The order also depends on the SafeDllSearchMode
(🔑 HKLM\System\CurrentControlSet\Control\Session Manager\SafeDllSearchMode
). Here is the quote from the docs:
If SafeDllSearchMode is enabled ✅, the search order is as follows:
- The directory from which the application loaded.
- The system directory. Use the GetSystemDirectory function to get the path of this directory.
- The 16-bit system directory. No function obtains the path of this directory, but it is searched.
- The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. >5. The current directory.
- The directories are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
If SafeDllSearchMode is disabled ⛔️, the search order is as follows:
- The directory from which the application loaded. > 2. The current directory.
- The system directory. Use the GetSystemDirectory function to get the path of this directory.
- The 16-bit system directory. No function obtains the path of this directory, but it is searched.
- The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
- The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
Phantom DLL hijacking
An executable is trying to load a very old dll
, even though it’s superfluous. Some don’t even exist anymore, for example, fxsst.dll
(fax service). The attacker creates a DLL with the same name and some nefarious functionality.
📚 Further Reading:
- forrest-orr.net/post/malicious-memory-artifacts-part-i-dll-hollowing
- https://resources.infosecinstitute.com/topic/dll-hijacking-attacks-revisited/
DLL Side-by-side loading (SxS)
Side-by-side loading (SxS) mechanism to introduce updated versions of DLL. It can be used to circumvent anti-virus. Example: PlugX RAT. Legitimately signed exe
uses SxS to load a malicious dll
. The attacker puts a nefarious dll
somewhere down the road to ensure it’s loaded instead of a legitimate one.
📚 Further Reading:
- https://www.bitdefender.com/blog/businessinsights/tech-explainer-what-is-dll-sideloading/
- https://www.mandiant.com/resources/reports/dll-side-loading-thorn-side-anti-virus-industry
Relative Path DLL Hijacking
The attacker discovers vulnerable executable files (ones that use relative paths to DLLs) and creates a malicious DLL to replace the legitimate one. Afterwards, they duplicate that executable and place it in the same directory as the malicious DLL. When that executable is launched, it will load the DLL living side by side rather than the legit one from a system directory.
Detection:
- 📂 Unusual locations for known libraries (especially, unsigned)
- 📂 Duplications
macOS
Dylibs Environment Variables
DYLD_*
DYLD_INSERT_LIBRARIES # all libs from this env will be loaded
DYLD_FRAMEWORK_PATH
These libraries are loaded within a trusted host process, not resulting in a new process.
A plist
for launch item - EnvironmentVariables
. For application - LSEnvironment
(Info.plist
).