Potential Process Injection via PowerShell

Detects the use of Windows API functions that are commonly abused by malware and security tools to load malicious code or inject it into remote processes.

Elastic rule (View on GitHub)

 1[metadata]
 2creation_date = "2021/10/14"
 3integration = ["windows"]
 4maturity = "production"
 5updated_date = "2024/10/28"
 6min_stack_version = "8.14.0"
 7min_stack_comments = "Breaking change at 8.14.0 for the Windows Integration."
 8
 9[rule]
10author = ["Elastic"]
11description = """
12Detects the use of Windows API functions that are commonly abused by malware and security tools to load malicious code
13or inject it into remote processes.
14"""
15false_positives = ["Legitimate PowerShell scripts that make use of these functions."]
16from = "now-9m"
17index = ["winlogbeat-*", "logs-windows.powershell*"]
18language = "kuery"
19license = "Elastic License v2"
20name = "Potential Process Injection via PowerShell"
21note = """## Triage and analysis
22
23### Investigating Potential Process Injection via PowerShell
24
25PowerShell is one of the main tools system administrators use for automation, report routines, and other tasks. This makes it available for use in various environments, and creates an attractive way for attackers to execute code.
26
27PowerShell also has solid capabilities to make the interaction with the Win32 API in an uncomplicated and reliable way, like the execution of inline C# code, PSReflect, Get-ProcAddress, etc.
28
29Red Team tooling and malware developers take advantage of these capabilities to develop stagers and loaders that inject payloads directly into the memory without touching the disk to circumvent file-based security protections.
30
31#### Possible investigation steps
32
33- Examine the script content that triggered the detection; look for suspicious DLL imports, collection or exfiltration capabilities, suspicious functions, encoded or compressed data, and other potentially malicious characteristics.
34- Investigate the script execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence, whether they are located in expected locations, and if they are signed with valid digital signatures.
35- Examine file or network events from the involved PowerShell process for suspicious behavior.
36- Investigate other alerts associated with the user/host during the past 48 hours.
37- Evaluate whether the user needs to use PowerShell to complete tasks.
38- Check if the imported function was executed and which process it targeted.
39- Check if the injected code can be retrieved (hardcoded in the script or on command line logs).
40
41### False positive analysis
42
43- This activity is unlikely to happen legitimately. Benign true positives (B-TPs) can be added as exceptions if necessary.
44
45### Related rules
46
47- PowerShell PSReflect Script - 56f2e9b5-4803-4e44-a0a4-a52dc79d57fe
48
49### Response and remediation
50
51- Initiate the incident response process based on the outcome of the triage.
52- Isolate the involved hosts to prevent further post-compromise behavior.
53- Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
54- Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
55- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
56- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
57- Using the incident response data, update logging and audit policies to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
58"""
59references = [
60    "https://github.com/EmpireProject/Empire/blob/master/data/module_source/management/Invoke-PSInject.ps1",
61    "https://github.com/EmpireProject/Empire/blob/master/data/module_source/management/Invoke-ReflectivePEInjection.ps1",
62    "https://github.com/BC-SECURITY/Empire/blob/master/empire/server/data/module_source/credentials/Invoke-Mimikatz.ps1",
63    "https://www.elastic.co/security-labs/detect-credential-access",
64]
65risk_score = 73
66rule_id = "2e29e96a-b67c-455a-afe4-de6183431d0d"
67setup = """## Setup
68
69The 'PowerShell Script Block Logging' logging policy must be enabled.
70Steps to implement the logging policy with Advanced Audit Configuration:

Computer Configuration > Administrative Templates > Windows PowerShell > Turn on PowerShell Script Block Logging (Enable)

1
2Steps to implement the logging policy via registry:

reg add "hklm\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" /v EnableScriptBlockLogging /t REG_DWORD /d 1

 1"""
 2severity = "high"
 3tags = [
 4    "Domain: Endpoint",
 5    "OS: Windows",
 6    "Use Case: Threat Detection",
 7    "Tactic: Defense Evasion",
 8    "Tactic: Execution",
 9    "Resources: Investigation Guide",
10    "Data Source: PowerShell Logs",
11]
12timestamp_override = "event.ingested"
13type = "query"
14
15query = '''
16event.category:process and host.os.type:windows and
17  powershell.file.script_block_text : (
18   (VirtualAlloc or VirtualAllocEx or VirtualProtect or LdrLoadDll or LoadLibrary or LoadLibraryA or
19      LoadLibraryEx or GetProcAddress or OpenProcess or OpenProcessToken or AdjustTokenPrivileges) and
20   (WriteProcessMemory or CreateRemoteThread or NtCreateThreadEx or CreateThread or QueueUserAPC or
21      SuspendThread or ResumeThread or GetDelegateForFunctionPointer)
22  ) and not 
23  file.directory: (
24    "C:\ProgramData\Microsoft\Windows Defender Advanced Threat Protection\SenseCM" or
25    "C:\ProgramData\Microsoft\Windows Defender Advanced Threat Protection\Downloads"
26  )
27'''
28
29
30[[rule.threat]]
31framework = "MITRE ATT&CK"
32[[rule.threat.technique]]
33id = "T1055"
34name = "Process Injection"
35reference = "https://attack.mitre.org/techniques/T1055/"
36[[rule.threat.technique.subtechnique]]
37id = "T1055.001"
38name = "Dynamic-link Library Injection"
39reference = "https://attack.mitre.org/techniques/T1055/001/"
40
41[[rule.threat.technique.subtechnique]]
42id = "T1055.002"
43name = "Portable Executable Injection"
44reference = "https://attack.mitre.org/techniques/T1055/002/"
45
46
47
48[rule.threat.tactic]
49id = "TA0005"
50name = "Defense Evasion"
51reference = "https://attack.mitre.org/tactics/TA0005/"
52[[rule.threat]]
53framework = "MITRE ATT&CK"
54[[rule.threat.technique]]
55id = "T1059"
56name = "Command and Scripting Interpreter"
57reference = "https://attack.mitre.org/techniques/T1059/"
58[[rule.threat.technique.subtechnique]]
59id = "T1059.001"
60name = "PowerShell"
61reference = "https://attack.mitre.org/techniques/T1059/001/"
62
63
64[[rule.threat.technique]]
65id = "T1106"
66name = "Native API"
67reference = "https://attack.mitre.org/techniques/T1106/"
68
69
70[rule.threat.tactic]
71id = "TA0002"
72name = "Execution"
73reference = "https://attack.mitre.org/tactics/TA0002/"

