Suspicious Dynamic .NET Compilation via Csc.exe
Attackers may use csc.exe to compile .NET code on the fly to evade detection, often placing the compiler and source code in suspicious locations, which can be detected by monitoring process creation events.
Attackers often utilize the .NET Framework’s command-line compiler, csc.exe, to compile malicious code dynamically on compromised systems. This tactic allows them to evade traditional signature-based detections and execute code in memory. The compilation often occurs from unusual or temporary directories such as \Perflogs\, \Users\Public\, or within the AppData directory. This technique has been observed in campaigns involving malware such as Agent Tesla and by actors like MuddyWater. Detection focuses on identifying csc.exe executions originating from or utilizing paths indicative of suspicious activity outside of normal software development workflows.
Attack Chain
- Initial Access: The attacker gains initial access to the system through an exploit or social engineering.
- Payload Delivery: A malicious payload containing .NET source code is delivered to the system, often dropped in a temporary directory or a user’s profile directory.
- Command Execution: The attacker uses a command-line interface (cmd.exe or powershell.exe) to execute
csc.exe. - Dynamic Compilation:
csc.execompiles the .NET source code into an executable or DLL file. - File Creation: The compiled assembly is written to disk in a specified location.
- Code Injection/Execution: The compiled assembly is loaded into memory and executed, often using techniques like reflective DLL injection.
- Persistence (Optional): The attacker may establish persistence by creating a scheduled task or modifying registry keys to recompile and execute the malicious code on system startup.
- Achieve Objectives: The attacker achieves their objectives, such as data exfiltration, lateral movement, or establishing a command and control channel.
Impact
Successful exploitation can lead to arbitrary code execution, allowing attackers to perform a wide range of malicious activities. This can result in data theft, system compromise, and the deployment of ransomware. While the number of victims and sectors targeted varies depending on the specific campaign, dynamic compilation techniques significantly increase the difficulty of detection and response, making systems vulnerable to persistent and stealthy attacks.
Recommendation
- Deploy the provided Sigma rules to your SIEM to detect suspicious executions of
csc.exefrom unusual locations (process_creation logs). - Tune the Sigma rules for your environment to reduce false positives, considering legitimate uses of
csc.exeby developers (Sigma rules). - Monitor process creation events for
csc.exewith command-line arguments containing suspicious directory locations like\Perflogs\,\Users\Public\,\AppData\Local\Temp\(process_creation logs). - Investigate any instances where
csc.exeis executed by processes other than legitimate software development tools, filtering out known good parent processes likesdiagnhost.exeorw3wp.exe(process_creation logs). - Consider blocking execution of
csc.exefrom user-writable directories if it is not a legitimate use case in your environment.
Detection coverage 2
Detect Csc.exe from Suspicious Locations
mediumDetects the execution of csc.exe from suspicious directories often used by attackers to evade detection.
Detect Csc.exe Compiling from User Favorites/Contacts/Pictures
mediumDetects csc.exe compiling code where the source files are located in user profile directories like Favorites or Contacts, which is suspicious.
Detection queries are kept inside the platform. Get full rules →