走進winrm ---powershell遠程連接的4個安全級別,詳解


---------【winrm的“四級”安全】---------

四級安全,就是最不安全的。
winrm默認使用http+5985端口,密碼傳輸加密,數據、命令傳輸明文。
有被人竊取機密,和插入攻擊命令的風險,也就是所謂的中間人攻擊。不過呢,其實問題不大。
解決的話,啟用https版的winrm,或者在http外面套上vpn即可。
強調一遍:win2012r2,win2016,win2019默認開啟這種http的5985端口。
 

---------【winrm的“三級”安全】---------

 
三級安全,是指給http套上ssl外衣。
給http套上ssl外衣后,傳輸內容加密了,連接過程還是需要認證用戶名和密碼。
這就是我說的【賴法,強制啟動,https版的winrm】,也就是說winrm客戶端連接winrm服務器使用ssl,但跳過自簽名證書,跳過域名綁定,我覺得這樣足夠了。
 
跳過證書,跳過域名綁定,的賴法,靠這兩個法寶。
-SkipCACheck #跳過證書
-SkipCNCheck #跳過服務器,機器名
 

winrm服務器:

#用管理員powershell:
Get-childitem WSMan:\Localhost\listener\
Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse
$winrm證書 = New-SelfSignedCertificate -CertstoreLocation Cert:\LocalMachine\My -DnsName 'powershell交流群183173532'
Get-childitem WSMan:\Localhost\listener\
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address *  -Force -CertificateThumbPrint $winrm證書.Thumbprint
Get-ChildItem Cert:\LocalMachine\My |Where-Object Thumbprint -eq $winrm證書.Thumbprint |Remove-Item  -Force
#防火牆添加5986端口
New-NetFirewallRule -DisplayName "ps傳教士winrm的https in" -Name "Windows Remote Management (HTTPS-In)" -Profile Any -LocalPort 5986 -RemoteAddress Any -Protocol TCP 

 

 

winrm客戶機:

#此命令要求輸入,winrm服務器上的賬戶密碼
Invoke-Command -ComputerName $winrm服務器ip -Port 5986 -Credential (Get-Credential) `
-UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) `
-ScriptBlock { dir c:\ }

 


---------【winrm的“二級”安全】---------

 

二級安全,是指在賴法的基礎上。winrm服務器/winrm客戶機連接,使用https自簽名證書。
 
需要在winrm服務器上,建立一對秘鑰,放在【Cert:\LocalMachine\my】里。沒錯,服務器上必須有公鑰+私鑰。
winrm客戶機上只需要公鑰。把公鑰導入,放到客戶機的【Cert:\LocalMachine\root】里。
 
連接時,winrm客戶機用公鑰,winrm服務器上用私鑰,這一點和ssh相反。winrm客戶機公鑰泄露問題不大,因為同時還必須要有winrm服務器的賬戶和密碼才能登陸。
 
winrm證書要求:
設置證書增強型密鑰使用 (EKU)“服務器身份驗證”(OID=1.3.6.1.5.5.7.3.1)。
將“證書主題”設置為“CN=HOSTNAME”。
 
winrm服務器:

#管理員權限powershell,在winrm服務器上,在【證書-本地計算機-》個人-》證書】中,建立證書:
 
$winrm證書參數 = @{
CertStoreLocation = "Cert:\LocalMachine\My"
KeyAlgorithm = 'RSA'
HashAlgorithm = 'sha256'
KeyLength = 4096 
Subject = "CN=powershell交流群183173532" 
NotBefore = (get-date) - [timespan]::FromDays(365)
NotAfter = (get-date) + [timespan]::FromDays(3650)
}

$winrm證書 = New-SelfSignedCertificate @winrm證書參數
#在winrm客戶機,只導出證書公鑰:
Export-Certificate -Cert $winrm證書 -FilePath 'd:\winrm證書公鑰.cer'

#讓winrm監聽6516端口:默認端口=5985(http),不監聽5986(https)
Get-childitem WSMan:\Localhost\listener\
Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse
Get-childitem WSMan:\Localhost\listener\
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address *  -Force -CertificateThumbPrint $winrm證書.Thumbprint

