Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit 5e1f6ac

Browse files
committed
Call to DllMain when unloading reflective DLL
Prior to this fix, DllMain with the ProessDetach flag was not called when unloading the reflectively loaded DLL. This was causing very weird crashes in the Invoke-NinjaCopy script which is built on this script. This should fix the crash.
1 parent 6807da4 commit 5e1f6ac

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

CodeExecution/Invoke-ReflectivePEInjection.ps1

+9
Original file line numberDiff line numberDiff line change
@@ -2620,6 +2620,15 @@ Blog on modifying mimikatz for reflective loading: http://clymb3r.wordpress.com/
26202620
$ImportDescriptorPtr = Add-SignedIntAsUnsigned ($ImportDescriptorPtr) ([System.Runtime.InteropServices.Marshal]::SizeOf($Win32Types.IMAGE_IMPORT_DESCRIPTOR))
26212621
}
26222622
}
2623+
2624+
#Call DllMain with process detach
2625+
Write-Verbose "Calling dllmain so the DLL knows it is being unloaded"
2626+
$DllMainPtr = Add-SignedIntAsUnsigned ($PEInfo.PEHandle) ($PEInfo.IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint)
2627+
$DllMainDelegate = Get-DelegateType @([IntPtr], [UInt32], [IntPtr]) ([Bool])
2628+
$DllMain = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($DllMainPtr, $DllMainDelegate)
2629+
2630+
$DllMain.Invoke($PEInfo.PEHandle, 0, [IntPtr]::Zero) | Out-Null
2631+
26232632

26242633
$Success = $Win32Functions.VirtualFree.Invoke($PEHandle, [UInt64]0, $Win32Constants.MEM_RELEASE)
26252634
if ($Success -eq $false)

0 commit comments

Comments
 (0)