Detect Compile After Delivery in Elastic Security
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/
Elastic Detection Query
sequence by host.id with maxspan=5m
[process where event.type == "start"
and process.name in~ ("csc.exe", "vbc.exe", "jsc.exe", "ilasm.exe", "msbuild.exe", "gcc", "g++", "cl.exe", "rc.exe", "mc.exe")
and (
process.executable : ("*\\Windows\\Microsoft.NET\\*", "*\\Microsoft Visual Studio\\*", "*\\MinGW\\*", "*\\Temp\\*", "*\\AppData\\*")
or process.args : ("*\\Temp\\*", "*\\AppData\\*", "*\\Downloads\\*", "*\\Users\\*")
)
and not process.args : ("*\\Windows\\WinSxS\\*", "*Visual Studio*", "*devenv*")
and process.parent.name in~ ("powershell.exe", "wscript.exe", "cscript.exe", "mshta.exe", "cmd.exe", "python.exe", "wmic.exe")
]
[file where event.type in ("creation", "change")
and file.extension in ("exe", "dll", "bin")
and file.path : ("*\\Temp\\*", "*\\AppData\\*", "*\\Downloads\\*", "*\\Users\\*")
] Detects compile-after-delivery via built-in compilers (csc.exe, vbc.exe, ilasm.exe, msbuild.exe, gcc) spawned from suspicious parent processes with source files in user-writable paths, correlated with output binary creation. Maps to T1027.004.
Data Sources
Required Tables
False Positives & Tuning
- Legitimate developer workstations running .NET or C++ build pipelines where build output lands in user temp directories during CI/CD agent execution
- IT automation tooling (e.g. SCCM, Ansible) that compiles scripts on-the-fly during software deployment to standard user paths
- Security software and EDR agents that use embedded compilers for JIT compilation or instrumentation, spawned from service processes
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.