Detection: Malicious Powershell Executed As A Service

Description

The following analytic identifies the execution of malicious PowerShell commands or payloads via the Windows SC.exe utility. It detects this activity by analyzing Windows System logs (EventCode 7045) and filtering for specific PowerShell-related patterns in the ImagePath field. This behavior is significant because it indicates potential abuse of the Windows Service Control Manager to run unauthorized or harmful scripts, which could lead to system compromise. If confirmed malicious, this activity could allow attackers to execute arbitrary code, escalate privileges, or maintain persistence within the environment.

 1`wineventlog_system`
 2EventCode=7045
 3
 4| eval l_ImagePath=lower(ImagePath)
 5
 6| regex l_ImagePath="powershell[.\s]
 7|powershell_ise[.\s]
 8|pwsh[.\s]
 9|psexec[.\s]"
10
11| regex l_ImagePath="-nop[rofile\s]+
12|-w[indowstyle]*\s+hid[den]*
13|-noe[xit\s]+
14|-enc[odedcommand\s]+"
15
16| stats count min(_time) as firstTime max(_time) as lastTime
17    by EventCode ImagePath ServiceName StartType
18       ServiceType AccountName UserID dest
19
20| rename UserID as user
21
22| `security_content_ctime(firstTime)`
23
24| `security_content_ctime(lastTime)`
25
26| `malicious_powershell_executed_as_a_service_filter`

Data Source

Name Platform Sourcetype Source
Windows Event Log System 7045 Windows icon Windows 'XmlWinEventLog' 'XmlWinEventLog:System'

Macros Used

Name Value
wineventlog_system eventtype="wineventlog_system" OR Channel="system" OR source="XmlWinEventLog:System" OR source="WinEventLog:System"
malicious_powershell_executed_as_a_service_filter search *
malicious_powershell_executed_as_a_service_filter is an empty macro by default. It allows the user to filter out any results (false positives) without editing the SPL.

Annotations

- MITRE ATT&CK
+ Kill Chain Phases
+ NIST
+ CIS
- Threat Actors
ID Technique Tactic
T1569.002 Service Execution Execution
Installation
DE.CM
CIS 10

Default Configuration

This detection is configured by default in Splunk Enterprise Security to run with the following settings:

Setting Value
Disabled true
Cron Schedule 0 * * * *
Earliest Time -70m@m
Latest Time -10m@m
Schedule Window auto
Creates Finding (Notable) Yes
Rule Title %name%
Rule Description %description%
Notable Event Fields user, dest
Creates Intermediate Finding (Risk Event) Yes
TTP detections generate a Finding (Notable) and may generate Intermediate Findings (Risk Events) for associated entities.

Implementation

To successfully implement this search, you need to be ingesting Windows System logs with the Service name, Service File Name Service Start type, and Service Type from your endpoints.

Known False Positives

Creating a hidden powershell service is rare and could key off of those instances.

Associated Analytic Story

Finding

Title Entity Field Entity Type Risk Score
Identifies the abuse the Windows SC.exe to execute malicious powerShell as a service $ImagePath$ by $user$ on $dest$ user user 50

Intermediate Findings

Message Entity Field Entity Type Risk Score
Identifies the abuse the Windows SC.exe to execute malicious powerShell as a service $ImagePath$ by $user$ on $dest$ dest system 50

References

Detection Testing

Test Type Status Dataset Source Sourcetype
Validation Passing N/A N/A N/A
Unit Passing Dataset XmlWinEventLog:System XmlWinEventLog
Integration ✅ Passing Dataset XmlWinEventLog:System XmlWinEventLog

Replay any dataset to Splunk Enterprise by using our replay.py tool or the UI. Alternatively you can replay a dataset into a Splunk Attack Range


Source: GitHub | Version: 13