#Requires -Version 5.1 #Requires -RunAsAdministrator <# One-time preparation of an existing Windows account, not a VPN access grant. Import the authorised tunnel as an administrator first. LimitedOperatorUI is machine-wide: operators can activate ALL tunnels stored by WireGuard here. #> [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] param( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $User, [Parameter(Mandatory = $true)] [switch] $ReviewedAllTunnels ) $ErrorActionPreference = 'Stop' if (-not $ReviewedAllTunnels) { throw 'An administrator must review every stored tunnel first.' } $account = New-Object System.Security.Principal.NTAccount($User) $sid = $account.Translate([System.Security.Principal.SecurityIdentifier]) if ($sid.Value -match '^S-1-5-(18|19|20)$') { throw 'Select a named person, not a service account.' } $group = Get-LocalGroup -SID 'S-1-5-32-556' $exe = Join-Path $env:ProgramFiles 'WireGuard\wireguard.exe' if (-not (Test-Path -LiteralPath $exe)) { throw 'Install the official WireGuard Windows client first.' } $signature = Get-AuthenticodeSignature -LiteralPath $exe if ($signature.Status -ne 'Valid' -or $signature.SignerCertificate.Subject -notmatch 'WireGuard') { throw 'WireGuard executable signature could not be verified.' } $service = Get-Service -Name 'WireGuardManager' -ErrorAction Stop if ($service.Status -ne 'Running') { throw 'Start WireGuard as administrator and import the approved tunnel first.' } $configs = Join-Path $env:ProgramFiles 'WireGuard\Data\Configurations' if (@(Get-ChildItem -LiteralPath $configs -Filter '*.conf.dpapi' -File).Count -eq 0) { throw 'No imported encrypted WireGuard tunnel found. Import as administrator first.' } $member = @(Get-LocalGroupMember -Group $group.Name | Where-Object { $_.SID.Value -eq $sid.Value }).Count -gt 0 $key = 'HKLM:\SOFTWARE\WireGuard' $old = Get-ItemProperty -LiteralPath $key -Name LimitedOperatorUI -ErrorAction SilentlyContinue $hadValue = $null -ne $old $oldValue = if ($hadValue) { $old.LimitedOperatorUI } else { $null } if ($hadValue -and (Get-Item -LiteralPath $key).GetValueKind('LimitedOperatorUI') -ne [Microsoft.Win32.RegistryValueKind]::DWord) { throw 'Existing registry value has an unexpected type; review before changing it.' } if (-not $PSCmdlet.ShouldProcess("$env:COMPUTERNAME / $($sid.Value)", 'Enable WireGuard operator UI and Network Configuration Operators membership')) { return } $dir = Join-Path $env:ProgramData 'DATPOL\WireGuardOperator' New-Item -ItemType Directory -Path $dir -Force | Out-Null $acl = New-Object System.Security.AccessControl.DirectorySecurity $acl.SetAccessRuleProtection($true, $false) foreach ($principal in @('S-1-5-18', 'S-1-5-32-544')) { $identity = New-Object System.Security.Principal.SecurityIdentifier($principal) $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($identity, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow') $acl.AddAccessRule($rule) } Set-Acl -LiteralPath $dir -AclObject $acl $record = Join-Path $dir ((Get-Date -Format 'yyyyMMdd-HHmmssfff') + '-' + $sid.Value + '.json') $audit = [ordered]@{ time = (Get-Date).ToUniversalTime().ToString('o'); computer = $env:COMPUTERNAME; actor = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name; user = $User; sid = $sid.Value; groupSid = 'S-1-5-32-556'; wasMember = $member; hadRegistryValue = $hadValue; oldRegistryValue = $oldValue; status = 'prepared' } $audit | ConvertTo-Json | Set-Content -LiteralPath $record -Encoding UTF8 $added = $false try { if (-not $member) { Add-LocalGroupMember -Group $group.Name -Member $sid.Value; $added = $true } New-Item -Path $key -Force | Out-Null New-ItemProperty -LiteralPath $key -Name LimitedOperatorUI -Value 1 -PropertyType DWord -Force | Out-Null $verifiedMember = @(Get-LocalGroupMember -Group $group.Name | Where-Object { $_.SID.Value -eq $sid.Value }).Count -gt 0 $verifiedValue = (Get-ItemProperty -LiteralPath $key -Name LimitedOperatorUI).LimitedOperatorUI if (-not $verifiedMember -or $verifiedValue -ne 1) { throw 'Configuration verification failed.' } $audit.status = 'configured_pending_user_sign_in' $audit | ConvertTo-Json | Set-Content -LiteralPath $record -Encoding UTF8 Write-Output 'Configured. Ask the user to save work, sign out and sign back in. No reboot or automatic sign-out was performed.' Write-Output 'Then verify Activate/Deactivate as that user. Import/edit/export remain administrator-only. DATPOL MFA and assigned server-side permissions remain required.' } catch { $failure = $_ try { if ($added) { Remove-LocalGroupMember -Group $group.Name -Member $sid.Value } if ($hadValue) { Set-ItemProperty -LiteralPath $key -Name LimitedOperatorUI -Value $oldValue } else { Remove-ItemProperty -LiteralPath $key -Name LimitedOperatorUI -ErrorAction SilentlyContinue } $audit.status = 'failed_changes_rolled_back' } catch { $audit.status = 'failed_rollback_requires_review' } $audit | ConvertTo-Json | Set-Content -LiteralPath $record -Encoding UTF8 throw $failure }