clrinject:向CLR Runtimes和AppDomain中注入程式碼的工具
前言
clrinject是一款可將C#,EXE或DLL程式集,注入另一個程序CLR Runtimes和AppDomain的工具。注入的程式集可訪問injectee程序類的靜態例項,從而影響其內部狀態。
使用
clrinject-cli.exe -p <processId/processName> -a <assemblyFile>
開啟id為<processId>或名稱為<processName>的程序,注入<assemblyFile> EXE並執行Main方法。
其他選項
-e:列舉所有已載入的CLR Runtimes時和建立的AppDomain。
-d <#>:僅注入<#> -th AppDomain。如果未指定數字或指定為零,則會將程式集註入到所有AppDomain。
-i <namespace>.<className>:從名稱空間<namespace>建立類<className>的例項。
示例
使用示例
從victim.exe列舉Runtimes和AppDomains:
clrinject-cli.exe -p victim.exe -e
將invader.exe從id為1234的程序注入第二個AppDomain:
clrinject-cli.exe -p 1234 -a "C:\Path\To\invader.exe" -d 2
在victim.exe中的每個AppDomain中建立Invader例項:
clrinject-cli.exe -p victim.exe -a "C:\Path\To\invader.dll" -i "Invader.Invader"
將x64程式集註入x64程序:
clrinject-cli64.exe -p victim64.exe -a "C:\Path\To\invader64.exe"
可注入程式集示例
以下程式碼可編譯為C#可執行檔案,然後注入到一個Shell/">PowerShell程序。這段程式碼將會訪問內部PowerShell類的靜態例項,並將控制檯文字的顏色更改為綠色。
using System; using System.Reflection; using Microsoft.PowerShell; using System.Management.Automation.Host; namespace Invader { class Invader { static void Main(string[] args) { try { var powerShellAssembly = typeof(ConsoleShell).Assembly; var consoleHostType = powerShellAssembly.GetType("Microsoft.PowerShell.ConsoleHost"); var consoleHost = consoleHostType.GetProperty("SingletonInstance", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); var ui = (PSHostUserInterface)consoleHostType.GetProperty("UI").GetValue(consoleHost); ui.RawUI.ForegroundColor = ConsoleColor.Green; } catch (Exception e) { Console.WriteLine(e.ToString()); } } } }
注入命令:
clrinject-cli64.exe -p powershell.exe -a "C:\Path\To\invader64.exe"
結果: