Detection rules › YARA-L

Potential Remote PowerShell Session Initiated

Severity
high
Type
Hunt
Time window
5m
Match by
hostname
Author
Roberto Rodriguez @Cyb3rWard0g
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
}

Detection logic

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

Events

$net NETWORK_CONNECTION (3)

  • metadata.product_event_type = "3"
  • target.port = "5985" or "5986"
  • additional.fields["Initiated"] = "true"
  • principal.ip cidr_match "0.0.0.0/0"
  • principal.user.userid contains "NETWORK SERVICE"
  • principal.user.userid contains "NETZWERKDIENST"
  • principal.user.userid contains "SERVICIO DE RED"
  • principal.user.userid contains "SERVIZIO DI RETE"
  • principal.user.userid contains "SERVICE R"
  • principal.user.userid contains "SEAU"
  • principal.ip = "::1"
  • principal.ip = "127.0.0.1"
  • target.ip = "::1"
  • target.ip = "127.0.0.1"
  • principal.process.file.full_path = "C:\\Program Files\\Avast Software\\Avast\\AvastSvc.exe" or "C:\\Program Files (x86)\\Avast Software\\Avast\\AvastSvc.exe"

Correlation

Match key
$hostname (principal.hostname)
Within
5m

Outcome

Fields the detection emits on a match. $risk_score drives alerting; Chronicle surfaces the rest on the detection.

FieldExpression
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)