
Techniques Overview
Each method detailed below serves as a potential avenue for executing PowerShell scripts, circumventing the restrictions imposed by execution policies. These techniques range from simple command-line arguments to more sophisticated methods involving encoding and remote execution strategies.
Echo and Pipe to PowerShell Standard In
# Paste commands into an Interactive PowerShell ConsoleWrite-Host 'Example Output'Write-Host 'Example Output'how to bypass execution policy in windows operating system
In PowerShell, the “Execution Policy” is a security feature that controls the execution of scripts. By default, PowerShell’s execution policy is set to “Restricted,” which prevents the execution of scripts. If you execute script without bypassing it, you will see below error.
.\cleanup.ps1 : File C:\Users\PC\Downloads\cleanup.ps1 cannot be loaded. The file C:\Users\PC\Downloads\cleanup.ps1 is
not digitally signed. You cannot run this script on the current system. For more information about running scripts and
setting execution policy, see about_Execution_Policies1. Temporary Bypass:
Set-ExecutionPolicy Bypass -Scope ProcessAfter running this command, you can execute your PowerShell script without any issues. Keep in mind that this change will only affect the current session and will not change the system-wide execution policy.
Set-ExecutionPolicy Bypass -Scope CurrentUserThis will allow you to execute scripts without bypassing the execution policy every time.
3. Remote Signed Execution Policy:
Alternatively, you can set the execution policy to “RemoteSigned,” which allows the execution of locally created scripts and requires a digital signature for remote scripts. This provides a balance between security and flexibility.
Set-ExecutionPolicy RemoteSignedRemember that changing the execution policy comes with security implications, so be cautious when modifying it. It’s recommended to revert to a more restrictive policy (like “Restricted” or “RemoteSigned”) after you’ve finished running your script.
Aug 02, 2023, 04:26 AM
Keyur Aghao
Domain Manager @Bajaj Finserv
Through various Red Team assessment, I discovered one of the simplest way to bypass the Implemented Execution Policy.
This is extremely helpful as it allows the team to execute scripts and tools that might otherwise be restricted by security policies. This helps in simulating real-world attack scenarios more accurately, identifying security gaps, and testing the effectiveness of existing defenses. It ensures a comprehensive evaluation of an organization’s security posture, revealing potential vulnerabilities that could be exploited by malicious actors.
1. Access the Execution Context: It retrieves the private field _context from the $executioncontext object, which holds the current session’s execution context.
2. Get the Authorization Manager: It then retrieves the private field _authorizationManager from this context, which controls the execution policy.
3. Override the Authorization Manager: Finally, it sets this field to a new instance of AuthorizationManager, effectively bypassing any restrictions set by the original execution policy.
In short, this function disables the PowerShell execution policy by directly modifying the internal security context, allowing scripts to run without being restricted by the policy settings.
Please find the attached POC for reference.
Help improve contributions
Contribution hidden for you
Privacy is a Myth!!
Privacy is a Myth!!
96 followers
The Solution
To run PowerShell scripts, it may be necessary to change the PowerShell execution policy. The execution policy on most modern Windows systems is set to Restricted by default, preventing the execution of any PowerShell scripts. This is a security measure as PowerShell scripts can have powerful and dangerous functionality.
Click to Copy
To temporarily bypass the execution policy and run scripts in a single PowerShell session, we can specify the flag -ExecutionPolicy Bypass when starting PowerShell from Windows CMD or the Run dialog.
For example, the command below will run MyScript.ps1 without modifying the execution policy:
Click to Copy
powershell -noexit -ExecutionPolicy Bypass -File MyScript.ps1
To enable script execution permanently, we can use PowerShell’s Set-ExecutionPolicy cmdlet.
Click to Copy
This will allow execution of any locally written PowerShell scripts, but will also require that PowerShell scripts downloaded from the internet are digitally signed by a trusted publisher. This is the default setting for Windows Servers.
If this policy is still too restrictive, we can remove all restrictions with this command:
Click to Copy
This will allow any script to be executed. You will still be prompted for permission when executing a script from the internet.
A full list of execution policy options is available here.
The ProblemJump To Solution
When I attempt to run a PowerShell script (for example, MyScript.ps1), I get the error:
Click to Copy
MyScript.ps1 cannot be loaded because the execution of scripts is disabled on this system.
How do I enable script execution?
More details about individual techniques
Piping Scripts: Using
Get-ContentorTYPEcommands allows for direct execution of scripts from the command line without creating a script file on disk.These methods are particularly useful in environments where writing to disk is restricted or monitored.
Ideal for executing payloads hosted on a controlled server, minimizing the footprint on the target system.
Base64 Encoding with
-EncodedCommand: Encodes the entire PowerShell command or script in base64 to bypass simple command line logging mechanisms and execute complex scripts.This approach is beneficial when dealing with systems that have command line argument logging, as the encoded command obscures the actual PowerShell code being executed.
Execution Policy Bypass Flags
Using
-ExecutionPolicy Bypass: Temporarily overrides the current execution policy to execute a script without making permanent changes to the system policy.Scoped Policy Adjustment: Applying execution policy changes to a specific scope without affecting the global execution policy.
- Process Scope: Applies only to the current PowerShell session.
- CurrentUser Scope: Affects all PowerShell sessions initiated by the current user, persisting across logins without needing administrative rights.
These scoped adjustments are critical in environments where you need to maintain operational stealth and avoid altering system-wide configurations that could alert defenders.
- Process Scope: Applies only to the current PowerShell session.
Considerations for Tactical Use
Selection of Technique: The choice of bypass technique should be guided by the target environment’s restrictions and monitoring capabilities. For instance, environments with stringent network egress monitoring may require localized execution methods to avoid detection.
Operational Security (OpSec): When employing these techniques, consider the digital footprint left behind. Techniques that avoid disk writes or obscure command execution through encoding are preferable in high-security environments.
Understanding these PowerShell execution policy bypass techniques in depth provides offensive security professionals with a versatile toolkit for navigating and exploiting secure environments. Mastery of these methods allows for the execution of PowerShell scripts under various constraints, enhancing the effectiveness of penetration testing and red teaming activities.




