certify icon indicating copy to clipboard operation
certify copied to clipboard

Possible PowerShell Script for RRAS (SSTP VPN)

Open j0mbie opened this issue 7 years ago • 7 comments

So far, this seems to work for me. After testing, you may want to include it in your PowerShell scripts that you include in the installation.

# Enable/redo certificate for SSTP VPN

param($result)

# Store certificate in variable
$cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Thumbprint -match $result.ManagedItem.CertificateThumbprintHash}

# Stop RRAS, set cert, start RRAS
Import-Module RemoteAccess
Stop-Service RemoteAccess
Set-RemoteAccess -SslCertificate $cert
Start-Service RemoteAccess

j0mbie avatar Mar 16 '19 20:03 j0mbie

Thanks, it's always good to have more examples!

webprofusion-chrisc avatar Mar 16 '19 23:03 webprofusion-chrisc

Wow. That's really a lot simpler than what I've had, heh. This is what replaced (believe it or not) a even worse hack. I've been using it for a few years:

Import-Module WebAdministration # required for iis binding

#fix RDP cert.
# get a reference to the config instance
$tsgs = Get-WmiObject -class "Win32_TSGeneralSetting" -Namespace root\cimv2\terminalservices -Filter "TerminalName='RDP-tcp'"

# grab the thumbprint of the first SSL cert in the computer store
$thumb = (Get-ChildItem -path cert:/LocalMachine/My | select -first 1).Thumbprint

# set the new thumbprint value
Set-WmiInstance -path $($tsgs.__path) -argument @{SSLCertificateSHA1Hash="$thumb"}

$cert = (Get-ChildItem -path cert:/LocalMachine/My | select -first 1)
[System.IO.MemoryStream]$stream = $cert.GetRawCertData()
$stream.position = 0
$hash = Get-FileHash -InputStream $stream -Algorithm SHA256

#old method for binding
$params = "http add sslcert ipport=[::]:443 certhash=$($hash.hash) appid={ba195980-cd49-458b-9e23-c84ee0adcd75} certstorename=MY"
$netsh = Start-Process -ArgumentList $params -FilePath C:\Windows\System32\netsh.exe -PassThru -Wait

#sstp hash new method
$location = "HKLM:\SYSTEM\CurrentControlSet\Services\SstpSvc\Parameters"
$h = $hash.hash
$b = ''
for ($i=0; $i -le $h.Length-2; $i = $i+2) {
#echo "$i is $($h.substring($i,2))"
$b += $h.substring($i,2)
if ($i -eq $h.Length-2) { } #echo "$i, no comma" }
else { $b += "," }
}
$hex = $b.Split(',') | ForEach-Object { "0x$_" }
New-ItemProperty -Path $location -Name "SHA256CertificateHash" -PropertyType Binary -Value ([byte[]]$hex) -Force
Get-Service sstpsvc | Stop-Service -Force -PassThru
Get-Service remoteaccess | Start-Service -PassThru

korhojoa avatar May 24 '19 13:05 korhojoa

The code from j0mbie worked great for me on newer servers, but would not run for me on server 2008 R2. I was able to get this working by using netsh.

param($result)

netsh ras set sstp-ssl-cert hash = $result.manageditem.certificatethumbprinthash
net stop remoteaccess
net start remoteaccess

Larry-bit avatar Sep 12 '19 22:09 Larry-bit

Thank you very much Larry-bit for your simple option to update RRAS. I screwed around for 2 hours trying to find a working solution for this on a server 2008 R2. This worked great.

Thank you also j0mbie.

RKENS avatar Oct 03 '19 18:10 RKENS

This is now a built-in Deployment Task as of v5 (beta), no scripting required. Testing appreciated when anyone gets time.

webprofusion-chrisc avatar May 12 '20 09:05 webprofusion-chrisc

With Server 2012 R2 the Deployment task for this says "Status: Success Task Completed OK" however i still need to update the hash manually. I presume this means i need to implement Larry-bit's 2008 code instead?

adamtmitchell avatar Apr 11 '22 02:04 adamtmitchell

@adamtmitchell yes, normally if any app is referring to the windows certificate store for a cert then $result.ManagedItem.CertificateThumbprintHash is the hash to use. This is particularly true for apps which use the standard windows schannel stuff for their tls implementation (which all microsoft stuff generally does). By all means try setting the hash using netsh. It seems like the RAS/SSTP stuff is particular difficult to pin down for the correct behaviour, especially across versions.

webprofusion-chrisc avatar Apr 11 '22 04:04 webprofusion-chrisc