Detect Compile After Delivery in Microsoft Sentinel
Adversaries may attempt to make payloads difficult to discover and analyze by delivering files to victims as uncompiled code. Text-based source code files may subvert analysis and scrutiny from protections targeting executables/binaries. These payloads will need to be compiled before execution; typically via native utilities such as csc.exe (C# compiler), ilasm.exe (.NET assembler), or GCC/MinGW. Source code payloads may also be encrypted or encoded. Threat actors including MuddyWater, Gamaredon Group, Rocke, Cardinal RAT, and DarkWatchman have used this technique to compile malware on victim systems using built-in compiler utilities.
MITRE ATT&CK
- Tactic
- Defense Evasion
- Technique
- T1027 Obfuscated Files or Information
- Sub-technique
- T1027.004 Compile After Delivery
- Canonical reference
- https://attack.mitre.org/techniques/T1027/004/
KQL Detection Query
let CompilerPaths = dynamic([
"csc.exe", "vbc.exe", "jsc.exe", "ilasm.exe", "msbuild.exe",
"gcc", "g++", "cl.exe", "rc.exe", "mc.exe"
]);
DeviceProcessEvents
| where Timestamp > ago(24h)
| where FileName in~ (CompilerPaths)
| where FolderPath has_any (
"\\Windows\\Microsoft.NET\\",
"\\Microsoft Visual Studio\\",
"\\MinGW\\",
"\\Temp\\",
"\\AppData\\"
)
| where ProcessCommandLine !contains "\\Windows\\WinSxS\\"
and ProcessCommandLine !contains "Visual Studio"
and ProcessCommandLine !contains "msbuild /t:Build"
| extend IsCSC = FileName =~ "csc.exe"
| extend IsMSBuild = FileName =~ "msbuild.exe"
| extend SourceFromTemp = ProcessCommandLine has_any ("\\Temp\\", "\\AppData\\", "\\Users\\", "\\Downloads\\")
| project Timestamp, DeviceName, AccountName, FileName, ProcessCommandLine,
InitiatingProcessFileName, InitiatingProcessCommandLine,
IsCSC, IsMSBuild, SourceFromTemp
| sort by Timestamp desc Detects execution of .NET compilers (csc.exe, vbc.exe, ilasm.exe), MSBuild, and native compilers (gcc, cl.exe) in contexts consistent with compile-after-delivery attacks. Specifically flags compiler invocations where the source file path is in a user-writable directory (Temp, AppData, Downloads, Users) rather than a standard development directory. MuddyWater and Gamaredon have used csc.exe to compile C# source delivered as phishing attachments.
Data Sources
Required Tables
False Positives & Tuning
- Legitimate software development activity on developer workstations where devs compile projects in user home directories
- MSBuild invocations by Visual Studio or CI/CD build agents that legitimately compile in workspace directories
- Package managers and build tools (NuGet, npm, Cargo) that invoke compilers as part of dependency compilation
- System administration scripts that use csc.exe to compile small C# utilities for automation tasks
Other platforms for T1027.004
Testing Methodology
Validate this detection against 4 adversary techniques from Atomic Red Team. Each test below lists the behaviour to exercise and the telemetry you should expect to see. Executable commands and cleanup steps are available with Pro.
- Test 1Compile and Execute C# Payload via csc.exe
Expected signal: Sysmon Event ID 11: payload.cs created in %TEMP%. Sysmon Event ID 1: csc.exe with /out: argument pointing to %TEMP%. Sysmon Event ID 11: compiled_payload.exe created. Sysmon Event ID 1: compiled_payload.exe execution.
- Test 2MSBuild Execution of Inline C# Task (LOLBin Abuse)
Expected signal: Sysmon Event ID 11: evil.proj created in %TEMP%. Sysmon Event ID 1: MSBuild.exe process creation with %TEMP%\evil.proj argument. The code executes within the MSBuild process itself — no separate child process for the C# code.
- Test 3Compile C Source Code with GCC (Linux/macOS)
Expected signal: Process creation events: gcc with /tmp/payload.c argument, then compiled_payload execution. Syslog/auditd: execve syscalls for gcc and the compiled binary. File creation events for /tmp/payload.c and /tmp/compiled_payload.
- Test 4PowerShell Spawning csc.exe to Compile Downloaded Source
Expected signal: Sysmon Event ID 1: powershell.exe with Set-Content and Start-Process commands. Sysmon Event ID 11: downloaded.cs and output.exe created in %TEMP%. Sysmon Event ID 1: csc.exe with parent PowerShell. Sysmon Event ID 1: output.exe executed.
References (5)
- https://attack.mitre.org/techniques/T1027/004/
- https://www.clearskysec.com/wp-content/uploads/2018/11/MuddyWater-Operations-in-Lebanon-and-Oman.pdf
- https://www.attackiq.com/2023/03/16/hiding-in-plain-sight/
- https://blog.trendmicro.com/trendlabs-security-intelligence/windows-app-runs-on-mac-downloads-info-stealer-and-adware/
- https://github.com/redcanaryco/atomic-red-team/blob/master/atomics/T1027.004/T1027.004.md
Unlock Pro Content
Get the full detection package for T1027.004 including response playbook, investigation guide, and atomic red team tests.