Advertisement
Guest User

Untitled

a guest
Jun 1st, 2018
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. #Requires -RunAsAdministrator
  2.  
  3. <#
  4. .SYNOPSIS
  5. Add or remove the Algo VPN
  6.  
  7. .DESCRIPTION
  8. Add or remove the Algo VPN
  9. See the examples for more information
  10.  
  11. .PARAMETER Add
  12. Add the VPN to the local system
  13.  
  14. .PARAMETER Remove
  15. Remove the VPN from the local system
  16.  
  17. .PARAMETER GetInstalledCerts
  18. Retrieve Algo certs, if any, from the system certificate store
  19.  
  20. .PARAMETER SaveCerts
  21. Save the Algo certs embedded in this file
  22.  
  23. .PARAMETER OutputDirectory
  24. When saving the Algo certs, save to this directory
  25.  
  26. .PARAMETER Pkcs12DecryptionPassword
  27. The decryption password for the user's PKCS12 certificate, sometimes called the "p12 password".
  28. Note that this must be passed in as a SecureString, not a regular string.
  29. You can create a secure string with the `Read-Host -AsSecureString` cmdlet.
  30. See the examples for more information.
  31.  
  32. .EXAMPLE
  33. client_USER.ps1 -Add
  34.  
  35. Adds the Algo VPN
  36.  
  37. .EXAMPLE
  38. $p12pass = Read-Host -AsSecureString; client_USER.ps1 -Add -Pkcs12DecryptionPassword $p12pass
  39.  
  40. Create a variable containing the PKCS12 decryption password, then use it when adding the VPN.
  41. This can be especially useful when troubleshooting, because you can use the same variable with
  42. multiple calls to client_USER.ps1, rather than having to type the PKCS12 password each time.
  43.  
  44. .EXAMPLE
  45. client_USER.ps1 -Remove
  46.  
  47. Removes the Algo VPN if installed.
  48.  
  49. .EXAMPLE
  50. client_USER.ps1 -GetIntalledCerts
  51.  
  52. Show the Algo VPN's installed certificates, if any.
  53.  
  54. .EXAMPLE
  55. client_USER.ps1 -SaveCerts -OutputDirectory $Home\Downloads
  56.  
  57. Save the embedded CA cert and encrypted user PKCS12 file.
  58. #>
  59. [CmdletBinding(DefaultParameterSetName="Add")] Param(
  60. [Parameter(ParameterSetName="Add")]
  61. [Switch] $Add,
  62.  
  63. [Parameter(ParameterSetName="Add")]
  64. [SecureString] $Pkcs12DecryptionPassword,
  65.  
  66. [Parameter(Mandatory, ParameterSetName="Remove")]
  67. [Switch] $Remove,
  68.  
  69. [Parameter(Mandatory, ParameterSetName="GetInstalledCerts")]
  70. [Switch] $GetInstalledCerts,
  71.  
  72. [Parameter(Mandatory, ParameterSetName="SaveCerts")]
  73. [Switch] $SaveCerts,
  74.  
  75. [Parameter(ParameterSetName="SaveCerts")]
  76. [string] $OutputDirectory = "$PWD"
  77. )
  78.  
  79. $ErrorActionPreference = "Stop"
  80.  
  81. $VpnServerAddress = "111.111.111.111"
  82. $VpnName = "Algo VPN 111.111.111.111 IKEv2"
  83. $VpnUser = "vpn-user"
  84. $CaCertificateBase64 = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJxVENDQVUrZ0F3SUJBZ0lKQVBuOFg2TVI0d2ZlTUFvR0NDcUdTTTQ5QkFNQ01CWXhGREFTQmdOVkJBTU0KQ3pFMU5TNDBMalExTGpFNE1CNFhEVEU0TURVek1EQTNNekUwTkZvWERUSTRNRFV5TnpBM016RTBORm93RmpFVQpNQklHQTFVRUF3d0xNVFUxTGpRdU5EVXVNVGd3V1RBVEJnY3Foa2pPUFFJQkJnZ3Foa2pPUFFNQkJ3TkNBQVJmCk54VXgxaDk5WFVCSG5JRnVqdGdpcWVRMjlEUS9uajVoRlhtYjhUTFVqT2dtVWtQeU1OTkRIeHMxdDhjRGRvWjAKWHlzL2ZHbTkvSTlhZHFMSDhlV2NvNEdGTUlHQ01CMEdBMVVkRGdRV0JCUWlOUlhHSVdNMnE5VGgxaVE5cjhycwpiKzNOSHpCR0JnTlZIU01FUHpBOWdCUWlOUlhHSVdNMnE5VGgxaVE5cjhyc2IrM05INkVhcEJnd0ZqRVVNQklHCkExVUVBd3dMTVRVMUxqUXVORFV1TVRpQ0NRRDUvRitqRWVNSDNqQU1CZ05WSFJNRUJUQURBUUgvTUFzR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bhamd5fAf7BMGbZgeSAAYZ7KjLUmaXWewp09nKOE61SwYlCSYx
  96. GoQ3e9TwIMecJxeNJ2hjHP9e+lWM8WDlqJg1U1gvjdectmkmh2KGK5eI31KM2rEBcekyjjmqa4yC
  97. QYNy5kuMp/5IvAzQCeUwllRTcon8X68aTTCCAScGCSqGSIb3DQEHAaCCARgEggEUMIIBEDCCAQwG
  98. CyqGSIb3DQEMCgECoIG0MIGxMBwGCiqGSIb3DQEMAQMwDgQINtyYN2NhCzICAggABIGQr0shF9LT
  99. MsbwG9YbqR1wOyoft7haS7DDly/mP6AcyluJI7nAObuf9I0CFjkajIusJbhmijUm/CANhYFcFwJS
  100. AymMTcVw1luiIcZfUObJD+jR2cmUiJ2wkblKps88kIwWoROQeuIcowOKDdA7UQiOx6wULH0ueAaa
  101. nisKt+wtwO8DfBY+OZoUYsnfuSB2mWQ0MUYwHwYJKoZIhvcNAQkUMRIeEAB2AHAAbgAtAG0AYQBr
  102. AHIwIwYJKoZIhvcNAQkVMRYEFLqDiS7oysXaLtYMF2K6sYJxO3yQMDEwITAJBgUrDgMCGgUABBRY
  103. OBJE6EONztNPQZ0j+20Lemr7uQQIiOZGy6nAvmwCAggA"
  104.  
  105. if ($PsCmdlet.ParameterSetName -eq "Add" -and -not $Pkcs12DecryptionPassword) {
  106. $Pkcs12DecryptionPassword = Read-Host -AsSecureString -Prompt "Pkcs12DecryptionPassword"
  107. }
  108.  
  109. <#
  110. .SYNOPSIS
  111. Create a temporary directory
  112. #>
  113. function New-TemporaryDirectory {
  114. [CmdletBinding()] Param()
  115. do {
  116. $guid = New-Guid | Select-Object -ExpandProperty Guid
  117. $newTempDirPath = Join-Path -Path $env:TEMP -ChildPath $guid
  118. } while (Test-Path -Path $newTempDirPath)
  119. New-Item -ItemType Directory -Path $newTempDirPath
  120. }
  121.  
  122. <#
  123. .SYNOPSIS
  124. Retrieve any installed Algo VPN certificates
  125. #>
  126. function Get-InstalledAlgoVpnCertificates {
  127. [CmdletBinding()] Param()
  128. Get-ChildItem -LiteralPath Cert:\LocalMachine\Root |
  129. Where-Object {
  130. $_.Subject -match "^CN=${VpnServerAddress}$" -and $_.Issuer -match "^CN=${VpnServerAddress}$"
  131. }
  132. Get-ChildItem -LiteralPath Cert:\LocalMachine\My |
  133. Where-Object {
  134. $_.Subject -match "^CN=${VpnUser}$" -and $_.Issuer -match "^CN=${VpnServerAddress}$"
  135. }
  136. }
  137.  
  138. function Save-AlgoVpnCertificates {
  139. [CmdletBinding()] Param(
  140. [String] $OutputDirectory = $PWD
  141. )
  142. $caCertPath = Join-Path -Path $OutputDirectory -ChildPath "cacert.pem"
  143. $userP12Path = Join-Path -Path $OutputDirectory -ChildPath "$VpnUser.p12"
  144. # NOTE: We cannot use ConvertFrom-Base64 here because it is not designed for binary data
  145. [IO.File]::WriteAllBytes(
  146. $caCertPath,
  147. [Convert]::FromBase64String($CaCertificateBase64))
  148. [IO.File]::WriteAllBytes(
  149. $userP12Path,
  150. [Convert]::FromBase64String($UserPkcs12Base64))
  151. return New-Object -TypeName PSObject -Property @{
  152. CaPem = $caCertPath
  153. UserPkcs12 = $userP12Path
  154. }
  155. }
  156.  
  157. function Add-AlgoVPN {
  158. [Cmdletbinding()] Param()
  159.  
  160. $workDir = New-TemporaryDirectory
  161.  
  162. try {
  163. $certs = Save-AlgoVpnCertificates -OutputDirectory $workDir
  164. $importPfxCertParams = @{
  165. Password = $Pkcs12DecryptionPassword
  166. FilePath = $certs.UserPkcs12
  167. CertStoreLocation = "Cert:\LocalMachine\My"
  168. }
  169. Import-PfxCertificate @importPfxCertParams
  170. $importCertParams = @{
  171. FilePath = $certs.CaPem
  172. CertStoreLocation = "Cert:\LocalMachine\Root"
  173. }
  174. Import-Certificate @importCertParams
  175. } finally {
  176. Remove-Item -Recurse -Force -LiteralPath $workDir
  177. }
  178.  
  179. $addVpnParams = @{
  180. Name = $VpnName
  181. ServerAddress = $VpnServerAddress
  182. TunnelType = "IKEv2"
  183. AuthenticationMethod = "MachineCertificate"
  184. EncryptionLevel = "Required"
  185. }
  186. Add-VpnConnection @addVpnParams
  187.  
  188. $setVpnParams = @{
  189. ConnectionName = $VpnName
  190. AuthenticationTransformConstants = "GCMAES128"
  191. CipherTransformConstants = "GCMAES128"
  192. EncryptionMethod = "AES128"
  193. IntegrityCheckMethod = "SHA384"
  194. DHGroup = "ECP256"
  195. PfsGroup = "ECP256"
  196. Force = $true
  197. }
  198. Set-VpnConnectionIPsecConfiguration @setVpnParams
  199. }
  200.  
  201. function Remove-AlgoVPN {
  202. [CmdletBinding()] Param()
  203. Get-InstalledAlgoVpnCertificates | Remove-Item -Force
  204. Remove-VpnConnection -Name $VpnName -Force
  205. }
  206.  
  207. switch ($PsCmdlet.ParameterSetName) {
  208. "Add" { Add-AlgoVPN }
  209. "Remove" { Remove-AlgoVPN }
  210. "GetInstalledCerts" { Get-InstalledAlgoVpnCertificates }
  211. "SaveCerts" {
  212. $certs = Save-AlgoVpnCertificates -OutputDirectory $OutputDirectory
  213. Get-Item -LiteralPath $certs.UserPkcs12, $certs.CaPem
  214. }
  215. default { throw "Unknown parameter set: '$($PsCmdlet.ParameterSetName)'" }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement