WinRM - Port 5985, 5986

Basic info

WinRM is essentially an HTTP-based interface for Windows Management Instrumentation (WMI), using SOAP to facilitate communication. Think of it as "SSH for Windows" - it allows administrators to execute commands and scripts remotely on Windows machines.

Default Ports

  • 5985/tcp - WinRM HTTP (unencrypted)

  • 5986/tcp - WinRM HTTPS (encrypted with TLS)

The presence of these open ports typically indicates that remote PowerShell access is configured and potentially accessible.

Architecture

When you connect via WinRM, the service creates a new process (wsmprovhost.exe) on the target system that handles your remote session. This process runs under the context of the authenticated user.

Reconnaissance & Enumeration

Port Scanning

Start with identifying WinRM services using Nmap:

Windows-Based Testing

If you're testing from a Windows machine, you can verify WinRM configuration:

Expected output from a properly configured target:

Shodan Queries

Find WinRM services exposed on the internet:

Authentication Methods

WinRM supports multiple authentication methods:

  1. Kerberos (default for domain-joined machines)

  2. NTLM (fallback, works with local accounts)

  3. Basic (username/password, requires HTTPS)

  4. Certificate (client certificate authentication)

  5. CredSSP (allows credential delegation)

Credential-Based Attacks

Username Enumeration

Before brute-forcing, try to enumerate valid usernames:

Brute Force Attacks

Warning: Be cautious with brute-forcing WinRM as it can lock out accounts. Always check the account lockout policy first.

Using CrackMapExec

Using Hydra

Using Metasploit

Password Spraying

Password spraying is safer than brute-forcing as it uses one password across many usernames:

Post-Authentication Exploitation

Evil-WinRM (Primary Tool)

Evil-WinRM is the go-to tool for WinRM exploitation. Install it:

Basic Connection

Advanced Evil-WinRM Features

Kerberos Authentication (v3.x+)

Certificate-Based Authentication

File Operations

Script Execution

Binary Execution

Session Logging

Bypass AMSI

PowerShell-Based Connections

From Windows Systems

Create Credential Object

Interactive Session

Persistent Sessions

Execute Single Commands

Execute Scripts

Get Reverse Shell

From Linux Systems

Using pypsrp

Using winrm-rb (Ruby)

Custom Ruby Script with Upload Capability

Using PowerShell Docker Container

NTLM Relay Attacks

Direct Relay to WinRM (Updated 2023+)

Since Impacket v0.11.0, you can relay NTLM authentication directly to WinRM:

Prerequisites:

  • Target has HTTP WinRM enabled (port 5985)

  • SMB signing not required on the network

  • Valid user authentication coerced

Setup Attack

Advanced Relay Commands

Mitigations

  • Disable HTTP listener: Set-Item WSMan:\localhost\Service\EnableCompatibilityHttpListener -Value false

  • Enable Extended Protection for Authentication (EPA)

  • Require SMB signing

  • Use HTTPS only for WinRM

Privilege Escalation via WinRM

Loading PowerShell Scripts

Credential Harvesting

Mimikatz via WinRM

Dump LSASS

Dump SAM/SYSTEM

Advanced Techniques

Constrained Language Mode Bypass

If PowerShell is in Constrained Language Mode:

Certificate-Based Authentication Exploitation

If certificate authentication is enabled:

WinRM + CredSSP for Double-Hop

CredSSP allows credential delegation (double-hop):

Forcing WinRM Remotely

If WinRM isn't enabled but you have credentials:

Cloud-Specific: Azure OMI Exploitation

OMIGOD (CVE-2021-38647)

Azure Linux VMs use OMI (Open Management Infrastructure) which exposes WS-MAN on ports 5985/5986:

Exploitation

Mitigation:

  • Update OMI to version β‰₯ 1.6.8-1

  • Block ports 5985/5986 from the internet

  • Use Azure NSG to restrict access

Lateral Movement

One-Liner Lateral Movement

Using CrackMapExec for Spray

Defense Evasion

AMSI Bypass

Logging Evasion

Obfuscation

Detection & Defense

Enable Logging

PowerShell Script Block Logging

WinRM Operational Logging

Important Event IDs:

  • Event 91/163: Shell created

  • Event 182: Authentication failure

  • Event 4262 (Security log): Records source IP (added July 2022)

Monitor for Suspicious Activity

Hardening WinRM

Network Segmentation

  • Restrict WinRM access to management VLANs only

  • Use host-based firewall rules:

Common Issues & Troubleshooting

Connection Errors

Error: "The WinRM client cannot process the request"

Error: "Access is denied"

  • Verify credentials are correct

  • Check if user is in "Remote Management Users" group

  • Verify firewall isn't blocking connection

Error: "Connection refused"

  • WinRM service not running: Start-Service WinRM

  • Firewall blocking: Enable-PSRemoting -Force

  • Listener not configured: Check with winrm enumerate winrm/config/listener

IPv6 Connections with Evil-WinRM

Practical Attack Scenarios

Scenario 1: Internal Penetration Test

Goal: Gain access to domain controller via WinRM

Scenario 2: Post-Exploitation

Goal: Establish persistence and exfiltrate data

Scenario 3: NTLM Relay Chain

Goal: Relay from compromised workstation to sensitive servers

Tool Reference

Must-Have Tools

  1. Evil-WinRM - Primary WinRM exploitation tool

  2. CrackMapExec - Network enumeration and exploitation

  3. Impacket - NTLM relay and remote execution

  4. PowerView - Domain enumeration

  5. Mimikatz - Credential dumping

  6. Rubeus - Kerberos exploitation

Installation Commands

Cheat Sheet

Quick Reference Commands

Conclusion

WinRM presents a significant attack surface when misconfigured or left accessible. As a penetration tester, understanding how to enumerate, exploit, and move laterally through WinRM services is critical for modern Windows environment assessments.

Key Takeaways:

  1. Always check for WinRM during network reconnaissance

  2. Password spraying is safer than brute-forcing

  3. Evil-WinRM is your primary tool for exploitation

  4. NTLM relay attacks can bypass credentials entirely

  5. Proper logging and monitoring are essential for defense

  6. Disable HTTP listeners and use HTTPS with EPA

  7. Restrict WinRM access to management networks only

Remember to always conduct these techniques within the scope of authorized penetration testing engagements. Unauthorized access to systems is illegal.

Additional Resources

circle-check

Last updated