Detection rules › YARA-L

Potential Remote PowerShell Session Initiated

Severity
high
Type
Hunt
Time window
5m
Match by
hostname
Source
github.com/chronicle/detection-rules

Detects a process that initiated a network connection over ports 5985 or 5986 from a non-network service account. This could potentially indicates a remote PowerShell connection.

MITRE ATT&CK coverage

References

Event coverage

Rule body yaral

/*
 * Copyright 2025 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

rule potential_remote_powershell_session_initiated {

  meta:
    author = "Roberto Rodriguez @Cyb3rWard0g"
    description = "Detects a process that initiated a network connection over ports 5985 or 5986 from a non-network service account. This could potentially indicates a remote PowerShell connection."
    reference = "https://github.com/SigmaHQ/sigma/blob/master/rules/windows/network_connection/net_connection_win_susp_remote_powershell_session.yml"
    license = "https://github.com/SigmaHQ/Detection-Rule-License/blob/main/LICENSE.Detection.Rules.md"
    rule_name = "Potential Remote PowerShell Session Initiated"
    sigma_uuid = "c539afac-c12a-46ed-b1bd-5a5567c9f045"
    sigma_status = "test"
    rule_id = "mr_7186feaf-ab01-4c41-b17f-994b08b5bc33"
    tactic = "TA0008"
    technique = "T1021.006"
    type = "Hunt"
    data_source = "Sysmon"
    platform = "Windows"
    severity = "High"
    priority = "High"
    false_positives = "Legitimate usage of remote PowerShell, e.g. remote administration and monitoring. Network Service user name of a not-covered localization"

  events:
    $net.metadata.event_type = "NETWORK_CONNECTION"
    $net.metadata.product_event_type = "3"
    $net.metadata.product_name = "Microsoft-Windows-Sysmon"
    $net.metadata.vendor_name = "Microsoft"
    (
        $net.target.port = 5985 or
        $net.target.port = 5986
    )
    //Parser extension added for Initiated/true field/value, not currently in Sysmon parser
    //Initiated: 'true'
    $net.additional.fields["Initiated"] = "true"
    net.ip_in_range_cidr($net.principal.ip, "0.0.0.0/0")
    NOT (
        strings.contains(strings.to_upper($net.principal.user.userid), "NETWORK SERVICE") or
        strings.contains(strings.to_upper($net.principal.user.userid), "NETZWERKDIENST") or
        strings.contains(strings.to_upper($net.principal.user.userid), "SERVICIO DE RED") or
        strings.contains(strings.to_upper($net.principal.user.userid), "SERVIZIO DI RETE")
        or
        (
            strings.contains(strings.to_upper($net.principal.user.userid), "SERVICE R") and
            strings.contains(strings.to_upper($net.principal.user.userid), "SEAU")
        )
    )
    NOT (
        $net.principal.ip = "::1" or
        $net.principal.ip = "127.0.0.1" or
        $net.target.ip = "::1" or
        $net.target.ip = "127.0.0.1"
    )
    NOT (
        $net.principal.process.file.full_path = "C:\\Program Files\\Avast Software\\Avast\\AvastSvc.exe" nocase or
        $net.principal.process.file.full_path = "C:\\Program Files (x86)\\Avast Software\\Avast\\AvastSvc.exe" nocase
    )

    $net.principal.hostname = $hostname

  match:
    $hostname over 5m

  outcome:
    //example usage of specifying test user and hostname to adjust risk score
    $risk_score = max(if($net.principal.user.userid = "user" and $net.principal.hostname = "hostname", 0, 15))
    $principal_ip = array_distinct($net.principal.ip)
    $principal_port = array_distinct($net.principal.port)
    $principal_process_pid = array_distinct($net.principal.process.pid)
    $principal_process_command_line = array_distinct($net.principal.process.command_line)
    $principal_process_file_sha256 = array_distinct($net.principal.process.file.sha256)
    $principal_process_file_full_path = array_distinct($net.principal.process.file.full_path)
    $principal_process_product_specific_process_id = array_distinct($net.principal.process.product_specific_process_id)
    $principal_process_parent_process_product_specific_process_id = array_distinct($net.principal.process.parent_process.product_specific_process_id)
    $principal_user_userid = array_distinct($net.principal.user.userid)
    $target_ip = array_distinct($net.target.ip)
    $target_port = array_distinct($net.target.port)

  condition:
    $net
}

Stages and Predicates

Stage 0: match + condition

match:
    $hostname over 5m
condition:
    $net

Fires when at least one $net event in the 5m window.

Stage 1: events: $net · NETWORK_CONNECTION

$net.metadata.event_type = "NETWORK_CONNECTION"
$net.metadata.product_event_type = "3"
$net.metadata.product_name = "Microsoft-Windows-Sysmon"
$net.metadata.vendor_name = "Microsoft"
(
    $net.target.port = 5985 or
    $net.target.port = 5986
)
//Parser extension added for Initiated/true field/value, not currently in Sysmon parser
//Initiated: 'true'
$net.additional.fields["Initiated"] = "true"
net.ip_in_range_cidr($net.principal.ip, "0.0.0.0/0")
NOT (
    strings.contains(strings.to_upper($net.principal.user.userid), "NETWORK SERVICE") or
    strings.contains(strings.to_upper($net.principal.user.userid), "NETZWERKDIENST") or
    strings.contains(strings.to_upper($net.principal.user.userid), "SERVICIO DE RED") or
    strings.contains(strings.to_upper($net.principal.user.userid), "SERVIZIO DI RETE")
    or
    (
        strings.contains(strings.to_upper($net.principal.user.userid), "SERVICE R") and
        strings.contains(strings.to_upper($net.principal.user.userid), "SEAU")
    )
)
NOT (
    $net.principal.ip = "::1" or
    $net.principal.ip = "127.0.0.1" or
    $net.target.ip = "::1" or
    $net.target.ip = "127.0.0.1"
)
NOT (
    $net.principal.process.file.full_path = "C:\\Program Files\\Avast Software\\Avast\\AvastSvc.exe" nocase or
    $net.principal.process.file.full_path = "C:\\Program Files (x86)\\Avast Software\\Avast\\AvastSvc.exe" nocase
)

$net.principal.hostname = $hostname

Exclusions

Top-level NOT(...) conjuncts: predicates this rule actively suppresses.

StageFieldKindExcluded values
1principal.user.useridcontainsSEAU
1principal.user.useridcontainsSERVICE R
1principal.user.useridcontainsNETWORK SERVICE
1principal.user.useridcontainsNETZWERKDIENST
1principal.user.useridcontainsSERVICIO DE RED
1principal.user.useridcontainsSERVIZIO DI RETE
1principal.ipeq127.0.0.1
1principal.ipeq::1
1target.ipeq127.0.0.1
1target.ipeq::1
1principal.process.file.full_patheqC:\\Program Files (x86)\\Avast Software\\Avast\\AvastSvc.exe
1principal.process.file.full_patheqC:\\Program Files\\Avast Software\\Avast\\AvastSvc.exe

Indicators

Each row is a field, operator, and value that the rule matches. The corpus column counts how many other rules in the catalog look for the same combination: high numbers point to widely-used, community-vetted indicators. Blank or 1 shows that the indicator is specific to this rule.

FieldKindValues
additional.fields["Initiated"]eq
  • true
metadata.product_event_typeeq
  • 3 corpus 16 (splunk 14, chronicle 1, kusto 1)
principal.ipcidr_match
  • 0.0.0.0/0
target.porteq
  • 5985 corpus 3 (sigma 2, chronicle 1)
  • 5986 corpus 3 (sigma 2, chronicle 1)

Output fields

Fields the rule emits when it matches. Chronicle authors list these in the outcome block; they appear on the detection and $risk_score drives alerting. Sentinel / Defender XDR rules build them up through project / summarize / extend stages. Sentinel maps these into alert fields via entityMappings and customDetails; Defender XDR custom detections surface them as alert fields directly.

FieldSource
risk_scoremax(if($net.principal.user.userid = "user" and $net.principal.hostname = "hostname", 0, 15))
principal_iparray_distinct($net.principal.ip)
principal_portarray_distinct($net.principal.port)
principal_process_pidarray_distinct($net.principal.process.pid)
principal_process_command_linearray_distinct($net.principal.process.command_line)
principal_process_file_sha256array_distinct($net.principal.process.file.sha256)
principal_process_file_full_patharray_distinct($net.principal.process.file.full_path)
principal_process_product_specific_process_idarray_distinct($net.principal.process.product_specific_process_id)
principal_process_parent_process_product_specific_process_idarray_distinct($net.principal.process.parent_process.product_specific_process_id)
principal_user_useridarray_distinct($net.principal.user.userid)
target_iparray_distinct($net.target.ip)
target_portarray_distinct($net.target.port)