当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 从正在运行的进程中解码 PowerShell 命令

作者:精品下载站 日期:2024-12-14 03:02:31 浏览:11 分类:玩电脑

从正在运行的进程中解码 PowerShell 命令


该示例仅在 Windows 平台上运行。

有时,您运行的 PowerShell 进程可能会占用大量资源。此进程可以在任务计划程序作业或 SQL Server 代理作业的上下文中运行。如果有多个 PowerShell 进程正在运行,则可能很难知道哪个进程出现了问题。本文介绍如何解码 PowerShell 进程当前正在运行的脚本块。

创建一个长时间运行的进程

要演示此场景,请打开一个新的 PowerShell 窗口并运行以下代码。它执行一个 PowerShell 命令,在 10 分钟内每分钟输出一个数字。

powershell.exe -Command {
    $i = 1
    while ( $i -le 10 )
    {
        Write-Output -InputObject $i
        Start-Sleep -Seconds 60
        $i++
    }
}

查看流程

PowerShell 正在执行的命令正文存储在 Win32_Process 类的 CommandLine 属性中。如果命令是编码命令,则 CommandLine 属性包含字符串“EncodedCommand”。使用此信息,可以通过以下过程对编码命令进行反混淆。

以管理员身份启动 PowerShell。 PowerShell 以管理员身份运行至关重要,否则查询正在运行的进程时不会返回结果。

执行以下命令以获取所有具有编码命令的 PowerShell 进程:

$powerShellProcesses = Get-CimInstance -ClassName Win32_Process -Filter 'CommandLine LIKE "%EncodedCommand%"'

以下命令创建一个自定义 PowerShell 对象,其中包含进程 ID 和编码的命令。

$commandDetails = $powerShellProcesses | Select-Object -Property ProcessId,
@{
    name       = 'EncodedCommand'
    expression = {
        if ( $_.CommandLine -match 'encodedCommand (.*) -inputFormat' )
        {
            return $matches[1]
        }
    }
}

现在可以对编码的命令进行解码。以下代码片段迭代命令详细信息对象,解码编码的命令,并将解码的命令添加回对象以进行进一步调查。

$commandDetails | ForEach-Object -Process {
    # Get the current process
    $currentProcess = $_

    # Convert the Base 64 string to a Byte Array
    $commandBytes = [System.Convert]::FromBase64String($currentProcess.EncodedCommand)

    # Convert the Byte Array to a string
    $decodedCommand = [System.Text.Encoding]::Unicode.GetString($commandBytes)

    # Add the decoded command back to the object
    $commandDetails |
        Where-Object -FilterScript { $_.ProcessId -eq $currentProcess.processId } |
        Add-Member -MemberType NoteProperty -Name DecodedCommand -Value $decodedCommand
}
$commandDetails[0] | Format-List -Property *

现在可以通过选择解码的命令属性来查看解码的命令。

ProcessId      : 8752
EncodedCommand : IAAKAAoACgAgAAoAIAAgACAAIAAkAGkAIAA9ACAAMQAgAAoACgAKACAACgAgACAAIAAgAHcAaABpAGwAZQAgACgAIAAkAGkAIAAtAG
                 wAZQAgADEAMAAgACkAIAAKAAoACgAgAAoAIAAgACAAIAB7ACAACgAKAAoAIAAKACAAIAAgACAAIAAgACAAIABXAHIAaQB0AGUALQBP
                 AHUAdABwAHUAdAAgAC0ASQBuAHAAdQB0AE8AYgBqAGUAYwB0ACAAJABpACAACgAKAAoAIAAKACAAIAAgACAAIAAgACAAIABTAHQAYQ
                 ByAHQALQBTAGwAZQBlAHAAIAAtAFMAZQBjAG8AbgBkAHMAIAA2ADAAIAAKAAoACgAgAAoAIAAgACAAIAAgACAAIAAgACQAaQArACsA
                 IAAKAAoACgAgAAoAIAAgACAAIAB9ACAACgAKAAoAIAAKAA==
DecodedCommand :
                     $i = 1
                     while ( $i -le 10 )
                     {
                         Write-Output -InputObject $i
                         Start-Sleep -Seconds 60
                         $i++
                     }

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