防火牆開啟,winrm服務器6516端口:
New-NetFirewallRule -DisplayName "ps傳教士winrm的https in" -Name "Windows Remote Management (HTTPS-In)" -Profile Any -LocalPort 5986 -RemoteAddress Any -Protocol TCP 

 

winrm客戶機:

復制公鑰文件'd:\winrm證書公鑰.cer',到客戶機上。
到winrm客戶機上,導入公鑰到【受信任證書頒發機構-》證書】:
管理員powershell:
Import-Certificate -Filepath 'd:\winrm證書公鑰.cer' -CertStoreLocation "Cert:\LocalMachine\root"

 

然后從winrm客戶機,連接winrm服務器,就不能跳過ca了。
請看代碼:
#在winrm客戶機上運行:此命令要求輸入,winrm服務器上的賬戶密碼
Invoke-Command -ComputerName $winrm服務器ip -Port 5986 -Credential (Get-Credential) `
-UseSSL -SessionOption (New-PSSessionOption -SkipCNCheck) `
-ScriptBlock { dir c:\ }

 


---------【winrm的“一級”安全】---------


一級安全,是指在二級的基礎上,從winrm客戶端,校驗winrm服務端證書的cn。這里的cn名,類似於域名。它是服務器的計算機名【$env:COMPUTERNAME】
 

winrm服務器:

$winrm證書參數 = @{
CertStoreLocation = "Cert:\LocalMachine\My"
KeyAlgorithm = 'RSA'
HashAlgorithm = 'sha256'
KeyLength = 4096 
Subject = "CN=$env:COMPUTERNAME" 
NotBefore = (get-date) - [timespan]::FromDays(365)
NotAfter = (get-date) + [timespan]::FromDays(3650)
}

$winrm證書 = New-SelfSignedCertificate @winrm證書參數

#在winrm客戶機,只導出證書公鑰:
Export-Certificate -Cert $winrm證書 -FilePath 'd:\winrm證書公鑰.cer'

#讓winrm監聽6516端口:默認端口=5985(http),不監聽5986(https)
Get-childitem WSMan:\Localhost\listener\
Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse
Get-childitem WSMan:\Localhost\listener\
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address *  -Force -CertificateThumbPrint $winrm證書.Thumbprint

#防火牆開啟,winrm服務器6516端口:
New-NetFirewallRule -DisplayName "ps傳教士winrm的https in" -Name "Windows Remote Management (HTTPS-In)" -Profile Any -LocalPort 5986 -RemoteAddress Any -Protocol TCP 

 

winrm客戶機:

復制公鑰文件'd:\winrm證書公鑰.cer',到客戶機上。
到winrm客戶機上,導入公鑰到【受信任證書頒發機構-》證書】:
管理員powershell:
Import-Certificate -Filepath 'd:\winrm證書公鑰.cer' -CertStoreLocation "Cert:\LocalMachine\root"

 

然后從winrm客戶機,連接winrm服務器,就不能跳過ca,也不能跳過cn了。請看代碼:
 
$PSRemoting服務器ip = 'DESKTOP-XXXX' #若ping DESKTOP-XXXX返回的ip不對,則要在host中添加ip 計算機名的映射。
$PSRemoting服務器用戶名 = 'abcd'
$用戶名 = "$PSRemoting服務器ip\$PSRemoting服務器用戶名"
$密碼明文 = '1234' 
$密碼密文 = ConvertTo-SecureString $密碼明文 -AsPlainText -Force
$用戶名和密碼捆綁后的授權信息 = New-Object System.Management.Automation.PSCredential ($用戶名,$密碼密文)
$連接1 = New-PSSession -ComputerName $PSRemoting服務器ip -Port 5986 -Credential $用戶名和密碼捆綁后的授權信息 -UseSSL 
Invoke-Command -session $連接1 -ScriptBlock {dir d:\}

 

---------【winrm的“最”安全】---------


最安全當然是,不用自簽名證書,購買證書了。
或者加入win的域控,成為域內機子。
從win中的powershell,連接到linux版powershell,使用ssh非對稱秘鑰,也很安全。它是服務器,客戶機雙向驗證。
 

---------【后記】---------


我寫這篇帖子的目的是,給winrm啟用https。因為winrm+https也可以做ps+web的后端,比如最微軟著名的web運維框架“火奴擼擼”。

謝謝觀看
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM