# T1574.001 - Hijack Execution Flow: DLL

## Description from ATT&CK

> Adversaries may abuse dynamic-link library files (DLLs) in order to achieve persistence, escalate privileges, and evade defenses. DLLs are libraries that contain code and data that can be simultaneously utilized by multiple programs. While DLLs are not malicious by nature, they can be abused through mechanisms such as side-loading, hijacking search order, and phantom DLL hijacking.(Citation: unit 42)
> 
> Specific ways DLLs are abused by adversaries include:
> 
> ### DLL Sideloading
> Adversaries may execute their own malicious payloads by side-loading DLLs. Side-loading involves hijacking which DLL a program loads by planting and then invoking a legitimate application that executes their payload(s).
> 
> Side-loading positions both the victim application and malicious payload(s) alongside each other. Adversaries likely use side-loading as a means of masking actions they perform under a legitimate, trusted, and potentially elevated system or software process. Benign executables used to side-load payloads may not be flagged during delivery and/or execution. Adversary payloads may also be encrypted/packed or otherwise obfuscated until loaded into the memory of the trusted process.
> 
> Adversaries may also side-load other packages, such as BPLs (Borland Package Library).(Citation: kroll bpl)
> 
> Adversaries may chain DLL sideloading multiple times to fragment functionality hindering analysis. Adversaries using multiple DLL files can split the loader functions across different DLLs, with a main DLL loading the separated export functions. (Citation: Virus Bulletin) Spreading loader functions across multiple DLLs makes analysis harder, since all files must be collected to fully understand the malware’s behavior.  Another method implements a “loader-for-a-loader”, where a malicious DLL’s sole role is to load a second DLL (or a chain of DLLs) that contain the real payload. (Citation: Sophos)
> 
> ### DLL Search Order Hijacking
> Adversaries may execute their own malicious payloads by hijacking the search order that Windows uses to load DLLs. This search order is a sequence of special and standard search locations that a program checks when loading a DLL. An adversary can plant a trojan DLL in a directory that will be prioritized by the DLL search order over the location of a legitimate library. This will cause Windows to load the malicious DLL when it is called for by the victim program.(Citation: unit 42)
> 
> ### DLL Redirection
> Adversaries may directly modify the search order via DLL redirection, which after being enabled (in the Registry or via the creation of a redirection file) may cause a program to load a DLL from a different location.(Citation: Microsoft redirection)(Citation: Microsoft - manifests/assembly)
> 
> ### Phantom DLL Hijacking
> Adversaries may leverage phantom DLL hijacking by targeting references to non-existent DLL files. They may be able to load their own malicious DLL by planting it with the correct name in the location of the missing module.(Citation: Hexacorn DLL Hijacking)(Citation: Hijack DLLs CrowdStrike)
> 
> ### DLL Substitution
> Adversaries may target existing, valid DLL files and substitute them with their own malicious DLLs, planting them with the same name and in the same location as the valid DLL file.(Citation: Wietze Beukema DLL Hijacking)
> 
> Programs that fall victim to DLL hijacking may appear to behave normally because malicious DLLs may be configured to also load the legitimate DLLs they were meant to replace, evading defenses.
> 
> Remote DLL hijacking can occur when a program sets its current directory to a remote location, such as a Web share, before loading a DLL.(Citation: dll pre load owasp)(Citation: microsoft remote preloading)
> 
> If a valid DLL is configured to run at a higher privilege level, then the adversary-controlled DLL that is loaded will also be executed at the higher level. In this case, the technique could be used for privilege escalation.

[Source](https://attack.mitre.org/techniques/T1574/001)

## Atomic Tests

- [Atomic Test #1: DLL Search Order Hijacking - amsi.dll](#atomic-test-1-dll-search-order-hijacking---amsidll)
- [Atomic Test #2: Phantom Dll Hijacking - WinAppXRT.dll](#atomic-test-2-phantom-dll-hijacking---winappxrtdll)
- [Atomic Test #3: Phantom Dll Hijacking - ualapi.dll](#atomic-test-3-phantom-dll-hijacking---ualapidll)
- [Atomic Test #4: DLL Side-Loading using the Notepad++ GUP.exe binary](#atomic-test-4-dll-side-loading-using-the-notepad-gupexe-binary)
- [Atomic Test #5: DLL Side-Loading using the dotnet startup hook environment variable](#atomic-test-5-dll-side-loading-using-the-dotnet-startup-hook-environment-variable)
- [Atomic Test #6: DLL Search Order Hijacking,DLL Sideloading Of KeyScramblerIE.DLL Via KeyScrambler.EXE](#atomic-test-6-dll-search-order-hijackingdll-sideloading-of-keyscrambleriedll-via-keyscramblerexe)

### Atomic Test #1: DLL Search Order Hijacking - amsi.dll

Adversaries can take advantage of insecure library loading by PowerShell to load a vulnerable version of amsi.dll in order to bypass AMSI (Anti-Malware Scanning Interface)
https://enigma0x3.net/2017/07/19/bypassing-amsi-via-com-server-hijacking/

Upon successful execution, powershell.exe will be copied and renamed to updater.exe and load amsi.dll from a non-standard path.

**Supported Platforms:** Windows

**auto_generated_guid:** `8549ad4b-b5df-4a2d-a3d7-2aee9e7052a3`

#### Attack Commands: Run with `command_prompt`! Elevation Required (e.g. root or admin)

```cmd
copy %windir%\System32\windowspowershell\v1.0\powershell.exe %APPDATA%\updater.exe
copy %windir%\System32\amsi.dll %APPDATA%\amsi.dll
%APPDATA%\updater.exe -Command exit
```

#### Cleanup Commands

```cmd
del %APPDATA%\updater.exe >nul 2>&1
del %APPDATA%\amsi.dll >nul 2>&1
```
### Atomic Test #2: Phantom Dll Hijacking - WinAppXRT.dll

.NET components (a couple of DLLs loaded anytime .NET apps are executed) when they are loaded they look for an environment variable called APPX_PROCESS
Setting the environmental variable and dropping the phantom WinAppXRT.dll in e.g. c:\windows\system32 (or any other location accessible via PATH) will ensure the 
WinAppXRT.dll is loaded everytime user launches an application using .NET.

Upon successful execution, amsi.dll will be copied and renamed to WinAppXRT.dll and then WinAppXRT.dll will be copied to system32 folder for loading during execution of any .NET application.

**Supported Platforms:** Windows

**auto_generated_guid:** `46ed938b-c617-429a-88dc-d49b5c9ffedb`

#### Attack Commands: Run with `command_prompt`! Elevation Required (e.g. root or admin)

```cmd
copy %windir%\System32\amsi.dll %APPDATA%\amsi.dll
ren %APPDATA%\amsi.dll WinAppXRT.dll
copy %APPDATA%\WinAppXRT.dll %windir%\System32\WinAppXRT.dll
reg add "HKEY_CURRENT_USER\Environment" /v APPX_PROCESS /t REG_EXPAND_SZ /d "1" /f
```

#### Cleanup Commands

```cmd
reg delete "HKEY_CUR