Triage and analysis

Investigating Potential Process Injection via PowerShell

PowerShell is one of the main tools system administrators use for automation, report routines, and other tasks. This makes it available for use in various environments, and creates an attractive way for attackers to execute code.

PowerShell also has solid capabilities to make the interaction with the Win32 API in an uncomplicated and reliable way, like the execution of inline C# code, PSReflect, Get-ProcAddress, etc.

Red Team tooling and malware developers take advantage of these capabilities to develop stagers and loaders that inject payloads directly into the memory without touching the disk to circumvent file-based security protections.

Possible investigation steps

  • Examine the script content that triggered the detection; look for suspicious DLL imports, collection or exfiltration capabilities, suspicious functions, encoded or compressed data, and other potentially malicious characteristics.
  • Investigate the script execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence, whether they are located in expected locations, and if they are signed with valid digital signatures.
  • Examine file or network events from the involved PowerShell process for suspicious behavior.
  • Investigate other alerts associated with the user/host during the past 48 hours.
  • Evaluate whether the user needs to use PowerShell to complete tasks.
  • Check if the imported function was executed and which process it targeted.
  • Check if the injected code can be retrieved (hardcoded in the script or on command line logs).

False positive analysis

  • This activity is unlikely to happen legitimately. Benign true positives (B-TPs) can be added as exceptions if necessary.
  • PowerShell PSReflect Script - 56f2e9b5-4803-4e44-a0a4-a52dc79d57fe

Response and remediation

  • Initiate the incident response process based on the outcome of the triage.
  • Isolate the involved hosts to prevent further post-compromise behavior.
  • Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
  • Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
  • Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
  • Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
  • Using the incident response data, update logging and audit policies to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).

References

Related rules

to-top