powershell加載EXE進內存運行


當實戰中我們想在目標上運行一些相當復雜的功能,這些功能常是 EXE 文件的一部分。我不想直接在目標上放置一個二進制文件,因為這樣可能會觸發反病毒機制。
一個很好的思路就是將二進制文件嵌入到 Powershell 腳本中,並直接通過腳本運行而不用將其寫入到磁盤里。

 

大致思路流程:將我們需要執行的exe文件二進制進過編碼轉換成字符串,使其成為powershell腳本的一部分,再直接加載進內存運行。

 

 

靶機環境:192.168.5.83 windows 8 x86

 

需求:將helloworld.exe用powershell加載進內存運行

 

 

單獨運行helloworld.exe效果:

 

0x01 將二進制文件進行 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;
}

 

 

 

 

 

 

執行

 . .\base64.ps1

 Convert-BinaryToString C:\Users\Administrator\Desktop\helloworld.exe

 

 

 

 

0X02 調用Invoke-ReflectivePEInjection執行

 

 

PS:Invoke-ReflectivePEInjectionPowerSpolit滲透框架下代碼執行模塊的一部分

 

地址:

https://github.com/PowerShellMafia/PowerSploit/blob/master/CodeExecution/Invoke-ReflectivePEInjection.ps1

 

 

 

在執行之前我需要把base64編碼的字符串轉換為字符數組

 

# base64 編碼的二進制文件

 

$InputString = '...........'

 

function Invoke-ReflectivePEInjection

 

{

 

   ......

   ......

   ......

 

}

 

# 將二進制字符串轉為字節數組

 

$PEBytes = [System.Convert]::FromBase64String($InputString)

 

# 在內存中運行 EXE

 

Invoke-ReflectivePEInjection -PEBytes $PEBytes -ExeArgs "Arg1 Arg2 Arg3 Arg4"

 

 

 

 

新建一個my.ps1 將helloworld.exe的base64寫入$InputString  運行

 

powershell -ExecutionPolicy Bypass -File my.ps1

 

 

根據嵌入的不同二進制文件,可能會出現以下錯誤:

PE platform doesn’t match the architecture of the process it is being loaded in (32/64bit)

解決這個問題只需要運行 32 位的 PowerShell 即可。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM